blob: e74b1966f60f4761a314bab95a61bff1f2b85428 [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){
CamVan Nguyen33275832018-03-13 18:38:37 -050015 var getScaledValue = function(value, scale){
16 scale = scale + "";
17 scale = parseInt(scale, 10);
18 var power = Math.abs(parseInt(scale,10));
19
20 if(scale > 0){
21 value = value * Math.pow(10, power);
22 }else if(scale < 0){
23 value = value / Math.pow(10, power);
24 }
25 return value;
26 };
Iftekharul Islam99d199f2017-03-24 15:28:25 -050027 var SERVICE = {
Iftekharul Islam99d199f2017-03-24 15:28:25 -050028 API_CREDENTIALS: Constants.API_CREDENTIALS,
29 API_RESPONSE: Constants.API_RESPONSE,
30 CHASSIS_POWER_STATE: Constants.CHASSIS_POWER_STATE,
31 HOST_STATE_TEXT: Constants.HOST_STATE,
32 HOST_STATE: Constants.HOST_STATE,
Iftekharul Islamcd789502017-04-19 14:37:55 -050033 LED_STATE: Constants.LED_STATE,
34 LED_STATE_TEXT: Constants.LED_STATE_TEXT,
Iftekharul Islam1acb4122017-11-02 13:20:32 -050035 HOST_SESSION_STORAGE_KEY: Constants.API_CREDENTIALS.host_storage_key,
Iftekharul Islam99d199f2017-03-24 15:28:25 -050036 getChassisState: function(callback){
37 $http({
38 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -050039 url: DataService.getHost() + "/xyz/openbmc_project/state/chassis0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -050040 headers: {
41 'Accept': 'application/json',
42 'Content-Type': 'application/json'
43 },
44 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -070045 }).then(function(response){
46 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -050047 var content = JSON.parse(json);
48 callback(content.data.CurrentPowerState);
Ed Tanousbbcf6702017-10-06 13:53:06 -070049 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050050 console.log(error);
51 });
52 },
53 getHostState: function(callback){
54 $http({
55 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -050056 url: DataService.getHost() + "/xyz/openbmc_project/state/host0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -050057 headers: {
58 'Accept': 'application/json',
59 'Content-Type': 'application/json'
60 },
61 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -070062 }).then(function(response){
63 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -050064 var content = JSON.parse(json);
65 callback(content.data.CurrentHostState);
Ed Tanousbbcf6702017-10-06 13:53:06 -070066 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050067 console.log(error);
68 });
69 },
Iftekharul Islam171c6a12017-08-11 08:35:47 -050070 getNetworkInfo: function(){
71 var deferred = $q.defer();
72 $http({
73 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -050074 url: DataService.getHost() + "/xyz/openbmc_project/network/enumerate",
Iftekharul Islam171c6a12017-08-11 08:35:47 -050075 headers: {
76 'Accept': 'application/json',
77 'Content-Type': 'application/json'
78 },
79 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -070080 }).then(function(response){
81 var json = JSON.stringify(response.data);
Iftekharul Islam171c6a12017-08-11 08:35:47 -050082 var content = JSON.parse(json);
83 var hostname = "";
84 var macAddress = "";
85
Iftekharul Islam2a489552017-11-02 13:23:08 -050086 function parseNetworkData(content){
87 var data = {
88 interface_ids: [],
89 interfaces: {
90 }
91 };
92 var interfaceId = '', keyParts = [], interfaceHash = '', interfaceType = '';
93 for(var key in content.data){
94 if(key.match(/network\/eth\d+$/ig)){
95 interfaceId = key.split("/").pop();
96 if(data.interface_ids.indexOf(interfaceId) == -1){
97 data.interface_ids.push(interfaceId);
98 data.interfaces[interfaceId] = {
99 interfaceIname: '',
100 domainName:'',
101 MACAddress:'',
102 Nameservers: [],
103 DHCPEnabled: 0,
104 ipv4:
105 {
106 ids: [],
107 values: []
108 },
109 ipv6:
110 {
111 ids: [],
112 values: []
113 }
114 };
115 data.interfaces[interfaceId].MACAddress = content.data[key].MACAddress;
116 data.interfaces[interfaceId].DomainName = content.data[key].DomainName.join(" ");
117 data.interfaces[interfaceId].Nameservers = content.data[key].Nameservers;
118 data.interfaces[interfaceId].DHCPEnabled = content.data[key].DHCPEnabled;
119 }
120 }else if(key.match(/network\/eth\d+\/ipv[4|6]\/[a-z0-9]+$/ig)){
121 keyParts = key.split("/");
122 interfaceHash = keyParts.pop();
123 interfaceType = keyParts.pop();
124 interfaceId = keyParts.pop();
125
126 if(data.interfaces[interfaceId][interfaceType].ids.indexOf(interfaceHash) == -1){
127 data.interfaces[interfaceId][interfaceType].ids.push(interfaceHash);
128 data.interfaces[interfaceId][interfaceType].values.push(content.data[key]);
129 }
130 }
131 }
132 return data;
133 }
134
Iftekharul Islam171c6a12017-08-11 08:35:47 -0500135 if(content.data.hasOwnProperty('/xyz/openbmc_project/network/config') &&
136 content.data['/xyz/openbmc_project/network/config'].hasOwnProperty('HostName')
137 ){
138 hostname = content.data['/xyz/openbmc_project/network/config'].HostName;
139 }
140
141 if(content.data.hasOwnProperty('/xyz/openbmc_project/network/eth0') &&
142 content.data['/xyz/openbmc_project/network/eth0'].hasOwnProperty('MACAddress')
143 ){
144 macAddress = content.data['/xyz/openbmc_project/network/eth0'].MACAddress;
145 }
146
147 deferred.resolve({
148 data: content.data,
149 hostname: hostname,
150 mac_address: macAddress,
Iftekharul Islam2a489552017-11-02 13:23:08 -0500151 formatted_data: parseNetworkData(content)
Iftekharul Islam171c6a12017-08-11 08:35:47 -0500152 });
Ed Tanousbbcf6702017-10-06 13:53:06 -0700153 }, function(error){
Iftekharul Islam171c6a12017-08-11 08:35:47 -0500154 console.log(error);
155 deferred.reject(error);
156 });
157 return deferred.promise;
158 },
Michael Davisdf3bd122017-08-10 11:03:42 -0500159 getLEDState: function(){
160 var deferred = $q.defer();
Iftekharul Islamcd789502017-04-19 14:37:55 -0500161 $http({
162 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500163 url: DataService.getHost() + "/xyz/openbmc_project/led/groups/enclosure_identify",
Iftekharul Islamcd789502017-04-19 14:37:55 -0500164 headers: {
165 'Accept': 'application/json',
166 'Content-Type': 'application/json'
167 },
168 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700169 }).then(function(response){
170 var json = JSON.stringify(response.data);
Michael Davisdf3bd122017-08-10 11:03:42 -0500171 var content = JSON.parse(json);
172 deferred.resolve(content.data.Asserted);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700173 }, function(error){
Iftekharul Islamcd789502017-04-19 14:37:55 -0500174 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500175 deferred.reject(error);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500176 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500177 return deferred.promise;
Iftekharul Islamcd789502017-04-19 14:37:55 -0500178 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500179 login: function(username, password, callback){
180 $http({
181 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500182 url: DataService.getHost() + "/login",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500183 headers: {
184 'Accept': 'application/json',
185 'Content-Type': 'application/json'
186 },
187 withCredentials: true,
188 data: JSON.stringify({"data": [username, password]})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700189 }).then(function(response){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500190 if(callback){
Ed Tanousbbcf6702017-10-06 13:53:06 -0700191 callback(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500192 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700193 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500194 if(callback){
Iftekharul Islamcd789502017-04-19 14:37:55 -0500195 if(error && error.status && error.status == 'error'){
196 callback(error);
197 }else{
198 callback(error, true);
199 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500200 }
201 console.log(error);
202 });
203 },
Gunnar Mills32581cf2018-03-16 15:52:54 -0500204 testPassword: function(username, password){
205 // Calls /login without the current session to verify the given password is correct
206 // ignore the interceptor logout on a bad password
207 DataService.ignoreHttpError = true;
Gunnar Mills7c3e8c12018-03-28 16:16:58 -0500208 return $http({
Gunnar Mills32581cf2018-03-16 15:52:54 -0500209 method: 'POST',
210 url: DataService.getHost() + "/login",
211 headers: {
212 'Accept': 'application/json',
213 'Content-Type': 'application/json'
214 },
215 withCredentials: false,
216 data: JSON.stringify({"data": [username, password]})
217 }).then(function(response){
Gunnar Mills7c3e8c12018-03-28 16:16:58 -0500218 return response.data;
Gunnar Mills32581cf2018-03-16 15:52:54 -0500219 });
Gunnar Mills32581cf2018-03-16 15:52:54 -0500220 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500221 logout: function(callback){
222 $http({
223 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500224 url: DataService.getHost() + "/logout",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500225 headers: {
226 'Accept': 'application/json',
227 'Content-Type': 'application/json'
228 },
229 withCredentials: true,
230 data: JSON.stringify({"data": []})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700231 }).then(function(response){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500232 if(callback){
Iftekharul Islamde883402018-02-05 11:16:21 -0600233 callback(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500234 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700235 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500236 if(callback){
237 callback(null, error);
238 }
239 console.log(error);
240 });
241 },
Gunnar Mills55a8e392018-03-14 15:37:44 -0500242 changePassword: function(user, newPassword){
243 var deferred = $q.defer();
244 $http({
245 method: 'POST',
246 url: DataService.getHost() + "/xyz/openbmc_project/user/" + user + "/action/SetPassword",
247 headers: {
248 'Accept': 'application/json',
249 'Content-Type': 'application/json'
250 },
251 withCredentials: true,
252 data: JSON.stringify({"data": [newPassword]}),
253 responseType: 'arraybuffer'
254 }).then(function(response, status, headers){
255 deferred.resolve({
256 data: response,
257 status: status,
258 headers: headers
259 });
260 }, function(error){
261 console.log(error);
262 deferred.reject(error);
263 });
264 return deferred.promise;
265 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500266 chassisPowerOn: function(callback){
267 $http({
268 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500269 url: DataService.getHost() + "/xyz/openbmc_project/state/host0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500270 headers: {
271 'Accept': 'application/json',
272 'Content-Type': 'application/json'
273 },
274 withCredentials: true,
275 data: JSON.stringify({"data": []})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700276 }).then(function(response){
277 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500278 var content = JSON.parse(json);
279 if(callback){
280 return callback(content.data.CurrentPowerState);
281 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700282 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500283 if(callback){
284 callback(error);
285 }else{
286 console.log(error);
287 }
288 });
289 },
290 chassisPowerOff: function(callback){
291 $http({
Gunnar Mills3aa8b532018-02-06 15:42:29 -0600292 method: 'PUT',
293 url: DataService.getHost() + "/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500294 headers: {
295 'Accept': 'application/json',
296 'Content-Type': 'application/json'
297 },
298 withCredentials: true,
Gunnar Mills3aa8b532018-02-06 15:42:29 -0600299 data: JSON.stringify({"data": "xyz.openbmc_project.State.Chassis.Transition.Off"})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700300 }).then(function(response){
301 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500302 var content = JSON.parse(json);
303 if(callback){
Gunnar Mills3aa8b532018-02-06 15:42:29 -0600304 return callback(content.status);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500305 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700306 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500307 if(callback){
308 callback(error);
309 }else{
310 console.log(error);
311 }
312 });
313 },
Iftekharul Islamcd789502017-04-19 14:37:55 -0500314 setLEDState: function(state, callback){
315 $http({
316 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500317 url: DataService.getHost() + "/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted",
Iftekharul Islamcd789502017-04-19 14:37:55 -0500318 headers: {
319 'Accept': 'application/json',
320 'Content-Type': 'application/json'
321 },
322 withCredentials: true,
323 data: JSON.stringify({"data": state})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700324 }).then(function(response){
325 var json = JSON.stringify(response.data);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500326 var content = JSON.parse(json);
327 if(callback){
328 return callback(content.status);
329 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700330 }, function(error){
Iftekharul Islamcd789502017-04-19 14:37:55 -0500331 if(callback){
332 callback(error);
333 }else{
334 console.log(error);
335 }
336 });
337 },
Iftekharul Islam55368122017-03-27 09:46:50 -0500338 bmcReboot: function(callback){
339 $http({
340 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500341 url: DataService.getHost() + "/xyz/openbmc_project/state/bmc0/attr/RequestedBmcTransition",
Iftekharul Islam55368122017-03-27 09:46:50 -0500342 headers: {
343 'Accept': 'application/json',
344 'Content-Type': 'application/json'
345 },
346 withCredentials: true,
347 data: JSON.stringify({"data": "xyz.openbmc_project.State.BMC.Transition.Reboot"})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700348 }).then(function(response){
349 var json = JSON.stringify(response.data);
Iftekharul Islam55368122017-03-27 09:46:50 -0500350 var content = JSON.parse(json);
351 if(callback){
352 return callback(content.status);
353 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700354 }, function(error){
Iftekharul Islam55368122017-03-27 09:46:50 -0500355 if(callback){
356 callback(error);
357 }else{
358 console.log(error);
359 }
360 });
361 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500362 hostPowerOn: function(callback){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500363 $http({
364 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500365 url: DataService.getHost() + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500366 headers: {
367 'Accept': 'application/json',
368 'Content-Type': 'application/json'
369 },
370 withCredentials: true,
371 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.On"})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700372 }).then(function(response){
373 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500374 var content = JSON.parse(json);
375 if(callback){
376 return callback(content.status);
377 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700378 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500379 if(callback){
380 callback(error);
381 }else{
382 console.log(error);
383 }
384 });
385 },
386 hostPowerOff: function(callback){
387 $http({
388 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500389 url: DataService.getHost() + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500390 headers: {
391 'Accept': 'application/json',
392 'Content-Type': 'application/json'
393 },
394 withCredentials: true,
395 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Off"})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700396 }).then(function(response){
397 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500398 var content = JSON.parse(json);
399 if(callback){
400 return callback(content.status);
401 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700402 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500403 if(callback){
404 callback(error);
405 }else{
406 console.log(error);
407 }
408 });
409 },
410 hostReboot: function(callback){
411 $http({
Gunnar Millscacfa6d2018-02-07 12:09:02 -0600412 method: 'PUT',
413 url: DataService.getHost() + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500414 headers: {
415 'Accept': 'application/json',
416 'Content-Type': 'application/json'
417 },
418 withCredentials: true,
Gunnar Millscacfa6d2018-02-07 12:09:02 -0600419 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Reboot"})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700420 }).then(function(response){
421 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500422 var content = JSON.parse(json);
423 if(callback){
Gunnar Millscacfa6d2018-02-07 12:09:02 -0600424 return callback(content.status);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500425 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700426 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500427 if(callback){
428 callback(error);
429 }else{
430 console.log(error);
431 }
432 });
433 },
434 hostShutdown: function(callback){
435 $http({
436 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500437 url: DataService.getHost() + "/xyz/openbmc_project/state/host0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500438 headers: {
439 'Accept': 'application/json',
440 'Content-Type': 'application/json'
441 },
442 withCredentials: true,
443 data: JSON.stringify({"data": []})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700444 }).then(function(response){
445 var json = JSON.stringify(response.data);
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500446 var content = JSON.parse(json);
447 if(callback){
448 return callback(content);
449 }
Ed Tanousbbcf6702017-10-06 13:53:06 -0700450 }, function(error){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500451 if(callback){
452 callback(error);
453 }else{
454 console.log(error);
455 }
456 });
Iftekharul Islamcd789502017-04-19 14:37:55 -0500457 },
Michael Davisdf3bd122017-08-10 11:03:42 -0500458 getLogs: function(){
459 var deferred = $q.defer();
Iftekharul Islamcd789502017-04-19 14:37:55 -0500460 $http({
461 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500462 url: DataService.getHost() + "/xyz/openbmc_project/logging/enumerate",
Iftekharul Islamcd789502017-04-19 14:37:55 -0500463 headers: {
464 'Accept': 'application/json',
465 'Content-Type': 'application/json'
466 },
467 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700468 }).then(function(response){
469 var json = JSON.stringify(response.data);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500470 var content = JSON.parse(json);
471 var dataClone = JSON.parse(JSON.stringify(content.data));
472 var data = [];
473 var severityCode = '';
474 var priority = '';
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500475 var health = '';
Iftekharul Islamcd789502017-04-19 14:37:55 -0500476 var relatedItems = [];
477
478 for(var key in content.data){
479 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Id')){
480 var severityFlags = {low: false, medium: false, high: false};
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500481 var healthFlags = {critical: false, warning: false, good: false};
Iftekharul Islamcd789502017-04-19 14:37:55 -0500482 severityCode = content.data[key].Severity.split(".").pop();
483 priority = Constants.SEVERITY_TO_PRIORITY_MAP[severityCode];
484 severityFlags[priority.toLowerCase()] = true;
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500485 health = Constants.SEVERITY_TO_HEALTH_MAP[severityCode];
486 healthFlags[health.toLowerCase()] = true;
Iftekharul Islamcd789502017-04-19 14:37:55 -0500487 relatedItems = [];
488 content.data[key].associations.forEach(function(item){
Iftekharul Islam532763f2017-04-25 09:44:40 -0500489 relatedItems.push(item[2]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500490 });
491
492 data.push(Object.assign({
493 path: key,
494 copied: false,
495 priority: priority,
496 severity_code: severityCode,
497 severity_flags: severityFlags,
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500498 health_flags: healthFlags,
Iftekharul Islamcd789502017-04-19 14:37:55 -0500499 additional_data: content.data[key].AdditionalData.join("\n"),
Gunnar Mills43a2ed12018-02-08 15:42:30 -0600500 type: content.data[key].Message,
Iftekharul Islamcd789502017-04-19 14:37:55 -0500501 selected: false,
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500502 search_text: ("#" + content.data[key].Id + " " + severityCode + " " + content.data[key].Severity + " " + content.data[key].AdditionalData.join(" ")).toLowerCase(),
Iftekharul Islamcd789502017-04-19 14:37:55 -0500503 meta: false,
504 confirm: false,
505 related_items: relatedItems,
506 data: {key: key, value: content.data[key]}
507 }, content.data[key]));
508 }
509 }
Michael Davisdf3bd122017-08-10 11:03:42 -0500510 deferred.resolve({data: data, original: dataClone});
Ed Tanousbbcf6702017-10-06 13:53:06 -0700511 }, function(error){
Iftekharul Islamcd789502017-04-19 14:37:55 -0500512 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500513 deferred.reject(error);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500514 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500515
516 return deferred.promise;
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500517 },
518 getAllSensorStatus: function(callback){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500519 $http({
520 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500521 url: DataService.getHost() + "/xyz/openbmc_project/sensors/enumerate",
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500522 headers: {
523 'Accept': 'application/json',
524 'Content-Type': 'application/json'
525 },
526 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700527 }).then(function(response){
528 var json = JSON.stringify(response.data);
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500529 var content = JSON.parse(json);
530 var dataClone = JSON.parse(JSON.stringify(content.data));
531 var sensorData = [];
Iftekharul Islam8947e702017-07-27 10:28:07 -0500532 var severity = {};
533 var title = "";
534 var tempKeyParts = [];
535 var order = 0;
Iftekharul Islam8a122842017-09-11 10:58:16 -0500536 var customOrder = 0;
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500537
538 function getSensorStatus(reading){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500539 var severityFlags = {critical: false, warning: false, normal: false}, severityText = '', order = 0;
540
Gunnar Millseedefd32018-02-28 17:02:34 -0600541 if(reading.hasOwnProperty('CriticalLow') &&
Iftekharul Islam8947e702017-07-27 10:28:07 -0500542 reading.Value < reading.CriticalLow
543 ){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500544 severityFlags.critical = true;
545 severityText = 'critical';
Iftekharul Islam8947e702017-07-27 10:28:07 -0500546 order = 2;
Gunnar Millseedefd32018-02-28 17:02:34 -0600547 }else if(reading.hasOwnProperty('CriticalHigh') &&
548 reading.Value > reading.CriticalHigh
Iftekharul Islam8947e702017-07-27 10:28:07 -0500549 ){
550 severityFlags.critical = true;
551 severityText = 'critical';
552 order = 2;
Gunnar Millseedefd32018-02-28 17:02:34 -0600553 }else if(reading.hasOwnProperty('CriticalLow') &&
554 reading.hasOwnProperty('WarningLow') &&
Iftekharul Islam8947e702017-07-27 10:28:07 -0500555 reading.Value >= reading.CriticalLow && reading.Value <= reading.WarningLow){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500556 severityFlags.warning = true;
557 severityText = 'warning';
Iftekharul Islam8947e702017-07-27 10:28:07 -0500558 order = 1;
Gunnar Millseedefd32018-02-28 17:02:34 -0600559 }else if(reading.hasOwnProperty('WarningHigh') &&
560 reading.hasOwnProperty('CriticalHigh') &&
Iftekharul Islam8947e702017-07-27 10:28:07 -0500561 reading.Value >= reading.WarningHigh && reading.Value <= reading.CriticalHigh){
562 severityFlags.warning = true;
563 severityText = 'warning';
564 order = 1;
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500565 }else{
566 severityFlags.normal = true;
567 severityText = 'normal';
568 }
Iftekharul Islam8947e702017-07-27 10:28:07 -0500569 return { flags: severityFlags, severityText: severityText, order: order};
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500570 }
571
572 for(var key in content.data){
573 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Unit')){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500574
575 severity = getSensorStatus(content.data[key]);
576
577 if(!content.data[key].hasOwnProperty('CriticalLow')){
578 content.data[key].CriticalLow = "--";
579 content.data[key].CriticalHigh = "--";
580 }
581
582 if(!content.data[key].hasOwnProperty('WarningLow')){
583 content.data[key].WarningLow = "--";
584 content.data[key].WarningHigh = "--";
585 }
586
587 tempKeyParts = key.split("/");
588 title = tempKeyParts.pop();
589 title = tempKeyParts.pop() + '_' + title;
590 title = title.split("_").map(function(item){
591 return item.toLowerCase().charAt(0).toUpperCase() + item.slice(1);
592 }).reduce(function(prev, el){
593 return prev + " " + el;
594 });
595
Iftekharul Islam96bbf312017-08-22 13:44:38 -0500596 content.data[key].Value = getScaledValue(content.data[key].Value, content.data[key].Scale);
Gunnar Mills022f5642018-02-07 14:30:17 -0600597 content.data[key].CriticalLow = getScaledValue(content.data[key].CriticalLow, content.data[key].Scale);
598 content.data[key].CriticalHigh = getScaledValue(content.data[key].CriticalHigh, content.data[key].Scale);
599 content.data[key].WarningLow = getScaledValue(content.data[key].WarningLow, content.data[key].Scale);
600 content.data[key].WarningHigh = getScaledValue(content.data[key].WarningHigh, content.data[key].Scale);
Iftekharul Islam8a122842017-09-11 10:58:16 -0500601 if(Constants.SENSOR_SORT_ORDER.indexOf(content.data[key].Unit) > -1){
602 customOrder = Constants.SENSOR_SORT_ORDER.indexOf(content.data[key].Unit);
603 }else{
604 customOrder = Constants.SENSOR_SORT_ORDER_DEFAULT;
605 }
606
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500607 sensorData.push(Object.assign({
608 path: key,
609 selected: false,
610 confirm: false,
611 copied: false,
Iftekharul Islam8947e702017-07-27 10:28:07 -0500612 title: title,
613 unit: Constants.SENSOR_UNIT_MAP[content.data[key].Unit],
614 severity_flags: severity.flags,
615 status: severity.severityText,
616 order: severity.order,
Iftekharul Islam8a122842017-09-11 10:58:16 -0500617 custom_order: customOrder,
Gunnar Millseedefd32018-02-28 17:02:34 -0600618 search_text: (title + " " + content.data[key].Value + " " +
619 Constants.SENSOR_UNIT_MAP[content.data[key].Unit] + " " +
620 severity.severityText + " " +
Iftekharul Islam8947e702017-07-27 10:28:07 -0500621 content.data[key].CriticalLow + " " +
622 content.data[key].CriticalHigh + " " +
623 content.data[key].WarningLow + " " +
624 content.data[key].WarningHigh + " "
625 ).toLowerCase(),
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500626 original_data: {key: key, value: content.data[key]}
627 }, content.data[key]));
628 }
629 }
630
Iftekharul Islam8947e702017-07-27 10:28:07 -0500631 callback(sensorData, dataClone);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700632 }, function(error){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500633 console.log(error);
634 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500635 },
Gunnar Mills033025f2018-03-06 14:49:40 -0600636 getActivation: function(imageId){
637 return $http({
638 method: 'GET',
639 url: DataService.getHost() + "/xyz/openbmc_project/software/" + imageId + "/attr/Activation",
640 headers: {
641 'Accept': 'application/json',
642 'Content-Type': 'application/json'
643 },
644 withCredentials: true
645 }).then(function(response){
646 return response.data;
647 });
648 },
Michael Davisdf3bd122017-08-10 11:03:42 -0500649 getFirmwares: function(){
650 var deferred = $q.defer();
Iftekharul Islamc0161392017-06-14 15:46:15 -0500651 $http({
652 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500653 url: DataService.getHost() + "/xyz/openbmc_project/software/enumerate",
Iftekharul Islamc0161392017-06-14 15:46:15 -0500654 headers: {
655 'Accept': 'application/json',
656 'Content-Type': 'application/json'
657 },
658 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700659 }).then(function(response){
660 var json = JSON.stringify(response.data);
Iftekharul Islamc0161392017-06-14 15:46:15 -0500661 var content = JSON.parse(json);
662 var data = [];
Gunnar Mills902c38c2018-02-23 10:12:39 -0600663 var activationStatus = "";
Iftekharul Islamc0161392017-06-14 15:46:15 -0500664 var isExtended = false;
665 var bmcActiveVersion = "";
666 var hostActiveVersion = "";
667 var imageType = "";
668 var extendedVersions = [];
Gunnar Mills9b733bd2018-02-23 21:16:20 -0600669 var functionalImages = [];
Iftekharul Islamc0161392017-06-14 15:46:15 -0500670
671 function getFormatedExtendedVersions(extendedVersion){
672 var versions = [];
673 extendedVersion = extendedVersion.split(",");
674
675 extendedVersion.forEach(function(item){
676 var parts = item.split("-");
677 var numberIndex = 0;
678 for(var i = 0; i < parts.length; i++){
679 if(/[0-9]/.test(parts[i])){
680 numberIndex = i;
681 break;
682 }
683 }
684 var titlePart = parts.splice(0, numberIndex);
685 titlePart = titlePart.join("");
686 titlePart = titlePart[0].toUpperCase() + titlePart.substr(1, titlePart.length);
687 var versionPart = parts.join("-");
688 versions.push({
689 title: titlePart,
690 version: versionPart
691 });
692 });
693
694 return versions;
695 }
696
Gunnar Mills9b733bd2018-02-23 21:16:20 -0600697 // Get the list of functional images so we can compare
698 // later if an image is functional
699 if (content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH])
700 {
701 functionalImages = content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH].endpoints;
702 }
Iftekharul Islamc0161392017-06-14 15:46:15 -0500703 for(var key in content.data){
704 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Version')){
Gunnar Mills902c38c2018-02-23 10:12:39 -0600705 // If the image is "Functional" use that for the
706 // activation status, else use the value of "Activation"
707 // github.com/openbmc/phosphor-dbus-interfaces/blob/master/xyz/openbmc_project/Software/Activation.interface.yaml
708 activationStatus = content.data[key].Activation.split(".").pop();
Gunnar Mills9b733bd2018-02-23 21:16:20 -0600709 if (functionalImages.includes(key))
Gunnar Mills902c38c2018-02-23 10:12:39 -0600710 {
711 activationStatus = "Functional";
712 }
Gunnar Mills9b733bd2018-02-23 21:16:20 -0600713
Iftekharul Islamc0161392017-06-14 15:46:15 -0500714 imageType = content.data[key].Purpose.split(".").pop();
715 isExtended = content.data[key].hasOwnProperty('ExtendedVersion') && content.data[key].ExtendedVersion != "";
716 if(isExtended){
717 extendedVersions = getFormatedExtendedVersions(content.data[key].ExtendedVersion);
718 }
719 data.push(Object.assign({
720 path: key,
Gunnar Mills902c38c2018-02-23 10:12:39 -0600721 activationStatus: activationStatus,
Iftekharul Islamc0161392017-06-14 15:46:15 -0500722 imageId: key.split("/").pop(),
723 imageType: imageType,
724 isExtended: isExtended,
725 extended: {
726 show: false,
727 versions: extendedVersions
728 },
729 data: {key: key, value: content.data[key]}
730 }, content.data[key]));
731
Gunnar Mills902c38c2018-02-23 10:12:39 -0600732 if(activationStatus == 'Functional' && imageType == 'BMC'){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500733 bmcActiveVersion = content.data[key].Version;
734 }
735
Gunnar Mills902c38c2018-02-23 10:12:39 -0600736 if(activationStatus == 'Functional' && imageType == 'Host'){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500737 hostActiveVersion = content.data[key].Version;
738 }
739 }
740 }
Michael Davisdf3bd122017-08-10 11:03:42 -0500741
742 deferred.resolve({
Gunnar Millseedefd32018-02-28 17:02:34 -0600743 data: data,
744 bmcActiveVersion: bmcActiveVersion,
Michael Davisdf3bd122017-08-10 11:03:42 -0500745 hostActiveVersion: hostActiveVersion
746 });
Ed Tanousbbcf6702017-10-06 13:53:06 -0700747 }, function(error){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500748 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500749 deferred.reject(error);
Iftekharul Islamc0161392017-06-14 15:46:15 -0500750 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500751
752 return deferred.promise;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500753 },
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500754 changePriority: function(imageId, priority){
755 var deferred = $q.defer();
756 $http({
757 method: 'PUT',
758 url: DataService.getHost() + "/xyz/openbmc_project/software/" + imageId + "/attr/Priority",
759 headers: {
Iftekharul Islam2a489552017-11-02 13:23:08 -0500760 'Accept': 'application/json',
761 'Content-Type': 'application/json'
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500762 },
763 withCredentials: true,
764 data: JSON.stringify({"data": priority})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700765 }).then(function(response){
766 var json = JSON.stringify(response.data);
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500767 var content = JSON.parse(json);
768 deferred.resolve(content);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700769 }, function(error){
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500770 console.log(error);
771 deferred.reject(error);
772 });
773
774 return deferred.promise;
775 },
Iftekharul Islam2a489552017-11-02 13:23:08 -0500776 deleteImage: function(imageId){
777 var deferred = $q.defer();
778 $http({
779 method: 'POST',
780 url: DataService.getHost() + "/xyz/openbmc_project/software/" + imageId + "/action/Delete",
781 headers: {
782 'Accept': 'application/json',
783 'Content-Type': 'application/json'
784 },
785 withCredentials: true,
786 data: JSON.stringify({"data": []})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700787 }).then(function(response){
788 var json = JSON.stringify(response.data);
Iftekharul Islam2a489552017-11-02 13:23:08 -0500789 var content = JSON.parse(json);
790 deferred.resolve(content);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700791 }, function(error){
Iftekharul Islam2a489552017-11-02 13:23:08 -0500792 console.log(error);
793 deferred.reject(error);
794 });
795
796 return deferred.promise;
797 },
798 activateImage: function(imageId){
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500799 var deferred = $q.defer();
Iftekharul Islamc0161392017-06-14 15:46:15 -0500800 $http({
801 method: 'PUT',
Iftekharul Islam2a489552017-11-02 13:23:08 -0500802 url: DataService.getHost() + "/xyz/openbmc_project/software/" + imageId + "/attr/RequestedActivation",
803 headers: {
804 'Accept': 'application/json',
805 'Content-Type': 'application/json'
806 },
807 withCredentials: true,
808 data: JSON.stringify({"data": Constants.FIRMWARE.ACTIVATE_FIRMWARE})
Ed Tanousbbcf6702017-10-06 13:53:06 -0700809 }).then(function(response){
810 var json = JSON.stringify(response.data);
Iftekharul Islam2a489552017-11-02 13:23:08 -0500811 var content = JSON.parse(json);
812 deferred.resolve(content);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700813 }, function(error){
Iftekharul Islam2a489552017-11-02 13:23:08 -0500814 console.log(error);
815 deferred.reject(error);
816 });
817
818 return deferred.promise;
819 },
820 uploadImage: function(file){
Gunnar Mills87c3db62018-04-13 16:14:41 -0500821 return $http({
Iftekharul Islam2a489552017-11-02 13:23:08 -0500822 method: 'POST',
Iftekharul Islamc0161392017-06-14 15:46:15 -0500823 timeout: 5 * 60 * 1000,
Gunnar Millsdddb1492018-02-19 16:27:27 -0600824 url: DataService.getHost() + "/upload/image",
Iftekharul Islamc0161392017-06-14 15:46:15 -0500825 headers: {
Iftekharul Islam2a489552017-11-02 13:23:08 -0500826 'Content-Type': 'application/octet-stream'
Iftekharul Islamc0161392017-06-14 15:46:15 -0500827 },
828 withCredentials: true,
829 data: file
Ed Tanousbbcf6702017-10-06 13:53:06 -0700830 }).then(function(response){
Gunnar Mills87c3db62018-04-13 16:14:41 -0500831 return response.data;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500832 });
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500833 },
834 downloadImage: function(host, filename){
Gunnar Mills36379b92018-04-02 16:53:53 -0500835 return $http({
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500836 method: 'POST',
Gunnar Millse345cac2018-02-20 11:16:53 -0600837 url: DataService.getHost() + "/xyz/openbmc_project/software/action/DownloadViaTFTP",
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500838 headers: {
Gunnar Mills36379b92018-04-02 16:53:53 -0500839 'Accept': 'application/json',
840 'Content-Type': 'application/json'
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500841 },
842 withCredentials: true,
Gunnar Millse345cac2018-02-20 11:16:53 -0600843 data: JSON.stringify({"data": [filename, host]}),
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500844 responseType: 'arraybuffer'
Gunnar Mills36379b92018-04-02 16:53:53 -0500845 }).then(function(response){
846 return response.data;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500847 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500848 },
Michael Davisdf3bd122017-08-10 11:03:42 -0500849 getBMCEthernetInfo: function(){
850 var deferred = $q.defer();
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500851 $http({
852 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500853 url: DataService.getHost() + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc/ethernet",
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500854 headers: {
855 'Accept': 'application/json',
856 'Content-Type': 'application/json'
857 },
858 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700859 }).then(function(response){
860 var json = JSON.stringify(response.data);
Michael Davisdf3bd122017-08-10 11:03:42 -0500861 var content = JSON.parse(json);
862 deferred.resolve(content.data);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700863 }, function(error){
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500864 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500865 deferred.reject(error);
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500866 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500867
868 return deferred.promise;
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500869 },
870 getBMCInfo: function(callback){
Michael Davisdf3bd122017-08-10 11:03:42 -0500871 var deferred = $q.defer();
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500872 $http({
873 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500874 url: DataService.getHost() + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc",
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500875 headers: {
876 'Accept': 'application/json',
877 'Content-Type': 'application/json'
878 },
879 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700880 }).then(function(response){
881 var json = JSON.stringify(response.data);
Michael Davisdf3bd122017-08-10 11:03:42 -0500882 var content = JSON.parse(json);
883 deferred.resolve(content.data);
Ed Tanousbbcf6702017-10-06 13:53:06 -0700884 }, function(error){
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500885 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500886 deferred.reject(error);
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500887 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500888 return deferred.promise;
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500889 },
Iftekharul Islamee27d752017-07-05 15:54:31 -0500890 getHardwares: function(callback){
891 $http({
892 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500893 url: DataService.getHost() + "/xyz/openbmc_project/inventory/enumerate",
Iftekharul Islamee27d752017-07-05 15:54:31 -0500894 headers: {
895 'Accept': 'application/json',
896 'Content-Type': 'application/json'
897 },
898 withCredentials: true
Ed Tanousbbcf6702017-10-06 13:53:06 -0700899 }).then(function(response){
900 var json = JSON.stringify(response.data);
Iftekharul Islamee27d752017-07-05 15:54:31 -0500901 var content = JSON.parse(json);
902 var hardwareData = [];
903 var keyIndexMap = {};
904 var title = "";
905 var data = [];
906 var searchText = "";
907 var componentIndex = -1;
908 var tempParts = [];
909
910
911 function isSubComponent(key){
912
913 for(var i = 0; i < Constants.HARDWARE.parent_components.length; i++){
914 if(key.split(Constants.HARDWARE.parent_components[i]).length == 2) return true;
915 }
916
917 return false;
918 }
919
920 function titlelize(title){
921 title = title.replace(/([A-Z0-9]+)/g, " $1").replace(/^\s+/, "");
922 for(var i = 0; i < Constants.HARDWARE.uppercase_titles.length; i++){
923 if(title.toLowerCase().indexOf((Constants.HARDWARE.uppercase_titles[i] + " ")) > -1){
924 return title.toUpperCase();
925 }
926 }
927
928 return title;
929 }
930
931 function camelcaseToLabel(obj){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500932 var transformed = [], label = "", value = "";
Iftekharul Islamee27d752017-07-05 15:54:31 -0500933 for(var key in obj){
934 label = key.replace(/([A-Z0-9]+)/g, " $1").replace(/^\s+/, "");
935 if(obj[key] !== ""){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500936 value = obj[key];
937 if(value == 1 || value == 0){
938 value = (value == 1) ? 'Yes' : 'No';
939 }
940 transformed.push({key:label, value: value});
Iftekharul Islamee27d752017-07-05 15:54:31 -0500941 }
942 }
943
944 return transformed;
945 }
946
947 function getSearchText(data){
948 var searchText = "";
949 for(var i = 0; i < data.length; i++){
950 searchText += " " + data[i].key + " " + data[i].value;
951 }
952
Gunnar Millseedefd32018-02-28 17:02:34 -0600953 return searchText;
Iftekharul Islamee27d752017-07-05 15:54:31 -0500954 }
955
956 for(var key in content.data){
Gunnar Millseedefd32018-02-28 17:02:34 -0600957 if(content.data.hasOwnProperty(key) &&
Iftekharul Islamee27d752017-07-05 15:54:31 -0500958 key.indexOf(Constants.HARDWARE.component_key_filter) == 0){
959
960 data = camelcaseToLabel(content.data[key]);
961 searchText = getSearchText(data);
962 title = key.split("/").pop();
963
964 title = titlelize(title);
965
966 if(!isSubComponent(key)){
967 hardwareData.push(Object.assign({
968 path: key,
969 title: title,
970 selected: false,
971 expanded: false,
972 search_text: title.toLowerCase() + " " + searchText.toLowerCase(),
Gunnar Millseedefd32018-02-28 17:02:34 -0600973 sub_components: [],
Iftekharul Islamee27d752017-07-05 15:54:31 -0500974 original_data: {key: key, value: content.data[key]}
975 }, {items: data}));
976
977 keyIndexMap[key] = hardwareData.length - 1;
978 }else{
979 var tempParts = key.split("/");
980 tempParts.pop();
981 tempParts = tempParts.join("/");
982 componentIndex = keyIndexMap[tempParts];
983 data = content.data[key];
984 data.title = title;
985 hardwareData[componentIndex].sub_components.push(data);
986 hardwareData[componentIndex].search_text += " " + title.toLowerCase();
Gunnar Millscdd9d6b2018-03-09 11:57:06 -0600987
988 // Sort the subcomponents alphanumeric so they are displayed on the
989 // inventory page in order (e.g. core 0, core 1, core 2, ... core 12, core 13)
990 hardwareData[componentIndex].sub_components.sort(function (a, b) {
991 return a.title.localeCompare(b.title, 'en', { numeric: true });
992 });
Iftekharul Islamee27d752017-07-05 15:54:31 -0500993 }
994 }
995 }
996
997 if(callback){
998 callback(hardwareData, content.data);
999 }else{
1000 return { data: hardwareData, original_data: content.data};
1001 }
1002 });
1003 },
Iftekharul Islamf2d74642017-07-10 16:42:14 -05001004 deleteLogs: function(logs) {
1005 var defer = $q.defer();
1006 var promises = [];
1007
1008 function finished(){
1009 defer.resolve();
1010 }
1011
1012 logs.forEach(function(item){
1013 promises.push($http({
1014 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -05001015 url: DataService.getHost() + "/xyz/openbmc_project/logging/entry/"+item.Id+"/action/Delete",
Iftekharul Islamf2d74642017-07-10 16:42:14 -05001016 headers: {
1017 'Accept': 'application/json',
1018 'Content-Type': 'application/json'
1019 },
1020 withCredentials: true,
1021 data: JSON.stringify({"data": []})
1022 }));
1023 });
1024
1025 $q.all(promises).then(finished);
1026
1027 return defer.promise;
1028 },
1029 resolveLogs: function(logs) {
1030 var defer = $q.defer();
1031 var promises = [];
1032
1033 function finished(){
1034 defer.resolve();
1035 }
1036
1037 logs.forEach(function(item){
1038 promises.push($http({
1039 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -05001040 url: DataService.getHost() + "/xyz/openbmc_project/logging/entry/"+item.Id+"/attr/Resolved",
Iftekharul Islamf2d74642017-07-10 16:42:14 -05001041 headers: {
1042 'Accept': 'application/json',
1043 'Content-Type': 'application/json'
1044 },
1045 withCredentials: true,
1046 data: JSON.stringify({"data": "1"})
1047 }));
1048 });
1049
1050 $q.all(promises).then(finished);
1051
1052 return defer.promise;
1053 },
CamVan Nguyen33275832018-03-13 18:38:37 -05001054 getPowerConsumption: function(){
1055 return $http({
1056 method: 'GET',
1057 url: DataService.getHost() + "/xyz/openbmc_project/sensors/power/total_power",
1058 headers: {
1059 'Accept': 'application/json',
1060 'Content-Type': 'application/json'
1061 },
1062 withCredentials: true
1063 }).then(function(response){
1064 var json = JSON.stringify(response.data);
1065 var content = JSON.parse(json);
1066
1067 return getScaledValue(content.data.Value,
1068 content.data.Scale) + ' ' +
1069 Constants.POWER_CONSUMPTION_TEXT[content.data.Unit];
1070 }, function(error){
1071 if ('Not Found' == error.statusText) {
1072 return Constants.POWER_CONSUMPTION_TEXT.notavailable;
1073 } else {
1074 console.log(error);
1075 }
1076 });
1077 },
1078 getPowerCap: function(){
1079 return $http({
1080 method: 'GET',
1081 url: DataService.getHost() + "/xyz/openbmc_project/control/host0/power_cap",
1082 headers: {
1083 'Accept': 'application/json',
1084 'Content-Type': 'application/json'
1085 },
1086 withCredentials: true
1087 }).then(function(response){
1088 var json = JSON.stringify(response.data);
1089 var content = JSON.parse(json);
1090
1091 return (false == content.data.PowerCapEnable) ?
1092 Constants.POWER_CAP_TEXT.disabled :
1093 content.data.PowerCap + ' ' + Constants.POWER_CAP_TEXT.unit;
1094 }, function(error){
1095 console.log(error);
1096 });
1097 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -05001098 };
1099 return SERVICE;
1100 }]);
1101
1102 })(window.angular);