blob: 3ed48a33cbe31c602466c5ca63d6a6d8129008d0 [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')
Michael Davisdfad5d52017-07-20 14:53:46 -050014<<<<<<< HEAD
Iftekharul Islamf2d74642017-07-10 16:42:14 -050015 .factory('APIUtils', ['$http', 'Constants', '$q', function($http, Constants, $q){
Michael Davisdfad5d52017-07-20 14:53:46 -050016=======
17 .factory('APIUtils', ['$http', 'Constants', function($http, Constants){
18>>>>>>> 4c1a3dd... Major update to code structure
Iftekharul Islam99d199f2017-03-24 15:28:25 -050019 var SERVICE = {
20 LOGIN_CREDENTIALS: Constants.LOGIN_CREDENTIALS,
21 API_CREDENTIALS: Constants.API_CREDENTIALS,
22 API_RESPONSE: Constants.API_RESPONSE,
23 CHASSIS_POWER_STATE: Constants.CHASSIS_POWER_STATE,
24 HOST_STATE_TEXT: Constants.HOST_STATE,
25 HOST_STATE: Constants.HOST_STATE,
Michael Davisdfad5d52017-07-20 14:53:46 -050026<<<<<<< HEAD
Iftekharul Islamcd789502017-04-19 14:37:55 -050027 LED_STATE: Constants.LED_STATE,
28 LED_STATE_TEXT: Constants.LED_STATE_TEXT,
Michael Davisdfad5d52017-07-20 14:53:46 -050029=======
30>>>>>>> 4c1a3dd... Major update to code structure
Iftekharul Islam99d199f2017-03-24 15:28:25 -050031 getChassisState: function(callback){
32 $http({
33 method: 'GET',
34 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/chassis0",
35 headers: {
36 'Accept': 'application/json',
37 'Content-Type': 'application/json'
38 },
39 withCredentials: true
40 }).success(function(response){
41 var json = JSON.stringify(response);
42 var content = JSON.parse(json);
43 callback(content.data.CurrentPowerState);
44 }).error(function(error){
45 console.log(error);
46 });
47 },
48 getHostState: function(callback){
49 $http({
50 method: 'GET',
51 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
52 headers: {
53 'Accept': 'application/json',
54 'Content-Type': 'application/json'
55 },
56 withCredentials: true
57 }).success(function(response){
58 var json = JSON.stringify(response);
59 var content = JSON.parse(json);
60 callback(content.data.CurrentHostState);
61 }).error(function(error){
62 console.log(error);
63 });
64 },
Michael Davisdfad5d52017-07-20 14:53:46 -050065<<<<<<< HEAD
Iftekharul Islamcd789502017-04-19 14:37:55 -050066 getLEDState: function(callback){
67 $http({
68 method: 'GET',
69 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/led/groups/enclosure_identify",
70 headers: {
71 'Accept': 'application/json',
72 'Content-Type': 'application/json'
73 },
74 withCredentials: true
75 }).success(function(response){
76 var json = JSON.stringify(response);
77 var content = JSON.parse(json);
Iftekharul Islam54c22e42017-06-28 11:06:16 -050078 if(callback){
79 callback(content.data.Asserted);
80 }else{
81 return content.data.Asserted;
82 }
Iftekharul Islamcd789502017-04-19 14:37:55 -050083 }).error(function(error){
84 console.log(error);
85 });
86 },
Michael Davisdfad5d52017-07-20 14:53:46 -050087=======
88>>>>>>> 4c1a3dd... Major update to code structure
Iftekharul Islam99d199f2017-03-24 15:28:25 -050089 login: function(username, password, callback){
90 $http({
91 method: 'POST',
92 url: SERVICE.API_CREDENTIALS.host + "/login",
93 headers: {
94 'Accept': 'application/json',
95 'Content-Type': 'application/json'
96 },
97 withCredentials: true,
98 data: JSON.stringify({"data": [username, password]})
99 }).success(function(response){
100 if(callback){
101 callback(response);
102 }
103 }).error(function(error){
104 if(callback){
Michael Davisdfad5d52017-07-20 14:53:46 -0500105<<<<<<< HEAD
Iftekharul Islamcd789502017-04-19 14:37:55 -0500106 if(error && error.status && error.status == 'error'){
107 callback(error);
108 }else{
109 callback(error, true);
110 }
Michael Davisdfad5d52017-07-20 14:53:46 -0500111=======
112 callback(null, true);
113>>>>>>> 4c1a3dd... Major update to code structure
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500114 }
115 console.log(error);
116 });
117 },
118 logout: function(callback){
119 $http({
120 method: 'POST',
121 url: SERVICE.API_CREDENTIALS.host + "/logout",
122 headers: {
123 'Accept': 'application/json',
124 'Content-Type': 'application/json'
125 },
126 withCredentials: true,
127 data: JSON.stringify({"data": []})
128 }).success(function(response){
129 if(callback){
130 callback(response);
131 }
132 }).error(function(error){
133 if(callback){
134 callback(null, error);
135 }
136 console.log(error);
137 });
138 },
139 chassisPowerOn: function(callback){
140 $http({
141 method: 'POST',
142 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
143 headers: {
144 'Accept': 'application/json',
145 'Content-Type': 'application/json'
146 },
147 withCredentials: true,
148 data: JSON.stringify({"data": []})
149 }).success(function(response){
150 var json = JSON.stringify(response);
151 var content = JSON.parse(json);
152 if(callback){
153 return callback(content.data.CurrentPowerState);
154 }
155 }).error(function(error){
156 if(callback){
157 callback(error);
158 }else{
159 console.log(error);
160 }
161 });
162 },
163 chassisPowerOff: function(callback){
164 $http({
165 method: 'POST',
166 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
167 headers: {
168 'Accept': 'application/json',
169 'Content-Type': 'application/json'
170 },
171 withCredentials: true,
172 data: JSON.stringify({"data": []})
173 }).success(function(response){
174 var json = JSON.stringify(response);
175 var content = JSON.parse(json);
176 if(callback){
177 return callback(content.data.CurrentPowerState);
178 }
179 }).error(function(error){
180 if(callback){
181 callback(error);
182 }else{
183 console.log(error);
184 }
185 });
186 },
Michael Davisdfad5d52017-07-20 14:53:46 -0500187<<<<<<< HEAD
Iftekharul Islamcd789502017-04-19 14:37:55 -0500188 setLEDState: function(state, callback){
189 $http({
190 method: 'PUT',
191 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted",
192 headers: {
193 'Accept': 'application/json',
194 'Content-Type': 'application/json'
195 },
196 withCredentials: true,
197 data: JSON.stringify({"data": state})
198 }).success(function(response){
199 var json = JSON.stringify(response);
200 var content = JSON.parse(json);
201 if(callback){
202 return callback(content.status);
203 }
204 }).error(function(error){
205 if(callback){
206 callback(error);
207 }else{
208 console.log(error);
209 }
210 });
211 },
Iftekharul Islam55368122017-03-27 09:46:50 -0500212 bmcReboot: function(callback){
213 $http({
214 method: 'PUT',
215 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/bmc0/attr/RequestedBmcTransition",
216 headers: {
217 'Accept': 'application/json',
218 'Content-Type': 'application/json'
219 },
220 withCredentials: true,
221 data: JSON.stringify({"data": "xyz.openbmc_project.State.BMC.Transition.Reboot"})
222 }).success(function(response){
223 var json = JSON.stringify(response);
224 var content = JSON.parse(json);
225 if(callback){
226 return callback(content.status);
227 }
228 }).error(function(error){
229 if(callback){
230 callback(error);
231 }else{
232 console.log(error);
233 }
234 });
235 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500236 hostPowerOn: function(callback){
Michael Davisdfad5d52017-07-20 14:53:46 -0500237=======
238 hostPowerOn: function(callback){
239 /**
240 curl -c cjar -b cjar -k -H "Content-Type: application/json" -d
241 "{\"data\": \"xyz.openbmc_project.State.Host.Transition.Off\"}"
242 -X PUT
243 https://9.3.164.147/xyz/openbmc_project/state/host0/attr/RequestedHostTransition
244 **/
245>>>>>>> 4c1a3dd... Major update to code structure
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500246 $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.On"})
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 hostPowerOff: function(callback){
270 $http({
271 method: 'PUT',
272 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
273 headers: {
274 'Accept': 'application/json',
275 'Content-Type': 'application/json'
276 },
277 withCredentials: true,
278 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Off"})
279 }).success(function(response){
280 var json = JSON.stringify(response);
281 var content = JSON.parse(json);
282 if(callback){
283 return callback(content.status);
284 }
285 }).error(function(error){
286 if(callback){
287 callback(error);
288 }else{
289 console.log(error);
290 }
291 });
292 },
293 hostReboot: 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 });
316 },
317 hostShutdown: function(callback){
318 $http({
319 method: 'POST',
320 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
321 headers: {
322 'Accept': 'application/json',
323 'Content-Type': 'application/json'
324 },
325 withCredentials: true,
326 data: JSON.stringify({"data": []})
327 }).success(function(response){
328 var json = JSON.stringify(response);
329 var content = JSON.parse(json);
330 if(callback){
331 return callback(content);
332 }
333 }).error(function(error){
334 if(callback){
335 callback(error);
336 }else{
337 console.log(error);
338 }
339 });
Michael Davisdfad5d52017-07-20 14:53:46 -0500340<<<<<<< HEAD
Iftekharul Islamcd789502017-04-19 14:37:55 -0500341 },
342 getLogs: function(callback){
343 $http({
344 method: 'GET',
345 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/logging/enumerate",
346 headers: {
347 'Accept': 'application/json',
348 'Content-Type': 'application/json'
349 },
350 withCredentials: true
351 }).success(function(response){
352 var json = JSON.stringify(response);
353 var content = JSON.parse(json);
354 var dataClone = JSON.parse(JSON.stringify(content.data));
355 var data = [];
356 var severityCode = '';
357 var priority = '';
Iftekharul Islamcd789502017-04-19 14:37:55 -0500358 var relatedItems = [];
359
360 for(var key in content.data){
361 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Id')){
362 var severityFlags = {low: false, medium: false, high: false};
363 severityCode = content.data[key].Severity.split(".").pop();
364 priority = Constants.SEVERITY_TO_PRIORITY_MAP[severityCode];
365 severityFlags[priority.toLowerCase()] = true;
366 relatedItems = [];
367 content.data[key].associations.forEach(function(item){
Iftekharul Islam532763f2017-04-25 09:44:40 -0500368 relatedItems.push(item[2]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500369 });
370
371 data.push(Object.assign({
372 path: key,
373 copied: false,
374 priority: priority,
375 severity_code: severityCode,
376 severity_flags: severityFlags,
377 additional_data: content.data[key].AdditionalData.join("\n"),
378 selected: false,
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500379 search_text: ("#" + content.data[key].Id + " " + severityCode + " " + content.data[key].Severity + " " + content.data[key].AdditionalData.join(" ")).toLowerCase(),
Iftekharul Islamcd789502017-04-19 14:37:55 -0500380 meta: false,
381 confirm: false,
382 related_items: relatedItems,
383 data: {key: key, value: content.data[key]}
384 }, content.data[key]));
385 }
386 }
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500387 if(callback){
388 callback(data, dataClone);
389 }else{
390 return data;
391 }
Iftekharul Islamcd789502017-04-19 14:37:55 -0500392 }).error(function(error){
393 console.log(error);
394 });
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500395 },
396 getAllSensorStatus: function(callback){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500397 $http({
398 method: 'GET',
Iftekharul Islam8947e702017-07-27 10:28:07 -0500399 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/sensors/enumerate",
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500400 headers: {
401 'Accept': 'application/json',
402 'Content-Type': 'application/json'
403 },
404 withCredentials: true
405 }).success(function(response){
406 var json = JSON.stringify(response);
407 var content = JSON.parse(json);
408 var dataClone = JSON.parse(JSON.stringify(content.data));
409 var sensorData = [];
Iftekharul Islam8947e702017-07-27 10:28:07 -0500410 var severity = {};
411 var title = "";
412 var tempKeyParts = [];
413 var order = 0;
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500414
415 function getSensorStatus(reading){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500416 var severityFlags = {critical: false, warning: false, normal: false}, severityText = '', order = 0;
417
418 if(reading.hasOwnProperty('CriticalLow') &&
419 reading.Value < reading.CriticalLow
420 ){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500421 severityFlags.critical = true;
422 severityText = 'critical';
Iftekharul Islam8947e702017-07-27 10:28:07 -0500423 order = 2;
424 }else if(reading.hasOwnProperty('CriticalHigh') &&
425 reading.Value > reading.CriticalHigh
426 ){
427 severityFlags.critical = true;
428 severityText = 'critical';
429 order = 2;
430 }else if(reading.hasOwnProperty('CriticalLow') &&
431 reading.hasOwnProperty('WarningLow') &&
432 reading.Value >= reading.CriticalLow && reading.Value <= reading.WarningLow){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500433 severityFlags.warning = true;
434 severityText = 'warning';
Iftekharul Islam8947e702017-07-27 10:28:07 -0500435 order = 1;
436 }else if(reading.hasOwnProperty('WarningHigh') &&
437 reading.hasOwnProperty('CriticalHigh') &&
438 reading.Value >= reading.WarningHigh && reading.Value <= reading.CriticalHigh){
439 severityFlags.warning = true;
440 severityText = 'warning';
441 order = 1;
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500442 }else{
443 severityFlags.normal = true;
444 severityText = 'normal';
445 }
Iftekharul Islam8947e702017-07-27 10:28:07 -0500446 return { flags: severityFlags, severityText: severityText, order: order};
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500447 }
448
449 for(var key in content.data){
450 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Unit')){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500451
452 severity = getSensorStatus(content.data[key]);
453
454 if(!content.data[key].hasOwnProperty('CriticalLow')){
455 content.data[key].CriticalLow = "--";
456 content.data[key].CriticalHigh = "--";
457 }
458
459 if(!content.data[key].hasOwnProperty('WarningLow')){
460 content.data[key].WarningLow = "--";
461 content.data[key].WarningHigh = "--";
462 }
463
464 tempKeyParts = key.split("/");
465 title = tempKeyParts.pop();
466 title = tempKeyParts.pop() + '_' + title;
467 title = title.split("_").map(function(item){
468 return item.toLowerCase().charAt(0).toUpperCase() + item.slice(1);
469 }).reduce(function(prev, el){
470 return prev + " " + el;
471 });
472
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500473 sensorData.push(Object.assign({
474 path: key,
475 selected: false,
476 confirm: false,
477 copied: false,
Iftekharul Islam8947e702017-07-27 10:28:07 -0500478 title: title,
479 unit: Constants.SENSOR_UNIT_MAP[content.data[key].Unit],
480 severity_flags: severity.flags,
481 status: severity.severityText,
482 order: severity.order,
483 search_text: (title + " " + content.data[key].Value + " " +
484 Constants.SENSOR_UNIT_MAP[content.data[key].Unit] + " " +
485 severity.severityText + " " +
486 content.data[key].CriticalLow + " " +
487 content.data[key].CriticalHigh + " " +
488 content.data[key].WarningLow + " " +
489 content.data[key].WarningHigh + " "
490 ).toLowerCase(),
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500491 original_data: {key: key, value: content.data[key]}
492 }, content.data[key]));
493 }
494 }
495
Iftekharul Islam8947e702017-07-27 10:28:07 -0500496 callback(sensorData, dataClone);
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500497 }).error(function(error){
498 console.log(error);
499 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500500 },
501 getFirmwares: function(callback){
502 $http({
503 method: 'GET',
504 //url: SERVICE.API_CREDENTIALS.mock_host + "/software",
505 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/software/enumerate",
506 headers: {
507 'Accept': 'application/json',
508 'Content-Type': 'application/json'
509 },
510 withCredentials: true
511 }).success(function(response){
512 var json = JSON.stringify(response);
513 var content = JSON.parse(json);
514 var data = [];
515 var active = false;
516 var isExtended = false;
517 var bmcActiveVersion = "";
518 var hostActiveVersion = "";
519 var imageType = "";
520 var extendedVersions = [];
521
522 function getFormatedExtendedVersions(extendedVersion){
523 var versions = [];
524 extendedVersion = extendedVersion.split(",");
525
526 extendedVersion.forEach(function(item){
527 var parts = item.split("-");
528 var numberIndex = 0;
529 for(var i = 0; i < parts.length; i++){
530 if(/[0-9]/.test(parts[i])){
531 numberIndex = i;
532 break;
533 }
534 }
535 var titlePart = parts.splice(0, numberIndex);
536 titlePart = titlePart.join("");
537 titlePart = titlePart[0].toUpperCase() + titlePart.substr(1, titlePart.length);
538 var versionPart = parts.join("-");
539 versions.push({
540 title: titlePart,
541 version: versionPart
542 });
543 });
544
545 return versions;
546 }
547
548 for(var key in content.data){
549 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Version')){
550 active = (/\.Active$/).test(content.data[key].Activation);
551 imageType = content.data[key].Purpose.split(".").pop();
552 isExtended = content.data[key].hasOwnProperty('ExtendedVersion') && content.data[key].ExtendedVersion != "";
553 if(isExtended){
554 extendedVersions = getFormatedExtendedVersions(content.data[key].ExtendedVersion);
555 }
556 data.push(Object.assign({
557 path: key,
558 active: active,
559 imageId: key.split("/").pop(),
560 imageType: imageType,
561 isExtended: isExtended,
562 extended: {
563 show: false,
564 versions: extendedVersions
565 },
566 data: {key: key, value: content.data[key]}
567 }, content.data[key]));
568
569 if(active && imageType == 'BMC'){
570 bmcActiveVersion = content.data[key].Version;
571 }
572
573 if(active && imageType == 'Host'){
574 hostActiveVersion = content.data[key].Version;
575 }
576 }
577 }
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500578 if(callback){
579 callback(data, bmcActiveVersion, hostActiveVersion);
580 }else{
581 return(data, bmcActiveVersion, hostActiveVersion);
582 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500583 }).error(function(error){
584 console.log(error);
585 });
586 },
587 uploadImage: function(file, callback){
588 $http({
589 method: 'PUT',
590 timeout: 5 * 60 * 1000,
591 //url: 'http://localhost:3002/upload',
592 url: SERVICE.API_CREDENTIALS.host + "/upload/image/",
593 headers: {
594 'Accept': 'application/octet-stream',
595 'Content-Type': 'application/octet-stream'
596 },
597 withCredentials: true,
598 data: file
599 }).success(function(response){
600 var json = JSON.stringify(response);
601 var content = JSON.parse(json);
602 if(callback){
603 return callback(content);
604 }
605 }).error(function(error){
606 if(callback){
607 callback(error);
608 }else{
609 console.log(error);
610 }
611 });
612 },
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500613 getBMCEthernetInfo: function(callback){
614 $http({
615 method: 'GET',
Iftekharul Islamf2d74642017-07-10 16:42:14 -0500616 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc/ethernet",
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500617 headers: {
618 'Accept': 'application/json',
619 'Content-Type': 'application/json'
620 },
621 withCredentials: true
622 }).success(function(response){
623 var json = JSON.stringify(response);
624 var content = JSON.parse(json);
625 if(callback){
626 callback(content.data);
627 }else{
628 return content.data;
629 }
630 }).error(function(error){
631 console.log(error);
632 });
633 },
634 getBMCInfo: function(callback){
635 $http({
636 method: 'GET',
Iftekharul Islamf2d74642017-07-10 16:42:14 -0500637 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc",
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500638 headers: {
639 'Accept': 'application/json',
640 'Content-Type': 'application/json'
641 },
642 withCredentials: true
643 }).success(function(response){
644 var json = JSON.stringify(response);
645 var content = JSON.parse(json);
646 if(callback){
647 callback(content.data);
648 }else{
649 return content.data;
650 }
651 }).error(function(error){
652 console.log(error);
653 });
654 },
Iftekharul Islamee27d752017-07-05 15:54:31 -0500655 getHardwares: function(callback){
656 $http({
657 method: 'GET',
Iftekharul Islamf2d74642017-07-10 16:42:14 -0500658 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory/enumerate",
Iftekharul Islamee27d752017-07-05 15:54:31 -0500659 headers: {
660 'Accept': 'application/json',
661 'Content-Type': 'application/json'
662 },
663 withCredentials: true
664 }).success(function(response){
665 var json = JSON.stringify(response);
666 var content = JSON.parse(json);
667 var hardwareData = [];
668 var keyIndexMap = {};
669 var title = "";
670 var data = [];
671 var searchText = "";
672 var componentIndex = -1;
673 var tempParts = [];
674
675
676 function isSubComponent(key){
677
678 for(var i = 0; i < Constants.HARDWARE.parent_components.length; i++){
679 if(key.split(Constants.HARDWARE.parent_components[i]).length == 2) return true;
680 }
681
682 return false;
683 }
684
685 function titlelize(title){
686 title = title.replace(/([A-Z0-9]+)/g, " $1").replace(/^\s+/, "");
687 for(var i = 0; i < Constants.HARDWARE.uppercase_titles.length; i++){
688 if(title.toLowerCase().indexOf((Constants.HARDWARE.uppercase_titles[i] + " ")) > -1){
689 return title.toUpperCase();
690 }
691 }
692
693 return title;
694 }
695
696 function camelcaseToLabel(obj){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500697 var transformed = [], label = "", value = "";
Iftekharul Islamee27d752017-07-05 15:54:31 -0500698 for(var key in obj){
699 label = key.replace(/([A-Z0-9]+)/g, " $1").replace(/^\s+/, "");
700 if(obj[key] !== ""){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500701 value = obj[key];
702 if(value == 1 || value == 0){
703 value = (value == 1) ? 'Yes' : 'No';
704 }
705 transformed.push({key:label, value: value});
Iftekharul Islamee27d752017-07-05 15:54:31 -0500706 }
707 }
708
709 return transformed;
710 }
711
712 function getSearchText(data){
713 var searchText = "";
714 for(var i = 0; i < data.length; i++){
715 searchText += " " + data[i].key + " " + data[i].value;
716 }
717
718 return searchText;
719 }
720
721 for(var key in content.data){
722 if(content.data.hasOwnProperty(key) &&
723 key.indexOf(Constants.HARDWARE.component_key_filter) == 0){
724
725 data = camelcaseToLabel(content.data[key]);
726 searchText = getSearchText(data);
727 title = key.split("/").pop();
728
729 title = titlelize(title);
730
731 if(!isSubComponent(key)){
732 hardwareData.push(Object.assign({
733 path: key,
734 title: title,
735 selected: false,
736 expanded: false,
737 search_text: title.toLowerCase() + " " + searchText.toLowerCase(),
738 sub_components: [],
739 original_data: {key: key, value: content.data[key]}
740 }, {items: data}));
741
742 keyIndexMap[key] = hardwareData.length - 1;
743 }else{
744 var tempParts = key.split("/");
745 tempParts.pop();
746 tempParts = tempParts.join("/");
747 componentIndex = keyIndexMap[tempParts];
748 data = content.data[key];
749 data.title = title;
750 hardwareData[componentIndex].sub_components.push(data);
751 hardwareData[componentIndex].search_text += " " + title.toLowerCase();
752 }
753 }
754 }
755
756 if(callback){
757 callback(hardwareData, content.data);
758 }else{
759 return { data: hardwareData, original_data: content.data};
760 }
761 });
762 },
Iftekharul Islamf2d74642017-07-10 16:42:14 -0500763 deleteLogs: function(logs) {
764 var defer = $q.defer();
765 var promises = [];
766
767 function finished(){
768 defer.resolve();
769 }
770
771 logs.forEach(function(item){
772 promises.push($http({
773 method: 'POST',
774 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/logging/entry/"+item.Id+"/action/Delete",
775 headers: {
776 'Accept': 'application/json',
777 'Content-Type': 'application/json'
778 },
779 withCredentials: true,
780 data: JSON.stringify({"data": []})
781 }));
782 });
783
784 $q.all(promises).then(finished);
785
786 return defer.promise;
787 },
788 resolveLogs: function(logs) {
789 var defer = $q.defer();
790 var promises = [];
791
792 function finished(){
793 defer.resolve();
794 }
795
796 logs.forEach(function(item){
797 promises.push($http({
798 method: 'PUT',
799 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/logging/entry/"+item.Id+"/attr/Resolved",
800 headers: {
801 'Accept': 'application/json',
802 'Content-Type': 'application/json'
803 },
804 withCredentials: true,
805 data: JSON.stringify({"data": "1"})
806 }));
807 });
808
809 $q.all(promises).then(finished);
810
811 return defer.promise;
812 },
Michael Davisdfad5d52017-07-20 14:53:46 -0500813=======
814 }
815>>>>>>> 4c1a3dd... Major update to code structure
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500816 };
817 return SERVICE;
818 }]);
819
820 })(window.angular);