blob: f73a5847e873f785048eb504a5532110bcadb821 [file] [log] [blame]
Iftekharul Islam99d199f2017-03-24 15:28:25 -05001/**
2 * API utilities service
3 *
4 * @module app/common/services/api-utils
5 * @exports APIUtils
6 * @name APIUtils
7 * @version 0.0.1
8 */
9
10window.angular && (function (angular) {
11 'use strict';
12 angular
13 .module('app.common.services')
Iftekharul Islam1acb4122017-11-02 13:20:32 -050014 .factory('APIUtils', ['$http', 'Constants', '$q', 'dataService',function($http, Constants, $q, DataService){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050015 var SERVICE = {
Iftekharul Islam99d199f2017-03-24 15:28:25 -050016 API_CREDENTIALS: Constants.API_CREDENTIALS,
17 API_RESPONSE: Constants.API_RESPONSE,
18 CHASSIS_POWER_STATE: Constants.CHASSIS_POWER_STATE,
19 HOST_STATE_TEXT: Constants.HOST_STATE,
20 HOST_STATE: Constants.HOST_STATE,
Iftekharul Islamcd789502017-04-19 14:37:55 -050021 LED_STATE: Constants.LED_STATE,
22 LED_STATE_TEXT: Constants.LED_STATE_TEXT,
Iftekharul Islam1acb4122017-11-02 13:20:32 -050023 HOST_SESSION_STORAGE_KEY: Constants.API_CREDENTIALS.host_storage_key,
Iftekharul Islam99d199f2017-03-24 15:28:25 -050024 getChassisState: function(callback){
25 $http({
26 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -050027 url: DataService.getHost() + "/xyz/openbmc_project/state/chassis0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -050028 headers: {
29 'Accept': 'application/json',
30 'Content-Type': 'application/json'
31 },
32 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -070033 }).then(function(response){
34 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -050035 var content = JSON.parse(json);
36 callback(content.data.CurrentPowerState);
Ed Tanousbbcf6702017-10-06 13:53:06 -070037 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050038 console.log(error);
39 });
40 },
41 getHostState: function(callback){
42 $http({
43 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -050044 url: DataService.getHost() + "/xyz/openbmc_project/state/host0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -050045 headers: {
46 'Accept': 'application/json',
47 'Content-Type': 'application/json'
48 },
49 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -070050 }).then(function(response){
51 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -050052 var content = JSON.parse(json);
53 callback(content.data.CurrentHostState);
Ed Tanousbbcf6702017-10-06 13:53:06 -070054 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050055 console.log(error);
56 });
57 },
Iftekharul Islam171c6a12017-08-11 08:35:47 -050058 getNetworkInfo: function(){
59 var deferred = $q.defer();
60 $http({
61 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -050062 url: DataService.getHost() + "/xyz/openbmc_project/network/enumerate",
Iftekharul Islam171c6a12017-08-11 08:35:47 -050063 headers: {
64 'Accept': 'application/json',
65 'Content-Type': 'application/json'
66 },
67 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -070068 }).then(function(response){
69 var json = JSON.stringify(response.data);
Iftekharul Islam171c6a12017-08-11 08:35:47 -050070 var content = JSON.parse(json);
71 var hostname = "";
72 var macAddress = "";
73
Iftekharul Islam2a489552017-11-02 13:23:08 -050074 function parseNetworkData(content){
75 var data = {
76 interface_ids: [],
77 interfaces: {
78 }
79 };
80 var interfaceId = '', keyParts = [], interfaceHash = '', interfaceType = '';
81 for(var key in content.data){
82 if(key.match(/network\/eth\d+$/ig)){
83 interfaceId = key.split("/").pop();
84 if(data.interface_ids.indexOf(interfaceId) == -1){
85 data.interface_ids.push(interfaceId);
86 data.interfaces[interfaceId] = {
87 interfaceIname: '',
88 domainName:'',
89 MACAddress:'',
90 Nameservers: [],
91 DHCPEnabled: 0,
92 ipv4:
93 {
94 ids: [],
95 values: []
96 },
97 ipv6:
98 {
99 ids: [],
100 values: []
101 }
102 };
103 data.interfaces[interfaceId].MACAddress = content.data[key].MACAddress;
104 data.interfaces[interfaceId].DomainName = content.data[key].DomainName.join(" ");
105 data.interfaces[interfaceId].Nameservers = content.data[key].Nameservers;
106 data.interfaces[interfaceId].DHCPEnabled = content.data[key].DHCPEnabled;
107 }
108 }else if(key.match(/network\/eth\d+\/ipv[4|6]\/[a-z0-9]+$/ig)){
109 keyParts = key.split("/");
110 interfaceHash = keyParts.pop();
111 interfaceType = keyParts.pop();
112 interfaceId = keyParts.pop();
113
114 if(data.interfaces[interfaceId][interfaceType].ids.indexOf(interfaceHash) == -1){
115 data.interfaces[interfaceId][interfaceType].ids.push(interfaceHash);
116 data.interfaces[interfaceId][interfaceType].values.push(content.data[key]);
117 }
118 }
119 }
120 return data;
121 }
122
Iftekharul Islam171c6a12017-08-11 08:35:47 -0500123 if(content.data.hasOwnProperty('/xyz/openbmc_project/network/config') &&
124 content.data['/xyz/openbmc_project/network/config'].hasOwnProperty('HostName')
125 ){
126 hostname = content.data['/xyz/openbmc_project/network/config'].HostName;
127 }
128
129 if(content.data.hasOwnProperty('/xyz/openbmc_project/network/eth0') &&
130 content.data['/xyz/openbmc_project/network/eth0'].hasOwnProperty('MACAddress')
131 ){
132 macAddress = content.data['/xyz/openbmc_project/network/eth0'].MACAddress;
133 }
134
135 deferred.resolve({
136 data: content.data,
137 hostname: hostname,
138 mac_address: macAddress,
Iftekharul Islam2a489552017-11-02 13:23:08 -0500139 formatted_data: parseNetworkData(content)
Iftekharul Islam171c6a12017-08-11 08:35:47 -0500140 });
Ed Tanousbbcf6702017-10-06 13:53:06 -0700141 }, function(error){
Iftekharul Islam171c6a12017-08-11 08:35:47 -0500142 console.log(error);
143 deferred.reject(error);
144 });
145 return deferred.promise;
146 },
Michael Davisdf3bd122017-08-10 11:03:42 -0500147 getLEDState: function(){
148 var deferred = $q.defer();
Iftekharul Islamcd789502017-04-19 14:37:55 -0500149 $http({
150 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500151 url: DataService.getHost() + "/xyz/openbmc_project/led/groups/enclosure_identify",
Iftekharul Islamcd789502017-04-19 14:37:55 -0500152 headers: {
153 'Accept': 'application/json',
154 'Content-Type': 'application/json'
155 },
156 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700157 }).then(function(response){
158 var json = JSON.stringify(response.data);
Michael Davisdf3bd122017-08-10 11:03:42 -0500159 var content = JSON.parse(json);
160 deferred.resolve(content.data.Asserted);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700161 }, function(error){
Iftekharul Islamcd789502017-04-19 14:37:55 -0500162 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500163 deferred.reject(error);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500164 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500165 return deferred.promise;
Iftekharul Islamcd789502017-04-19 14:37:55 -0500166 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500167 login: function(username, password, callback){
168 $http({
169 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500170 url: DataService.getHost() + "/login",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500171 headers: {
172 'Accept': 'application/json',
173 'Content-Type': 'application/json'
174 },
175 withCredentials: true,
176 data: JSON.stringify({"data": [username, password]})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700177 }).then(function(response){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500178 if(callback){
Ed Tanousbbcf6702017-10-06 13:53:06 -0700179 callback(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500180 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700181 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500182 if(callback){
Iftekharul Islamcd789502017-04-19 14:37:55 -0500183 if(error && error.status && error.status == 'error'){
184 callback(error);
185 }else{
186 callback(error, true);
187 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500188 }
189 console.log(error);
190 });
191 },
Gunnar Mills32581cf2018-03-16 15:52:54 -0500192 testPassword: function(username, password){
193 // Calls /login without the current session to verify the given password is correct
194 // ignore the interceptor logout on a bad password
195 DataService.ignoreHttpError = true;
Gunnar Mills7c3e8c12018-03-28 16:16:58 -0500196 return $http({
Gunnar Mills32581cf2018-03-16 15:52:54 -0500197 method: 'POST',
198 url: DataService.getHost() + "/login",
199 headers: {
200 'Accept': 'application/json',
201 'Content-Type': 'application/json'
202 },
203 withCredentials: false,
204 data: JSON.stringify({"data": [username, password]})
205 }).then(function(response){
Gunnar Mills7c3e8c12018-03-28 16:16:58 -0500206 return response.data;
Gunnar Mills32581cf2018-03-16 15:52:54 -0500207 });
Gunnar Mills32581cf2018-03-16 15:52:54 -0500208 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500209 logout: function(callback){
210 $http({
211 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500212 url: DataService.getHost() + "/logout",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500213 headers: {
214 'Accept': 'application/json',
215 'Content-Type': 'application/json'
216 },
217 withCredentials: true,
218 data: JSON.stringify({"data": []})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700219 }).then(function(response){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500220 if(callback){
Iftekharul Islamde883402018-02-05 11:16:21 -0600221 callback(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500222 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700223 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500224 if(callback){
225 callback(null, error);
226 }
227 console.log(error);
228 });
229 },
Gunnar Mills55a8e392018-03-14 15:37:44 -0500230 changePassword: function(user, newPassword){
231 var deferred = $q.defer();
232 $http({
233 method: 'POST',
234 url: DataService.getHost() + "/xyz/openbmc_project/user/" + user + "/action/SetPassword",
235 headers: {
236 'Accept': 'application/json',
237 'Content-Type': 'application/json'
238 },
239 withCredentials: true,
240 data: JSON.stringify({"data": [newPassword]}),
241 responseType: 'arraybuffer'
242 }).then(function(response, status, headers){
243 deferred.resolve({
244 data: response,
245 status: status,
246 headers: headers
247 });
248 }, function(error){
249 console.log(error);
250 deferred.reject(error);
251 });
252 return deferred.promise;
253 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500254 chassisPowerOn: function(callback){
255 $http({
256 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500257 url: DataService.getHost() + "/xyz/openbmc_project/state/host0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500258 headers: {
259 'Accept': 'application/json',
260 'Content-Type': 'application/json'
261 },
262 withCredentials: true,
263 data: JSON.stringify({"data": []})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700264 }).then(function(response){
265 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500266 var content = JSON.parse(json);
267 if(callback){
268 return callback(content.data.CurrentPowerState);
269 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700270 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500271 if(callback){
272 callback(error);
273 }else{
274 console.log(error);
275 }
276 });
277 },
278 chassisPowerOff: function(callback){
279 $http({
Gunnar Mills3aa8b532018-02-06 15:42:29 -0600280 method: 'PUT',
281 url: DataService.getHost() + "/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500282 headers: {
283 'Accept': 'application/json',
284 'Content-Type': 'application/json'
285 },
286 withCredentials: true,
Gunnar Mills3aa8b532018-02-06 15:42:29 -0600287 data: JSON.stringify({"data": "xyz.openbmc_project.State.Chassis.Transition.Off"})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700288 }).then(function(response){
289 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500290 var content = JSON.parse(json);
291 if(callback){
Gunnar Mills3aa8b532018-02-06 15:42:29 -0600292 return callback(content.status);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500293 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700294 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500295 if(callback){
296 callback(error);
297 }else{
298 console.log(error);
299 }
300 });
301 },
Iftekharul Islamcd789502017-04-19 14:37:55 -0500302 setLEDState: function(state, callback){
303 $http({
304 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500305 url: DataService.getHost() + "/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted",
Iftekharul Islamcd789502017-04-19 14:37:55 -0500306 headers: {
307 'Accept': 'application/json',
308 'Content-Type': 'application/json'
309 },
310 withCredentials: true,
311 data: JSON.stringify({"data": state})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700312 }).then(function(response){
313 var json = JSON.stringify(response.data);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500314 var content = JSON.parse(json);
315 if(callback){
316 return callback(content.status);
317 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700318 }, function(error){
Iftekharul Islamcd789502017-04-19 14:37:55 -0500319 if(callback){
320 callback(error);
321 }else{
322 console.log(error);
323 }
324 });
325 },
Iftekharul Islam55368122017-03-27 09:46:50 -0500326 bmcReboot: function(callback){
327 $http({
328 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500329 url: DataService.getHost() + "/xyz/openbmc_project/state/bmc0/attr/RequestedBmcTransition",
Iftekharul Islam55368122017-03-27 09:46:50 -0500330 headers: {
331 'Accept': 'application/json',
332 'Content-Type': 'application/json'
333 },
334 withCredentials: true,
335 data: JSON.stringify({"data": "xyz.openbmc_project.State.BMC.Transition.Reboot"})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700336 }).then(function(response){
337 var json = JSON.stringify(response.data);
Iftekharul Islam55368122017-03-27 09:46:50 -0500338 var content = JSON.parse(json);
339 if(callback){
340 return callback(content.status);
341 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700342 }, function(error){
Iftekharul Islam55368122017-03-27 09:46:50 -0500343 if(callback){
344 callback(error);
345 }else{
346 console.log(error);
347 }
348 });
349 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500350 hostPowerOn: function(callback){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500351 $http({
352 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500353 url: DataService.getHost() + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500354 headers: {
355 'Accept': 'application/json',
356 'Content-Type': 'application/json'
357 },
358 withCredentials: true,
359 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.On"})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700360 }).then(function(response){
361 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500362 var content = JSON.parse(json);
363 if(callback){
364 return callback(content.status);
365 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700366 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500367 if(callback){
368 callback(error);
369 }else{
370 console.log(error);
371 }
372 });
373 },
374 hostPowerOff: function(callback){
375 $http({
376 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500377 url: DataService.getHost() + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500378 headers: {
379 'Accept': 'application/json',
380 'Content-Type': 'application/json'
381 },
382 withCredentials: true,
383 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Off"})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700384 }).then(function(response){
385 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500386 var content = JSON.parse(json);
387 if(callback){
388 return callback(content.status);
389 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700390 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500391 if(callback){
392 callback(error);
393 }else{
394 console.log(error);
395 }
396 });
397 },
398 hostReboot: function(callback){
399 $http({
Gunnar Millscacfa6d2018-02-07 12:09:02 -0600400 method: 'PUT',
401 url: DataService.getHost() + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500402 headers: {
403 'Accept': 'application/json',
404 'Content-Type': 'application/json'
405 },
406 withCredentials: true,
Gunnar Millscacfa6d2018-02-07 12:09:02 -0600407 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Reboot"})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700408 }).then(function(response){
409 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500410 var content = JSON.parse(json);
411 if(callback){
Gunnar Millscacfa6d2018-02-07 12:09:02 -0600412 return callback(content.status);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500413 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700414 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500415 if(callback){
416 callback(error);
417 }else{
418 console.log(error);
419 }
420 });
421 },
422 hostShutdown: function(callback){
423 $http({
424 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500425 url: DataService.getHost() + "/xyz/openbmc_project/state/host0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500426 headers: {
427 'Accept': 'application/json',
428 'Content-Type': 'application/json'
429 },
430 withCredentials: true,
431 data: JSON.stringify({"data": []})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700432 }).then(function(response){
433 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500434 var content = JSON.parse(json);
435 if(callback){
436 return callback(content);
437 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700438 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500439 if(callback){
440 callback(error);
441 }else{
442 console.log(error);
443 }
444 });
Iftekharul Islamcd789502017-04-19 14:37:55 -0500445 },
Michael Davisdf3bd122017-08-10 11:03:42 -0500446 getLogs: function(){
447 var deferred = $q.defer();
Iftekharul Islamcd789502017-04-19 14:37:55 -0500448 $http({
449 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500450 url: DataService.getHost() + "/xyz/openbmc_project/logging/enumerate",
Iftekharul Islamcd789502017-04-19 14:37:55 -0500451 headers: {
452 'Accept': 'application/json',
453 'Content-Type': 'application/json'
454 },
455 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700456 }).then(function(response){
457 var json = JSON.stringify(response.data);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500458 var content = JSON.parse(json);
459 var dataClone = JSON.parse(JSON.stringify(content.data));
460 var data = [];
461 var severityCode = '';
462 var priority = '';
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500463 var health = '';
Iftekharul Islamcd789502017-04-19 14:37:55 -0500464 var relatedItems = [];
465
466 for(var key in content.data){
467 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Id')){
468 var severityFlags = {low: false, medium: false, high: false};
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500469 var healthFlags = {critical: false, warning: false, good: false};
Iftekharul Islamcd789502017-04-19 14:37:55 -0500470 severityCode = content.data[key].Severity.split(".").pop();
471 priority = Constants.SEVERITY_TO_PRIORITY_MAP[severityCode];
472 severityFlags[priority.toLowerCase()] = true;
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500473 health = Constants.SEVERITY_TO_HEALTH_MAP[severityCode];
474 healthFlags[health.toLowerCase()] = true;
Iftekharul Islamcd789502017-04-19 14:37:55 -0500475 relatedItems = [];
476 content.data[key].associations.forEach(function(item){
Iftekharul Islam532763f2017-04-25 09:44:40 -0500477 relatedItems.push(item[2]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500478 });
479
480 data.push(Object.assign({
481 path: key,
482 copied: false,
483 priority: priority,
484 severity_code: severityCode,
485 severity_flags: severityFlags,
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500486 health_flags: healthFlags,
Iftekharul Islamcd789502017-04-19 14:37:55 -0500487 additional_data: content.data[key].AdditionalData.join("\n"),
Gunnar Mills43a2ed12018-02-08 15:42:30 -0600488 type: content.data[key].Message,
Iftekharul Islamcd789502017-04-19 14:37:55 -0500489 selected: false,
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500490 search_text: ("#" + content.data[key].Id + " " + severityCode + " " + content.data[key].Severity + " " + content.data[key].AdditionalData.join(" ")).toLowerCase(),
Iftekharul Islamcd789502017-04-19 14:37:55 -0500491 meta: false,
492 confirm: false,
493 related_items: relatedItems,
494 data: {key: key, value: content.data[key]}
495 }, content.data[key]));
496 }
497 }
Michael Davisdf3bd122017-08-10 11:03:42 -0500498 deferred.resolve({data: data, original: dataClone});
Ed Tanousbbcf6702017-10-06 13:53:06 -0700499 }, function(error){
Iftekharul Islamcd789502017-04-19 14:37:55 -0500500 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500501 deferred.reject(error);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500502 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500503
504 return deferred.promise;
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500505 },
506 getAllSensorStatus: function(callback){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500507 $http({
508 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500509 url: DataService.getHost() + "/xyz/openbmc_project/sensors/enumerate",
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500510 headers: {
511 'Accept': 'application/json',
512 'Content-Type': 'application/json'
513 },
514 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700515 }).then(function(response){
516 var json = JSON.stringify(response.data);
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500517 var content = JSON.parse(json);
518 var dataClone = JSON.parse(JSON.stringify(content.data));
519 var sensorData = [];
Iftekharul Islam8947e702017-07-27 10:28:07 -0500520 var severity = {};
521 var title = "";
522 var tempKeyParts = [];
523 var order = 0;
Iftekharul Islam8a122842017-09-11 10:58:16 -0500524 var customOrder = 0;
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500525
Iftekharul Islam96bbf312017-08-22 13:44:38 -0500526 function getScaledValue(value, scale){
527 scale = scale + "";
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500528 scale = parseInt(scale, 10);
529 var power = Math.abs(parseInt(scale,10));
Iftekharul Islam96bbf312017-08-22 13:44:38 -0500530
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500531 if(scale > 0){
Iftekharul Islam96bbf312017-08-22 13:44:38 -0500532 value = value * Math.pow(10, power);
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500533 }else if(scale < 0){
Iftekharul Islam96bbf312017-08-22 13:44:38 -0500534 value = value / Math.pow(10, power);
535 }
536 return value;
537 }
538
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500539 function getSensorStatus(reading){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500540 var severityFlags = {critical: false, warning: false, normal: false}, severityText = '', order = 0;
541
Gunnar Millseedefd32018-02-28 17:02:34 -0600542 if(reading.hasOwnProperty('CriticalLow') &&
Iftekharul Islam8947e702017-07-27 10:28:07 -0500543 reading.Value < reading.CriticalLow
544 ){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500545 severityFlags.critical = true;
546 severityText = 'critical';
Iftekharul Islam8947e702017-07-27 10:28:07 -0500547 order = 2;
Gunnar Millseedefd32018-02-28 17:02:34 -0600548 }else if(reading.hasOwnProperty('CriticalHigh') &&
549 reading.Value > reading.CriticalHigh
Iftekharul Islam8947e702017-07-27 10:28:07 -0500550 ){
551 severityFlags.critical = true;
552 severityText = 'critical';
553 order = 2;
Gunnar Millseedefd32018-02-28 17:02:34 -0600554 }else if(reading.hasOwnProperty('CriticalLow') &&
555 reading.hasOwnProperty('WarningLow') &&
Iftekharul Islam8947e702017-07-27 10:28:07 -0500556 reading.Value >= reading.CriticalLow && reading.Value <= reading.WarningLow){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500557 severityFlags.warning = true;
558 severityText = 'warning';
Iftekharul Islam8947e702017-07-27 10:28:07 -0500559 order = 1;
Gunnar Millseedefd32018-02-28 17:02:34 -0600560 }else if(reading.hasOwnProperty('WarningHigh') &&
561 reading.hasOwnProperty('CriticalHigh') &&
Iftekharul Islam8947e702017-07-27 10:28:07 -0500562 reading.Value >= reading.WarningHigh && reading.Value <= reading.CriticalHigh){
563 severityFlags.warning = true;
564 severityText = 'warning';
565 order = 1;
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500566 }else{
567 severityFlags.normal = true;
568 severityText = 'normal';
569 }
Iftekharul Islam8947e702017-07-27 10:28:07 -0500570 return { flags: severityFlags, severityText: severityText, order: order};
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500571 }
572
573 for(var key in content.data){
574 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Unit')){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500575
576 severity = getSensorStatus(content.data[key]);
577
578 if(!content.data[key].hasOwnProperty('CriticalLow')){
579 content.data[key].CriticalLow = "--";
580 content.data[key].CriticalHigh = "--";
581 }
582
583 if(!content.data[key].hasOwnProperty('WarningLow')){
584 content.data[key].WarningLow = "--";
585 content.data[key].WarningHigh = "--";
586 }
587
588 tempKeyParts = key.split("/");
589 title = tempKeyParts.pop();
590 title = tempKeyParts.pop() + '_' + title;
591 title = title.split("_").map(function(item){
592 return item.toLowerCase().charAt(0).toUpperCase() + item.slice(1);
593 }).reduce(function(prev, el){
594 return prev + " " + el;
595 });
596
Iftekharul Islam96bbf312017-08-22 13:44:38 -0500597 content.data[key].Value = getScaledValue(content.data[key].Value, content.data[key].Scale);
Gunnar Mills022f5642018-02-07 14:30:17 -0600598 content.data[key].CriticalLow = getScaledValue(content.data[key].CriticalLow, content.data[key].Scale);
599 content.data[key].CriticalHigh = getScaledValue(content.data[key].CriticalHigh, content.data[key].Scale);
600 content.data[key].WarningLow = getScaledValue(content.data[key].WarningLow, content.data[key].Scale);
601 content.data[key].WarningHigh = getScaledValue(content.data[key].WarningHigh, content.data[key].Scale);
Iftekharul Islam8a122842017-09-11 10:58:16 -0500602 if(Constants.SENSOR_SORT_ORDER.indexOf(content.data[key].Unit) > -1){
603 customOrder = Constants.SENSOR_SORT_ORDER.indexOf(content.data[key].Unit);
604 }else{
605 customOrder = Constants.SENSOR_SORT_ORDER_DEFAULT;
606 }
607
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500608 sensorData.push(Object.assign({
609 path: key,
610 selected: false,
611 confirm: false,
612 copied: false,
Iftekharul Islam8947e702017-07-27 10:28:07 -0500613 title: title,
614 unit: Constants.SENSOR_UNIT_MAP[content.data[key].Unit],
615 severity_flags: severity.flags,
616 status: severity.severityText,
617 order: severity.order,
Iftekharul Islam8a122842017-09-11 10:58:16 -0500618 custom_order: customOrder,
Gunnar Millseedefd32018-02-28 17:02:34 -0600619 search_text: (title + " " + content.data[key].Value + " " +
620 Constants.SENSOR_UNIT_MAP[content.data[key].Unit] + " " +
621 severity.severityText + " " +
Iftekharul Islam8947e702017-07-27 10:28:07 -0500622 content.data[key].CriticalLow + " " +
623 content.data[key].CriticalHigh + " " +
624 content.data[key].WarningLow + " " +
625 content.data[key].WarningHigh + " "
626 ).toLowerCase(),
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500627 original_data: {key: key, value: content.data[key]}
628 }, content.data[key]));
629 }
630 }
631
Iftekharul Islam8947e702017-07-27 10:28:07 -0500632 callback(sensorData, dataClone);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700633 }, function(error){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500634 console.log(error);
635 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500636 },
Gunnar Mills033025f2018-03-06 14:49:40 -0600637 getActivation: function(imageId){
638 return $http({
639 method: 'GET',
640 url: DataService.getHost() + "/xyz/openbmc_project/software/" + imageId + "/attr/Activation",
641 headers: {
642 'Accept': 'application/json',
643 'Content-Type': 'application/json'
644 },
645 withCredentials: true
646 }).then(function(response){
647 return response.data;
648 });
649 },
Michael Davisdf3bd122017-08-10 11:03:42 -0500650 getFirmwares: function(){
651 var deferred = $q.defer();
Iftekharul Islamc0161392017-06-14 15:46:15 -0500652 $http({
653 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500654 url: DataService.getHost() + "/xyz/openbmc_project/software/enumerate",
Iftekharul Islamc0161392017-06-14 15:46:15 -0500655 headers: {
656 'Accept': 'application/json',
657 'Content-Type': 'application/json'
658 },
659 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700660 }).then(function(response){
661 var json = JSON.stringify(response.data);
Iftekharul Islamc0161392017-06-14 15:46:15 -0500662 var content = JSON.parse(json);
663 var data = [];
Gunnar Mills902c38c2018-02-23 10:12:39 -0600664 var activationStatus = "";
Iftekharul Islamc0161392017-06-14 15:46:15 -0500665 var isExtended = false;
666 var bmcActiveVersion = "";
667 var hostActiveVersion = "";
668 var imageType = "";
669 var extendedVersions = [];
Gunnar Mills9b733bd2018-02-23 21:16:20 -0600670 var functionalImages = [];
Iftekharul Islamc0161392017-06-14 15:46:15 -0500671
672 function getFormatedExtendedVersions(extendedVersion){
673 var versions = [];
674 extendedVersion = extendedVersion.split(",");
675
676 extendedVersion.forEach(function(item){
677 var parts = item.split("-");
678 var numberIndex = 0;
679 for(var i = 0; i < parts.length; i++){
680 if(/[0-9]/.test(parts[i])){
681 numberIndex = i;
682 break;
683 }
684 }
685 var titlePart = parts.splice(0, numberIndex);
686 titlePart = titlePart.join("");
687 titlePart = titlePart[0].toUpperCase() + titlePart.substr(1, titlePart.length);
688 var versionPart = parts.join("-");
689 versions.push({
690 title: titlePart,
691 version: versionPart
692 });
693 });
694
695 return versions;
696 }
697
Gunnar Mills9b733bd2018-02-23 21:16:20 -0600698 // Get the list of functional images so we can compare
699 // later if an image is functional
700 if (content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH])
701 {
702 functionalImages = content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH].endpoints;
703 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500704 for(var key in content.data){
705 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Version')){
Gunnar Mills902c38c2018-02-23 10:12:39 -0600706 // If the image is "Functional" use that for the
707 // activation status, else use the value of "Activation"
708 // github.com/openbmc/phosphor-dbus-interfaces/blob/master/xyz/openbmc_project/Software/Activation.interface.yaml
709 activationStatus = content.data[key].Activation.split(".").pop();
Gunnar Mills9b733bd2018-02-23 21:16:20 -0600710 if (functionalImages.includes(key))
Gunnar Mills902c38c2018-02-23 10:12:39 -0600711 {
712 activationStatus = "Functional";
713 }
Gunnar Mills9b733bd2018-02-23 21:16:20 -0600714
Iftekharul Islamc0161392017-06-14 15:46:15 -0500715 imageType = content.data[key].Purpose.split(".").pop();
716 isExtended = content.data[key].hasOwnProperty('ExtendedVersion') && content.data[key].ExtendedVersion != "";
717 if(isExtended){
718 extendedVersions = getFormatedExtendedVersions(content.data[key].ExtendedVersion);
719 }
720 data.push(Object.assign({
721 path: key,
Gunnar Mills902c38c2018-02-23 10:12:39 -0600722 activationStatus: activationStatus,
Iftekharul Islamc0161392017-06-14 15:46:15 -0500723 imageId: key.split("/").pop(),
724 imageType: imageType,
725 isExtended: isExtended,
726 extended: {
727 show: false,
728 versions: extendedVersions
729 },
730 data: {key: key, value: content.data[key]}
731 }, content.data[key]));
732
Gunnar Mills902c38c2018-02-23 10:12:39 -0600733 if(activationStatus == 'Functional' && imageType == 'BMC'){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500734 bmcActiveVersion = content.data[key].Version;
735 }
736
Gunnar Mills902c38c2018-02-23 10:12:39 -0600737 if(activationStatus == 'Functional' && imageType == 'Host'){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500738 hostActiveVersion = content.data[key].Version;
739 }
740 }
741 }
Michael Davisdf3bd122017-08-10 11:03:42 -0500742
743 deferred.resolve({
Gunnar Millseedefd32018-02-28 17:02:34 -0600744 data: data,
745 bmcActiveVersion: bmcActiveVersion,
Michael Davisdf3bd122017-08-10 11:03:42 -0500746 hostActiveVersion: hostActiveVersion
747 });
Ed Tanousbbcf6702017-10-06 13:53:06 -0700748 }, function(error){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500749 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500750 deferred.reject(error);
Iftekharul Islamc0161392017-06-14 15:46:15 -0500751 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500752
753 return deferred.promise;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500754 },
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500755 changePriority: function(imageId, priority){
756 var deferred = $q.defer();
757 $http({
758 method: 'PUT',
759 url: DataService.getHost() + "/xyz/openbmc_project/software/" + imageId + "/attr/Priority",
760 headers: {
Iftekharul Islam2a489552017-11-02 13:23:08 -0500761 'Accept': 'application/json',
762 'Content-Type': 'application/json'
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500763 },
764 withCredentials: true,
765 data: JSON.stringify({"data": priority})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700766 }).then(function(response){
767 var json = JSON.stringify(response.data);
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500768 var content = JSON.parse(json);
769 deferred.resolve(content);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700770 }, function(error){
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500771 console.log(error);
772 deferred.reject(error);
773 });
774
775 return deferred.promise;
776 },
Iftekharul Islam2a489552017-11-02 13:23:08 -0500777 deleteImage: function(imageId){
778 var deferred = $q.defer();
779 $http({
780 method: 'POST',
781 url: DataService.getHost() + "/xyz/openbmc_project/software/" + imageId + "/action/Delete",
782 headers: {
783 'Accept': 'application/json',
784 'Content-Type': 'application/json'
785 },
786 withCredentials: true,
787 data: JSON.stringify({"data": []})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700788 }).then(function(response){
789 var json = JSON.stringify(response.data);
Iftekharul Islam2a489552017-11-02 13:23:08 -0500790 var content = JSON.parse(json);
791 deferred.resolve(content);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700792 }, function(error){
Iftekharul Islam2a489552017-11-02 13:23:08 -0500793 console.log(error);
794 deferred.reject(error);
795 });
796
797 return deferred.promise;
798 },
799 activateImage: function(imageId){
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500800 var deferred = $q.defer();
Iftekharul Islamc0161392017-06-14 15:46:15 -0500801 $http({
802 method: 'PUT',
Iftekharul Islam2a489552017-11-02 13:23:08 -0500803 url: DataService.getHost() + "/xyz/openbmc_project/software/" + imageId + "/attr/RequestedActivation",
804 headers: {
805 'Accept': 'application/json',
806 'Content-Type': 'application/json'
807 },
808 withCredentials: true,
809 data: JSON.stringify({"data": Constants.FIRMWARE.ACTIVATE_FIRMWARE})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700810 }).then(function(response){
811 var json = JSON.stringify(response.data);
Iftekharul Islam2a489552017-11-02 13:23:08 -0500812 var content = JSON.parse(json);
813 deferred.resolve(content);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700814 }, function(error){
Iftekharul Islam2a489552017-11-02 13:23:08 -0500815 console.log(error);
816 deferred.reject(error);
817 });
818
819 return deferred.promise;
820 },
821 uploadImage: function(file){
822 var deferred = $q.defer();
823 $http({
824 method: 'POST',
Iftekharul Islamc0161392017-06-14 15:46:15 -0500825 timeout: 5 * 60 * 1000,
Gunnar Millsdddb1492018-02-19 16:27:27 -0600826 url: DataService.getHost() + "/upload/image",
Iftekharul Islamc0161392017-06-14 15:46:15 -0500827 headers: {
Iftekharul Islam2a489552017-11-02 13:23:08 -0500828 'Content-Type': 'application/octet-stream'
Iftekharul Islamc0161392017-06-14 15:46:15 -0500829 },
830 withCredentials: true,
831 data: file
Ed Tanousbbcf6702017-10-06 13:53:06 -0700832 }).then(function(response){
833 var json = JSON.stringify(response.data);
Iftekharul Islamc0161392017-06-14 15:46:15 -0500834 var content = JSON.parse(json);
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500835 deferred.resolve(content);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700836 }, function(error){
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500837 console.log(error);
838 deferred.reject(error);
Iftekharul Islamc0161392017-06-14 15:46:15 -0500839 });
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500840
841 return deferred.promise;
842 },
843 downloadImage: function(host, filename){
Gunnar Mills36379b92018-04-02 16:53:53 -0500844 return $http({
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500845 method: 'POST',
Gunnar Millse345cac2018-02-20 11:16:53 -0600846 url: DataService.getHost() + "/xyz/openbmc_project/software/action/DownloadViaTFTP",
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500847 headers: {
Gunnar Mills36379b92018-04-02 16:53:53 -0500848 'Accept': 'application/json',
849 'Content-Type': 'application/json'
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500850 },
851 withCredentials: true,
Gunnar Millse345cac2018-02-20 11:16:53 -0600852 data: JSON.stringify({"data": [filename, host]}),
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500853 responseType: 'arraybuffer'
Gunnar Mills36379b92018-04-02 16:53:53 -0500854 }).then(function(response){
855 return response.data;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500856 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500857 },
Michael Davisdf3bd122017-08-10 11:03:42 -0500858 getBMCEthernetInfo: function(){
859 var deferred = $q.defer();
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500860 $http({
861 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500862 url: DataService.getHost() + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc/ethernet",
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500863 headers: {
864 'Accept': 'application/json',
865 'Content-Type': 'application/json'
866 },
867 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700868 }).then(function(response){
869 var json = JSON.stringify(response.data);
Michael Davisdf3bd122017-08-10 11:03:42 -0500870 var content = JSON.parse(json);
871 deferred.resolve(content.data);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700872 }, function(error){
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500873 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500874 deferred.reject(error);
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500875 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500876
877 return deferred.promise;
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500878 },
879 getBMCInfo: function(callback){
Michael Davisdf3bd122017-08-10 11:03:42 -0500880 var deferred = $q.defer();
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500881 $http({
882 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500883 url: DataService.getHost() + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc",
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500884 headers: {
885 'Accept': 'application/json',
886 'Content-Type': 'application/json'
887 },
888 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700889 }).then(function(response){
890 var json = JSON.stringify(response.data);
Michael Davisdf3bd122017-08-10 11:03:42 -0500891 var content = JSON.parse(json);
892 deferred.resolve(content.data);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700893 }, function(error){
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500894 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500895 deferred.reject(error);
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500896 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500897 return deferred.promise;
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500898 },
Iftekharul Islamee27d752017-07-05 15:54:31 -0500899 getHardwares: function(callback){
900 $http({
901 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500902 url: DataService.getHost() + "/xyz/openbmc_project/inventory/enumerate",
Iftekharul Islamee27d752017-07-05 15:54:31 -0500903 headers: {
904 'Accept': 'application/json',
905 'Content-Type': 'application/json'
906 },
907 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700908 }).then(function(response){
909 var json = JSON.stringify(response.data);
Iftekharul Islamee27d752017-07-05 15:54:31 -0500910 var content = JSON.parse(json);
911 var hardwareData = [];
912 var keyIndexMap = {};
913 var title = "";
914 var data = [];
915 var searchText = "";
916 var componentIndex = -1;
917 var tempParts = [];
918
919
920 function isSubComponent(key){
921
922 for(var i = 0; i < Constants.HARDWARE.parent_components.length; i++){
923 if(key.split(Constants.HARDWARE.parent_components[i]).length == 2) return true;
924 }
925
926 return false;
927 }
928
929 function titlelize(title){
930 title = title.replace(/([A-Z0-9]+)/g, " $1").replace(/^\s+/, "");
931 for(var i = 0; i < Constants.HARDWARE.uppercase_titles.length; i++){
932 if(title.toLowerCase().indexOf((Constants.HARDWARE.uppercase_titles[i] + " ")) > -1){
933 return title.toUpperCase();
934 }
935 }
936
937 return title;
938 }
939
940 function camelcaseToLabel(obj){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500941 var transformed = [], label = "", value = "";
Iftekharul Islamee27d752017-07-05 15:54:31 -0500942 for(var key in obj){
943 label = key.replace(/([A-Z0-9]+)/g, " $1").replace(/^\s+/, "");
944 if(obj[key] !== ""){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500945 value = obj[key];
946 if(value == 1 || value == 0){
947 value = (value == 1) ? 'Yes' : 'No';
948 }
949 transformed.push({key:label, value: value});
Iftekharul Islamee27d752017-07-05 15:54:31 -0500950 }
951 }
952
953 return transformed;
954 }
955
956 function getSearchText(data){
957 var searchText = "";
958 for(var i = 0; i < data.length; i++){
959 searchText += " " + data[i].key + " " + data[i].value;
960 }
961
Gunnar Millseedefd32018-02-28 17:02:34 -0600962 return searchText;
Iftekharul Islamee27d752017-07-05 15:54:31 -0500963 }
964
965 for(var key in content.data){
Gunnar Millseedefd32018-02-28 17:02:34 -0600966 if(content.data.hasOwnProperty(key) &&
Iftekharul Islamee27d752017-07-05 15:54:31 -0500967 key.indexOf(Constants.HARDWARE.component_key_filter) == 0){
968
969 data = camelcaseToLabel(content.data[key]);
970 searchText = getSearchText(data);
971 title = key.split("/").pop();
972
973 title = titlelize(title);
974
975 if(!isSubComponent(key)){
976 hardwareData.push(Object.assign({
977 path: key,
978 title: title,
979 selected: false,
980 expanded: false,
981 search_text: title.toLowerCase() + " " + searchText.toLowerCase(),
Gunnar Millseedefd32018-02-28 17:02:34 -0600982 sub_components: [],
Iftekharul Islamee27d752017-07-05 15:54:31 -0500983 original_data: {key: key, value: content.data[key]}
984 }, {items: data}));
985
986 keyIndexMap[key] = hardwareData.length - 1;
987 }else{
988 var tempParts = key.split("/");
989 tempParts.pop();
990 tempParts = tempParts.join("/");
991 componentIndex = keyIndexMap[tempParts];
992 data = content.data[key];
993 data.title = title;
994 hardwareData[componentIndex].sub_components.push(data);
995 hardwareData[componentIndex].search_text += " " + title.toLowerCase();
Gunnar Millscdd9d6b2018-03-09 11:57:06 -0600996
997 // Sort the subcomponents alphanumeric so they are displayed on the
998 // inventory page in order (e.g. core 0, core 1, core 2, ... core 12, core 13)
999 hardwareData[componentIndex].sub_components.sort(function (a, b) {
1000 return a.title.localeCompare(b.title, 'en', { numeric: true });
1001 });
Iftekharul Islamee27d752017-07-05 15:54:31 -05001002 }
1003 }
1004 }
1005
1006 if(callback){
1007 callback(hardwareData, content.data);
1008 }else{
1009 return { data: hardwareData, original_data: content.data};
1010 }
1011 });
1012 },
Iftekharul Islamf2d74642017-07-10 16:42:14 -05001013 deleteLogs: function(logs) {
1014 var defer = $q.defer();
1015 var promises = [];
1016
1017 function finished(){
1018 defer.resolve();
1019 }
1020
1021 logs.forEach(function(item){
1022 promises.push($http({
1023 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -05001024 url: DataService.getHost() + "/xyz/openbmc_project/logging/entry/"+item.Id+"/action/Delete",
Iftekharul Islamf2d74642017-07-10 16:42:14 -05001025 headers: {
1026 'Accept': 'application/json',
1027 'Content-Type': 'application/json'
1028 },
1029 withCredentials: true,
1030 data: JSON.stringify({"data": []})
1031 }));
1032 });
1033
1034 $q.all(promises).then(finished);
1035
1036 return defer.promise;
1037 },
1038 resolveLogs: function(logs) {
1039 var defer = $q.defer();
1040 var promises = [];
1041
1042 function finished(){
1043 defer.resolve();
1044 }
1045
1046 logs.forEach(function(item){
1047 promises.push($http({
1048 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -05001049 url: DataService.getHost() + "/xyz/openbmc_project/logging/entry/"+item.Id+"/attr/Resolved",
Iftekharul Islamf2d74642017-07-10 16:42:14 -05001050 headers: {
1051 'Accept': 'application/json',
1052 'Content-Type': 'application/json'
1053 },
1054 withCredentials: true,
1055 data: JSON.stringify({"data": "1"})
1056 }));
1057 });
1058
1059 $q.all(promises).then(finished);
1060
1061 return defer.promise;
1062 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -05001063 };
1064 return SERVICE;
1065 }]);
1066
1067 })(window.angular);