1/**
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', '$q', function($http, Constants, $q){
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,
22              LED_STATE: Constants.LED_STATE,
23              LED_STATE_TEXT: Constants.LED_STATE_TEXT,
24              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              },
58              getNetworkInfo: function(){
59                var deferred = $q.defer();
60                $http({
61                  method: 'GET',
62                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/network/enumerate",
63                  headers: {
64                      'Accept': 'application/json',
65                      'Content-Type': 'application/json'
66                  },
67                  withCredentials: true
68                }).success(function(response){
69                    var json = JSON.stringify(response);
70                    var content = JSON.parse(json);
71                    var hostname = "";
72                    var macAddress = "";
73
74                    if(content.data.hasOwnProperty('/xyz/openbmc_project/network/config') &&
75                      content.data['/xyz/openbmc_project/network/config'].hasOwnProperty('HostName')
76                      ){
77                      hostname = content.data['/xyz/openbmc_project/network/config'].HostName;
78                    }
79
80                    if(content.data.hasOwnProperty('/xyz/openbmc_project/network/eth0') &&
81                      content.data['/xyz/openbmc_project/network/eth0'].hasOwnProperty('MACAddress')
82                      ){
83                      macAddress = content.data['/xyz/openbmc_project/network/eth0'].MACAddress;
84                    }
85
86                    deferred.resolve({
87                      data: content.data,
88                      hostname: hostname,
89                      mac_address: macAddress,
90                    });
91                }).error(function(error){
92                  console.log(error);
93                  deferred.reject(error);
94                });
95                return deferred.promise;
96              },
97              getLEDState: function(){
98                var deferred = $q.defer();
99                $http({
100                  method: 'GET',
101                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/led/groups/enclosure_identify",
102                  headers: {
103                      'Accept': 'application/json',
104                      'Content-Type': 'application/json'
105                  },
106                  withCredentials: true
107                }).success(function(response){
108                    var json = JSON.stringify(response);
109                    var content = JSON.parse(json);
110                    deferred.resolve(content.data.Asserted);
111                }).error(function(error){
112                  console.log(error);
113                  deferred.reject(error);
114                });
115                return deferred.promise;
116              },
117              login: function(username, password, callback){
118                $http({
119                  method: 'POST',
120                  url: SERVICE.API_CREDENTIALS.host + "/login",
121                  headers: {
122                      'Accept': 'application/json',
123                      'Content-Type': 'application/json'
124                  },
125                  withCredentials: true,
126                  data: JSON.stringify({"data": [username, password]})
127                }).success(function(response){
128                  if(callback){
129                      callback(response);
130                  }
131                }).error(function(error){
132                  if(callback){
133                      if(error && error.status && error.status == 'error'){
134                        callback(error);
135                      }else{
136                        callback(error, true);
137                      }
138                  }
139                  console.log(error);
140                });
141              },
142              logout: function(callback){
143                $http({
144                  method: 'POST',
145                  url: SERVICE.API_CREDENTIALS.host + "/logout",
146                  headers: {
147                      'Accept': 'application/json',
148                      'Content-Type': 'application/json'
149                  },
150                  withCredentials: true,
151                  data: JSON.stringify({"data": []})
152                }).success(function(response){
153                  if(callback){
154                      callback(response);
155                  }
156                }).error(function(error){
157                  if(callback){
158                      callback(null, error);
159                  }
160                  console.log(error);
161                });
162              },
163              chassisPowerOn: 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              },
187              chassisPowerOff: function(callback){
188                $http({
189                  method: 'POST',
190                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
191                  headers: {
192                      'Accept': 'application/json',
193                      'Content-Type': 'application/json'
194                  },
195                  withCredentials: true,
196                  data: JSON.stringify({"data": []})
197                }).success(function(response){
198                      var json = JSON.stringify(response);
199                      var content = JSON.parse(json);
200                      if(callback){
201                          return callback(content.data.CurrentPowerState);
202                      }
203                }).error(function(error){
204                  if(callback){
205                      callback(error);
206                  }else{
207                      console.log(error);
208                  }
209                });
210              },
211              setLEDState: function(state, callback){
212                $http({
213                  method: 'PUT',
214                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted",
215                  headers: {
216                      'Accept': 'application/json',
217                      'Content-Type': 'application/json'
218                  },
219                  withCredentials: true,
220                  data: JSON.stringify({"data": state})
221                }).success(function(response){
222                      var json = JSON.stringify(response);
223                      var content = JSON.parse(json);
224                      if(callback){
225                          return callback(content.status);
226                      }
227                }).error(function(error){
228                  if(callback){
229                      callback(error);
230                  }else{
231                      console.log(error);
232                  }
233                });
234              },
235              bmcReboot: function(callback){
236                $http({
237                  method: 'PUT',
238                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/bmc0/attr/RequestedBmcTransition",
239                  headers: {
240                      'Accept': 'application/json',
241                      'Content-Type': 'application/json'
242                  },
243                  withCredentials: true,
244                  data: JSON.stringify({"data": "xyz.openbmc_project.State.BMC.Transition.Reboot"})
245                }).success(function(response){
246                      var json = JSON.stringify(response);
247                      var content = JSON.parse(json);
248                      if(callback){
249                          return callback(content.status);
250                      }
251                }).error(function(error){
252                  if(callback){
253                      callback(error);
254                  }else{
255                      console.log(error);
256                  }
257                });
258              },
259              hostPowerOn: function(callback){
260                $http({
261                  method: 'PUT',
262                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
263                  headers: {
264                      'Accept': 'application/json',
265                      'Content-Type': 'application/json'
266                  },
267                  withCredentials: true,
268                  data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.On"})
269                }).success(function(response){
270                      var json = JSON.stringify(response);
271                      var content = JSON.parse(json);
272                      if(callback){
273                          return callback(content.status);
274                      }
275                }).error(function(error){
276                  if(callback){
277                      callback(error);
278                  }else{
279                      console.log(error);
280                  }
281                });
282              },
283              hostPowerOff: function(callback){
284                $http({
285                  method: 'PUT',
286                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
287                  headers: {
288                      'Accept': 'application/json',
289                      'Content-Type': 'application/json'
290                  },
291                  withCredentials: true,
292                  data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Off"})
293                }).success(function(response){
294                      var json = JSON.stringify(response);
295                      var content = JSON.parse(json);
296                      if(callback){
297                          return callback(content.status);
298                      }
299                }).error(function(error){
300                  if(callback){
301                      callback(error);
302                  }else{
303                      console.log(error);
304                  }
305                });
306              },
307              hostReboot: function(callback){
308                $http({
309                  method: 'POST',
310                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
311                  headers: {
312                      'Accept': 'application/json',
313                      'Content-Type': 'application/json'
314                  },
315                  withCredentials: true,
316                  data: JSON.stringify({"data": []}),
317                }).success(function(response){
318                      var json = JSON.stringify(response);
319                      var content = JSON.parse(json);
320                      if(callback){
321                          return callback(content);
322                      }
323                }).error(function(error){
324                  if(callback){
325                      callback(error);
326                  }else{
327                      console.log(error);
328                  }
329                });
330              },
331              hostShutdown: function(callback){
332                $http({
333                  method: 'POST',
334                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
335                  headers: {
336                      'Accept': 'application/json',
337                      'Content-Type': 'application/json'
338                  },
339                  withCredentials: true,
340                  data: JSON.stringify({"data": []})
341                }).success(function(response){
342                      var json = JSON.stringify(response);
343                      var content = JSON.parse(json);
344                      if(callback){
345                          return callback(content);
346                      }
347                }).error(function(error){
348                  if(callback){
349                      callback(error);
350                  }else{
351                      console.log(error);
352                  }
353                });
354              },
355              getLogs: function(){
356                var deferred = $q.defer();
357                $http({
358                  method: 'GET',
359                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/logging/enumerate",
360                  headers: {
361                      'Accept': 'application/json',
362                      'Content-Type': 'application/json'
363                  },
364                  withCredentials: true
365                }).success(function(response){
366                      var json = JSON.stringify(response);
367                      var content = JSON.parse(json);
368                      var dataClone = JSON.parse(JSON.stringify(content.data));
369                      var data = [];
370                      var severityCode = '';
371                      var priority = '';
372                      var health = '';
373                      var relatedItems = [];
374
375                      for(var key in content.data){
376                        if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Id')){
377                          var severityFlags = {low: false, medium: false, high: false};
378                          var healthFlags = {critical: false, warning: false, good: false};
379                          severityCode = content.data[key].Severity.split(".").pop();
380                          priority = Constants.SEVERITY_TO_PRIORITY_MAP[severityCode];
381                          severityFlags[priority.toLowerCase()] = true;
382                          health = Constants.SEVERITY_TO_HEALTH_MAP[severityCode];
383                          healthFlags[health.toLowerCase()] = true;
384                          relatedItems = [];
385                          content.data[key].associations.forEach(function(item){
386                            relatedItems.push(item[2]);
387                          });
388
389                          data.push(Object.assign({
390                            path: key,
391                            copied: false,
392                            priority: priority,
393                            severity_code: severityCode,
394                            severity_flags: severityFlags,
395                            health_flags: healthFlags,
396                            additional_data: content.data[key].AdditionalData.join("\n"),
397                            selected: false,
398                            search_text: ("#" + content.data[key].Id + " " + severityCode + " " + content.data[key].Severity + " " + content.data[key].AdditionalData.join(" ")).toLowerCase(),
399                            meta: false,
400                            confirm: false,
401                            related_items: relatedItems,
402                            data: {key: key, value: content.data[key]}
403                          }, content.data[key]));
404                        }
405                      }
406                      deferred.resolve({data: data, original: dataClone});
407                }).error(function(error){
408                  console.log(error);
409                  deferred.reject(error);
410                });
411
412                return deferred.promise;
413              },
414              getAllSensorStatus: function(callback){
415                $http({
416                  method: 'GET',
417                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/sensors/enumerate",
418                  headers: {
419                      'Accept': 'application/json',
420                      'Content-Type': 'application/json'
421                  },
422                  withCredentials: true
423                }).success(function(response){
424                      var json = JSON.stringify(response);
425                      var content = JSON.parse(json);
426                      var dataClone = JSON.parse(JSON.stringify(content.data));
427                      var sensorData = [];
428                      var severity = {};
429                      var title = "";
430                      var tempKeyParts = [];
431                      var order = 0;
432
433                      function getScaledValue(value, scale){
434                        scale = scale + "";
435                        scale = parseInt(scale, 10);
436                        var power = Math.abs(parseInt(scale,10));
437
438                        if(scale > 0){
439                          value = value * Math.pow(10, power);
440                        }else if(scale < 0){
441                          value = value / Math.pow(10, power);
442                        }
443                        return value;
444                      }
445
446                      function getSensorStatus(reading){
447                        var severityFlags = {critical: false, warning: false, normal: false}, severityText = '', order = 0;
448
449                        if(reading.hasOwnProperty('CriticalLow') &&
450                          reading.Value < reading.CriticalLow
451                          ){
452                          severityFlags.critical = true;
453                          severityText = 'critical';
454                          order = 2;
455                        }else if(reading.hasOwnProperty('CriticalHigh') &&
456                          reading.Value > reading.CriticalHigh
457                          ){
458                          severityFlags.critical = true;
459                          severityText = 'critical';
460                          order = 2;
461                        }else if(reading.hasOwnProperty('CriticalLow') &&
462                          reading.hasOwnProperty('WarningLow') &&
463                          reading.Value >= reading.CriticalLow && reading.Value <= reading.WarningLow){
464                          severityFlags.warning = true;
465                          severityText = 'warning';
466                          order = 1;
467                        }else if(reading.hasOwnProperty('WarningHigh') &&
468                          reading.hasOwnProperty('CriticalHigh') &&
469                          reading.Value >= reading.WarningHigh && reading.Value <= reading.CriticalHigh){
470                          severityFlags.warning = true;
471                          severityText = 'warning';
472                          order = 1;
473                        }else{
474                          severityFlags.normal = true;
475                          severityText = 'normal';
476                        }
477                        return { flags: severityFlags, severityText: severityText, order: order};
478                      }
479
480                      for(var key in content.data){
481                        if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Unit')){
482
483                          severity = getSensorStatus(content.data[key]);
484
485                          if(!content.data[key].hasOwnProperty('CriticalLow')){
486                            content.data[key].CriticalLow = "--";
487                            content.data[key].CriticalHigh = "--";
488                          }
489
490                          if(!content.data[key].hasOwnProperty('WarningLow')){
491                            content.data[key].WarningLow = "--";
492                            content.data[key].WarningHigh = "--";
493                          }
494
495                          tempKeyParts = key.split("/");
496                          title = tempKeyParts.pop();
497                          title = tempKeyParts.pop() + '_' + title;
498                          title = title.split("_").map(function(item){
499                             return item.toLowerCase().charAt(0).toUpperCase() + item.slice(1);
500                          }).reduce(function(prev, el){
501                            return prev + " " + el;
502                          });
503
504                          content.data[key].Value = getScaledValue(content.data[key].Value, content.data[key].Scale);
505
506                          sensorData.push(Object.assign({
507                            path: key,
508                            selected: false,
509                            confirm: false,
510                            copied: false,
511                            title: title,
512                            unit: Constants.SENSOR_UNIT_MAP[content.data[key].Unit],
513                            severity_flags: severity.flags,
514                            status: severity.severityText,
515                            order: severity.order,
516                            search_text: (title + " " + content.data[key].Value + " " +
517                               Constants.SENSOR_UNIT_MAP[content.data[key].Unit] + " " +
518                               severity.severityText + " " +
519                               content.data[key].CriticalLow + " " +
520                               content.data[key].CriticalHigh + " " +
521                               content.data[key].WarningLow + " " +
522                               content.data[key].WarningHigh + " "
523                               ).toLowerCase(),
524                            original_data: {key: key, value: content.data[key]}
525                          }, content.data[key]));
526                        }
527                      }
528
529                      callback(sensorData, dataClone);
530                }).error(function(error){
531                  console.log(error);
532                });
533              },
534              getFirmwares: function(){
535                var deferred = $q.defer();
536                $http({
537                  method: 'GET',
538                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/software/enumerate",
539                  headers: {
540                      'Accept': 'application/json',
541                      'Content-Type': 'application/json'
542                  },
543                  withCredentials: true
544                }).success(function(response){
545                      var json = JSON.stringify(response);
546                      var content = JSON.parse(json);
547                      var data = [];
548                      var active = false;
549                      var isExtended = false;
550                      var bmcActiveVersion = "";
551                      var hostActiveVersion = "";
552                      var imageType = "";
553                      var extendedVersions = [];
554
555                      function getFormatedExtendedVersions(extendedVersion){
556                        var versions = [];
557                        extendedVersion = extendedVersion.split(",");
558
559                        extendedVersion.forEach(function(item){
560                          var parts = item.split("-");
561                          var numberIndex = 0;
562                          for(var i = 0; i < parts.length; i++){
563                            if(/[0-9]/.test(parts[i])){
564                              numberIndex = i;
565                              break;
566                            }
567                          }
568                          var titlePart = parts.splice(0, numberIndex);
569                          titlePart = titlePart.join("");
570                          titlePart = titlePart[0].toUpperCase() + titlePart.substr(1, titlePart.length);
571                          var versionPart = parts.join("-");
572                          versions.push({
573                            title: titlePart,
574                            version: versionPart
575                          });
576                        });
577
578                        return versions;
579                      }
580
581                      for(var key in content.data){
582                        if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Version')){
583                          active = (/\.Active$/).test(content.data[key].Activation);
584                          imageType = content.data[key].Purpose.split(".").pop();
585                          isExtended = content.data[key].hasOwnProperty('ExtendedVersion') && content.data[key].ExtendedVersion != "";
586                          if(isExtended){
587                            extendedVersions = getFormatedExtendedVersions(content.data[key].ExtendedVersion);
588                          }
589                          data.push(Object.assign({
590                            path: key,
591                            active: active,
592                            imageId: key.split("/").pop(),
593                            imageType: imageType,
594                            isExtended: isExtended,
595                            extended: {
596                              show: false,
597                              versions: extendedVersions
598                            },
599                            data: {key: key, value: content.data[key]}
600                          }, content.data[key]));
601
602                          if(active && imageType == 'BMC'){
603                            bmcActiveVersion = content.data[key].Version;
604                          }
605
606                          if(active && imageType == 'Host'){
607                            hostActiveVersion = content.data[key].Version;
608                          }
609                        }
610                      }
611
612                      deferred.resolve({
613                          data: data,
614                          bmcActiveVersion: bmcActiveVersion,
615                          hostActiveVersion: hostActiveVersion
616                      });
617                }).error(function(error){
618                  console.log(error);
619                  deferred.reject(error);
620                });
621
622                return deferred.promise;
623              },
624              uploadImage: function(file, callback){
625                $http({
626                  method: 'PUT',
627                  timeout: 5 * 60 * 1000,
628                  //url: 'http://localhost:3002/upload',
629                  url: SERVICE.API_CREDENTIALS.host + "/upload/image/",
630                  headers: {
631                      'Accept': 'application/octet-stream',
632                      'Content-Type': 'application/octet-stream'
633                  },
634                  withCredentials: true,
635                  data: file
636                }).success(function(response){
637                      var json = JSON.stringify(response);
638                      var content = JSON.parse(json);
639                      if(callback){
640                          return callback(content);
641                      }
642                }).error(function(error){
643                  if(callback){
644                      callback(error);
645                  }else{
646                      console.log(error);
647                  }
648                });
649              },
650              getBMCEthernetInfo: function(){
651                var deferred = $q.defer();
652                $http({
653                  method: 'GET',
654                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc/ethernet",
655                  headers: {
656                      'Accept': 'application/json',
657                      'Content-Type': 'application/json'
658                  },
659                  withCredentials: true
660                }).success(function(response){
661                    var json = JSON.stringify(response);
662                    var content = JSON.parse(json);
663                    deferred.resolve(content.data);
664                }).error(function(error){
665                  console.log(error);
666                  deferred.reject(error);
667                });
668
669                return deferred.promise;
670              },
671              getBMCInfo: function(callback){
672                var deferred = $q.defer();
673                $http({
674                  method: 'GET',
675                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc",
676                  headers: {
677                      'Accept': 'application/json',
678                      'Content-Type': 'application/json'
679                  },
680                  withCredentials: true
681                }).success(function(response){
682                    var json = JSON.stringify(response);
683                    var content = JSON.parse(json);
684                    deferred.resolve(content.data);
685                }).error(function(error){
686                  console.log(error);
687                  deferred.reject(error);
688                });
689                return deferred.promise;
690              },
691              getHardwares: function(callback){
692                $http({
693                  method: 'GET',
694                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/inventory/enumerate",
695                  headers: {
696                      'Accept': 'application/json',
697                      'Content-Type': 'application/json'
698                  },
699                  withCredentials: true
700                }).success(function(response){
701                      var json = JSON.stringify(response);
702                      var content = JSON.parse(json);
703                      var hardwareData = [];
704                      var keyIndexMap = {};
705                      var title = "";
706                      var data = [];
707                      var searchText = "";
708                      var componentIndex = -1;
709                      var tempParts = [];
710
711
712                      function isSubComponent(key){
713
714                        for(var i = 0; i < Constants.HARDWARE.parent_components.length; i++){
715                          if(key.split(Constants.HARDWARE.parent_components[i]).length == 2) return true;
716                        }
717
718                        return false;
719                      }
720
721                      function titlelize(title){
722                        title = title.replace(/([A-Z0-9]+)/g, " $1").replace(/^\s+/, "");
723                        for(var i = 0; i < Constants.HARDWARE.uppercase_titles.length; i++){
724                          if(title.toLowerCase().indexOf((Constants.HARDWARE.uppercase_titles[i] + " ")) > -1){
725                            return title.toUpperCase();
726                          }
727                        }
728
729                        return title;
730                      }
731
732                      function camelcaseToLabel(obj){
733                        var transformed = [], label = "", value = "";
734                        for(var key in obj){
735                          label = key.replace(/([A-Z0-9]+)/g, " $1").replace(/^\s+/, "");
736                          if(obj[key] !== ""){
737                            value = obj[key];
738                            if(value == 1 || value == 0){
739                              value = (value == 1) ? 'Yes' : 'No';
740                            }
741                            transformed.push({key:label, value: value});
742                          }
743                        }
744
745                        return transformed;
746                      }
747
748                      function getSearchText(data){
749                        var searchText = "";
750                        for(var i = 0; i < data.length; i++){
751                          searchText += " " + data[i].key + " " + data[i].value;
752                        }
753
754                        return searchText;
755                      }
756
757                      for(var key in content.data){
758                        if(content.data.hasOwnProperty(key) &&
759                           key.indexOf(Constants.HARDWARE.component_key_filter) == 0){
760
761                          data = camelcaseToLabel(content.data[key]);
762                          searchText = getSearchText(data);
763                          title = key.split("/").pop();
764
765                          title = titlelize(title);
766
767                          if(!isSubComponent(key)){
768                              hardwareData.push(Object.assign({
769                                path: key,
770                                title: title,
771                                selected: false,
772                                expanded: false,
773                                search_text: title.toLowerCase() + " " + searchText.toLowerCase(),
774                                sub_components: [],
775                                original_data: {key: key, value: content.data[key]}
776                              }, {items: data}));
777
778                              keyIndexMap[key] = hardwareData.length - 1;
779                          }else{
780                            var tempParts = key.split("/");
781                            tempParts.pop();
782                            tempParts = tempParts.join("/");
783                            componentIndex = keyIndexMap[tempParts];
784                            data = content.data[key];
785                            data.title = title;
786                            hardwareData[componentIndex].sub_components.push(data);
787                            hardwareData[componentIndex].search_text += " " + title.toLowerCase();
788                          }
789                      }
790                    }
791
792                    if(callback){
793                       callback(hardwareData, content.data);
794                    }else{
795                       return { data: hardwareData, original_data: content.data};
796                    }
797                });
798              },
799              deleteLogs: function(logs) {
800                  var defer = $q.defer();
801                  var promises = [];
802
803                  function finished(){
804                      defer.resolve();
805                  }
806
807                  logs.forEach(function(item){
808                    promises.push($http({
809                                      method: 'POST',
810                                      url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/logging/entry/"+item.Id+"/action/Delete",
811                                      headers: {
812                                          'Accept': 'application/json',
813                                          'Content-Type': 'application/json'
814                                      },
815                                      withCredentials: true,
816                                      data: JSON.stringify({"data": []})
817                                 }));
818                  });
819
820                  $q.all(promises).then(finished);
821
822                  return defer.promise;
823              },
824              resolveLogs: function(logs) {
825                  var defer = $q.defer();
826                  var promises = [];
827
828                  function finished(){
829                      defer.resolve();
830                  }
831
832                  logs.forEach(function(item){
833                    promises.push($http({
834                                      method: 'PUT',
835                                      url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/logging/entry/"+item.Id+"/attr/Resolved",
836                                      headers: {
837                                          'Accept': 'application/json',
838                                          'Content-Type': 'application/json'
839                                      },
840                                      withCredentials: true,
841                                      data: JSON.stringify({"data": "1"})
842                                 }));
843                  });
844
845                  $q.all(promises).then(finished);
846
847                  return defer.promise;
848              },
849          };
850          return SERVICE;
851        }]);
852
853        })(window.angular);
854