199d199f3SIftekharul Islam/**
299d199f3SIftekharul Islam * API utilities service
399d199f3SIftekharul Islam *
499d199f3SIftekharul Islam * @module app/common/services/api-utils
599d199f3SIftekharul Islam * @exports APIUtils
699d199f3SIftekharul Islam * @name APIUtils
799d199f3SIftekharul Islam */
899d199f3SIftekharul Islam
999d199f3SIftekharul Islamwindow.angular && (function(angular) {
1099d199f3SIftekharul Islam  'use strict';
11d27bb135SAndrew Geissler  angular.module('app.common.services').factory('APIUtils', [
126a8d180fSJames Feist    '$http', '$cookies', 'Constants', '$q', 'dataService', '$interval',
136a8d180fSJames Feist    function($http, $cookies, Constants, $q, DataService, $interval) {
1499d199f3SIftekharul Islam      var SERVICE = {
1599d199f3SIftekharul Islam        API_CREDENTIALS: Constants.API_CREDENTIALS,
1699d199f3SIftekharul Islam        API_RESPONSE: Constants.API_RESPONSE,
1799d199f3SIftekharul Islam        HOST_STATE_TEXT: Constants.HOST_STATE,
18cd789508SIftekharul Islam        LED_STATE: Constants.LED_STATE,
19cd789508SIftekharul Islam        LED_STATE_TEXT: Constants.LED_STATE_TEXT,
201acb412dSIftekharul Islam        HOST_SESSION_STORAGE_KEY: Constants.API_CREDENTIALS.host_storage_key,
216549114eSGunnar Mills        validIPV4IP: function(ip) {
226549114eSGunnar Mills          // Checks for [0-255].[0-255].[0-255].[0-255]
236549114eSGunnar Mills          return ip.match(
246549114eSGunnar Mills              /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/);
256549114eSGunnar Mills        },
26b1289ec9SAppaRao Puli        getRedfishSysName: function() {
27b1289ec9SAppaRao Puli          return $http({
28b1289ec9SAppaRao Puli                   method: 'GET',
29b1289ec9SAppaRao Puli                   url: DataService.getHost() + '/redfish/v1/Systems',
30b1289ec9SAppaRao Puli                   withCredentials: true
31b1289ec9SAppaRao Puli                 })
32b1289ec9SAppaRao Puli              .then(
33b1289ec9SAppaRao Puli                  function(response) {
34b1289ec9SAppaRao Puli                    var sysUrl = response.data['Members'][0]['@odata.id'];
35b1289ec9SAppaRao Puli                    return sysUrl.split('/').pop(-1);
36b1289ec9SAppaRao Puli                  },
37b1289ec9SAppaRao Puli                  function(error) {
38b1289ec9SAppaRao Puli                    console.log(JSON.stringify(error));
39b1289ec9SAppaRao Puli                  });
40b1289ec9SAppaRao Puli        },
41b1289ec9SAppaRao Puli        getSystemLogs: function(recordType) {
42b1289ec9SAppaRao Puli          var uri = '/redfish/v1/Systems/' + DataService.systemName +
43b1289ec9SAppaRao Puli              '/LogServices/EventLog/Entries';
44494c6edbSTim Lee          if (recordType == 'Oem') {
45494c6edbSTim Lee            var uri = '/redfish/v1/Systems/' + DataService.systemName +
46494c6edbSTim Lee                '/LogServices/Crashdump/Entries';
47494c6edbSTim Lee          }
48b1289ec9SAppaRao Puli          return $http({
49b1289ec9SAppaRao Puli                   method: 'GET',
50b1289ec9SAppaRao Puli                   url: DataService.getHost() + uri,
51b1289ec9SAppaRao Puli                   withCredentials: true
52b1289ec9SAppaRao Puli                 })
53b1289ec9SAppaRao Puli              .then(
54b1289ec9SAppaRao Puli                  function(response) {
55b1289ec9SAppaRao Puli                    var logEntries = [];
56b1289ec9SAppaRao Puli                    angular.forEach(response.data['Members'], function(log) {
57b1289ec9SAppaRao Puli                      if (log.hasOwnProperty('EntryType')) {
58b1289ec9SAppaRao Puli                        if (log['EntryType'] == recordType) {
59b1289ec9SAppaRao Puli                          logEntries.push(log);
60b1289ec9SAppaRao Puli                        }
61b1289ec9SAppaRao Puli                      }
62b1289ec9SAppaRao Puli                    });
63b1289ec9SAppaRao Puli                    return logEntries;
64b1289ec9SAppaRao Puli                  },
65b1289ec9SAppaRao Puli                  function(error) {
66b1289ec9SAppaRao Puli                    console.log(JSON.stringify(error));
67b1289ec9SAppaRao Puli                  });
68b1289ec9SAppaRao Puli        },
69494c6edbSTim Lee        clearSystemLogs: function(selectedRecordType) {
70b1289ec9SAppaRao Puli          var uri = '/redfish/v1/Systems/' + DataService.systemName +
71b1289ec9SAppaRao Puli              '/LogServices/EventLog/Actions/LogService.ClearLog';
72494c6edbSTim Lee          if (selectedRecordType == 'Oem') {
73494c6edbSTim Lee            var uri = '/redfish/v1/Systems/' + DataService.systemName +
74494c6edbSTim Lee                '/LogServices/Crashdump/Actions/LogService.ClearLog';
75494c6edbSTim Lee          }
76b1289ec9SAppaRao Puli          return $http({
77b1289ec9SAppaRao Puli            method: 'POST',
78b1289ec9SAppaRao Puli            url: DataService.getHost() + uri,
79b1289ec9SAppaRao Puli            withCredentials: true
80b1289ec9SAppaRao Puli          });
81b1289ec9SAppaRao Puli        },
82854fbba1SGunnar Mills        deleteObject: function(path) {
83854fbba1SGunnar Mills          return $http({
84854fbba1SGunnar Mills                   method: 'POST',
85854fbba1SGunnar Mills                   url: DataService.getHost() + path + '/action/Delete',
86854fbba1SGunnar Mills                   withCredentials: true,
87854fbba1SGunnar Mills                   data: JSON.stringify({'data': []})
88854fbba1SGunnar Mills                 })
89854fbba1SGunnar Mills              .then(function(response) {
90854fbba1SGunnar Mills                return response.data;
91854fbba1SGunnar Mills              });
92854fbba1SGunnar Mills        },
93a1d238f3SIftekharul Islam        getHostState: function() {
94a1d238f3SIftekharul Islam          var deferred = $q.defer();
9599d199f3SIftekharul Islam          $http({
9699d199f3SIftekharul Islam            method: 'GET',
97d27bb135SAndrew Geissler            url: DataService.getHost() +
98d27bb135SAndrew Geissler                '/xyz/openbmc_project/state/host0/attr/CurrentHostState',
9999d199f3SIftekharul Islam            withCredentials: true
100d27bb135SAndrew Geissler          })
101d27bb135SAndrew Geissler              .then(
102d27bb135SAndrew Geissler                  function(response) {
103bbcf670aSEd Tanous                    var json = JSON.stringify(response.data);
10499d199f3SIftekharul Islam                    var content = JSON.parse(json);
105a1d238f3SIftekharul Islam                    deferred.resolve(content.data);
106d27bb135SAndrew Geissler                  },
107d27bb135SAndrew Geissler                  function(error) {
10899d199f3SIftekharul Islam                    console.log(error);
109a1d238f3SIftekharul Islam                    deferred.reject(error);
11099d199f3SIftekharul Islam                  });
111a1d238f3SIftekharul Islam          return deferred.promise;
11299d199f3SIftekharul Islam        },
113ff64c54aSGunnar Mills        getSNMPManagers: function() {
114ff64c54aSGunnar Mills          return $http({
115ff64c54aSGunnar Mills                   method: 'GET',
116ff64c54aSGunnar Mills                   url: DataService.getHost() +
117ff64c54aSGunnar Mills                       '/xyz/openbmc_project/network/snmp/manager/enumerate',
118ff64c54aSGunnar Mills                   withCredentials: true
119ff64c54aSGunnar Mills                 })
120ff64c54aSGunnar Mills              .then(function(response) {
121ff64c54aSGunnar Mills                return response.data;
122ff64c54aSGunnar Mills              });
123ff64c54aSGunnar Mills        },
124c3abaa9bSbeccabroek        pollHostStatusTillOn: function() {
125c3abaa9bSbeccabroek          var deferred = $q.defer();
126c3abaa9bSbeccabroek          var hostOnTimeout = setTimeout(function() {
127c3abaa9bSbeccabroek            ws.close();
128c3abaa9bSbeccabroek            deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_ON_TIMEOUT));
129c3abaa9bSbeccabroek          }, Constants.TIMEOUT.HOST_ON);
1306a8d180fSJames Feist          var token = $cookies.get('XSRF-TOKEN');
1316a8d180fSJames Feist          var ws = new WebSocket(
1326a8d180fSJames Feist              'wss://' + DataService.server_id + '/subscribe', [token]);
133c3abaa9bSbeccabroek          var data = JSON.stringify({
134c3abaa9bSbeccabroek            'paths': ['/xyz/openbmc_project/state/host0'],
135c3abaa9bSbeccabroek            'interfaces': ['xyz.openbmc_project.State.Host']
136c3abaa9bSbeccabroek          });
137c3abaa9bSbeccabroek          ws.onopen = function() {
138c3abaa9bSbeccabroek            ws.send(data);
139c3abaa9bSbeccabroek          };
140c3abaa9bSbeccabroek          ws.onmessage = function(evt) {
141c3abaa9bSbeccabroek            var content = JSON.parse(evt.data);
142c3abaa9bSbeccabroek            var hostState = content.properties.CurrentHostState;
143c3abaa9bSbeccabroek            if (hostState === Constants.HOST_STATE_TEXT.on_code) {
144c3abaa9bSbeccabroek              clearTimeout(hostOnTimeout);
145c3abaa9bSbeccabroek              ws.close();
146c3abaa9bSbeccabroek              deferred.resolve();
147c3abaa9bSbeccabroek            } else if (hostState === Constants.HOST_STATE_TEXT.error_code) {
148c3abaa9bSbeccabroek              clearTimeout(hostOnTimeout);
149c3abaa9bSbeccabroek              ws.close();
150c3abaa9bSbeccabroek              deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_QUIESCED));
151c3abaa9bSbeccabroek            }
152c3abaa9bSbeccabroek          };
153c3abaa9bSbeccabroek        },
154c3abaa9bSbeccabroek
155c3abaa9bSbeccabroek        pollHostStatusTilReboot: function() {
156c3abaa9bSbeccabroek          var deferred = $q.defer();
157c3abaa9bSbeccabroek          var onState = Constants.HOST_STATE_TEXT.on_code;
158c3abaa9bSbeccabroek          var offState = Constants.HOST_STATE_TEXT.on_code;
159c3abaa9bSbeccabroek          var hostTimeout;
160c3abaa9bSbeccabroek          var setHostTimeout = function(message, timeout) {
161c3abaa9bSbeccabroek            hostTimeout = setTimeout(function() {
162c3abaa9bSbeccabroek              ws.close();
163c3abaa9bSbeccabroek              deferred.reject(new Error(message));
164c3abaa9bSbeccabroek            }, timeout);
165c3abaa9bSbeccabroek          };
1666a8d180fSJames Feist          var token = $cookies.get('XSRF-TOKEN');
1676a8d180fSJames Feist          var ws = new WebSocket(
1686a8d180fSJames Feist              'wss://' + DataService.server_id + '/subscribe', [token]);
169c3abaa9bSbeccabroek          var data = JSON.stringify({
170c3abaa9bSbeccabroek            'paths': ['/xyz/openbmc_project/state/host0'],
171c3abaa9bSbeccabroek            'interfaces': ['xyz.openbmc_project.State.Host']
172c3abaa9bSbeccabroek          });
173c3abaa9bSbeccabroek          ws.onopen = function() {
174c3abaa9bSbeccabroek            ws.send(data);
175c3abaa9bSbeccabroek          };
176c3abaa9bSbeccabroek          setHostTimeout(
177c3abaa9bSbeccabroek              Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT,
178c3abaa9bSbeccabroek              Constants.TIMEOUT.HOST_OFF);
179c3abaa9bSbeccabroek          var pollState = offState;
180c3abaa9bSbeccabroek          ws.onmessage = function(evt) {
181c3abaa9bSbeccabroek            var content = JSON.parse(evt.data);
182c3abaa9bSbeccabroek            var hostState = content.properties.CurrentHostState;
183c3abaa9bSbeccabroek            if (hostState === pollState) {
184c3abaa9bSbeccabroek              if (pollState === offState) {
185c3abaa9bSbeccabroek                clearTimeout(hostTimeout);
186c3abaa9bSbeccabroek                pollState = onState;
187c3abaa9bSbeccabroek                setHostTimeout(
188c3abaa9bSbeccabroek                    Constants.MESSAGES.POLL.HOST_ON_TIMEOUT,
189c3abaa9bSbeccabroek                    Constants.TIMEOUT.HOST_ON);
190c3abaa9bSbeccabroek              }
191c3abaa9bSbeccabroek              if (pollState === onState) {
192c3abaa9bSbeccabroek                clearTimeout(hostTimeout);
193c3abaa9bSbeccabroek                ws.close();
194c3abaa9bSbeccabroek                deferred.resolve();
195c3abaa9bSbeccabroek              }
196c3abaa9bSbeccabroek            } else if (hostState === Constants.HOST_STATE_TEXT.error_code) {
197c3abaa9bSbeccabroek              clearTimeout(hostTimeout);
198c3abaa9bSbeccabroek              ws.close();
199c3abaa9bSbeccabroek              deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_QUIESCED));
200c3abaa9bSbeccabroek            }
201c3abaa9bSbeccabroek          };
202c3abaa9bSbeccabroek        },
203c3abaa9bSbeccabroek
204c3abaa9bSbeccabroek        pollHostStatusTillOff: function() {
205c3abaa9bSbeccabroek          var deferred = $q.defer();
206c3abaa9bSbeccabroek          var hostOffTimeout = setTimeout(function() {
207c3abaa9bSbeccabroek            ws.close();
208c3abaa9bSbeccabroek            deferred.reject(
209c3abaa9bSbeccabroek                new Error(Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT));
210c3abaa9bSbeccabroek          }, Constants.TIMEOUT.HOST_OFF);
211c3abaa9bSbeccabroek
2126a8d180fSJames Feist          var token = $cookies.get('XSRF-TOKEN');
2136a8d180fSJames Feist          var ws = new WebSocket(
2146a8d180fSJames Feist              'wss://' + DataService.server_id + '/subscribe', [token]);
215c3abaa9bSbeccabroek          var data = JSON.stringify({
216c3abaa9bSbeccabroek            'paths': ['/xyz/openbmc_project/state/host0'],
217c3abaa9bSbeccabroek            'interfaces': ['xyz.openbmc_project.State.Host']
218c3abaa9bSbeccabroek          });
219c3abaa9bSbeccabroek          ws.onopen = function() {
220c3abaa9bSbeccabroek            ws.send(data);
221c3abaa9bSbeccabroek          };
222c3abaa9bSbeccabroek          ws.onmessage = function(evt) {
223c3abaa9bSbeccabroek            var content = JSON.parse(evt.data);
224c3abaa9bSbeccabroek            var hostState = content.properties.CurrentHostState;
225c3abaa9bSbeccabroek            if (hostState === Constants.HOST_STATE_TEXT.off_code) {
226c3abaa9bSbeccabroek              clearTimeout(hostOffTimeout);
227c3abaa9bSbeccabroek              ws.close();
228c3abaa9bSbeccabroek              deferred.resolve();
229c3abaa9bSbeccabroek            }
230c3abaa9bSbeccabroek          };
231c3abaa9bSbeccabroek        },
232854fbba1SGunnar Mills        addSNMPManager: function(address, port) {
233854fbba1SGunnar Mills          return $http({
234854fbba1SGunnar Mills                   method: 'POST',
235854fbba1SGunnar Mills                   url: DataService.getHost() +
236854fbba1SGunnar Mills                       '/xyz/openbmc_project/network/snmp/manager/action/Client',
237854fbba1SGunnar Mills                   withCredentials: true,
238854fbba1SGunnar Mills                   data: JSON.stringify({'data': [address, +port]})
239854fbba1SGunnar Mills                 })
240854fbba1SGunnar Mills              .then(function(response) {
241854fbba1SGunnar Mills                return response.data;
242854fbba1SGunnar Mills              });
243854fbba1SGunnar Mills        },
244854fbba1SGunnar Mills        setSNMPManagerPort: function(snmpManagerPath, port) {
245854fbba1SGunnar Mills          return $http({
246854fbba1SGunnar Mills                   method: 'PUT',
247854fbba1SGunnar Mills                   url: DataService.getHost() + snmpManagerPath + '/attr/Port',
248854fbba1SGunnar Mills                   withCredentials: true,
249854fbba1SGunnar Mills                   data: JSON.stringify({'data': +port})
250854fbba1SGunnar Mills                 })
251854fbba1SGunnar Mills              .then(function(response) {
252854fbba1SGunnar Mills                return response.data;
253854fbba1SGunnar Mills              });
254854fbba1SGunnar Mills        },
255854fbba1SGunnar Mills        setSNMPManagerAddress: function(snmpManagerPath, address) {
256854fbba1SGunnar Mills          return $http({
257854fbba1SGunnar Mills                   method: 'PUT',
258854fbba1SGunnar Mills                   url: DataService.getHost() + snmpManagerPath +
259854fbba1SGunnar Mills                       '/attr/Address',
260854fbba1SGunnar Mills                   withCredentials: true,
261854fbba1SGunnar Mills                   data: JSON.stringify({'data': address})
262854fbba1SGunnar Mills                 })
263854fbba1SGunnar Mills              .then(function(response) {
264854fbba1SGunnar Mills                return response.data;
265854fbba1SGunnar Mills              });
266854fbba1SGunnar Mills        },
267171c6a1eSIftekharul Islam        getNetworkInfo: function() {
268171c6a1eSIftekharul Islam          var deferred = $q.defer();
269171c6a1eSIftekharul Islam          $http({
270171c6a1eSIftekharul Islam            method: 'GET',
271d27bb135SAndrew Geissler            url: DataService.getHost() +
272d27bb135SAndrew Geissler                '/xyz/openbmc_project/network/enumerate',
273171c6a1eSIftekharul Islam            withCredentials: true
274d27bb135SAndrew Geissler          })
275d27bb135SAndrew Geissler              .then(
276d27bb135SAndrew Geissler                  function(response) {
277bbcf670aSEd Tanous                    var json = JSON.stringify(response.data);
278171c6a1eSIftekharul Islam                    var content = JSON.parse(json);
279ba5e3f34SAndrew Geissler                    var hostname = '';
280e9f5fe77SGunnar Mills                    var defaultgateway = '';
281ba5e3f34SAndrew Geissler                    var macAddress = '';
282171c6a1eSIftekharul Islam
2832a489554SIftekharul Islam                    function parseNetworkData(content) {
2842a489554SIftekharul Islam                      var data = {
2852a489554SIftekharul Islam                        interface_ids: [],
286fbb63db4SCamVan Nguyen                        interfaces: {},
287d27bb135SAndrew Geissler                        ip_addresses: {ipv4: [], ipv6: []},
2882a489554SIftekharul Islam                      };
289d27bb135SAndrew Geissler                      var interfaceId = '', keyParts = [], interfaceHash = '',
290ba5e3f34SAndrew Geissler                          interfaceType = '';
2912a489554SIftekharul Islam                      for (var key in content.data) {
2923efbe2dfSGunnar Mills                        if (key.match(/network\/eth\d+(_\d+)?$/ig)) {
293ba5e3f34SAndrew Geissler                          interfaceId = key.split('/').pop();
2942a489554SIftekharul Islam                          if (data.interface_ids.indexOf(interfaceId) == -1) {
2952a489554SIftekharul Islam                            data.interface_ids.push(interfaceId);
2962a489554SIftekharul Islam                            data.interfaces[interfaceId] = {
2972a489554SIftekharul Islam                              interfaceIname: '',
298db44b097SGunnar Mills                              DomainName: '',
2992a489554SIftekharul Islam                              MACAddress: '',
3002a489554SIftekharul Islam                              Nameservers: [],
3012a489554SIftekharul Islam                              DHCPEnabled: 0,
302d27bb135SAndrew Geissler                              ipv4: {ids: [], values: []},
303d27bb135SAndrew Geissler                              ipv6: {ids: [], values: []}
3042a489554SIftekharul Islam                            };
305d27bb135SAndrew Geissler                            data.interfaces[interfaceId].MACAddress =
306d27bb135SAndrew Geissler                                content.data[key].MACAddress;
307d27bb135SAndrew Geissler                            data.interfaces[interfaceId].DomainName =
308d27bb135SAndrew Geissler                                content.data[key].DomainName.join(' ');
309d27bb135SAndrew Geissler                            data.interfaces[interfaceId].Nameservers =
310d27bb135SAndrew Geissler                                content.data[key].Nameservers;
311d27bb135SAndrew Geissler                            data.interfaces[interfaceId].DHCPEnabled =
312d27bb135SAndrew Geissler                                content.data[key].DHCPEnabled;
3132a489554SIftekharul Islam                          }
314d27bb135SAndrew Geissler                        } else if (
315d27bb135SAndrew Geissler                            key.match(
3163efbe2dfSGunnar Mills                                /network\/eth\d+(_\d+)?\/ipv[4|6]\/[a-z0-9]+$/ig)) {
317ba5e3f34SAndrew Geissler                          keyParts = key.split('/');
3182a489554SIftekharul Islam                          interfaceHash = keyParts.pop();
3192a489554SIftekharul Islam                          interfaceType = keyParts.pop();
3202a489554SIftekharul Islam                          interfaceId = keyParts.pop();
3212a489554SIftekharul Islam
322d27bb135SAndrew Geissler                          if (data.interfaces[interfaceId][interfaceType]
323d27bb135SAndrew Geissler                                  .ids.indexOf(interfaceHash) == -1) {
324d27bb135SAndrew Geissler                            data.interfaces[interfaceId][interfaceType]
325d27bb135SAndrew Geissler                                .ids.push(interfaceHash);
326d27bb135SAndrew Geissler                            data.interfaces[interfaceId][interfaceType]
327d27bb135SAndrew Geissler                                .values.push(content.data[key]);
328d27bb135SAndrew Geissler                            data.ip_addresses[interfaceType].push(
329d27bb135SAndrew Geissler                                content.data[key]['Address']);
3302a489554SIftekharul Islam                          }
3312a489554SIftekharul Islam                        }
3322a489554SIftekharul Islam                      }
3332a489554SIftekharul Islam                      return data;
3342a489554SIftekharul Islam                    }
3352a489554SIftekharul Islam
336d27bb135SAndrew Geissler                    if (content.data.hasOwnProperty(
337e9f5fe77SGunnar Mills                            '/xyz/openbmc_project/network/config')) {
338e9f5fe77SGunnar Mills                      if (content.data['/xyz/openbmc_project/network/config']
339d27bb135SAndrew Geissler                              .hasOwnProperty('HostName')) {
340d27bb135SAndrew Geissler                        hostname =
341d27bb135SAndrew Geissler                            content.data['/xyz/openbmc_project/network/config']
342d27bb135SAndrew Geissler                                .HostName;
343171c6a1eSIftekharul Islam                      }
344e9f5fe77SGunnar Mills                      if (content.data['/xyz/openbmc_project/network/config']
345e9f5fe77SGunnar Mills                              .hasOwnProperty('DefaultGateway')) {
346e9f5fe77SGunnar Mills                        defaultgateway =
347e9f5fe77SGunnar Mills                            content.data['/xyz/openbmc_project/network/config']
348e9f5fe77SGunnar Mills                                .DefaultGateway;
349e9f5fe77SGunnar Mills                      }
350e9f5fe77SGunnar Mills                    }
351171c6a1eSIftekharul Islam
352d27bb135SAndrew Geissler                    if (content.data.hasOwnProperty(
353d27bb135SAndrew Geissler                            '/xyz/openbmc_project/network/eth0') &&
354d27bb135SAndrew Geissler                        content.data['/xyz/openbmc_project/network/eth0']
355d27bb135SAndrew Geissler                            .hasOwnProperty('MACAddress')) {
356d27bb135SAndrew Geissler                      macAddress =
357d27bb135SAndrew Geissler                          content.data['/xyz/openbmc_project/network/eth0']
358d27bb135SAndrew Geissler                              .MACAddress;
359171c6a1eSIftekharul Islam                    }
360171c6a1eSIftekharul Islam
361171c6a1eSIftekharul Islam                    deferred.resolve({
362171c6a1eSIftekharul Islam                      data: content.data,
363171c6a1eSIftekharul Islam                      hostname: hostname,
364e9f5fe77SGunnar Mills                      defaultgateway: defaultgateway,
365171c6a1eSIftekharul Islam                      mac_address: macAddress,
3662a489554SIftekharul Islam                      formatted_data: parseNetworkData(content)
367171c6a1eSIftekharul Islam                    });
368d27bb135SAndrew Geissler                  },
369d27bb135SAndrew Geissler                  function(error) {
370171c6a1eSIftekharul Islam                    console.log(error);
371171c6a1eSIftekharul Islam                    deferred.reject(error);
372171c6a1eSIftekharul Islam                  });
373171c6a1eSIftekharul Islam          return deferred.promise;
374171c6a1eSIftekharul Islam        },
3757ddc7274SGunnar Mills        setMACAddress: function(interface_name, mac_address) {
3767ddc7274SGunnar Mills          return $http({
3777ddc7274SGunnar Mills                   method: 'PUT',
3787ddc7274SGunnar Mills                   url: DataService.getHost() +
3797ddc7274SGunnar Mills                       '/xyz/openbmc_project/network/' + interface_name +
3807ddc7274SGunnar Mills                       '/attr/MACAddress',
3817ddc7274SGunnar Mills                   withCredentials: true,
3827ddc7274SGunnar Mills                   data: JSON.stringify({'data': mac_address})
3837ddc7274SGunnar Mills                 })
3847ddc7274SGunnar Mills              .then(function(response) {
3857ddc7274SGunnar Mills                return response.data;
3867ddc7274SGunnar Mills              });
3877ddc7274SGunnar Mills        },
388dca79d73SGunnar Mills        setDefaultGateway: function(defaultGateway) {
389dca79d73SGunnar Mills          return $http({
390dca79d73SGunnar Mills                   method: 'PUT',
391dca79d73SGunnar Mills                   url: DataService.getHost() +
392dca79d73SGunnar Mills                       '/xyz/openbmc_project/network/config/attr/DefaultGateway',
393dca79d73SGunnar Mills                   withCredentials: true,
394dca79d73SGunnar Mills                   data: JSON.stringify({'data': defaultGateway})
395dca79d73SGunnar Mills                 })
396dca79d73SGunnar Mills              .then(function(response) {
397dca79d73SGunnar Mills                return response.data;
398dca79d73SGunnar Mills              });
399dca79d73SGunnar Mills        },
400cb2c3060SGunnar Mills        setDHCPEnabled: function(interfaceName, dhcpEnabled) {
401cb2c3060SGunnar Mills          return $http({
402cb2c3060SGunnar Mills                   method: 'PUT',
403cb2c3060SGunnar Mills                   url: DataService.getHost() +
404cb2c3060SGunnar Mills                       '/xyz/openbmc_project/network/' + interfaceName +
405cb2c3060SGunnar Mills                       '/attr/DHCPEnabled',
406cb2c3060SGunnar Mills                   withCredentials: true,
407cb2c3060SGunnar Mills                   data: JSON.stringify({'data': dhcpEnabled})
408cb2c3060SGunnar Mills                 })
409cb2c3060SGunnar Mills              .then(function(response) {
410cb2c3060SGunnar Mills                return response.data;
411cb2c3060SGunnar Mills              });
412cb2c3060SGunnar Mills        },
4130646782dSGunnar Mills        setNameservers: function(interfaceName, dnsServers) {
4140646782dSGunnar Mills          return $http({
4150646782dSGunnar Mills                   method: 'PUT',
4160646782dSGunnar Mills                   url: DataService.getHost() +
4170646782dSGunnar Mills                       '/xyz/openbmc_project/network/' + interfaceName +
4180646782dSGunnar Mills                       '/attr/Nameservers',
4190646782dSGunnar Mills                   withCredentials: true,
4200646782dSGunnar Mills                   data: JSON.stringify({'data': dnsServers})
4210646782dSGunnar Mills                 })
4220646782dSGunnar Mills              .then(function(response) {
4230646782dSGunnar Mills                return response.data;
4240646782dSGunnar Mills              });
4250646782dSGunnar Mills        },
426a45c3852SGunnar Mills        deleteIPV4: function(interfaceName, networkID) {
427a45c3852SGunnar Mills          return $http({
428a45c3852SGunnar Mills                   method: 'POST',
429a45c3852SGunnar Mills                   url: DataService.getHost() +
430a45c3852SGunnar Mills                       '/xyz/openbmc_project/network/' + interfaceName +
431a45c3852SGunnar Mills                       '/ipv4/' + networkID + '/action/Delete',
432a45c3852SGunnar Mills                   withCredentials: true,
433a45c3852SGunnar Mills                   data: JSON.stringify({'data': []})
434a45c3852SGunnar Mills                 })
435a45c3852SGunnar Mills              .then(function(response) {
436a45c3852SGunnar Mills                return response.data;
437a45c3852SGunnar Mills              });
438a45c3852SGunnar Mills        },
439a45c3852SGunnar Mills        addIPV4: function(
440a45c3852SGunnar Mills            interfaceName, ipAddress, netmaskPrefixLength, gateway) {
441a45c3852SGunnar Mills          return $http({
442a45c3852SGunnar Mills                   method: 'POST',
443a45c3852SGunnar Mills                   url: DataService.getHost() +
444a45c3852SGunnar Mills                       '/xyz/openbmc_project/network/' + interfaceName +
445a45c3852SGunnar Mills                       '/action/IP',
446a45c3852SGunnar Mills                   withCredentials: true,
447a45c3852SGunnar Mills                   data: JSON.stringify({
448a45c3852SGunnar Mills                     'data': [
449a45c3852SGunnar Mills                       'xyz.openbmc_project.Network.IP.Protocol.IPv4',
450a45c3852SGunnar Mills                       ipAddress, +netmaskPrefixLength, gateway
451a45c3852SGunnar Mills                     ]
452a45c3852SGunnar Mills                   })
453a45c3852SGunnar Mills                 })
454a45c3852SGunnar Mills              .then(function(response) {
455a45c3852SGunnar Mills                return response.data;
456a45c3852SGunnar Mills              });
457a45c3852SGunnar Mills        },
458df3bd124SMichael Davis        getLEDState: function() {
459df3bd124SMichael Davis          var deferred = $q.defer();
460cd789508SIftekharul Islam          $http({
461cd789508SIftekharul Islam            method: 'GET',
462d27bb135SAndrew Geissler            url: DataService.getHost() +
463d27bb135SAndrew Geissler                '/xyz/openbmc_project/led/groups/enclosure_identify',
464cd789508SIftekharul Islam            withCredentials: true
465d27bb135SAndrew Geissler          })
466d27bb135SAndrew Geissler              .then(
467d27bb135SAndrew Geissler                  function(response) {
468bbcf670aSEd Tanous                    var json = JSON.stringify(response.data);
469cd789508SIftekharul Islam                    var content = JSON.parse(json);
470df3bd124SMichael Davis                    deferred.resolve(content.data.Asserted);
471d27bb135SAndrew Geissler                  },
472d27bb135SAndrew Geissler                  function(error) {
473cd789508SIftekharul Islam                    console.log(error);
474df3bd124SMichael Davis                    deferred.reject(error);
475cd789508SIftekharul Islam                  });
476df3bd124SMichael Davis          return deferred.promise;
477cd789508SIftekharul Islam        },
47899d199f3SIftekharul Islam        login: function(username, password, callback) {
47999d199f3SIftekharul Islam          $http({
48099d199f3SIftekharul Islam            method: 'POST',
481ba5e3f34SAndrew Geissler            url: DataService.getHost() + '/login',
48299d199f3SIftekharul Islam            withCredentials: true,
483d27bb135SAndrew Geissler            data: JSON.stringify({'data': [username, password]})
484ba5e3f34SAndrew Geissler          })
485d27bb135SAndrew Geissler              .then(
486d27bb135SAndrew Geissler                  function(response) {
48799d199f3SIftekharul Islam                    if (callback) {
488bbcf670aSEd Tanous                      callback(response.data);
48999d199f3SIftekharul Islam                    }
490d27bb135SAndrew Geissler                  },
491d27bb135SAndrew Geissler                  function(error) {
49299d199f3SIftekharul Islam                    if (callback) {
493cd789508SIftekharul Islam                      if (error && error.status && error.status == 'error') {
494cd789508SIftekharul Islam                        callback(error);
495d27bb135SAndrew Geissler                      } else {
496cd789508SIftekharul Islam                        callback(error, true);
497cd789508SIftekharul Islam                      }
49899d199f3SIftekharul Islam                    }
49999d199f3SIftekharul Islam                    console.log(error);
50099d199f3SIftekharul Islam                  });
50199d199f3SIftekharul Islam        },
50299d199f3SIftekharul Islam        logout: function(callback) {
50399d199f3SIftekharul Islam          $http({
50499d199f3SIftekharul Islam            method: 'POST',
505ba5e3f34SAndrew Geissler            url: DataService.getHost() + '/logout',
50699d199f3SIftekharul Islam            withCredentials: true,
507d27bb135SAndrew Geissler            data: JSON.stringify({'data': []})
508ba5e3f34SAndrew Geissler          })
509d27bb135SAndrew Geissler              .then(
510d27bb135SAndrew Geissler                  function(response) {
51199d199f3SIftekharul Islam                    if (callback) {
512de88340dSIftekharul Islam                      callback(response.data);
51399d199f3SIftekharul Islam                    }
514d27bb135SAndrew Geissler                  },
515d27bb135SAndrew Geissler                  function(error) {
51699d199f3SIftekharul Islam                    if (callback) {
51799d199f3SIftekharul Islam                      callback(null, error);
51899d199f3SIftekharul Islam                    }
51999d199f3SIftekharul Islam                    console.log(error);
52099d199f3SIftekharul Islam                  });
52199d199f3SIftekharul Islam        },
522cf7219ceSAppaRao Puli        getAccountServiceRoles: function() {
523cf7219ceSAppaRao Puli          var roles = [];
524cf7219ceSAppaRao Puli
525cf7219ceSAppaRao Puli          return $http({
526cf7219ceSAppaRao Puli                   method: 'GET',
527cf7219ceSAppaRao Puli                   url: DataService.getHost() +
528cf7219ceSAppaRao Puli                       '/redfish/v1/AccountService/Roles',
529cf7219ceSAppaRao Puli                   withCredentials: true
530cf7219ceSAppaRao Puli                 })
531fa56273dSYoshie Muranaka              .then(function(response) {
532cf7219ceSAppaRao Puli                var members = response.data['Members'];
533cf7219ceSAppaRao Puli                angular.forEach(members, function(member) {
534cf7219ceSAppaRao Puli                  roles.push(member['@odata.id'].split('/').pop());
535cf7219ceSAppaRao Puli                });
536cf7219ceSAppaRao Puli                return roles;
537cf7219ceSAppaRao Puli              });
538cf7219ceSAppaRao Puli        },
539cf7219ceSAppaRao Puli        getAllUserAccounts: function() {
54028711a6aSAppaRao Puli          var deferred = $q.defer();
54128711a6aSAppaRao Puli          var promises = [];
54228711a6aSAppaRao Puli
54328711a6aSAppaRao Puli          $http({
54428711a6aSAppaRao Puli            method: 'GET',
5454693ddb2SGunnar Mills            url: DataService.getHost() + '/redfish/v1/AccountService/Accounts',
54628711a6aSAppaRao Puli            withCredentials: true
54728711a6aSAppaRao Puli          })
54828711a6aSAppaRao Puli              .then(
54928711a6aSAppaRao Puli                  function(response) {
55028711a6aSAppaRao Puli                    var members = response.data['Members'];
55128711a6aSAppaRao Puli                    angular.forEach(members, function(member) {
55228711a6aSAppaRao Puli                      promises.push(
55328711a6aSAppaRao Puli                          $http({
55428711a6aSAppaRao Puli                            method: 'GET',
55528711a6aSAppaRao Puli                            url: DataService.getHost() + member['@odata.id'],
55628711a6aSAppaRao Puli                            withCredentials: true
55728711a6aSAppaRao Puli                          }).then(function(res) {
55828711a6aSAppaRao Puli                            return res.data;
55928711a6aSAppaRao Puli                          }));
56028711a6aSAppaRao Puli                    });
56128711a6aSAppaRao Puli
56228711a6aSAppaRao Puli                    $q.all(promises).then(
56328711a6aSAppaRao Puli                        function(results) {
56428711a6aSAppaRao Puli                          deferred.resolve(results);
56528711a6aSAppaRao Puli                        },
56628711a6aSAppaRao Puli                        function(errors) {
56728711a6aSAppaRao Puli                          deferred.reject(errors);
56828711a6aSAppaRao Puli                        });
56928711a6aSAppaRao Puli                  },
57028711a6aSAppaRao Puli                  function(error) {
57128711a6aSAppaRao Puli                    console.log(error);
57228711a6aSAppaRao Puli                    deferred.reject(error);
57328711a6aSAppaRao Puli                  });
57428711a6aSAppaRao Puli          return deferred.promise;
57528711a6aSAppaRao Puli        },
576b1e7c863SAppaRao Puli
577fa56273dSYoshie Muranaka        getAllUserAccountProperties: function() {
578b1e7c863SAppaRao Puli          return $http({
579b1e7c863SAppaRao Puli                   method: 'GET',
580b1e7c863SAppaRao Puli                   url: DataService.getHost() + '/redfish/v1/AccountService',
581b1e7c863SAppaRao Puli                   withCredentials: true
582b1e7c863SAppaRao Puli                 })
583fa56273dSYoshie Muranaka              .then(function(response) {
584b1e7c863SAppaRao Puli                return response.data;
585b1e7c863SAppaRao Puli              });
586b1e7c863SAppaRao Puli        },
587b1e7c863SAppaRao Puli
588b1e7c863SAppaRao Puli        saveUserAccountProperties: function(lockoutduration, lockoutthreshold) {
589b1e7c863SAppaRao Puli          var data = {};
590b1e7c863SAppaRao Puli          if (lockoutduration != undefined) {
591b1e7c863SAppaRao Puli            data['AccountLockoutDuration'] = lockoutduration;
592b1e7c863SAppaRao Puli          }
593b1e7c863SAppaRao Puli          if (lockoutthreshold != undefined) {
594b1e7c863SAppaRao Puli            data['AccountLockoutThreshold'] = lockoutthreshold;
595b1e7c863SAppaRao Puli          }
596b1e7c863SAppaRao Puli
597b1e7c863SAppaRao Puli          return $http({
598b1e7c863SAppaRao Puli            method: 'PATCH',
599b1e7c863SAppaRao Puli            url: DataService.getHost() + '/redfish/v1/AccountService',
600b1e7c863SAppaRao Puli            withCredentials: true,
601b1e7c863SAppaRao Puli            data: data
602b1e7c863SAppaRao Puli          });
603b1e7c863SAppaRao Puli        },
604b1e7c863SAppaRao Puli
6055e258e43Sbeccabroek        saveLdapProperties: function(properties) {
6065e258e43Sbeccabroek          return $http({
6075e258e43Sbeccabroek            method: 'PATCH',
6085e258e43Sbeccabroek            url: DataService.getHost() + '/redfish/v1/AccountService',
6095e258e43Sbeccabroek            withCredentials: true,
6105e258e43Sbeccabroek            data: properties
6115e258e43Sbeccabroek          });
6125e258e43Sbeccabroek        },
61328711a6aSAppaRao Puli        createUser: function(user, passwd, role, enabled) {
61428711a6aSAppaRao Puli          var data = {};
61528711a6aSAppaRao Puli          data['UserName'] = user;
61628711a6aSAppaRao Puli          data['Password'] = passwd;
61728711a6aSAppaRao Puli          data['RoleId'] = role;
61828711a6aSAppaRao Puli          data['Enabled'] = enabled;
61928711a6aSAppaRao Puli
62028711a6aSAppaRao Puli          return $http({
62128711a6aSAppaRao Puli            method: 'POST',
6224693ddb2SGunnar Mills            url: DataService.getHost() + '/redfish/v1/AccountService/Accounts',
62328711a6aSAppaRao Puli            withCredentials: true,
62428711a6aSAppaRao Puli            data: data
62528711a6aSAppaRao Puli          });
62628711a6aSAppaRao Puli        },
627b4d9c09aSYoshie Muranaka        updateUser: function(user, newUser, passwd, role, enabled, locked) {
62828711a6aSAppaRao Puli          var data = {};
62928711a6aSAppaRao Puli          if ((newUser !== undefined) && (newUser != null)) {
63028711a6aSAppaRao Puli            data['UserName'] = newUser;
63128711a6aSAppaRao Puli          }
63228711a6aSAppaRao Puli          if ((role !== undefined) && (role != null)) {
63328711a6aSAppaRao Puli            data['RoleId'] = role;
63428711a6aSAppaRao Puli          }
63528711a6aSAppaRao Puli          if ((enabled !== undefined) && (enabled != null)) {
63628711a6aSAppaRao Puli            data['Enabled'] = enabled;
63728711a6aSAppaRao Puli          }
63828711a6aSAppaRao Puli          if ((passwd !== undefined) && (passwd != null)) {
63928711a6aSAppaRao Puli            data['Password'] = passwd;
64028711a6aSAppaRao Puli          }
641b4d9c09aSYoshie Muranaka          if ((locked !== undefined) && (locked !== null)) {
642b4d9c09aSYoshie Muranaka            data['Locked'] = locked
643b4d9c09aSYoshie Muranaka          }
64428711a6aSAppaRao Puli          return $http({
64528711a6aSAppaRao Puli            method: 'PATCH',
64628711a6aSAppaRao Puli            url: DataService.getHost() +
64728711a6aSAppaRao Puli                '/redfish/v1/AccountService/Accounts/' + user,
64828711a6aSAppaRao Puli            withCredentials: true,
64928711a6aSAppaRao Puli            data: data
65028711a6aSAppaRao Puli          });
65128711a6aSAppaRao Puli        },
65228711a6aSAppaRao Puli        deleteUser: function(user) {
65328711a6aSAppaRao Puli          return $http({
65428711a6aSAppaRao Puli            method: 'DELETE',
65528711a6aSAppaRao Puli            url: DataService.getHost() +
65628711a6aSAppaRao Puli                '/redfish/v1/AccountService/Accounts/' + user,
65728711a6aSAppaRao Puli            withCredentials: true,
65828711a6aSAppaRao Puli          });
65928711a6aSAppaRao Puli        },
660a1d238f3SIftekharul Islam        chassisPowerOff: function() {
661a1d238f3SIftekharul Islam          var deferred = $q.defer();
66299d199f3SIftekharul Islam          $http({
6633aa8b535SGunnar Mills            method: 'PUT',
664d27bb135SAndrew Geissler            url: DataService.getHost() +
665d27bb135SAndrew Geissler                '/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition',
66699d199f3SIftekharul Islam            withCredentials: true,
667d27bb135SAndrew Geissler            data: JSON.stringify(
668d27bb135SAndrew Geissler                {'data': 'xyz.openbmc_project.State.Chassis.Transition.Off'})
669ba5e3f34SAndrew Geissler          })
670d27bb135SAndrew Geissler              .then(
671d27bb135SAndrew Geissler                  function(response) {
672bbcf670aSEd Tanous                    var json = JSON.stringify(response.data);
67399d199f3SIftekharul Islam                    var content = JSON.parse(json);
674a1d238f3SIftekharul Islam                    deferred.resolve(content.status);
675d27bb135SAndrew Geissler                  },
676d27bb135SAndrew Geissler                  function(error) {
67799d199f3SIftekharul Islam                    console.log(error);
678a1d238f3SIftekharul Islam                    deferred.reject(error);
67999d199f3SIftekharul Islam                  });
680a1d238f3SIftekharul Islam          return deferred.promise;
68199d199f3SIftekharul Islam        },
682b7f0ee19Sbeccabroek        setLEDState: function(state) {
683b7f0ee19Sbeccabroek          return $http({
684cd789508SIftekharul Islam            method: 'PUT',
685d27bb135SAndrew Geissler            url: DataService.getHost() +
686d27bb135SAndrew Geissler                '/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted',
687cd789508SIftekharul Islam            withCredentials: true,
688d27bb135SAndrew Geissler            data: JSON.stringify({'data': state})
689ba5e3f34SAndrew Geissler          })
690cd789508SIftekharul Islam        },
691e368108fSDixsie Wolmers        getBootOptions: function() {
692e368108fSDixsie Wolmers          return $http({
693e368108fSDixsie Wolmers                   method: 'GET',
694e368108fSDixsie Wolmers                   url: DataService.getHost() + '/redfish/v1/Systems/system',
695e368108fSDixsie Wolmers                   withCredentials: true
696e368108fSDixsie Wolmers                 })
697e368108fSDixsie Wolmers              .then(function(response) {
698e368108fSDixsie Wolmers                return response.data;
699e368108fSDixsie Wolmers              });
700e368108fSDixsie Wolmers        },
701e368108fSDixsie Wolmers        saveBootSettings: function(data) {
702e368108fSDixsie Wolmers          return $http({
703e368108fSDixsie Wolmers            method: 'PATCH',
704e368108fSDixsie Wolmers            url: DataService.getHost() + '/redfish/v1/Systems/system',
705e368108fSDixsie Wolmers            withCredentials: true,
706e368108fSDixsie Wolmers            data: data
707e368108fSDixsie Wolmers          });
708e368108fSDixsie Wolmers        },
709e368108fSDixsie Wolmers        getTPMStatus: function() {
710e368108fSDixsie Wolmers          return $http({
711e368108fSDixsie Wolmers                   method: 'GET',
712e368108fSDixsie Wolmers                   url: DataService.getHost() +
713e368108fSDixsie Wolmers                       '/xyz/openbmc_project/control/host0/TPMEnable',
714e368108fSDixsie Wolmers                   withCredentials: true
715e368108fSDixsie Wolmers                 })
716e368108fSDixsie Wolmers              .then(function(response) {
717e368108fSDixsie Wolmers                return response.data;
718e368108fSDixsie Wolmers              });
719e368108fSDixsie Wolmers        },
720e368108fSDixsie Wolmers        saveTPMEnable: function(data) {
721e368108fSDixsie Wolmers          return $http({
722e368108fSDixsie Wolmers            method: 'PUT',
723e368108fSDixsie Wolmers            url: DataService.getHost() +
724e368108fSDixsie Wolmers                '/xyz/openbmc_project/control/host0/TPMEnable/attr/TPMEnable',
725e368108fSDixsie Wolmers            withCredentials: true,
726e368108fSDixsie Wolmers            data: JSON.stringify({'data': data})
727e368108fSDixsie Wolmers          })
728e368108fSDixsie Wolmers        },
729e368108fSDixsie Wolmers
730c57ec32fSDixsie Wolmers        bmcReboot: function() {
731c57ec32fSDixsie Wolmers          return $http({
73255368129SIftekharul Islam            method: 'PUT',
733d27bb135SAndrew Geissler            url: DataService.getHost() +
73470086984SGunnar Mills                '/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition',
73555368129SIftekharul Islam            withCredentials: true,
736d27bb135SAndrew Geissler            data: JSON.stringify(
737d27bb135SAndrew Geissler                {'data': 'xyz.openbmc_project.State.BMC.Transition.Reboot'})
73855368129SIftekharul Islam          });
73955368129SIftekharul Islam        },
740bfc99907Sbeccabroek        getLastRebootTime: function() {
741bfc99907Sbeccabroek          return $http({
742bfc99907Sbeccabroek                   method: 'GET',
743bfc99907Sbeccabroek                   url: DataService.getHost() +
744bfc99907Sbeccabroek                       '/xyz/openbmc_project/state/bmc0/attr/LastRebootTime',
745bfc99907Sbeccabroek                   withCredentials: true
746bfc99907Sbeccabroek                 })
747bfc99907Sbeccabroek              .then(function(response) {
748bfc99907Sbeccabroek                return response.data;
749bfc99907Sbeccabroek              });
750bfc99907Sbeccabroek        },
751a1d238f3SIftekharul Islam        hostPowerOn: function() {
752a1d238f3SIftekharul Islam          var deferred = $q.defer();
75399d199f3SIftekharul Islam          $http({
75499d199f3SIftekharul Islam            method: 'PUT',
755d27bb135SAndrew Geissler            url: DataService.getHost() +
756d27bb135SAndrew Geissler                '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition',
75799d199f3SIftekharul Islam            withCredentials: true,
758d27bb135SAndrew Geissler            data: JSON.stringify(
759d27bb135SAndrew Geissler                {'data': 'xyz.openbmc_project.State.Host.Transition.On'})
760ba5e3f34SAndrew Geissler          })
761d27bb135SAndrew Geissler              .then(
762d27bb135SAndrew Geissler                  function(response) {
763bbcf670aSEd Tanous                    var json = JSON.stringify(response.data);
76499d199f3SIftekharul Islam                    var content = JSON.parse(json);
765a1d238f3SIftekharul Islam                    deferred.resolve(content.status);
766d27bb135SAndrew Geissler                  },
767d27bb135SAndrew Geissler                  function(error) {
76899d199f3SIftekharul Islam                    console.log(error);
769a1d238f3SIftekharul Islam                    deferred.reject(error);
77099d199f3SIftekharul Islam                  });
771a1d238f3SIftekharul Islam          return deferred.promise;
77299d199f3SIftekharul Islam        },
773a1d238f3SIftekharul Islam        hostPowerOff: function() {
774a1d238f3SIftekharul Islam          var deferred = $q.defer();
77599d199f3SIftekharul Islam          $http({
77699d199f3SIftekharul Islam            method: 'PUT',
777d27bb135SAndrew Geissler            url: DataService.getHost() +
778d27bb135SAndrew Geissler                '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition',
77999d199f3SIftekharul Islam            withCredentials: true,
780d27bb135SAndrew Geissler            data: JSON.stringify(
781d27bb135SAndrew Geissler                {'data': 'xyz.openbmc_project.State.Host.Transition.Off'})
782ba5e3f34SAndrew Geissler          })
783d27bb135SAndrew Geissler              .then(
784d27bb135SAndrew Geissler                  function(response) {
785bbcf670aSEd Tanous                    var json = JSON.stringify(response.data);
78699d199f3SIftekharul Islam                    var content = JSON.parse(json);
787a1d238f3SIftekharul Islam                    deferred.resolve(content.status);
788d27bb135SAndrew Geissler                  },
789d27bb135SAndrew Geissler                  function(error) {
79099d199f3SIftekharul Islam                    console.log(error);
791a1d238f3SIftekharul Islam                    deferred.reject(error);
79299d199f3SIftekharul Islam                  });
793a1d238f3SIftekharul Islam          return deferred.promise;
79499d199f3SIftekharul Islam        },
795a1d238f3SIftekharul Islam        hostReboot: function() {
796a1d238f3SIftekharul Islam          var deferred = $q.defer();
79799d199f3SIftekharul Islam          $http({
798cacfa6dfSGunnar Mills            method: 'PUT',
799d27bb135SAndrew Geissler            url: DataService.getHost() +
800d27bb135SAndrew Geissler                '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition',
80199d199f3SIftekharul Islam            withCredentials: true,
802d27bb135SAndrew Geissler            data: JSON.stringify(
803d27bb135SAndrew Geissler                {'data': 'xyz.openbmc_project.State.Host.Transition.Reboot'})
804ba5e3f34SAndrew Geissler          })
805d27bb135SAndrew Geissler              .then(
806d27bb135SAndrew Geissler                  function(response) {
807bbcf670aSEd Tanous                    var json = JSON.stringify(response.data);
80899d199f3SIftekharul Islam                    var content = JSON.parse(json);
809a1d238f3SIftekharul Islam                    deferred.resolve(content.status);
810d27bb135SAndrew Geissler                  },
811d27bb135SAndrew Geissler                  function(error) {
81299d199f3SIftekharul Islam                    console.log(error);
813a1d238f3SIftekharul Islam                    deferred.reject(error);
81499d199f3SIftekharul Islam                  });
815a1d238f3SIftekharul Islam
816a1d238f3SIftekharul Islam          return deferred.promise;
81799d199f3SIftekharul Islam        },
8185674425bSbeccabroek        getLastPowerTime: function() {
8195674425bSbeccabroek          return $http({
8205674425bSbeccabroek                   method: 'GET',
8215674425bSbeccabroek                   url: DataService.getHost() +
8225674425bSbeccabroek                       '/xyz/openbmc_project/state/chassis0/attr/LastStateChangeTime',
8235674425bSbeccabroek                   withCredentials: true
8245674425bSbeccabroek                 })
8255674425bSbeccabroek              .then(function(response) {
8265674425bSbeccabroek                return response.data;
8275674425bSbeccabroek              });
8285674425bSbeccabroek        },
829df3bd124SMichael Davis        getLogs: function() {
830df3bd124SMichael Davis          var deferred = $q.defer();
831cd789508SIftekharul Islam          $http({
832cd789508SIftekharul Islam            method: 'GET',
833d27bb135SAndrew Geissler            url: DataService.getHost() +
834d27bb135SAndrew Geissler                '/xyz/openbmc_project/logging/enumerate',
835cd789508SIftekharul Islam            withCredentials: true
836d27bb135SAndrew Geissler          })
837d27bb135SAndrew Geissler              .then(
838d27bb135SAndrew Geissler                  function(response) {
839bbcf670aSEd Tanous                    var json = JSON.stringify(response.data);
840cd789508SIftekharul Islam                    var content = JSON.parse(json);
841cd789508SIftekharul Islam                    var dataClone = JSON.parse(JSON.stringify(content.data));
842cd789508SIftekharul Islam                    var data = [];
843cd789508SIftekharul Islam                    var severityCode = '';
844cd789508SIftekharul Islam                    var priority = '';
845ec6bcd10SIftekharul Islam                    var health = '';
846cd789508SIftekharul Islam                    var relatedItems = [];
847845acdc5SMatt Spinler                    var eventID = 'None';
848845acdc5SMatt Spinler                    var description = 'None';
849cd789508SIftekharul Islam
850cd789508SIftekharul Islam                    for (var key in content.data) {
851d27bb135SAndrew Geissler                      if (content.data.hasOwnProperty(key) &&
852d27bb135SAndrew Geissler                          content.data[key].hasOwnProperty('Id')) {
853ba5e3f34SAndrew Geissler                        var severityFlags = {
854ba5e3f34SAndrew Geissler                          low: false,
855ba5e3f34SAndrew Geissler                          medium: false,
856ba5e3f34SAndrew Geissler                          high: false
857ba5e3f34SAndrew Geissler                        };
858d27bb135SAndrew Geissler                        severityCode =
859d27bb135SAndrew Geissler                            content.data[key].Severity.split('.').pop();
860d27bb135SAndrew Geissler                        priority =
861d27bb135SAndrew Geissler                            Constants.SEVERITY_TO_PRIORITY_MAP[severityCode];
862cd789508SIftekharul Islam                        severityFlags[priority.toLowerCase()] = true;
863cd789508SIftekharul Islam                        relatedItems = [];
86496c498a3SGunnar Mills                        if (content.data[key].hasOwnProperty(
86596c498a3SGunnar Mills                                ['Associations'])) {
86696c498a3SGunnar Mills                          content.data[key].Associations.forEach(function(
86796c498a3SGunnar Mills                              item) {
868532763f4SIftekharul Islam                            relatedItems.push(item[2]);
869cd789508SIftekharul Islam                          });
87096c498a3SGunnar Mills                        }
871845acdc5SMatt Spinler                        if (content.data[key].hasOwnProperty(['EventID'])) {
872845acdc5SMatt Spinler                          eventID = content.data[key].EventID;
873845acdc5SMatt Spinler                        }
874845acdc5SMatt Spinler
875845acdc5SMatt Spinler                        if (content.data[key].hasOwnProperty(['Description'])) {
876845acdc5SMatt Spinler                          description = content.data[key].Description;
877845acdc5SMatt Spinler                        }
878845acdc5SMatt Spinler
879d27bb135SAndrew Geissler                        data.push(Object.assign(
880d27bb135SAndrew Geissler                            {
881cd789508SIftekharul Islam                              path: key,
882cd789508SIftekharul Islam                              copied: false,
883cd789508SIftekharul Islam                              priority: priority,
884cd789508SIftekharul Islam                              severity_code: severityCode,
885cd789508SIftekharul Islam                              severity_flags: severityFlags,
886d27bb135SAndrew Geissler                              additional_data:
887d27bb135SAndrew Geissler                                  content.data[key].AdditionalData.join('\n'),
88843a2ed12SGunnar Mills                              type: content.data[key].Message,
889cd789508SIftekharul Islam                              selected: false,
890cd789508SIftekharul Islam                              meta: false,
891cd789508SIftekharul Islam                              confirm: false,
892cd789508SIftekharul Islam                              related_items: relatedItems,
893845acdc5SMatt Spinler                              eventID: eventID,
894845acdc5SMatt Spinler                              description: description,
895ee78862dSYoshie Muranaka                              logId: '#' + content.data[key].Id,
896d27bb135SAndrew Geissler                              data: {key: key, value: content.data[key]}
897d27bb135SAndrew Geissler                            },
898d27bb135SAndrew Geissler                            content.data[key]));
899cd789508SIftekharul Islam                      }
900cd789508SIftekharul Islam                    }
901d27bb135SAndrew Geissler                    deferred.resolve({data: data, original: dataClone});
902d27bb135SAndrew Geissler                  },
903d27bb135SAndrew Geissler                  function(error) {
904cd789508SIftekharul Islam                    console.log(error);
905df3bd124SMichael Davis                    deferred.reject(error);
906cd789508SIftekharul Islam                  });
907df3bd124SMichael Davis
908df3bd124SMichael Davis          return deferred.promise;
909d2269e22SIftekharul Islam        },
910615a2f89SGunnar Mills        getAllSensorStatus: function(callback) {
9112f481e4cSAppaRao Puli          $http({
9122f481e4cSAppaRao Puli            method: 'GET',
913615a2f89SGunnar Mills            url: DataService.getHost() +
914615a2f89SGunnar Mills                '/xyz/openbmc_project/sensors/enumerate',
9152f481e4cSAppaRao Puli            withCredentials: true
9162f481e4cSAppaRao Puli          })
9172f481e4cSAppaRao Puli              .then(
9182f481e4cSAppaRao Puli                  function(response) {
919615a2f89SGunnar Mills                    var json = JSON.stringify(response.data);
920615a2f89SGunnar Mills                    var content = JSON.parse(json);
921615a2f89SGunnar Mills                    var dataClone = JSON.parse(JSON.stringify(content.data));
922615a2f89SGunnar Mills                    var sensorData = [];
923615a2f89SGunnar Mills                    var severity = {};
924615a2f89SGunnar Mills                    var title = '';
925615a2f89SGunnar Mills                    var tempKeyParts = [];
926615a2f89SGunnar Mills                    var order = 0;
927615a2f89SGunnar Mills                    var customOrder = 0;
928615a2f89SGunnar Mills
929615a2f89SGunnar Mills                    function getSensorStatus(reading) {
930615a2f89SGunnar Mills                      var severityFlags = {
931615a2f89SGunnar Mills                        critical: false,
932615a2f89SGunnar Mills                        warning: false,
933615a2f89SGunnar Mills                        normal: false
9342f481e4cSAppaRao Puli                      },
935615a2f89SGunnar Mills                          severityText = '', order = 0;
936615a2f89SGunnar Mills
937615a2f89SGunnar Mills                      if (reading.hasOwnProperty('CriticalLow') &&
938615a2f89SGunnar Mills                          reading.Value < reading.CriticalLow) {
939615a2f89SGunnar Mills                        severityFlags.critical = true;
940615a2f89SGunnar Mills                        severityText = 'critical';
941615a2f89SGunnar Mills                        order = 2;
942615a2f89SGunnar Mills                      } else if (
943615a2f89SGunnar Mills                          reading.hasOwnProperty('CriticalHigh') &&
944615a2f89SGunnar Mills                          reading.Value > reading.CriticalHigh) {
945615a2f89SGunnar Mills                        severityFlags.critical = true;
946615a2f89SGunnar Mills                        severityText = 'critical';
947615a2f89SGunnar Mills                        order = 2;
948615a2f89SGunnar Mills                      } else if (
949615a2f89SGunnar Mills                          reading.hasOwnProperty('CriticalLow') &&
950615a2f89SGunnar Mills                          reading.hasOwnProperty('WarningLow') &&
951615a2f89SGunnar Mills                          reading.Value >= reading.CriticalLow &&
952615a2f89SGunnar Mills                          reading.Value <= reading.WarningLow) {
953615a2f89SGunnar Mills                        severityFlags.warning = true;
954615a2f89SGunnar Mills                        severityText = 'warning';
955615a2f89SGunnar Mills                        order = 1;
956615a2f89SGunnar Mills                      } else if (
957615a2f89SGunnar Mills                          reading.hasOwnProperty('WarningHigh') &&
958615a2f89SGunnar Mills                          reading.hasOwnProperty('CriticalHigh') &&
959615a2f89SGunnar Mills                          reading.Value >= reading.WarningHigh &&
960615a2f89SGunnar Mills                          reading.Value <= reading.CriticalHigh) {
961615a2f89SGunnar Mills                        severityFlags.warning = true;
962615a2f89SGunnar Mills                        severityText = 'warning';
963615a2f89SGunnar Mills                        order = 1;
964615a2f89SGunnar Mills                      } else {
965615a2f89SGunnar Mills                        severityFlags.normal = true;
966615a2f89SGunnar Mills                        severityText = 'normal';
967615a2f89SGunnar Mills                      }
968615a2f89SGunnar Mills                      return {
969615a2f89SGunnar Mills                        flags: severityFlags,
970615a2f89SGunnar Mills                        severityText: severityText,
971615a2f89SGunnar Mills                        order: order
972615a2f89SGunnar Mills                      };
973615a2f89SGunnar Mills                    }
974615a2f89SGunnar Mills
975615a2f89SGunnar Mills                    for (var key in content.data) {
976615a2f89SGunnar Mills                      if (content.data.hasOwnProperty(key) &&
977615a2f89SGunnar Mills                          content.data[key].hasOwnProperty('Unit')) {
978615a2f89SGunnar Mills                        severity = getSensorStatus(content.data[key]);
979615a2f89SGunnar Mills
980615a2f89SGunnar Mills                        if (!content.data[key].hasOwnProperty('CriticalLow')) {
981615a2f89SGunnar Mills                          content.data[key].CriticalLow = '--';
982615a2f89SGunnar Mills                          content.data[key].CriticalHigh = '--';
983615a2f89SGunnar Mills                        }
984615a2f89SGunnar Mills
985615a2f89SGunnar Mills                        if (!content.data[key].hasOwnProperty('WarningLow')) {
986615a2f89SGunnar Mills                          content.data[key].WarningLow = '--';
987615a2f89SGunnar Mills                          content.data[key].WarningHigh = '--';
988615a2f89SGunnar Mills                        }
989615a2f89SGunnar Mills
990615a2f89SGunnar Mills                        tempKeyParts = key.split('/');
991615a2f89SGunnar Mills                        title = tempKeyParts.pop();
992615a2f89SGunnar Mills                        title = tempKeyParts.pop() + '_' + title;
993615a2f89SGunnar Mills                        title = title.split('_')
994615a2f89SGunnar Mills                                    .map(function(item) {
995615a2f89SGunnar Mills                                      return item.toLowerCase()
996615a2f89SGunnar Mills                                                 .charAt(0)
997615a2f89SGunnar Mills                                                 .toUpperCase() +
998615a2f89SGunnar Mills                                          item.slice(1);
999615a2f89SGunnar Mills                                    })
1000615a2f89SGunnar Mills                                    .reduce(function(prev, el) {
1001615a2f89SGunnar Mills                                      return prev + ' ' + el;
10022f481e4cSAppaRao Puli                                    });
1003615a2f89SGunnar Mills
1004615a2f89SGunnar Mills                        if (Constants.SENSOR_SORT_ORDER.indexOf(
1005615a2f89SGunnar Mills                                content.data[key].Unit) > -1) {
1006615a2f89SGunnar Mills                          customOrder = Constants.SENSOR_SORT_ORDER.indexOf(
1007615a2f89SGunnar Mills                              content.data[key].Unit);
1008615a2f89SGunnar Mills                        } else {
1009615a2f89SGunnar Mills                          customOrder = Constants.SENSOR_SORT_ORDER_DEFAULT;
1010615a2f89SGunnar Mills                        }
1011615a2f89SGunnar Mills
1012615a2f89SGunnar Mills                        sensorData.push(Object.assign(
1013615a2f89SGunnar Mills                            {
1014615a2f89SGunnar Mills                              path: key,
1015615a2f89SGunnar Mills                              selected: false,
1016615a2f89SGunnar Mills                              confirm: false,
1017615a2f89SGunnar Mills                              copied: false,
1018615a2f89SGunnar Mills                              title: title,
1019615a2f89SGunnar Mills                              unit:
1020615a2f89SGunnar Mills                                  Constants
1021615a2f89SGunnar Mills                                      .SENSOR_UNIT_MAP[content.data[key].Unit],
1022615a2f89SGunnar Mills                              severity_flags: severity.flags,
1023615a2f89SGunnar Mills                              status: severity.severityText,
1024615a2f89SGunnar Mills                              order: severity.order,
1025615a2f89SGunnar Mills                              custom_order: customOrder,
1026615a2f89SGunnar Mills                              search_text:
1027615a2f89SGunnar Mills                                  (title + ' ' + content.data[key].Value + ' ' +
1028615a2f89SGunnar Mills                                   Constants.SENSOR_UNIT_MAP[content.data[key]
1029615a2f89SGunnar Mills                                                                 .Unit] +
1030615a2f89SGunnar Mills                                   ' ' + severity.severityText + ' ' +
1031615a2f89SGunnar Mills                                   content.data[key].CriticalLow + ' ' +
1032615a2f89SGunnar Mills                                   content.data[key].CriticalHigh + ' ' +
1033615a2f89SGunnar Mills                                   content.data[key].WarningLow + ' ' +
1034615a2f89SGunnar Mills                                   content.data[key].WarningHigh + ' ')
1035615a2f89SGunnar Mills                                      .toLowerCase(),
1036615a2f89SGunnar Mills                              original_data:
1037615a2f89SGunnar Mills                                  {key: key, value: content.data[key]}
1038615a2f89SGunnar Mills                            },
1039615a2f89SGunnar Mills                            content.data[key]));
1040615a2f89SGunnar Mills                      }
1041615a2f89SGunnar Mills                    }
1042615a2f89SGunnar Mills
1043e4ae854cSAlexander Filippov                    sensorData.sort(function(a, b) {
1044e4ae854cSAlexander Filippov                      return a.title.localeCompare(
1045e4ae854cSAlexander Filippov                          b.title, 'en', {numeric: true});
1046e4ae854cSAlexander Filippov                    });
1047e4ae854cSAlexander Filippov
1048615a2f89SGunnar Mills                    callback(sensorData, dataClone);
10492f481e4cSAppaRao Puli                  },
10502f481e4cSAppaRao Puli                  function(error) {
1051615a2f89SGunnar Mills                    console.log(error);
10522f481e4cSAppaRao Puli                  });
10532f481e4cSAppaRao Puli        },
1054033025f3SGunnar Mills        getActivation: function(imageId) {
1055033025f3SGunnar Mills          return $http({
1056033025f3SGunnar Mills                   method: 'GET',
1057d27bb135SAndrew Geissler                   url: DataService.getHost() +
1058d27bb135SAndrew Geissler                       '/xyz/openbmc_project/software/' + imageId +
1059d27bb135SAndrew Geissler                       '/attr/Activation',
1060033025f3SGunnar Mills                   withCredentials: true
1061d27bb135SAndrew Geissler                 })
1062d27bb135SAndrew Geissler              .then(function(response) {
1063033025f3SGunnar Mills                return response.data;
1064033025f3SGunnar Mills              });
1065033025f3SGunnar Mills        },
1066df3bd124SMichael Davis        getFirmwares: function() {
1067df3bd124SMichael Davis          var deferred = $q.defer();
1068c016139fSIftekharul Islam          $http({
1069c016139fSIftekharul Islam            method: 'GET',
1070d27bb135SAndrew Geissler            url: DataService.getHost() +
1071d27bb135SAndrew Geissler                '/xyz/openbmc_project/software/enumerate',
1072c016139fSIftekharul Islam            withCredentials: true
1073d27bb135SAndrew Geissler          })
1074d27bb135SAndrew Geissler              .then(
1075d27bb135SAndrew Geissler                  function(response) {
1076bbcf670aSEd Tanous                    var json = JSON.stringify(response.data);
1077c016139fSIftekharul Islam                    var content = JSON.parse(json);
1078c016139fSIftekharul Islam                    var data = [];
1079c016139fSIftekharul Islam                    var isExtended = false;
1080ba5e3f34SAndrew Geissler                    var bmcActiveVersion = '';
1081ba5e3f34SAndrew Geissler                    var hostActiveVersion = '';
1082ba5e3f34SAndrew Geissler                    var imageType = '';
1083c016139fSIftekharul Islam                    var extendedVersions = [];
10849b733bdcSGunnar Mills                    var functionalImages = [];
1085c016139fSIftekharul Islam
1086c016139fSIftekharul Islam                    function getFormatedExtendedVersions(extendedVersion) {
1087c016139fSIftekharul Islam                      var versions = [];
1088ba5e3f34SAndrew Geissler                      extendedVersion = extendedVersion.split(',');
1089c016139fSIftekharul Islam
1090c016139fSIftekharul Islam                      extendedVersion.forEach(function(item) {
1091ba5e3f34SAndrew Geissler                        var parts = item.split('-');
1092c016139fSIftekharul Islam                        var numberIndex = 0;
1093c016139fSIftekharul Islam                        for (var i = 0; i < parts.length; i++) {
1094c016139fSIftekharul Islam                          if (/[0-9]/.test(parts[i])) {
1095c016139fSIftekharul Islam                            numberIndex = i;
1096c016139fSIftekharul Islam                            break;
109799d199f3SIftekharul Islam                          }
1098c016139fSIftekharul Islam                        }
109922d7822dSGeorge Liu                        if (numberIndex > 0) {
1100c016139fSIftekharul Islam                          var titlePart = parts.splice(0, numberIndex);
1101ba5e3f34SAndrew Geissler                          titlePart = titlePart.join('');
1102d27bb135SAndrew Geissler                          titlePart = titlePart[0].toUpperCase() +
1103d27bb135SAndrew Geissler                              titlePart.substr(1, titlePart.length);
1104ba5e3f34SAndrew Geissler                          var versionPart = parts.join('-');
110522d7822dSGeorge Liu                          versions.push(
110622d7822dSGeorge Liu                              {title: titlePart, version: versionPart});
110722d7822dSGeorge Liu                        }
1108c016139fSIftekharul Islam                      });
1109c016139fSIftekharul Islam
1110c016139fSIftekharul Islam                      return versions;
1111c016139fSIftekharul Islam                    }
1112c016139fSIftekharul Islam
11139b733bdcSGunnar Mills                    // Get the list of functional images so we can compare
11149b733bdcSGunnar Mills                    // later if an image is functional
1115ba5e3f34SAndrew Geissler                    if (content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH]) {
1116d27bb135SAndrew Geissler                      functionalImages =
1117d27bb135SAndrew Geissler                          content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH]
1118d27bb135SAndrew Geissler                              .endpoints;
11199b733bdcSGunnar Mills                    }
1120c016139fSIftekharul Islam                    for (var key in content.data) {
1121d27bb135SAndrew Geissler                      if (content.data.hasOwnProperty(key) &&
1122d27bb135SAndrew Geissler                          content.data[key].hasOwnProperty('Version')) {
1123ac9131e0SGunnar Mills                        var activationStatus = '';
1124ac9131e0SGunnar Mills
1125902c38caSGunnar Mills                        // If the image is "Functional" use that for the
112628711a6aSAppaRao Puli                        // activation status, else use the value of
112728711a6aSAppaRao Puli                        // "Activation"
1128902c38caSGunnar Mills                        // github.com/openbmc/phosphor-dbus-interfaces/blob/master/xyz/openbmc_project/Software/Activation.interface.yaml
1129ac9131e0SGunnar Mills                        if (content.data[key].Activation) {
1130d27bb135SAndrew Geissler                          activationStatus =
1131d27bb135SAndrew Geissler                              content.data[key].Activation.split('.').pop();
1132ac9131e0SGunnar Mills                        }
1133ac9131e0SGunnar Mills
1134ba5e3f34SAndrew Geissler                        if (functionalImages.includes(key)) {
1135ba5e3f34SAndrew Geissler                          activationStatus = 'Functional';
1136902c38caSGunnar Mills                        }
11379b733bdcSGunnar Mills
1138ba5e3f34SAndrew Geissler                        imageType = content.data[key].Purpose.split('.').pop();
1139d27bb135SAndrew Geissler                        isExtended = content.data[key].hasOwnProperty(
1140d27bb135SAndrew Geissler                                         'ExtendedVersion') &&
1141d27bb135SAndrew Geissler                            content.data[key].ExtendedVersion != '';
1142c016139fSIftekharul Islam                        if (isExtended) {
1143d27bb135SAndrew Geissler                          extendedVersions = getFormatedExtendedVersions(
1144d27bb135SAndrew Geissler                              content.data[key].ExtendedVersion);
1145c016139fSIftekharul Islam                        }
1146d27bb135SAndrew Geissler                        data.push(Object.assign(
1147d27bb135SAndrew Geissler                            {
1148c016139fSIftekharul Islam                              path: key,
1149902c38caSGunnar Mills                              activationStatus: activationStatus,
1150ba5e3f34SAndrew Geissler                              imageId: key.split('/').pop(),
1151c016139fSIftekharul Islam                              imageType: imageType,
1152c016139fSIftekharul Islam                              isExtended: isExtended,
1153d27bb135SAndrew Geissler                              extended:
1154d27bb135SAndrew Geissler                                  {show: false, versions: extendedVersions},
1155d27bb135SAndrew Geissler                              data: {key: key, value: content.data[key]}
1156c016139fSIftekharul Islam                            },
1157d27bb135SAndrew Geissler                            content.data[key]));
1158c016139fSIftekharul Islam
1159d27bb135SAndrew Geissler                        if (activationStatus == 'Functional' &&
1160d27bb135SAndrew Geissler                            imageType == 'BMC') {
1161c016139fSIftekharul Islam                          bmcActiveVersion = content.data[key].Version;
1162c016139fSIftekharul Islam                        }
1163c016139fSIftekharul Islam
1164d27bb135SAndrew Geissler                        if (activationStatus == 'Functional' &&
1165d27bb135SAndrew Geissler                            imageType == 'Host') {
1166c016139fSIftekharul Islam                          hostActiveVersion = content.data[key].Version;
1167c016139fSIftekharul Islam                        }
1168c016139fSIftekharul Islam                      }
1169c016139fSIftekharul Islam                    }
1170df3bd124SMichael Davis
1171df3bd124SMichael Davis                    deferred.resolve({
1172df3bd124SMichael Davis                      data: data,
1173df3bd124SMichael Davis                      bmcActiveVersion: bmcActiveVersion,
1174df3bd124SMichael Davis                      hostActiveVersion: hostActiveVersion
1175df3bd124SMichael Davis                    });
1176d27bb135SAndrew Geissler                  },
1177d27bb135SAndrew Geissler                  function(error) {
1178c016139fSIftekharul Islam                    console.log(error);
1179df3bd124SMichael Davis                    deferred.reject(error);
1180c016139fSIftekharul Islam                  });
1181df3bd124SMichael Davis
1182df3bd124SMichael Davis          return deferred.promise;
1183c016139fSIftekharul Islam        },
11841acb412dSIftekharul Islam        changePriority: function(imageId, priority) {
1185f7f59469SGunnar Mills          return $http({
11861acb412dSIftekharul Islam                   method: 'PUT',
1187f7f59469SGunnar Mills                   url: DataService.getHost() +
1188f7f59469SGunnar Mills                       '/xyz/openbmc_project/software/' + imageId +
1189f7f59469SGunnar Mills                       '/attr/Priority',
11901acb412dSIftekharul Islam                   withCredentials: true,
1191d27bb135SAndrew Geissler                   data: JSON.stringify({'data': priority})
1192ba5e3f34SAndrew Geissler                 })
1193f7f59469SGunnar Mills              .then(function(response) {
1194f7f59469SGunnar Mills                return response.data;
11951acb412dSIftekharul Islam              });
11961acb412dSIftekharul Islam        },
11972a489554SIftekharul Islam        deleteImage: function(imageId) {
1198f7f59469SGunnar Mills          return $http({
11992a489554SIftekharul Islam                   method: 'POST',
1200f7f59469SGunnar Mills                   url: DataService.getHost() +
1201f7f59469SGunnar Mills                       '/xyz/openbmc_project/software/' + imageId +
1202f7f59469SGunnar Mills                       '/action/Delete',
12032a489554SIftekharul Islam                   withCredentials: true,
1204d27bb135SAndrew Geissler                   data: JSON.stringify({'data': []})
1205ba5e3f34SAndrew Geissler                 })
1206f7f59469SGunnar Mills              .then(function(response) {
1207f7f59469SGunnar Mills                return response.data;
12082a489554SIftekharul Islam              });
12092a489554SIftekharul Islam        },
12102a489554SIftekharul Islam        activateImage: function(imageId) {
1211f7f59469SGunnar Mills          return $http({
1212c016139fSIftekharul Islam                   method: 'PUT',
1213f7f59469SGunnar Mills                   url: DataService.getHost() +
1214f7f59469SGunnar Mills                       '/xyz/openbmc_project/software/' + imageId +
1215f7f59469SGunnar Mills                       '/attr/RequestedActivation',
12162a489554SIftekharul Islam                   withCredentials: true,
1217f7f59469SGunnar Mills                   data: JSON.stringify(
1218f7f59469SGunnar Mills                       {'data': Constants.FIRMWARE.ACTIVATE_FIRMWARE})
1219ba5e3f34SAndrew Geissler                 })
1220f7f59469SGunnar Mills              .then(function(response) {
1221f7f59469SGunnar Mills                return response.data;
12222a489554SIftekharul Islam              });
12232a489554SIftekharul Islam        },
12242a489554SIftekharul Islam        uploadImage: function(file) {
122587c3db65SGunnar Mills          return $http({
12262a489554SIftekharul Islam                   method: 'POST',
1227c016139fSIftekharul Islam                   timeout: 5 * 60 * 1000,
1228ba5e3f34SAndrew Geissler                   url: DataService.getHost() + '/upload/image',
1229dd9d4c32SGunnar Mills                   // Overwrite the default 'application/json' Content-Type
1230d27bb135SAndrew Geissler                   headers: {'Content-Type': 'application/octet-stream'},
1231c016139fSIftekharul Islam                   withCredentials: true,
1232c016139fSIftekharul Islam                   data: file
1233d27bb135SAndrew Geissler                 })
1234d27bb135SAndrew Geissler              .then(function(response) {
123587c3db65SGunnar Mills                return response.data;
1236c016139fSIftekharul Islam              });
12371acb412dSIftekharul Islam        },
12381acb412dSIftekharul Islam        downloadImage: function(host, filename) {
123936379b99SGunnar Mills          return $http({
12401acb412dSIftekharul Islam                   method: 'POST',
1241d27bb135SAndrew Geissler                   url: DataService.getHost() +
1242d27bb135SAndrew Geissler                       '/xyz/openbmc_project/software/action/DownloadViaTFTP',
12431acb412dSIftekharul Islam                   withCredentials: true,
1244d27bb135SAndrew Geissler                   data: JSON.stringify({'data': [filename, host]}),
12451acb412dSIftekharul Islam                   responseType: 'arraybuffer'
1246d27bb135SAndrew Geissler                 })
1247d27bb135SAndrew Geissler              .then(function(response) {
124836379b99SGunnar Mills                return response.data;
12491acb412dSIftekharul Islam              });
1250c016139fSIftekharul Islam        },
125117708f2aSGunnar Mills        getServerInfo: function() {
12525ea75751SGunnar Mills          // TODO: openbmc/openbmc#3117 Need a way via REST to get
12535ea75751SGunnar Mills          // interfaces so we can get the system object(s) by the looking
12545ea75751SGunnar Mills          // for the system interface.
125517708f2aSGunnar Mills          return $http({
125617708f2aSGunnar Mills                   method: 'GET',
1257d27bb135SAndrew Geissler                   url: DataService.getHost() +
1258d27bb135SAndrew Geissler                       '/xyz/openbmc_project/inventory/system',
125917708f2aSGunnar Mills                   withCredentials: true
1260d27bb135SAndrew Geissler                 })
1261d27bb135SAndrew Geissler              .then(function(response) {
126217708f2aSGunnar Mills                return response.data;
126317708f2aSGunnar Mills              });
126417708f2aSGunnar Mills        },
1265ed96f8bbSGunnar Mills        getBMCTime: function() {
1266ed96f8bbSGunnar Mills          return $http({
1267ed96f8bbSGunnar Mills                   method: 'GET',
1268ba5e3f34SAndrew Geissler                   url: DataService.getHost() + '/xyz/openbmc_project/time/bmc',
1269ed96f8bbSGunnar Mills                   withCredentials: true
1270d27bb135SAndrew Geissler                 })
1271d27bb135SAndrew Geissler              .then(function(response) {
1272ed96f8bbSGunnar Mills                return response.data;
1273ed96f8bbSGunnar Mills              });
1274ed96f8bbSGunnar Mills        },
1275c74d434cSGunnar Mills        getTime: function() {
1276c74d434cSGunnar Mills          return $http({
1277c74d434cSGunnar Mills                   method: 'GET',
1278c74d434cSGunnar Mills                   url: DataService.getHost() +
1279c74d434cSGunnar Mills                       '/xyz/openbmc_project/time/enumerate',
1280c74d434cSGunnar Mills                   withCredentials: true
1281c74d434cSGunnar Mills                 })
1282c74d434cSGunnar Mills              .then(function(response) {
1283c74d434cSGunnar Mills                return response.data;
1284c74d434cSGunnar Mills              });
1285c74d434cSGunnar Mills        },
1286b7ea2790SGunnar Mills        // Even though NTPServers is a network interface specific path
1287b7ea2790SGunnar Mills        // (e.g. /xyz/openbmc_project/network/eth0/attr/NTPServers) it acts
1288b7ea2790SGunnar Mills        // like a global setting. Just use eth0 for setting and getting the
1289b7ea2790SGunnar Mills        // NTP Servers until it is moved to a non-network interface specific
1290b7ea2790SGunnar Mills        // path like it is in Redfish. TODO: openbmc/phosphor-time-manager#4
1291b7ea2790SGunnar Mills        getNTPServers: function() {
1292b7ea2790SGunnar Mills          return $http({
1293b7ea2790SGunnar Mills                   method: 'GET',
1294b7ea2790SGunnar Mills                   url: DataService.getHost() +
1295b7ea2790SGunnar Mills                       '/xyz/openbmc_project/network/eth0/attr/NTPServers',
1296b7ea2790SGunnar Mills                   withCredentials: true
1297b7ea2790SGunnar Mills                 })
1298b7ea2790SGunnar Mills              .then(function(response) {
1299b7ea2790SGunnar Mills                return response.data;
1300b7ea2790SGunnar Mills              });
1301b7ea2790SGunnar Mills        },
1302b7ea2790SGunnar Mills        setNTPServers: function(ntpServers) {
1303b7ea2790SGunnar Mills          return $http({
1304b7ea2790SGunnar Mills                   method: 'PUT',
1305b7ea2790SGunnar Mills                   url: DataService.getHost() +
1306b7ea2790SGunnar Mills                       '/xyz/openbmc_project/network/eth0/attr/NTPServers',
1307b7ea2790SGunnar Mills                   withCredentials: true,
1308b7ea2790SGunnar Mills                   data: JSON.stringify({'data': ntpServers})
1309b7ea2790SGunnar Mills                 })
1310b7ea2790SGunnar Mills              .then(function(response) {
1311b7ea2790SGunnar Mills                return response.data;
1312b7ea2790SGunnar Mills              });
1313b7ea2790SGunnar Mills        },
1314b7ea2790SGunnar Mills        setTimeMode: function(timeMode) {
1315b7ea2790SGunnar Mills          return $http({
1316b7ea2790SGunnar Mills                   method: 'PUT',
1317b7ea2790SGunnar Mills                   url: DataService.getHost() +
1318b7ea2790SGunnar Mills                       '/xyz/openbmc_project/time/sync_method/attr/TimeSyncMethod',
1319b7ea2790SGunnar Mills                   withCredentials: true,
1320b7ea2790SGunnar Mills                   data: JSON.stringify({'data': timeMode})
1321b7ea2790SGunnar Mills                 })
1322b7ea2790SGunnar Mills              .then(function(response) {
1323b7ea2790SGunnar Mills                return response.data;
1324b7ea2790SGunnar Mills              });
1325b7ea2790SGunnar Mills        },
1326b7ea2790SGunnar Mills        setTimeOwner: function(timeOwner) {
1327b7ea2790SGunnar Mills          return $http({
1328b7ea2790SGunnar Mills                   method: 'PUT',
1329b7ea2790SGunnar Mills                   url: DataService.getHost() +
1330b7ea2790SGunnar Mills                       '/xyz/openbmc_project/time/owner/attr/TimeOwner',
1331b7ea2790SGunnar Mills                   withCredentials: true,
1332b7ea2790SGunnar Mills                   data: JSON.stringify({'data': timeOwner})
1333b7ea2790SGunnar Mills                 })
1334b7ea2790SGunnar Mills              .then(function(response) {
1335b7ea2790SGunnar Mills                return response.data;
1336b7ea2790SGunnar Mills              });
1337b7ea2790SGunnar Mills        },
1338b7ea2790SGunnar Mills        setBMCTime: function(time) {
1339b7ea2790SGunnar Mills          return $http({
1340b7ea2790SGunnar Mills                   method: 'PUT',
1341b7ea2790SGunnar Mills                   url: DataService.getHost() +
1342b7ea2790SGunnar Mills                       '/xyz/openbmc_project/time/bmc/attr/Elapsed',
1343b7ea2790SGunnar Mills                   withCredentials: true,
1344b7ea2790SGunnar Mills                   data: JSON.stringify({'data': time})
1345b7ea2790SGunnar Mills                 })
1346b7ea2790SGunnar Mills              .then(function(response) {
1347b7ea2790SGunnar Mills                return response.data;
1348b7ea2790SGunnar Mills              });
1349b7ea2790SGunnar Mills        },
1350b7ea2790SGunnar Mills        setHostTime: function(time) {
1351b7ea2790SGunnar Mills          return $http({
1352b7ea2790SGunnar Mills                   method: 'PUT',
1353b7ea2790SGunnar Mills                   url: DataService.getHost() +
1354b7ea2790SGunnar Mills                       '/xyz/openbmc_project/time/host/attr/Elapsed',
1355b7ea2790SGunnar Mills                   withCredentials: true,
1356b7ea2790SGunnar Mills                   data: JSON.stringify({'data': time})
1357b7ea2790SGunnar Mills                 })
1358b7ea2790SGunnar Mills              .then(function(response) {
1359b7ea2790SGunnar Mills                return response.data;
1360b7ea2790SGunnar Mills              });
1361b7ea2790SGunnar Mills        },
1362309b5da3Sbeccabroek        getCertificateLocations: function() {
1363309b5da3Sbeccabroek          return $http({
1364309b5da3Sbeccabroek                   method: 'GET',
1365309b5da3Sbeccabroek                   url: DataService.getHost() +
1366309b5da3Sbeccabroek                       '/redfish/v1/CertificateService/CertificateLocations',
1367309b5da3Sbeccabroek                   withCredentials: true
1368309b5da3Sbeccabroek                 })
1369309b5da3Sbeccabroek              .then(function(response) {
1370309b5da3Sbeccabroek                return response.data;
1371309b5da3Sbeccabroek              });
1372309b5da3Sbeccabroek        },
1373309b5da3Sbeccabroek        getCertificate: function(location) {
1374309b5da3Sbeccabroek          return $http({
1375309b5da3Sbeccabroek                   method: 'GET',
1376309b5da3Sbeccabroek                   url: DataService.getHost() + location,
1377309b5da3Sbeccabroek                   withCredentials: true
1378309b5da3Sbeccabroek                 })
1379309b5da3Sbeccabroek              .then(function(response) {
1380309b5da3Sbeccabroek                return response.data;
1381309b5da3Sbeccabroek              });
1382309b5da3Sbeccabroek        },
1383309b5da3Sbeccabroek        addNewCertificate: function(file, type) {
1384309b5da3Sbeccabroek          return $http({
1385309b5da3Sbeccabroek                   method: 'POST',
1386309b5da3Sbeccabroek                   url: DataService.getHost() + type.location,
1387309b5da3Sbeccabroek                   headers: {'Content-Type': 'application/x-pem-file'},
1388309b5da3Sbeccabroek                   withCredentials: true,
1389309b5da3Sbeccabroek                   data: file
1390309b5da3Sbeccabroek                 })
1391309b5da3Sbeccabroek              .then(function(response) {
1392309b5da3Sbeccabroek                return response.data;
1393309b5da3Sbeccabroek              });
1394309b5da3Sbeccabroek        },
13955e8785d3Smiramurali23        createCSRCertificate: function(data) {
13965e8785d3Smiramurali23          return $http({
13975e8785d3Smiramurali23                   method: 'POST',
13985e8785d3Smiramurali23                   url: DataService.getHost() +
13995e8785d3Smiramurali23                       '/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR',
14005e8785d3Smiramurali23                   withCredentials: true,
14015e8785d3Smiramurali23                   data: data
14025e8785d3Smiramurali23                 })
14035e8785d3Smiramurali23              .then(function(response) {
14045e8785d3Smiramurali23                return response.data['CSRString'];
14055e8785d3Smiramurali23              });
14065e8785d3Smiramurali23        },
1407309b5da3Sbeccabroek        replaceCertificate: function(data) {
1408309b5da3Sbeccabroek          return $http({
1409309b5da3Sbeccabroek                   method: 'POST',
1410309b5da3Sbeccabroek                   url: DataService.getHost() +
1411309b5da3Sbeccabroek                       '/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate',
1412309b5da3Sbeccabroek                   withCredentials: true,
1413309b5da3Sbeccabroek                   data: data
1414309b5da3Sbeccabroek                 })
1415309b5da3Sbeccabroek              .then(function(response) {
1416309b5da3Sbeccabroek                return response.data;
1417309b5da3Sbeccabroek              });
1418309b5da3Sbeccabroek        },
1419f70f4255SZbigniew Kurzynski        deleteRedfishObject: function(objectPath) {
1420f70f4255SZbigniew Kurzynski          return $http({
1421f70f4255SZbigniew Kurzynski                   method: 'DELETE',
1422f70f4255SZbigniew Kurzynski                   url: DataService.getHost() + objectPath,
1423f70f4255SZbigniew Kurzynski                   withCredentials: true
1424f70f4255SZbigniew Kurzynski                 })
1425f70f4255SZbigniew Kurzynski              .then(function(response) {
1426f70f4255SZbigniew Kurzynski                return response.data;
1427f70f4255SZbigniew Kurzynski              });
1428f70f4255SZbigniew Kurzynski        },
1429ee27d754SIftekharul Islam        getHardwares: function(callback) {
1430ee27d754SIftekharul Islam          $http({
1431ee27d754SIftekharul Islam            method: 'GET',
1432d27bb135SAndrew Geissler            url: DataService.getHost() +
1433d27bb135SAndrew Geissler                '/xyz/openbmc_project/inventory/enumerate',
1434ee27d754SIftekharul Islam            withCredentials: true
1435bbcf670aSEd Tanous          }).then(function(response) {
1436bbcf670aSEd Tanous            var json = JSON.stringify(response.data);
1437ee27d754SIftekharul Islam            var content = JSON.parse(json);
1438ee27d754SIftekharul Islam            var hardwareData = [];
1439ee27d754SIftekharul Islam            var keyIndexMap = {};
1440ba5e3f34SAndrew Geissler            var title = '';
1441628ba8b3Sbeccabroek            var depth = '';
1442ee27d754SIftekharul Islam            var data = [];
1443ba5e3f34SAndrew Geissler            var searchText = '';
1444ee27d754SIftekharul Islam            var componentIndex = -1;
1445628ba8b3Sbeccabroek            var parent = '';
1446ee27d754SIftekharul Islam
1447ee27d754SIftekharul Islam            function isSubComponent(key) {
1448d27bb135SAndrew Geissler              for (var i = 0; i < Constants.HARDWARE.parent_components.length;
1449d27bb135SAndrew Geissler                   i++) {
1450d27bb135SAndrew Geissler                if (key.split(Constants.HARDWARE.parent_components[i]).length ==
1451d27bb135SAndrew Geissler                    2)
1452d27bb135SAndrew Geissler                  return true;
1453ee27d754SIftekharul Islam              }
1454ee27d754SIftekharul Islam
1455ee27d754SIftekharul Islam              return false;
1456ee27d754SIftekharul Islam            }
1457ee27d754SIftekharul Islam
1458ee27d754SIftekharul Islam            function titlelize(title) {
1459ba5e3f34SAndrew Geissler              title = title.replace(/([A-Z0-9]+)/g, ' $1').replace(/^\s+/, '');
1460d27bb135SAndrew Geissler              for (var i = 0; i < Constants.HARDWARE.uppercase_titles.length;
1461d27bb135SAndrew Geissler                   i++) {
1462d27bb135SAndrew Geissler                if (title.toLowerCase().indexOf(
1463d27bb135SAndrew Geissler                        (Constants.HARDWARE.uppercase_titles[i] + ' ')) > -1) {
1464ee27d754SIftekharul Islam                  return title.toUpperCase();
1465ee27d754SIftekharul Islam                }
1466ee27d754SIftekharul Islam              }
1467ee27d754SIftekharul Islam
1468ee27d754SIftekharul Islam              return title;
1469ee27d754SIftekharul Islam            }
1470ee27d754SIftekharul Islam
1471ee27d754SIftekharul Islam            function camelcaseToLabel(obj) {
1472d27bb135SAndrew Geissler              var transformed = [], label = '', value = '';
1473ee27d754SIftekharul Islam              for (var key in obj) {
1474ba5e3f34SAndrew Geissler                label = key.replace(/([A-Z0-9]+)/g, ' $1').replace(/^\s+/, '');
1475ba5e3f34SAndrew Geissler                if (obj[key] !== '') {
14768947e701SIftekharul Islam                  value = obj[key];
14778947e701SIftekharul Islam                  if (value == 1 || value == 0) {
14788947e701SIftekharul Islam                    value = (value == 1) ? 'Yes' : 'No';
14798947e701SIftekharul Islam                  }
1480d27bb135SAndrew Geissler                  transformed.push({key: label, value: value});
1481ee27d754SIftekharul Islam                }
1482ee27d754SIftekharul Islam              }
1483ee27d754SIftekharul Islam
1484ee27d754SIftekharul Islam              return transformed;
1485ee27d754SIftekharul Islam            }
1486ee27d754SIftekharul Islam
1487628ba8b3Sbeccabroek            function determineParent(key) {
1488628ba8b3Sbeccabroek              var levels = key.split('/');
1489628ba8b3Sbeccabroek              levels.pop();
1490628ba8b3Sbeccabroek              return levels.join('/');
1491628ba8b3Sbeccabroek            }
1492628ba8b3Sbeccabroek
1493ee27d754SIftekharul Islam            function getSearchText(data) {
1494ba5e3f34SAndrew Geissler              var searchText = '';
1495ee27d754SIftekharul Islam              for (var i = 0; i < data.length; i++) {
1496ba5e3f34SAndrew Geissler                searchText += ' ' + data[i].key + ' ' + data[i].value;
1497ee27d754SIftekharul Islam              }
1498ee27d754SIftekharul Islam
1499ee27d754SIftekharul Islam              return searchText;
1500ee27d754SIftekharul Islam            }
1501ee27d754SIftekharul Islam
1502ee27d754SIftekharul Islam            for (var key in content.data) {
1503ee27d754SIftekharul Islam              if (content.data.hasOwnProperty(key) &&
1504ee27d754SIftekharul Islam                  key.indexOf(Constants.HARDWARE.component_key_filter) == 0) {
1505eedf0b92SGunnar Mills                // All and only associations have the property "endpoints".
1506c9c8e67bSGunnar Mills                // We don't want to show forward/reverse association objects
1507c9c8e67bSGunnar Mills                // that the mapper created on the inventory panel.
1508eedf0b92SGunnar Mills                // Example: An association from the BMC inventory item to the
1509c9c8e67bSGunnar Mills                // BMC firmware images. See:
15102ac4eda3SGunnar Mills                // https://github.com/openbmc/docs/blob/master/architecture/object-mapper.md#associations
1511eedf0b92SGunnar Mills                if (content.data[key].hasOwnProperty('endpoints')) {
1512eedf0b92SGunnar Mills                  continue;
1513eedf0b92SGunnar Mills                }
1514c9c8e67bSGunnar Mills                // There is also an "Associations" property created by the
1515c9c8e67bSGunnar Mills                // Association interface. These would show on the inventory
1516c9c8e67bSGunnar Mills                // panel under the individual inventory item dropdown. There
1517c9c8e67bSGunnar Mills                // can be a lot of associations in this property and they are
1518c9c8e67bSGunnar Mills                // long, full D-Bus paths. Not particularly useful. Remove
1519c9c8e67bSGunnar Mills                // for now.
1520ee27d754SIftekharul Islam
1521c9c8e67bSGunnar Mills                if (content.data[key].hasOwnProperty('Associations')) {
1522c9c8e67bSGunnar Mills                  delete content.data[key].Associations;
1523c9c8e67bSGunnar Mills                }
1524c9c8e67bSGunnar Mills
15255ccf9e36SGunnar Mills                // Remove the Purpose property from any inventory item.
15265ccf9e36SGunnar Mills                // The purpose property isn't useful to a user.
15275ccf9e36SGunnar Mills                // E.g. in a Power Supply:
15285ccf9e36SGunnar Mills                // Purpose
15295ccf9e36SGunnar Mills                // xyz.openbmc_project.Software.Version.VersionPurpose.Other
15305ccf9e36SGunnar Mills                // Remove when we move inventory to Redfish
15315ccf9e36SGunnar Mills                if (content.data[key].hasOwnProperty('Purpose')) {
15325ccf9e36SGunnar Mills                  delete content.data[key].Purpose;
15335ccf9e36SGunnar Mills                }
15345ccf9e36SGunnar Mills
1535c9c8e67bSGunnar Mills                data = camelcaseToLabel(content.data[key]);
1536c9c8e67bSGunnar Mills                searchText = getSearchText(data);
1537c9c8e67bSGunnar Mills                title = key.split('/').pop();
1538ee27d754SIftekharul Islam                title = titlelize(title);
1539628ba8b3Sbeccabroek                // e.g. /xyz/openbmc_project/inventory/system and
1540628ba8b3Sbeccabroek                // /xyz/openbmc_project/inventory/system/chassis are depths of 5
1541628ba8b3Sbeccabroek                // and 6.
1542628ba8b3Sbeccabroek                depth = key.split('/').length;
1543628ba8b3Sbeccabroek                parent = determineParent(key);
1544ee27d754SIftekharul Islam
1545ee27d754SIftekharul Islam                if (!isSubComponent(key)) {
1546d27bb135SAndrew Geissler                  hardwareData.push(Object.assign(
1547d27bb135SAndrew Geissler                      {
1548ee27d754SIftekharul Islam                        path: key,
1549ee27d754SIftekharul Islam                        title: title,
1550628ba8b3Sbeccabroek                        depth: depth,
1551628ba8b3Sbeccabroek                        parent: parent,
1552ee27d754SIftekharul Islam                        selected: false,
1553ee27d754SIftekharul Islam                        expanded: false,
1554d27bb135SAndrew Geissler                        search_text: title.toLowerCase() + ' ' +
1555d27bb135SAndrew Geissler                            searchText.toLowerCase(),
1556ee27d754SIftekharul Islam                        sub_components: [],
1557d27bb135SAndrew Geissler                        original_data: {key: key, value: content.data[key]}
1558d27bb135SAndrew Geissler                      },
1559d27bb135SAndrew Geissler                      {items: data}));
1560ee27d754SIftekharul Islam
1561c9c8e67bSGunnar Mills
1562ee27d754SIftekharul Islam                  keyIndexMap[key] = hardwareData.length - 1;
1563d27bb135SAndrew Geissler                } else {
1564628ba8b3Sbeccabroek                  parent = determineParent(key)
1565628ba8b3Sbeccabroek                  componentIndex = keyIndexMap[parent];
1566ee27d754SIftekharul Islam                  data = content.data[key];
1567ee27d754SIftekharul Islam                  data.title = title;
1568ee27d754SIftekharul Islam                  hardwareData[componentIndex].sub_components.push(data);
1569d27bb135SAndrew Geissler                  hardwareData[componentIndex].search_text +=
1570d27bb135SAndrew Geissler                      ' ' + title.toLowerCase();
1571cdd9d6bcSGunnar Mills
1572d27bb135SAndrew Geissler                  // Sort the subcomponents alphanumeric so they are displayed
1573d27bb135SAndrew Geissler                  // on the inventory page in order (e.g. core 0, core 1, core
1574d27bb135SAndrew Geissler                  // 2, ... core 12, core 13)
1575d27bb135SAndrew Geissler                  hardwareData[componentIndex].sub_components.sort(function(
1576d27bb135SAndrew Geissler                      a, b) {
1577d27bb135SAndrew Geissler                    return a.title.localeCompare(
1578d27bb135SAndrew Geissler                        b.title, 'en', {numeric: true});
1579cdd9d6bcSGunnar Mills                  });
1580ee27d754SIftekharul Islam                }
1581ee27d754SIftekharul Islam              }
1582ee27d754SIftekharul Islam            }
1583628ba8b3Sbeccabroek            // First, order the components by depth and then place the child
1584628ba8b3Sbeccabroek            // components beneath their parent component alphanumerically. Can
1585628ba8b3Sbeccabroek            // be removed with completion of
1586628ba8b3Sbeccabroek            // https://github.com/openbmc/openbmc/issues/3401
1587628ba8b3Sbeccabroek            // TODO: Remove this once implemented in back end
1588628ba8b3Sbeccabroek            hardwareData.sort(function(a, b) {
1589628ba8b3Sbeccabroek              if (a.depth < b.depth) return -1;
1590628ba8b3Sbeccabroek              if (a.depth > b.depth) return 1;
1591628ba8b3Sbeccabroek              return b.title.localeCompare(a.title, 'en', {numeric: true});
1592628ba8b3Sbeccabroek            });
1593628ba8b3Sbeccabroek
1594628ba8b3Sbeccabroek            var orderedComponents = [];
1595628ba8b3Sbeccabroek
1596628ba8b3Sbeccabroek            for (var i = 0; i < hardwareData.length; i++) {
1597628ba8b3Sbeccabroek              if (!keyIndexMap[hardwareData[i].parent]) {
1598628ba8b3Sbeccabroek                orderedComponents.push(hardwareData[i]);
1599628ba8b3Sbeccabroek              } else {
1600628ba8b3Sbeccabroek                for (var j = 0; j < orderedComponents.length; j++) {
1601628ba8b3Sbeccabroek                  if (orderedComponents[j].path === hardwareData[i].parent) {
1602628ba8b3Sbeccabroek                    var child = hardwareData[i];
1603628ba8b3Sbeccabroek                    orderedComponents.splice(j + 1, 0, child);
1604628ba8b3Sbeccabroek                  }
1605628ba8b3Sbeccabroek                }
1606628ba8b3Sbeccabroek              }
1607628ba8b3Sbeccabroek            }
1608ee27d754SIftekharul Islam
1609ee27d754SIftekharul Islam            if (callback) {
1610628ba8b3Sbeccabroek              callback(orderedComponents, content.data);
1611d27bb135SAndrew Geissler            } else {
1612628ba8b3Sbeccabroek              return {data: orderedComponents, original_data: content.data};
1613ee27d754SIftekharul Islam            }
1614ee27d754SIftekharul Islam          });
1615ee27d754SIftekharul Islam        },
1616f2d74644SIftekharul Islam        deleteLogs: function(logs) {
1617f2d74644SIftekharul Islam          var defer = $q.defer();
1618f2d74644SIftekharul Islam          var promises = [];
1619f2d74644SIftekharul Islam
1620f2d74644SIftekharul Islam          function finished() {
1621f2d74644SIftekharul Islam            defer.resolve();
1622f2d74644SIftekharul Islam          }
1623f2d74644SIftekharul Islam
1624f2d74644SIftekharul Islam          logs.forEach(function(item) {
1625f2d74644SIftekharul Islam            promises.push($http({
1626f2d74644SIftekharul Islam              method: 'POST',
1627d27bb135SAndrew Geissler              url: DataService.getHost() +
1628d27bb135SAndrew Geissler                  '/xyz/openbmc_project/logging/entry/' + item.Id +
1629d27bb135SAndrew Geissler                  '/action/Delete',
1630f2d74644SIftekharul Islam              withCredentials: true,
1631d27bb135SAndrew Geissler              data: JSON.stringify({'data': []})
1632f2d74644SIftekharul Islam            }));
1633f2d74644SIftekharul Islam          });
1634f2d74644SIftekharul Islam
1635f2d74644SIftekharul Islam          $q.all(promises).then(finished);
1636f2d74644SIftekharul Islam
1637f2d74644SIftekharul Islam          return defer.promise;
1638f2d74644SIftekharul Islam        },
1639f2d74644SIftekharul Islam        resolveLogs: function(logs) {
1640f2d74644SIftekharul Islam          var promises = [];
1641f2d74644SIftekharul Islam
1642f2d74644SIftekharul Islam          logs.forEach(function(item) {
1643f2d74644SIftekharul Islam            promises.push($http({
1644f2d74644SIftekharul Islam              method: 'PUT',
1645d27bb135SAndrew Geissler              url: DataService.getHost() +
1646d27bb135SAndrew Geissler                  '/xyz/openbmc_project/logging/entry/' + item.Id +
1647d27bb135SAndrew Geissler                  '/attr/Resolved',
1648f2d74644SIftekharul Islam              withCredentials: true,
16497e48d081SGunnar Mills              data: JSON.stringify({'data': true})
1650f2d74644SIftekharul Islam            }));
1651f2d74644SIftekharul Islam          });
16527e48d081SGunnar Mills          return $q.all(promises);
1653f2d74644SIftekharul Islam        },
1654e4194ce0SYoshie Muranaka        setRemoteLoggingServer: (data) => {
1655e4194ce0SYoshie Muranaka          const ip = data.hostname;
1656e4194ce0SYoshie Muranaka          const port = data.port;
1657e4194ce0SYoshie Muranaka          const setIPRequest = $http({
1658e4194ce0SYoshie Muranaka            method: 'PUT',
1659e4194ce0SYoshie Muranaka            url: DataService.getHost() +
1660e4194ce0SYoshie Muranaka                '/xyz/openbmc_project/logging/config/remote/attr/Address',
1661e4194ce0SYoshie Muranaka            withCredentials: true,
1662e4194ce0SYoshie Muranaka            data: {'data': ip}
1663e4194ce0SYoshie Muranaka          });
1664e4194ce0SYoshie Muranaka          const setPortRequest = $http({
1665e4194ce0SYoshie Muranaka            method: 'PUT',
1666e4194ce0SYoshie Muranaka            url: DataService.getHost() +
1667e4194ce0SYoshie Muranaka                '/xyz/openbmc_project/logging/config/remote/attr/Port',
1668e4194ce0SYoshie Muranaka            withCredentials: true,
1669e4194ce0SYoshie Muranaka            data: {'data': port}
1670e4194ce0SYoshie Muranaka          });
1671e4194ce0SYoshie Muranaka          const promises = [setIPRequest, setPortRequest];
1672e4194ce0SYoshie Muranaka          return $q.all(promises);
1673e4194ce0SYoshie Muranaka        },
1674e4194ce0SYoshie Muranaka        getRemoteLoggingServer: () => {
1675e4194ce0SYoshie Muranaka          return $http({
1676e4194ce0SYoshie Muranaka                   method: 'GET',
1677e4194ce0SYoshie Muranaka                   url: DataService.getHost() +
1678e4194ce0SYoshie Muranaka                       '/xyz/openbmc_project/logging/config/remote',
1679e4194ce0SYoshie Muranaka                   withCredentials: true
1680e4194ce0SYoshie Muranaka                 })
1681e4194ce0SYoshie Muranaka              .then((response) => {
1682e4194ce0SYoshie Muranaka                const remoteServer = response.data.data;
1683e4194ce0SYoshie Muranaka                if (remoteServer === undefined) {
1684e4194ce0SYoshie Muranaka                  return undefined;
1685e4194ce0SYoshie Muranaka                }
1686e4194ce0SYoshie Muranaka                const hostname = remoteServer.Address;
1687e4194ce0SYoshie Muranaka                const port = remoteServer.Port;
1688e4194ce0SYoshie Muranaka                if (hostname === '') {
1689e4194ce0SYoshie Muranaka                  return undefined;
1690e4194ce0SYoshie Muranaka                } else {
1691e4194ce0SYoshie Muranaka                  return {
1692e4194ce0SYoshie Muranaka                    hostname, port
1693e4194ce0SYoshie Muranaka                  }
1694e4194ce0SYoshie Muranaka                }
1695e4194ce0SYoshie Muranaka              });
1696e4194ce0SYoshie Muranaka        },
1697e4194ce0SYoshie Muranaka        disableRemoteLoggingServer: () => {
1698e4194ce0SYoshie Muranaka          return SERVICE.setRemoteLoggingServer({hostname: '', port: 0});
1699e4194ce0SYoshie Muranaka        },
1700e4194ce0SYoshie Muranaka        updateRemoteLoggingServer: (data) => {
1701e4194ce0SYoshie Muranaka          // Recommended to disable existing configuration
1702e4194ce0SYoshie Muranaka          // before updating config to new server
1703e4194ce0SYoshie Muranaka          // https://github.com/openbmc/phosphor-logging#changing-the-rsyslog-server
1704e4194ce0SYoshie Muranaka          return SERVICE.disableRemoteLoggingServer()
1705e4194ce0SYoshie Muranaka              .then(() => {
1706e4194ce0SYoshie Muranaka                return SERVICE.setRemoteLoggingServer(data);
1707e4194ce0SYoshie Muranaka              })
1708e4194ce0SYoshie Muranaka              .catch(() => {
1709e4194ce0SYoshie Muranaka                // try updating server even if initial disable attempt fails
1710e4194ce0SYoshie Muranaka                return SERVICE.setRemoteLoggingServer(data);
1711e4194ce0SYoshie Muranaka              });
1712e4194ce0SYoshie Muranaka        },
171333275839SCamVan Nguyen        getPowerConsumption: function() {
171433275839SCamVan Nguyen          return $http({
171533275839SCamVan Nguyen                   method: 'GET',
1716d27bb135SAndrew Geissler                   url: DataService.getHost() +
1717d27bb135SAndrew Geissler                       '/xyz/openbmc_project/sensors/power/total_power',
171833275839SCamVan Nguyen                   withCredentials: true
1719d27bb135SAndrew Geissler                 })
1720d27bb135SAndrew Geissler              .then(
1721d27bb135SAndrew Geissler                  function(response) {
172233275839SCamVan Nguyen                    var json = JSON.stringify(response.data);
172333275839SCamVan Nguyen                    var content = JSON.parse(json);
172433275839SCamVan Nguyen
1725*7bb1d9e6SGunnar Mills                    return content.data.Value + ' ' +
172633275839SCamVan Nguyen                        Constants.POWER_CONSUMPTION_TEXT[content.data.Unit];
1727d27bb135SAndrew Geissler                  },
1728d27bb135SAndrew Geissler                  function(error) {
172933275839SCamVan Nguyen                    if ('Not Found' == error.statusText) {
173033275839SCamVan Nguyen                      return Constants.POWER_CONSUMPTION_TEXT.notavailable;
1731d27bb135SAndrew Geissler                    } else {
17327db0e9acSCamVan Nguyen                      throw error;
173333275839SCamVan Nguyen                    }
173433275839SCamVan Nguyen                  });
173533275839SCamVan Nguyen        },
173633275839SCamVan Nguyen        getPowerCap: function() {
173733275839SCamVan Nguyen          return $http({
173833275839SCamVan Nguyen                   method: 'GET',
1739d27bb135SAndrew Geissler                   url: DataService.getHost() +
1740d27bb135SAndrew Geissler                       '/xyz/openbmc_project/control/host0/power_cap',
174133275839SCamVan Nguyen                   withCredentials: true
1742d27bb135SAndrew Geissler                 })
1743d27bb135SAndrew Geissler              .then(function(response) {
1744006aaa0fSGunnar Mills                return response.data;
1745006aaa0fSGunnar Mills              });
1746006aaa0fSGunnar Mills        },
1747006aaa0fSGunnar Mills        setPowerCapEnable: function(powerCapEnable) {
1748006aaa0fSGunnar Mills          return $http({
1749006aaa0fSGunnar Mills                   method: 'PUT',
1750006aaa0fSGunnar Mills                   url: DataService.getHost() +
1751006aaa0fSGunnar Mills                       '/xyz/openbmc_project/control/host0/power_cap/attr/PowerCapEnable',
1752006aaa0fSGunnar Mills                   withCredentials: true,
1753006aaa0fSGunnar Mills                   data: JSON.stringify({'data': powerCapEnable})
1754006aaa0fSGunnar Mills                 })
1755006aaa0fSGunnar Mills              .then(function(response) {
1756006aaa0fSGunnar Mills                return response.data;
1757006aaa0fSGunnar Mills              });
1758006aaa0fSGunnar Mills        },
1759006aaa0fSGunnar Mills        setPowerCap: function(powerCap) {
1760006aaa0fSGunnar Mills          return $http({
1761006aaa0fSGunnar Mills                   method: 'PUT',
1762006aaa0fSGunnar Mills                   url: DataService.getHost() +
1763006aaa0fSGunnar Mills                       '/xyz/openbmc_project/control/host0/power_cap/attr/PowerCap',
1764006aaa0fSGunnar Mills                   withCredentials: true,
1765006aaa0fSGunnar Mills                   data: JSON.stringify({'data': powerCap})
1766006aaa0fSGunnar Mills                 })
1767006aaa0fSGunnar Mills              .then(function(response) {
1768006aaa0fSGunnar Mills                return response.data;
176933275839SCamVan Nguyen              });
177033275839SCamVan Nguyen        },
1771e672c7cdSAndrew Geissler        setHostname: function(hostname) {
1772e672c7cdSAndrew Geissler          return $http({
1773e672c7cdSAndrew Geissler                   method: 'PUT',
1774d27bb135SAndrew Geissler                   url: DataService.getHost() +
1775d27bb135SAndrew Geissler                       '/xyz/openbmc_project/network/config/attr/HostName',
1776e672c7cdSAndrew Geissler                   withCredentials: true,
1777d27bb135SAndrew Geissler                   data: JSON.stringify({'data': hostname})
1778ba5e3f34SAndrew Geissler                 })
1779d27bb135SAndrew Geissler              .then(function(response) {
1780e672c7cdSAndrew Geissler                return response.data;
1781e672c7cdSAndrew Geissler              });
1782e672c7cdSAndrew Geissler        },
178399d199f3SIftekharul Islam      };
178499d199f3SIftekharul Islam      return SERVICE;
1785d27bb135SAndrew Geissler    }
1786d27bb135SAndrew Geissler  ]);
178799d199f3SIftekharul Islam})(window.angular);
1788