blob: ed10768135448411ee4af6da10cb62af9074f2a5 [file] [log] [blame]
Iftekharul Islam99d199f2017-03-24 15:28:25 -05001/**
2 * API utilities service
3 *
4 * @module app/common/services/api-utils
5 * @exports APIUtils
6 * @name APIUtils
7 * @version 0.0.1
8 */
9
10window.angular && (function (angular) {
11 'use strict';
12 angular
13 .module('app.common.services')
14 .factory('APIUtils', ['$http', 'Constants', function($http, Constants){
15 var SERVICE = {
16 LOGIN_CREDENTIALS: Constants.LOGIN_CREDENTIALS,
17 API_CREDENTIALS: Constants.API_CREDENTIALS,
18 API_RESPONSE: Constants.API_RESPONSE,
19 CHASSIS_POWER_STATE: Constants.CHASSIS_POWER_STATE,
20 HOST_STATE_TEXT: Constants.HOST_STATE,
21 HOST_STATE: Constants.HOST_STATE,
Iftekharul Islamcd789502017-04-19 14:37:55 -050022 LED_STATE: Constants.LED_STATE,
23 LED_STATE_TEXT: Constants.LED_STATE_TEXT,
Iftekharul Islam99d199f2017-03-24 15:28:25 -050024 getChassisState: function(callback){
25 $http({
26 method: 'GET',
27 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/chassis0",
28 headers: {
29 'Accept': 'application/json',
30 'Content-Type': 'application/json'
31 },
32 withCredentials: true
33 }).success(function(response){
34 var json = JSON.stringify(response);
35 var content = JSON.parse(json);
36 callback(content.data.CurrentPowerState);
37 }).error(function(error){
38 console.log(error);
39 });
40 },
41 getHostState: function(callback){
42 $http({
43 method: 'GET',
44 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
45 headers: {
46 'Accept': 'application/json',
47 'Content-Type': 'application/json'
48 },
49 withCredentials: true
50 }).success(function(response){
51 var json = JSON.stringify(response);
52 var content = JSON.parse(json);
53 callback(content.data.CurrentHostState);
54 }).error(function(error){
55 console.log(error);
56 });
57 },
Iftekharul Islamcd789502017-04-19 14:37:55 -050058 getLEDState: function(callback){
59 $http({
60 method: 'GET',
61 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/led/groups/enclosure_identify",
62 headers: {
63 'Accept': 'application/json',
64 'Content-Type': 'application/json'
65 },
66 withCredentials: true
67 }).success(function(response){
68 var json = JSON.stringify(response);
69 var content = JSON.parse(json);
Iftekharul Islam54c22e42017-06-28 11:06:16 -050070 if(callback){
71 callback(content.data.Asserted);
72 }else{
73 return content.data.Asserted;
74 }
Iftekharul Islamcd789502017-04-19 14:37:55 -050075 }).error(function(error){
76 console.log(error);
77 });
78 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -050079 login: function(username, password, callback){
80 $http({
81 method: 'POST',
82 url: SERVICE.API_CREDENTIALS.host + "/login",
83 headers: {
84 'Accept': 'application/json',
85 'Content-Type': 'application/json'
86 },
87 withCredentials: true,
88 data: JSON.stringify({"data": [username, password]})
89 }).success(function(response){
90 if(callback){
91 callback(response);
92 }
93 }).error(function(error){
94 if(callback){
Iftekharul Islamcd789502017-04-19 14:37:55 -050095 if(error && error.status && error.status == 'error'){
96 callback(error);
97 }else{
98 callback(error, true);
99 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500100 }
101 console.log(error);
102 });
103 },
104 logout: function(callback){
105 $http({
106 method: 'POST',
107 url: SERVICE.API_CREDENTIALS.host + "/logout",
108 headers: {
109 'Accept': 'application/json',
110 'Content-Type': 'application/json'
111 },
112 withCredentials: true,
113 data: JSON.stringify({"data": []})
114 }).success(function(response){
115 if(callback){
116 callback(response);
117 }
118 }).error(function(error){
119 if(callback){
120 callback(null, error);
121 }
122 console.log(error);
123 });
124 },
125 chassisPowerOn: function(callback){
126 $http({
127 method: 'POST',
128 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
129 headers: {
130 'Accept': 'application/json',
131 'Content-Type': 'application/json'
132 },
133 withCredentials: true,
134 data: JSON.stringify({"data": []})
135 }).success(function(response){
136 var json = JSON.stringify(response);
137 var content = JSON.parse(json);
138 if(callback){
139 return callback(content.data.CurrentPowerState);
140 }
141 }).error(function(error){
142 if(callback){
143 callback(error);
144 }else{
145 console.log(error);
146 }
147 });
148 },
149 chassisPowerOff: function(callback){
150 $http({
151 method: 'POST',
152 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
153 headers: {
154 'Accept': 'application/json',
155 'Content-Type': 'application/json'
156 },
157 withCredentials: true,
158 data: JSON.stringify({"data": []})
159 }).success(function(response){
160 var json = JSON.stringify(response);
161 var content = JSON.parse(json);
162 if(callback){
163 return callback(content.data.CurrentPowerState);
164 }
165 }).error(function(error){
166 if(callback){
167 callback(error);
168 }else{
169 console.log(error);
170 }
171 });
172 },
Iftekharul Islamcd789502017-04-19 14:37:55 -0500173 setLEDState: function(state, callback){
174 $http({
175 method: 'PUT',
176 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted",
177 headers: {
178 'Accept': 'application/json',
179 'Content-Type': 'application/json'
180 },
181 withCredentials: true,
182 data: JSON.stringify({"data": state})
183 }).success(function(response){
184 var json = JSON.stringify(response);
185 var content = JSON.parse(json);
186 if(callback){
187 return callback(content.status);
188 }
189 }).error(function(error){
190 if(callback){
191 callback(error);
192 }else{
193 console.log(error);
194 }
195 });
196 },
Iftekharul Islam55368122017-03-27 09:46:50 -0500197 bmcReboot: function(callback){
198 $http({
199 method: 'PUT',
200 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/bmc0/attr/RequestedBmcTransition",
201 headers: {
202 'Accept': 'application/json',
203 'Content-Type': 'application/json'
204 },
205 withCredentials: true,
206 data: JSON.stringify({"data": "xyz.openbmc_project.State.BMC.Transition.Reboot"})
207 }).success(function(response){
208 var json = JSON.stringify(response);
209 var content = JSON.parse(json);
210 if(callback){
211 return callback(content.status);
212 }
213 }).error(function(error){
214 if(callback){
215 callback(error);
216 }else{
217 console.log(error);
218 }
219 });
220 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500221 hostPowerOn: function(callback){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500222 $http({
223 method: 'PUT',
224 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
225 headers: {
226 'Accept': 'application/json',
227 'Content-Type': 'application/json'
228 },
229 withCredentials: true,
230 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.On"})
231 }).success(function(response){
232 var json = JSON.stringify(response);
233 var content = JSON.parse(json);
234 if(callback){
235 return callback(content.status);
236 }
237 }).error(function(error){
238 if(callback){
239 callback(error);
240 }else{
241 console.log(error);
242 }
243 });
244 },
245 hostPowerOff: function(callback){
246 $http({
247 method: 'PUT',
248 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
249 headers: {
250 'Accept': 'application/json',
251 'Content-Type': 'application/json'
252 },
253 withCredentials: true,
254 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Off"})
255 }).success(function(response){
256 var json = JSON.stringify(response);
257 var content = JSON.parse(json);
258 if(callback){
259 return callback(content.status);
260 }
261 }).error(function(error){
262 if(callback){
263 callback(error);
264 }else{
265 console.log(error);
266 }
267 });
268 },
269 hostReboot: function(callback){
270 $http({
271 method: 'POST',
272 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
273 headers: {
274 'Accept': 'application/json',
275 'Content-Type': 'application/json'
276 },
277 withCredentials: true,
278 data: JSON.stringify({"data": []}),
279 }).success(function(response){
280 var json = JSON.stringify(response);
281 var content = JSON.parse(json);
282 if(callback){
283 return callback(content);
284 }
285 }).error(function(error){
286 if(callback){
287 callback(error);
288 }else{
289 console.log(error);
290 }
291 });
292 },
293 hostShutdown: function(callback){
294 $http({
295 method: 'POST',
296 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
297 headers: {
298 'Accept': 'application/json',
299 'Content-Type': 'application/json'
300 },
301 withCredentials: true,
302 data: JSON.stringify({"data": []})
303 }).success(function(response){
304 var json = JSON.stringify(response);
305 var content = JSON.parse(json);
306 if(callback){
307 return callback(content);
308 }
309 }).error(function(error){
310 if(callback){
311 callback(error);
312 }else{
313 console.log(error);
314 }
315 });
Iftekharul Islamcd789502017-04-19 14:37:55 -0500316 },
317 getLogs: function(callback){
318 $http({
319 method: 'GET',
320 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/logging/enumerate",
321 headers: {
322 'Accept': 'application/json',
323 'Content-Type': 'application/json'
324 },
325 withCredentials: true
326 }).success(function(response){
327 var json = JSON.stringify(response);
328 var content = JSON.parse(json);
329 var dataClone = JSON.parse(JSON.stringify(content.data));
330 var data = [];
331 var severityCode = '';
332 var priority = '';
Iftekharul Islamcd789502017-04-19 14:37:55 -0500333 var relatedItems = [];
334
335 for(var key in content.data){
336 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Id')){
337 var severityFlags = {low: false, medium: false, high: false};
338 severityCode = content.data[key].Severity.split(".").pop();
339 priority = Constants.SEVERITY_TO_PRIORITY_MAP[severityCode];
340 severityFlags[priority.toLowerCase()] = true;
341 relatedItems = [];
342 content.data[key].associations.forEach(function(item){
Iftekharul Islam532763f2017-04-25 09:44:40 -0500343 relatedItems.push(item[2]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500344 });
345
346 data.push(Object.assign({
347 path: key,
348 copied: false,
349 priority: priority,
350 severity_code: severityCode,
351 severity_flags: severityFlags,
352 additional_data: content.data[key].AdditionalData.join("\n"),
353 selected: false,
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500354 search_text: ("#" + content.data[key].Id + " " + severityCode + " " + content.data[key].Severity + " " + content.data[key].AdditionalData.join(" ")).toLowerCase(),
Iftekharul Islamcd789502017-04-19 14:37:55 -0500355 meta: false,
356 confirm: false,
357 related_items: relatedItems,
358 data: {key: key, value: content.data[key]}
359 }, content.data[key]));
360 }
361 }
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500362 if(callback){
363 callback(data, dataClone);
364 }else{
365 return data;
366 }
Iftekharul Islamcd789502017-04-19 14:37:55 -0500367 }).error(function(error){
368 console.log(error);
369 });
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500370 },
371 getAllSensorStatus: function(callback){
372 /**
373 GET https://9.3.185.156/xyz/openbmc_project/sensors/enumerate
374 */
375 $http({
376 method: 'GET',
377 url: "/assets/mocks/sensors.json",
378 headers: {
379 'Accept': 'application/json',
380 'Content-Type': 'application/json'
381 },
382 withCredentials: true
383 }).success(function(response){
384 var json = JSON.stringify(response);
385 var content = JSON.parse(json);
386 var dataClone = JSON.parse(JSON.stringify(content.data));
387 var sensorData = [];
388 var allSensorSeveries = [];
389 var allSensorRows = [];
390 var total = 0;
391 var status = 'normal';
392 var data = {
393 total: 0,
394 status: '',
395 sensors: [{
396 title: 'All Sensors',
397 type: 'all',
398 status: '',
399 severity_flags: {},
400 search_text: '',
401 display_headers: ['Sensor (Unit)', 'Reading', 'State'],
402 data: []
403 }]
404 };
405
406 function getSensorStatus(reading){
407 var severityFlags = {critical: false, warning: false, normal: false}, severityText = '';
408 if(reading.Value >= reading.CriticalLow && reading.Value <= reading.CriticalHigh){
409 severityFlags.critical = true;
410 severityText = 'critical';
411 }
412 else if(reading.Value >= reading.WarningLow && reading.Value <= reading.WarningHigh){
413 severityFlags.warning = true;
414 severityText = 'warning';
415 }else{
416 severityFlags.normal = true;
417 severityText = 'normal';
418 }
419 return { flags: severityFlags, severityText: severityText};
420 }
421
422 for(var key in content.data){
423 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Unit')){
424 sensorData.push(Object.assign({
425 path: key,
426 selected: false,
427 confirm: false,
428 copied: false,
429 original_data: {key: key, value: content.data[key]}
430 }, content.data[key]));
431 }
432 }
433
434 Constants.SENSOR_DATA_TEMPLATE.sensors.forEach(function(sensor){
435 var rowData = [];
436 var severities = [];
437 var thisSensorData = sensorData.filter(function(el){
438 return el.path.indexOf('sensors/'+sensor.key_search) > -1;
439 });
440
441 for(var i = 0; i < thisSensorData.length; i++){
442
443 var severity = getSensorStatus(thisSensorData[i]);
444 severities.push(severity.severityText);
445 rowData.push(Object.assign({
446 title: sensor.sensor_row.title + (i+1),
447 status: severity.severityText,
448 severity_flags: severity.flags,
449 reading: thisSensorData[i].Value + sensor.sensor_row.reading,
450 search_text: (sensor.sensor_row.title + (i+1) + " " + severity.severityText + " " + thisSensorData[i].Value + sensor.sensor_row.reading).toLowerCase(),
451 indicator: (severity.flags.critical) ? '90%' : ((severity.flags.warning) ? '15%' : '50%')
452 }, thisSensorData[i]));
453 }
454
455 status = (severities.indexOf('critical') > -1) ? 'critical' : ((severities.indexOf('warning') > -1) ? 'warning' : 'normal');
456 total += rowData.length;
457 allSensorSeveries.push(status);
458 var sevFlags = {critical: false, warning: false, normal: false};
459 sevFlags[status] = true;
460 data.sensors.push({
461 title: sensor.title,
462 type: sensor.type,
463 status: status,
464 severity_flags: sevFlags,
465 search_text: (sensor.title + " " + status).toLowerCase(),
466 display_headers: sensor.display_headers,
467 data: rowData
468 });
469 Array.prototype.push.apply(allSensorRows, rowData);
470 });
471
472 data.status = (allSensorSeveries.indexOf('critical') > -1) ? 'critical' : ((allSensorSeveries.indexOf('warning') > -1) ? 'warning' : 'normal');
473 data.total = total;
474 if(allSensorRows.length){
475 data.sensors[0].status = data.status;
476 data.sensors[0].data = allSensorRows;
477 data.sensors[0].search_text = (data.sensors[0].title + " " + data.sensors[0].status).toLowerCase();
478 var flags = {critical: false, warning: false, normal: false};
479 flags[data.status] = true;
480 data.sensors[0].severity_flags = flags;
481 }
482 callback(data, dataClone);
483 }).error(function(error){
484 console.log(error);
485 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500486 },
487 getFirmwares: function(callback){
488 $http({
489 method: 'GET',
490 //url: SERVICE.API_CREDENTIALS.mock_host + "/software",
491 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/software/enumerate",
492 headers: {
493 'Accept': 'application/json',
494 'Content-Type': 'application/json'
495 },
496 withCredentials: true
497 }).success(function(response){
498 var json = JSON.stringify(response);
499 var content = JSON.parse(json);
500 var data = [];
501 var active = false;
502 var isExtended = false;
503 var bmcActiveVersion = "";
504 var hostActiveVersion = "";
505 var imageType = "";
506 var extendedVersions = [];
507
508 function getFormatedExtendedVersions(extendedVersion){
509 var versions = [];
510 extendedVersion = extendedVersion.split(",");
511
512 extendedVersion.forEach(function(item){
513 var parts = item.split("-");
514 var numberIndex = 0;
515 for(var i = 0; i < parts.length; i++){
516 if(/[0-9]/.test(parts[i])){
517 numberIndex = i;
518 break;
519 }
520 }
521 var titlePart = parts.splice(0, numberIndex);
522 titlePart = titlePart.join("");
523 titlePart = titlePart[0].toUpperCase() + titlePart.substr(1, titlePart.length);
524 var versionPart = parts.join("-");
525 versions.push({
526 title: titlePart,
527 version: versionPart
528 });
529 });
530
531 return versions;
532 }
533
534 for(var key in content.data){
535 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Version')){
536 active = (/\.Active$/).test(content.data[key].Activation);
537 imageType = content.data[key].Purpose.split(".").pop();
538 isExtended = content.data[key].hasOwnProperty('ExtendedVersion') && content.data[key].ExtendedVersion != "";
539 if(isExtended){
540 extendedVersions = getFormatedExtendedVersions(content.data[key].ExtendedVersion);
541 }
542 data.push(Object.assign({
543 path: key,
544 active: active,
545 imageId: key.split("/").pop(),
546 imageType: imageType,
547 isExtended: isExtended,
548 extended: {
549 show: false,
550 versions: extendedVersions
551 },
552 data: {key: key, value: content.data[key]}
553 }, content.data[key]));
554
555 if(active && imageType == 'BMC'){
556 bmcActiveVersion = content.data[key].Version;
557 }
558
559 if(active && imageType == 'Host'){
560 hostActiveVersion = content.data[key].Version;
561 }
562 }
563 }
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500564 if(callback){
565 callback(data, bmcActiveVersion, hostActiveVersion);
566 }else{
567 return(data, bmcActiveVersion, hostActiveVersion);
568 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500569 }).error(function(error){
570 console.log(error);
571 });
572 },
573 uploadImage: function(file, callback){
574 $http({
575 method: 'PUT',
576 timeout: 5 * 60 * 1000,
577 //url: 'http://localhost:3002/upload',
578 url: SERVICE.API_CREDENTIALS.host + "/upload/image/",
579 headers: {
580 'Accept': 'application/octet-stream',
581 'Content-Type': 'application/octet-stream'
582 },
583 withCredentials: true,
584 data: file
585 }).success(function(response){
586 var json = JSON.stringify(response);
587 var content = JSON.parse(json);
588 if(callback){
589 return callback(content);
590 }
591 }).error(function(error){
592 if(callback){
593 callback(error);
594 }else{
595 console.log(error);
596 }
597 });
598 },
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500599 getBMCEthernetInfo: function(callback){
600 $http({
601 method: 'GET',
602 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc/ethernet",
603 headers: {
604 'Accept': 'application/json',
605 'Content-Type': 'application/json'
606 },
607 withCredentials: true
608 }).success(function(response){
609 var json = JSON.stringify(response);
610 var content = JSON.parse(json);
611 if(callback){
612 callback(content.data);
613 }else{
614 return content.data;
615 }
616 }).error(function(error){
617 console.log(error);
618 });
619 },
620 getBMCInfo: function(callback){
621 $http({
622 method: 'GET',
623 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc",
624 headers: {
625 'Accept': 'application/json',
626 'Content-Type': 'application/json'
627 },
628 withCredentials: true
629 }).success(function(response){
630 var json = JSON.stringify(response);
631 var content = JSON.parse(json);
632 if(callback){
633 callback(content.data);
634 }else{
635 return content.data;
636 }
637 }).error(function(error){
638 console.log(error);
639 });
640 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500641 };
642 return SERVICE;
643 }]);
644
645 })(window.angular);