blob: d7e4e7f02d248070c98268f64d5f7137cd5a04e4 [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
Iftekharul Islam99d199f2017-03-24 15:28:25 -05007 */
8
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07009window.angular && (function(angular) {
10 'use strict';
Andrew Geisslerd27bb132018-05-24 11:07:27 -070011 angular.module('app.common.services').factory('APIUtils', [
12 '$http', 'Constants', '$q', 'dataService',
13 function($http, Constants, $q, DataService) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070014 var getScaledValue = function(value, scale) {
15 scale = scale + '';
16 scale = parseInt(scale, 10);
17 var power = Math.abs(parseInt(scale, 10));
CamVan Nguyen33275832018-03-13 18:38:37 -050018
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070019 if (scale > 0) {
20 value = value * Math.pow(10, power);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070021 } else if (scale < 0) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070022 value = value / Math.pow(10, power);
23 }
24 return value;
25 };
26 var SERVICE = {
27 API_CREDENTIALS: Constants.API_CREDENTIALS,
28 API_RESPONSE: Constants.API_RESPONSE,
29 CHASSIS_POWER_STATE: Constants.CHASSIS_POWER_STATE,
30 HOST_STATE_TEXT: Constants.HOST_STATE,
31 HOST_STATE: Constants.HOST_STATE,
32 LED_STATE: Constants.LED_STATE,
33 LED_STATE_TEXT: Constants.LED_STATE_TEXT,
34 HOST_SESSION_STORAGE_KEY: Constants.API_CREDENTIALS.host_storage_key,
35 getChassisState: function() {
36 var deferred = $q.defer();
37 $http({
38 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -070039 url: DataService.getHost() +
40 '/xyz/openbmc_project/state/chassis0/attr/CurrentPowerState',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070041 headers: {
42 'Accept': 'application/json',
43 'Content-Type': 'application/json'
44 },
45 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -070046 })
47 .then(
48 function(response) {
49 var json = JSON.stringify(response.data);
50 var content = JSON.parse(json);
51 deferred.resolve(content.data);
52 },
53 function(error) {
54 console.log(error);
55 deferred.reject(error);
56 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070057 return deferred.promise;
58 },
Gunnar Mills65491142018-06-04 14:23:33 -050059 validIPV4IP: function(ip) {
60 // Checks for [0-255].[0-255].[0-255].[0-255]
61 return ip.match(
62 /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/);
63 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070064 getHostState: function() {
65 var deferred = $q.defer();
66 $http({
67 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -070068 url: DataService.getHost() +
69 '/xyz/openbmc_project/state/host0/attr/CurrentHostState',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070070 headers: {
71 'Accept': 'application/json',
72 'Content-Type': 'application/json'
73 },
74 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -070075 })
76 .then(
77 function(response) {
78 var json = JSON.stringify(response.data);
79 var content = JSON.parse(json);
80 deferred.resolve(content.data);
81 },
82 function(error) {
83 console.log(error);
84 deferred.reject(error);
85 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070086 return deferred.promise;
87 },
88 getNetworkInfo: function() {
89 var deferred = $q.defer();
90 $http({
91 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -070092 url: DataService.getHost() +
93 '/xyz/openbmc_project/network/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070094 headers: {
95 'Accept': 'application/json',
96 'Content-Type': 'application/json'
97 },
98 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -070099 })
100 .then(
101 function(response) {
102 var json = JSON.stringify(response.data);
103 var content = JSON.parse(json);
104 var hostname = '';
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500105 var defaultgateway = '';
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700106 var macAddress = '';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700107
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700108 function parseNetworkData(content) {
109 var data = {
110 interface_ids: [],
111 interfaces: {},
112 ip_addresses: {ipv4: [], ipv6: []},
113 };
114 var interfaceId = '', keyParts = [], interfaceHash = '',
115 interfaceType = '';
116 for (var key in content.data) {
Gunnar Mills3efbe2d2018-06-07 14:47:19 -0500117 if (key.match(/network\/eth\d+(_\d+)?$/ig)) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700118 interfaceId = key.split('/').pop();
119 if (data.interface_ids.indexOf(interfaceId) == -1) {
120 data.interface_ids.push(interfaceId);
121 data.interfaces[interfaceId] = {
122 interfaceIname: '',
123 DomainName: '',
124 MACAddress: '',
125 Nameservers: [],
126 DHCPEnabled: 0,
127 ipv4: {ids: [], values: []},
128 ipv6: {ids: [], values: []}
129 };
130 data.interfaces[interfaceId].MACAddress =
131 content.data[key].MACAddress;
132 data.interfaces[interfaceId].DomainName =
133 content.data[key].DomainName.join(' ');
134 data.interfaces[interfaceId].Nameservers =
135 content.data[key].Nameservers;
136 data.interfaces[interfaceId].DHCPEnabled =
137 content.data[key].DHCPEnabled;
138 }
139 } else if (
140 key.match(
Gunnar Mills3efbe2d2018-06-07 14:47:19 -0500141 /network\/eth\d+(_\d+)?\/ipv[4|6]\/[a-z0-9]+$/ig)) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700142 keyParts = key.split('/');
143 interfaceHash = keyParts.pop();
144 interfaceType = keyParts.pop();
145 interfaceId = keyParts.pop();
146
147 if (data.interfaces[interfaceId][interfaceType]
148 .ids.indexOf(interfaceHash) == -1) {
149 data.interfaces[interfaceId][interfaceType]
150 .ids.push(interfaceHash);
151 data.interfaces[interfaceId][interfaceType]
152 .values.push(content.data[key]);
153 data.ip_addresses[interfaceType].push(
154 content.data[key]['Address']);
155 }
156 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700157 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700158 return data;
159 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700160
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700161 if (content.data.hasOwnProperty(
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500162 '/xyz/openbmc_project/network/config')) {
163 if (content.data['/xyz/openbmc_project/network/config']
164 .hasOwnProperty('HostName')) {
165 hostname =
166 content.data['/xyz/openbmc_project/network/config']
167 .HostName;
168 }
169 if (content.data['/xyz/openbmc_project/network/config']
170 .hasOwnProperty('DefaultGateway')) {
171 defaultgateway =
172 content.data['/xyz/openbmc_project/network/config']
173 .DefaultGateway;
174 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700175 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700176
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700177 if (content.data.hasOwnProperty(
178 '/xyz/openbmc_project/network/eth0') &&
179 content.data['/xyz/openbmc_project/network/eth0']
180 .hasOwnProperty('MACAddress')) {
181 macAddress =
182 content.data['/xyz/openbmc_project/network/eth0']
183 .MACAddress;
184 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700185
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700186 deferred.resolve({
187 data: content.data,
188 hostname: hostname,
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500189 defaultgateway: defaultgateway,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700190 mac_address: macAddress,
191 formatted_data: parseNetworkData(content)
192 });
193 },
194 function(error) {
195 console.log(error);
196 deferred.reject(error);
197 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700198 return deferred.promise;
199 },
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500200 setMACAddress: function(interface_name, mac_address) {
201 return $http({
202 method: 'PUT',
203 url: DataService.getHost() +
204 '/xyz/openbmc_project/network/' + interface_name +
205 '/attr/MACAddress',
206 headers: {
207 'Accept': 'application/json',
208 'Content-Type': 'application/json'
209 },
210 withCredentials: true,
211 data: JSON.stringify({'data': mac_address})
212 })
213 .then(function(response) {
214 return response.data;
215 });
216 },
Gunnar Millsdca79d72018-05-30 13:07:01 -0500217 setDefaultGateway: function(defaultGateway) {
218 return $http({
219 method: 'PUT',
220 url: DataService.getHost() +
221 '/xyz/openbmc_project/network/config/attr/DefaultGateway',
222 headers: {
223 'Accept': 'application/json',
224 'Content-Type': 'application/json'
225 },
226 withCredentials: true,
227 data: JSON.stringify({'data': defaultGateway})
228 })
229 .then(function(response) {
230 return response.data;
231 });
232 },
Gunnar Millscb2c3062018-05-31 13:13:30 -0500233 setDHCPEnabled: function(interfaceName, dhcpEnabled) {
234 return $http({
235 method: 'PUT',
236 url: DataService.getHost() +
237 '/xyz/openbmc_project/network/' + interfaceName +
238 '/attr/DHCPEnabled',
239 headers: {
240 'Accept': 'application/json',
241 'Content-Type': 'application/json'
242 },
243 withCredentials: true,
244 data: JSON.stringify({'data': dhcpEnabled})
245 })
246 .then(function(response) {
247 return response.data;
248 });
249 },
Gunnar Millsa45c3852018-05-30 16:18:45 -0500250 deleteIPV4: function(interfaceName, networkID) {
251 return $http({
252 method: 'POST',
253 url: DataService.getHost() +
254 '/xyz/openbmc_project/network/' + interfaceName +
255 '/ipv4/' + networkID + '/action/Delete',
256 headers: {
257 'Accept': 'application/json',
258 'Content-Type': 'application/json'
259 },
260 withCredentials: true,
261 data: JSON.stringify({'data': []})
262 })
263 .then(function(response) {
264 return response.data;
265 });
266 },
267 addIPV4: function(
268 interfaceName, ipAddress, netmaskPrefixLength, gateway) {
269 return $http({
270 method: 'POST',
271 url: DataService.getHost() +
272 '/xyz/openbmc_project/network/' + interfaceName +
273 '/action/IP',
274 headers: {
275 'Accept': 'application/json',
276 'Content-Type': 'application/json'
277 },
278 withCredentials: true,
279 data: JSON.stringify({
280 'data': [
281 'xyz.openbmc_project.Network.IP.Protocol.IPv4',
282 ipAddress, +netmaskPrefixLength, gateway
283 ]
284 })
285 })
286 .then(function(response) {
287 return response.data;
288 });
289 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700290 getLEDState: function() {
291 var deferred = $q.defer();
292 $http({
293 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700294 url: DataService.getHost() +
295 '/xyz/openbmc_project/led/groups/enclosure_identify',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700296 headers: {
297 'Accept': 'application/json',
298 'Content-Type': 'application/json'
299 },
300 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700301 })
302 .then(
303 function(response) {
304 var json = JSON.stringify(response.data);
305 var content = JSON.parse(json);
306 deferred.resolve(content.data.Asserted);
307 },
308 function(error) {
309 console.log(error);
310 deferred.reject(error);
311 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700312 return deferred.promise;
313 },
314 login: function(username, password, callback) {
315 $http({
316 method: 'POST',
317 url: DataService.getHost() + '/login',
318 headers: {
319 'Accept': 'application/json',
320 'Content-Type': 'application/json'
321 },
322 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700323 data: JSON.stringify({'data': [username, password]})
324 })
325 .then(
326 function(response) {
327 if (callback) {
328 callback(response.data);
329 }
330 },
331 function(error) {
332 if (callback) {
333 if (error && error.status && error.status == 'error') {
334 callback(error);
335 } else {
336 callback(error, true);
337 }
338 }
339 console.log(error);
340 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700341 },
342 testPassword: function(username, password) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700343 // Calls /login without the current session to verify the given
344 // password is correct ignore the interceptor logout on a bad password
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700345 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700346 method: 'POST',
347 url: DataService.getHost() + '/login',
348 headers: {
349 'Accept': 'application/json',
350 'Content-Type': 'application/json'
351 },
352 withCredentials: false,
353 data: JSON.stringify({'data': [username, password]})
354 })
355 .then(function(response) {
356 return response.data;
357 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700358 },
359 logout: function(callback) {
360 $http({
361 method: 'POST',
362 url: DataService.getHost() + '/logout',
363 headers: {
364 'Accept': 'application/json',
365 'Content-Type': 'application/json'
366 },
367 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700368 data: JSON.stringify({'data': []})
369 })
370 .then(
371 function(response) {
372 if (callback) {
373 callback(response.data);
374 }
375 },
376 function(error) {
377 if (callback) {
378 callback(null, error);
379 }
380 console.log(error);
381 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700382 },
383 changePassword: function(user, newPassword) {
384 var deferred = $q.defer();
385 $http({
386 method: 'POST',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700387 url: DataService.getHost() + '/xyz/openbmc_project/user/' + user +
388 '/action/SetPassword',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700389 headers: {
390 'Accept': 'application/json',
391 'Content-Type': 'application/json'
392 },
393 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700394 data: JSON.stringify({'data': [newPassword]}),
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700395 responseType: 'arraybuffer'
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700396 })
397 .then(
398 function(response, status, headers) {
399 deferred.resolve(
400 {data: response, status: status, headers: headers});
401 },
402 function(error) {
403 console.log(error);
404 deferred.reject(error);
405 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700406 return deferred.promise;
407 },
408 chassisPowerOn: function(callback) {
409 $http({
410 method: 'POST',
411 url: DataService.getHost() + '/xyz/openbmc_project/state/host0',
412 headers: {
413 'Accept': 'application/json',
414 'Content-Type': 'application/json'
415 },
416 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700417 data: JSON.stringify({'data': []})
418 })
419 .then(
420 function(response) {
421 var json = JSON.stringify(response.data);
422 var content = JSON.parse(json);
423 if (callback) {
424 return callback(content.data.CurrentPowerState);
425 }
426 },
427 function(error) {
428 if (callback) {
429 callback(error);
430 } else {
431 console.log(error);
432 }
433 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700434 },
435 chassisPowerOff: function() {
436 var deferred = $q.defer();
437 $http({
438 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700439 url: DataService.getHost() +
440 '/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700441 headers: {
442 'Accept': 'application/json',
443 'Content-Type': 'application/json'
444 },
445 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700446 data: JSON.stringify(
447 {'data': 'xyz.openbmc_project.State.Chassis.Transition.Off'})
448 })
449 .then(
450 function(response) {
451 var json = JSON.stringify(response.data);
452 var content = JSON.parse(json);
453 deferred.resolve(content.status);
454 },
455 function(error) {
456 console.log(error);
457 deferred.reject(error);
458 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700459 return deferred.promise;
460 },
461 setLEDState: function(state, callback) {
462 $http({
463 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700464 url: DataService.getHost() +
465 '/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700466 headers: {
467 'Accept': 'application/json',
468 'Content-Type': 'application/json'
469 },
470 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700471 data: JSON.stringify({'data': state})
472 })
473 .then(
474 function(response) {
475 var json = JSON.stringify(response.data);
476 var content = JSON.parse(json);
477 if (callback) {
478 return callback(content.status);
479 }
480 },
481 function(error) {
482 if (callback) {
483 callback(error);
484 } else {
485 console.log(error);
486 }
487 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700488 },
489 bmcReboot: function(callback) {
490 $http({
491 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700492 url: DataService.getHost() +
493 '/xyz/openbmc_project/state/bmc0/attr/RequestedBmcTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700494 headers: {
495 'Accept': 'application/json',
496 'Content-Type': 'application/json'
497 },
498 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700499 data: JSON.stringify(
500 {'data': 'xyz.openbmc_project.State.BMC.Transition.Reboot'})
501 })
502 .then(
503 function(response) {
504 var json = JSON.stringify(response.data);
505 var content = JSON.parse(json);
506 if (callback) {
507 return callback(content.status);
508 }
509 },
510 function(error) {
511 if (callback) {
512 callback(error);
513 } else {
514 console.log(error);
515 }
516 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700517 },
518 hostPowerOn: function() {
519 var deferred = $q.defer();
520 $http({
521 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700522 url: DataService.getHost() +
523 '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700524 headers: {
525 'Accept': 'application/json',
526 'Content-Type': 'application/json'
527 },
528 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700529 data: JSON.stringify(
530 {'data': 'xyz.openbmc_project.State.Host.Transition.On'})
531 })
532 .then(
533 function(response) {
534 var json = JSON.stringify(response.data);
535 var content = JSON.parse(json);
536 deferred.resolve(content.status);
537 },
538 function(error) {
539 console.log(error);
540 deferred.reject(error);
541 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700542 return deferred.promise;
543 },
544 hostPowerOff: function() {
545 var deferred = $q.defer();
546 $http({
547 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700548 url: DataService.getHost() +
549 '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700550 headers: {
551 'Accept': 'application/json',
552 'Content-Type': 'application/json'
553 },
554 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700555 data: JSON.stringify(
556 {'data': 'xyz.openbmc_project.State.Host.Transition.Off'})
557 })
558 .then(
559 function(response) {
560 var json = JSON.stringify(response.data);
561 var content = JSON.parse(json);
562 deferred.resolve(content.status);
563 },
564 function(error) {
565 console.log(error);
566 deferred.reject(error);
567 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700568 return deferred.promise;
569 },
570 hostReboot: function() {
571 var deferred = $q.defer();
572 $http({
573 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700574 url: DataService.getHost() +
575 '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700576 headers: {
577 'Accept': 'application/json',
578 'Content-Type': 'application/json'
579 },
580 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700581 data: JSON.stringify(
582 {'data': 'xyz.openbmc_project.State.Host.Transition.Reboot'})
583 })
584 .then(
585 function(response) {
586 var json = JSON.stringify(response.data);
587 var content = JSON.parse(json);
588 deferred.resolve(content.status);
589 },
590 function(error) {
591 console.log(error);
592 deferred.reject(error);
593 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700594
595 return deferred.promise;
596 },
597 hostShutdown: function(callback) {
598 $http({
599 method: 'POST',
600 url: DataService.getHost() + '/xyz/openbmc_project/state/host0',
601 headers: {
602 'Accept': 'application/json',
603 'Content-Type': 'application/json'
604 },
605 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700606 data: JSON.stringify({'data': []})
607 })
608 .then(
609 function(response) {
610 var json = JSON.stringify(response.data);
611 var content = JSON.parse(json);
612 if (callback) {
613 return callback(content);
614 }
615 },
616 function(error) {
617 if (callback) {
618 callback(error);
619 } else {
620 console.log(error);
621 }
622 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700623 },
624 getLogs: function() {
625 var deferred = $q.defer();
626 $http({
627 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700628 url: DataService.getHost() +
629 '/xyz/openbmc_project/logging/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700630 headers: {
631 'Accept': 'application/json',
632 'Content-Type': 'application/json'
633 },
634 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700635 })
636 .then(
637 function(response) {
638 var json = JSON.stringify(response.data);
639 var content = JSON.parse(json);
640 var dataClone = JSON.parse(JSON.stringify(content.data));
641 var data = [];
642 var severityCode = '';
643 var priority = '';
644 var health = '';
645 var relatedItems = [];
Matt Spinler845acdc2018-05-01 16:41:28 -0500646 var eventID = 'None';
647 var description = 'None';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700648
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700649 for (var key in content.data) {
650 if (content.data.hasOwnProperty(key) &&
651 content.data[key].hasOwnProperty('Id')) {
652 var severityFlags = {
653 low: false,
654 medium: false,
655 high: false
656 };
657 var healthFlags = {
658 critical: false,
659 warning: false,
660 good: false
661 };
662 severityCode =
663 content.data[key].Severity.split('.').pop();
664 priority =
665 Constants.SEVERITY_TO_PRIORITY_MAP[severityCode];
666 severityFlags[priority.toLowerCase()] = true;
667 health = Constants.SEVERITY_TO_HEALTH_MAP[severityCode];
668 healthFlags[health.toLowerCase()] = true;
669 relatedItems = [];
670 content.data[key].associations.forEach(function(item) {
671 relatedItems.push(item[2]);
672 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700673
Matt Spinler845acdc2018-05-01 16:41:28 -0500674 if (content.data[key].hasOwnProperty(['EventID'])) {
675 eventID = content.data[key].EventID;
676 }
677
678 if (content.data[key].hasOwnProperty(['Description'])) {
679 description = content.data[key].Description;
680 }
681
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700682 data.push(Object.assign(
683 {
684 path: key,
685 copied: false,
686 priority: priority,
687 severity_code: severityCode,
688 severity_flags: severityFlags,
689 health_flags: healthFlags,
690 additional_data:
691 content.data[key].AdditionalData.join('\n'),
692 type: content.data[key].Message,
693 selected: false,
694 search_text:
695 ('#' + content.data[key].Id + ' ' +
696 severityCode + ' ' +
697 content.data[key].Severity + ' ' +
698 content.data[key].AdditionalData.join(' '))
699 .toLowerCase(),
700 meta: false,
701 confirm: false,
702 related_items: relatedItems,
Matt Spinler845acdc2018-05-01 16:41:28 -0500703 eventID: eventID,
704 description: description,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700705 data: {key: key, value: content.data[key]}
706 },
707 content.data[key]));
708 }
709 }
710 deferred.resolve({data: data, original: dataClone});
711 },
712 function(error) {
713 console.log(error);
714 deferred.reject(error);
715 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700716
717 return deferred.promise;
718 },
719 getAllSensorStatus: function(callback) {
720 $http({
721 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700722 url: DataService.getHost() +
723 '/xyz/openbmc_project/sensors/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700724 headers: {
725 'Accept': 'application/json',
726 'Content-Type': 'application/json'
727 },
728 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700729 })
730 .then(
731 function(response) {
732 var json = JSON.stringify(response.data);
733 var content = JSON.parse(json);
734 var dataClone = JSON.parse(JSON.stringify(content.data));
735 var sensorData = [];
736 var severity = {};
737 var title = '';
738 var tempKeyParts = [];
739 var order = 0;
740 var customOrder = 0;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700741
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700742 function getSensorStatus(reading) {
743 var severityFlags = {
744 critical: false,
745 warning: false,
746 normal: false
747 },
748 severityText = '', order = 0;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700749
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700750 if (reading.hasOwnProperty('CriticalLow') &&
751 reading.Value < reading.CriticalLow) {
752 severityFlags.critical = true;
753 severityText = 'critical';
754 order = 2;
755 } else if (
756 reading.hasOwnProperty('CriticalHigh') &&
757 reading.Value > reading.CriticalHigh) {
758 severityFlags.critical = true;
759 severityText = 'critical';
760 order = 2;
761 } else if (
762 reading.hasOwnProperty('CriticalLow') &&
763 reading.hasOwnProperty('WarningLow') &&
764 reading.Value >= reading.CriticalLow &&
765 reading.Value <= reading.WarningLow) {
766 severityFlags.warning = true;
767 severityText = 'warning';
768 order = 1;
769 } else if (
770 reading.hasOwnProperty('WarningHigh') &&
771 reading.hasOwnProperty('CriticalHigh') &&
772 reading.Value >= reading.WarningHigh &&
773 reading.Value <= reading.CriticalHigh) {
774 severityFlags.warning = true;
775 severityText = 'warning';
776 order = 1;
777 } else {
778 severityFlags.normal = true;
779 severityText = 'normal';
780 }
781 return {
782 flags: severityFlags,
783 severityText: severityText,
784 order: order
785 };
786 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700787
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700788 for (var key in content.data) {
789 if (content.data.hasOwnProperty(key) &&
790 content.data[key].hasOwnProperty('Unit')) {
791 severity = getSensorStatus(content.data[key]);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700792
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700793 if (!content.data[key].hasOwnProperty('CriticalLow')) {
794 content.data[key].CriticalLow = '--';
795 content.data[key].CriticalHigh = '--';
796 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700797
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700798 if (!content.data[key].hasOwnProperty('WarningLow')) {
799 content.data[key].WarningLow = '--';
800 content.data[key].WarningHigh = '--';
801 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700802
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700803 tempKeyParts = key.split('/');
804 title = tempKeyParts.pop();
805 title = tempKeyParts.pop() + '_' + title;
806 title = title.split('_')
807 .map(function(item) {
808 return item.toLowerCase()
809 .charAt(0)
810 .toUpperCase() +
811 item.slice(1);
812 })
813 .reduce(function(prev, el) {
814 return prev + ' ' + el;
815 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700816
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700817 content.data[key].Value = getScaledValue(
818 content.data[key].Value, content.data[key].Scale);
819 content.data[key].CriticalLow = getScaledValue(
820 content.data[key].CriticalLow,
821 content.data[key].Scale);
822 content.data[key].CriticalHigh = getScaledValue(
823 content.data[key].CriticalHigh,
824 content.data[key].Scale);
825 content.data[key].WarningLow = getScaledValue(
826 content.data[key].WarningLow,
827 content.data[key].Scale);
828 content.data[key].WarningHigh = getScaledValue(
829 content.data[key].WarningHigh,
830 content.data[key].Scale);
831 if (Constants.SENSOR_SORT_ORDER.indexOf(
832 content.data[key].Unit) > -1) {
833 customOrder = Constants.SENSOR_SORT_ORDER.indexOf(
834 content.data[key].Unit);
835 } else {
836 customOrder = Constants.SENSOR_SORT_ORDER_DEFAULT;
837 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700838
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700839 sensorData.push(Object.assign(
840 {
841 path: key,
842 selected: false,
843 confirm: false,
844 copied: false,
845 title: title,
846 unit:
847 Constants
848 .SENSOR_UNIT_MAP[content.data[key].Unit],
849 severity_flags: severity.flags,
850 status: severity.severityText,
851 order: severity.order,
852 custom_order: customOrder,
853 search_text:
854 (title + ' ' + content.data[key].Value + ' ' +
855 Constants.SENSOR_UNIT_MAP[content.data[key]
856 .Unit] +
857 ' ' + severity.severityText + ' ' +
858 content.data[key].CriticalLow + ' ' +
859 content.data[key].CriticalHigh + ' ' +
860 content.data[key].WarningLow + ' ' +
861 content.data[key].WarningHigh + ' ')
862 .toLowerCase(),
863 original_data:
864 {key: key, value: content.data[key]}
865 },
866 content.data[key]));
867 }
868 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700869
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700870 callback(sensorData, dataClone);
871 },
872 function(error) {
873 console.log(error);
874 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700875 },
876 getActivation: function(imageId) {
877 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700878 method: 'GET',
879 url: DataService.getHost() +
880 '/xyz/openbmc_project/software/' + imageId +
881 '/attr/Activation',
882 headers: {
883 'Accept': 'application/json',
884 'Content-Type': 'application/json'
885 },
886 withCredentials: true
887 })
888 .then(function(response) {
889 return response.data;
890 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700891 },
892 getFirmwares: function() {
893 var deferred = $q.defer();
894 $http({
895 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700896 url: DataService.getHost() +
897 '/xyz/openbmc_project/software/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700898 headers: {
899 'Accept': 'application/json',
900 'Content-Type': 'application/json'
901 },
902 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700903 })
904 .then(
905 function(response) {
906 var json = JSON.stringify(response.data);
907 var content = JSON.parse(json);
908 var data = [];
909 var activationStatus = '';
910 var isExtended = false;
911 var bmcActiveVersion = '';
912 var hostActiveVersion = '';
913 var imageType = '';
914 var extendedVersions = [];
915 var functionalImages = [];
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700916
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700917 function getFormatedExtendedVersions(extendedVersion) {
918 var versions = [];
919 extendedVersion = extendedVersion.split(',');
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700920
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700921 extendedVersion.forEach(function(item) {
922 var parts = item.split('-');
923 var numberIndex = 0;
924 for (var i = 0; i < parts.length; i++) {
925 if (/[0-9]/.test(parts[i])) {
926 numberIndex = i;
927 break;
928 }
929 }
930 var titlePart = parts.splice(0, numberIndex);
931 titlePart = titlePart.join('');
932 titlePart = titlePart[0].toUpperCase() +
933 titlePart.substr(1, titlePart.length);
934 var versionPart = parts.join('-');
935 versions.push({title: titlePart, version: versionPart});
936 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700937
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700938 return versions;
939 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700940
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700941 // Get the list of functional images so we can compare
942 // later if an image is functional
943 if (content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH]) {
944 functionalImages =
945 content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH]
946 .endpoints;
947 }
948 for (var key in content.data) {
949 if (content.data.hasOwnProperty(key) &&
950 content.data[key].hasOwnProperty('Version')) {
951 // If the image is "Functional" use that for the
952 // activation status, else use the value of "Activation"
953 // github.com/openbmc/phosphor-dbus-interfaces/blob/master/xyz/openbmc_project/Software/Activation.interface.yaml
954 activationStatus =
955 content.data[key].Activation.split('.').pop();
956 if (functionalImages.includes(key)) {
957 activationStatus = 'Functional';
958 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700959
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700960 imageType = content.data[key].Purpose.split('.').pop();
961 isExtended = content.data[key].hasOwnProperty(
962 'ExtendedVersion') &&
963 content.data[key].ExtendedVersion != '';
964 if (isExtended) {
965 extendedVersions = getFormatedExtendedVersions(
966 content.data[key].ExtendedVersion);
967 }
968 data.push(Object.assign(
969 {
970 path: key,
971 activationStatus: activationStatus,
972 imageId: key.split('/').pop(),
973 imageType: imageType,
974 isExtended: isExtended,
975 extended:
976 {show: false, versions: extendedVersions},
977 data: {key: key, value: content.data[key]}
978 },
979 content.data[key]));
980
981 if (activationStatus == 'Functional' &&
982 imageType == 'BMC') {
983 bmcActiveVersion = content.data[key].Version;
984 }
985
986 if (activationStatus == 'Functional' &&
987 imageType == 'Host') {
988 hostActiveVersion = content.data[key].Version;
989 }
990 }
991 }
992
993 deferred.resolve({
994 data: data,
995 bmcActiveVersion: bmcActiveVersion,
996 hostActiveVersion: hostActiveVersion
997 });
Iftekharul Islam171c6a12017-08-11 08:35:47 -0500998 },
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700999 function(error) {
1000 console.log(error);
1001 deferred.reject(error);
1002 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001003
1004 return deferred.promise;
1005 },
1006 changePriority: function(imageId, priority) {
1007 var deferred = $q.defer();
1008 $http({
1009 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001010 url: DataService.getHost() + '/xyz/openbmc_project/software/' +
1011 imageId + '/attr/Priority',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001012 headers: {
1013 'Accept': 'application/json',
1014 'Content-Type': 'application/json'
1015 },
1016 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001017 data: JSON.stringify({'data': priority})
1018 })
1019 .then(
1020 function(response) {
1021 var json = JSON.stringify(response.data);
1022 var content = JSON.parse(json);
1023 deferred.resolve(content);
1024 },
1025 function(error) {
1026 console.log(error);
1027 deferred.reject(error);
1028 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001029
1030 return deferred.promise;
1031 },
1032 deleteImage: function(imageId) {
1033 var deferred = $q.defer();
1034 $http({
1035 method: 'POST',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001036 url: DataService.getHost() + '/xyz/openbmc_project/software/' +
1037 imageId + '/action/Delete',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001038 headers: {
1039 'Accept': 'application/json',
1040 'Content-Type': 'application/json'
1041 },
1042 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001043 data: JSON.stringify({'data': []})
1044 })
1045 .then(
1046 function(response) {
1047 var json = JSON.stringify(response.data);
1048 var content = JSON.parse(json);
1049 deferred.resolve(content);
1050 },
1051 function(error) {
1052 console.log(error);
1053 deferred.reject(error);
1054 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001055
1056 return deferred.promise;
1057 },
1058 activateImage: function(imageId) {
1059 var deferred = $q.defer();
1060 $http({
1061 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001062 url: DataService.getHost() + '/xyz/openbmc_project/software/' +
1063 imageId + '/attr/RequestedActivation',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001064 headers: {
1065 'Accept': 'application/json',
1066 'Content-Type': 'application/json'
1067 },
1068 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001069 data:
1070 JSON.stringify({'data': Constants.FIRMWARE.ACTIVATE_FIRMWARE})
1071 })
1072 .then(
1073 function(response) {
1074 var json = JSON.stringify(response.data);
1075 var content = JSON.parse(json);
1076 deferred.resolve(content);
1077 },
1078 function(error) {
1079 console.log(error);
1080 deferred.reject(error);
1081 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001082
1083 return deferred.promise;
1084 },
1085 uploadImage: function(file) {
1086 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001087 method: 'POST',
1088 timeout: 5 * 60 * 1000,
1089 url: DataService.getHost() + '/upload/image',
1090 headers: {'Content-Type': 'application/octet-stream'},
1091 withCredentials: true,
1092 data: file
1093 })
1094 .then(function(response) {
1095 return response.data;
1096 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001097 },
1098 downloadImage: function(host, filename) {
1099 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001100 method: 'POST',
1101 url: DataService.getHost() +
1102 '/xyz/openbmc_project/software/action/DownloadViaTFTP',
1103 headers: {
1104 'Accept': 'application/json',
1105 'Content-Type': 'application/json'
1106 },
1107 withCredentials: true,
1108 data: JSON.stringify({'data': [filename, host]}),
1109 responseType: 'arraybuffer'
1110 })
1111 .then(function(response) {
1112 return response.data;
1113 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001114 },
1115 getBMCEthernetInfo: function() {
1116 var deferred = $q.defer();
1117 $http({
1118 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001119 url: DataService.getHost() +
1120 '/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc/ethernet',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001121 headers: {
1122 'Accept': 'application/json',
1123 'Content-Type': 'application/json'
1124 },
1125 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001126 })
1127 .then(
1128 function(response) {
1129 var json = JSON.stringify(response.data);
1130 var content = JSON.parse(json);
1131 deferred.resolve(content.data);
1132 },
1133 function(error) {
1134 console.log(error);
1135 deferred.reject(error);
1136 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001137
1138 return deferred.promise;
1139 },
1140 getBMCInfo: function(callback) {
1141 var deferred = $q.defer();
1142 $http({
1143 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001144 url: DataService.getHost() +
1145 '/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001146 headers: {
1147 'Accept': 'application/json',
1148 'Content-Type': 'application/json'
1149 },
1150 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001151 })
1152 .then(
1153 function(response) {
1154 var json = JSON.stringify(response.data);
1155 var content = JSON.parse(json);
1156 deferred.resolve(content.data);
1157 },
1158 function(error) {
1159 console.log(error);
1160 deferred.reject(error);
1161 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001162 return deferred.promise;
1163 },
1164 getServerInfo: function() {
1165 // TODO: openbmc/openbmc#3117 Need a way via REST to get
1166 // interfaces so we can get the system object(s) by the looking
1167 // for the system interface.
1168 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001169 method: 'GET',
1170 url: DataService.getHost() +
1171 '/xyz/openbmc_project/inventory/system',
1172 headers: {
1173 'Accept': 'application/json',
1174 'Content-Type': 'application/json'
1175 },
1176 withCredentials: true
1177 })
1178 .then(function(response) {
1179 return response.data;
1180 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001181 },
1182 getBMCTime: function() {
1183 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001184 method: 'GET',
1185 url: DataService.getHost() + '/xyz/openbmc_project/time/bmc',
1186 headers: {
1187 'Accept': 'application/json',
1188 'Content-Type': 'application/json'
1189 },
1190 withCredentials: true
1191 })
1192 .then(function(response) {
1193 return response.data;
1194 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001195 },
1196 getHardwares: function(callback) {
1197 $http({
1198 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001199 url: DataService.getHost() +
1200 '/xyz/openbmc_project/inventory/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001201 headers: {
1202 'Accept': 'application/json',
1203 'Content-Type': 'application/json'
1204 },
1205 withCredentials: true
1206 }).then(function(response) {
1207 var json = JSON.stringify(response.data);
1208 var content = JSON.parse(json);
1209 var hardwareData = [];
1210 var keyIndexMap = {};
1211 var title = '';
1212 var data = [];
1213 var searchText = '';
1214 var componentIndex = -1;
1215 var tempParts = [];
1216
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001217 function isSubComponent(key) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001218 for (var i = 0; i < Constants.HARDWARE.parent_components.length;
1219 i++) {
1220 if (key.split(Constants.HARDWARE.parent_components[i]).length ==
1221 2)
1222 return true;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001223 }
1224
1225 return false;
1226 }
1227
1228 function titlelize(title) {
1229 title = title.replace(/([A-Z0-9]+)/g, ' $1').replace(/^\s+/, '');
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001230 for (var i = 0; i < Constants.HARDWARE.uppercase_titles.length;
1231 i++) {
1232 if (title.toLowerCase().indexOf(
1233 (Constants.HARDWARE.uppercase_titles[i] + ' ')) > -1) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001234 return title.toUpperCase();
1235 }
1236 }
1237
1238 return title;
1239 }
1240
1241 function camelcaseToLabel(obj) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001242 var transformed = [], label = '', value = '';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001243 for (var key in obj) {
1244 label = key.replace(/([A-Z0-9]+)/g, ' $1').replace(/^\s+/, '');
1245 if (obj[key] !== '') {
1246 value = obj[key];
1247 if (value == 1 || value == 0) {
1248 value = (value == 1) ? 'Yes' : 'No';
1249 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001250 transformed.push({key: label, value: value});
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001251 }
1252 }
1253
1254 return transformed;
1255 }
1256
1257 function getSearchText(data) {
1258 var searchText = '';
1259 for (var i = 0; i < data.length; i++) {
1260 searchText += ' ' + data[i].key + ' ' + data[i].value;
1261 }
1262
1263 return searchText;
1264 }
1265
1266 for (var key in content.data) {
1267 if (content.data.hasOwnProperty(key) &&
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001268 key.indexOf(Constants.HARDWARE.component_key_filter) == 0) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001269 data = camelcaseToLabel(content.data[key]);
1270 searchText = getSearchText(data);
1271 title = key.split('/').pop();
1272
1273 title = titlelize(title);
1274
1275 if (!isSubComponent(key)) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001276 hardwareData.push(Object.assign(
1277 {
1278 path: key,
1279 title: title,
1280 selected: false,
1281 expanded: false,
1282 search_text: title.toLowerCase() + ' ' +
1283 searchText.toLowerCase(),
1284 sub_components: [],
1285 original_data: {key: key, value: content.data[key]}
1286 },
1287 {items: data}));
Iftekharul Islam2a489552017-11-02 13:23:08 -05001288
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001289 keyIndexMap[key] = hardwareData.length - 1;
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001290 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001291 var tempParts = key.split('/');
1292 tempParts.pop();
1293 tempParts = tempParts.join('/');
1294 componentIndex = keyIndexMap[tempParts];
1295 data = content.data[key];
1296 data.title = title;
1297 hardwareData[componentIndex].sub_components.push(data);
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001298 hardwareData[componentIndex].search_text +=
1299 ' ' + title.toLowerCase();
Iftekharul Islam171c6a12017-08-11 08:35:47 -05001300
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001301 // Sort the subcomponents alphanumeric so they are displayed
1302 // on the inventory page in order (e.g. core 0, core 1, core
1303 // 2, ... core 12, core 13)
1304 hardwareData[componentIndex].sub_components.sort(function(
1305 a, b) {
1306 return a.title.localeCompare(
1307 b.title, 'en', {numeric: true});
Gunnar Mills55a8e392018-03-14 15:37:44 -05001308 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001309 }
1310 }
1311 }
1312
1313 if (callback) {
1314 callback(hardwareData, content.data);
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001315 } else {
1316 return {data: hardwareData, original_data: content.data};
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001317 }
1318 });
1319 },
1320 deleteLogs: function(logs) {
1321 var defer = $q.defer();
1322 var promises = [];
1323
1324 function finished() {
1325 defer.resolve();
1326 }
1327
1328 logs.forEach(function(item) {
1329 promises.push($http({
1330 method: 'POST',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001331 url: DataService.getHost() +
1332 '/xyz/openbmc_project/logging/entry/' + item.Id +
1333 '/action/Delete',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001334 headers: {
1335 'Accept': 'application/json',
1336 'Content-Type': 'application/json'
Gunnar Mills55a8e392018-03-14 15:37:44 -05001337 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001338 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001339 data: JSON.stringify({'data': []})
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001340 }));
1341 });
1342
1343 $q.all(promises).then(finished);
1344
1345 return defer.promise;
1346 },
1347 resolveLogs: function(logs) {
1348 var defer = $q.defer();
1349 var promises = [];
1350
1351 function finished() {
1352 defer.resolve();
1353 }
1354
1355 logs.forEach(function(item) {
1356 promises.push($http({
1357 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001358 url: DataService.getHost() +
1359 '/xyz/openbmc_project/logging/entry/' + item.Id +
1360 '/attr/Resolved',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001361 headers: {
1362 'Accept': 'application/json',
1363 'Content-Type': 'application/json'
Iftekharul Islam99d199f2017-03-24 15:28:25 -05001364 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001365 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001366 data: JSON.stringify({'data': '1'})
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001367 }));
1368 });
Iftekharul Islama1d238f2018-02-26 12:29:45 -06001369
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001370 $q.all(promises).then(finished);
Iftekharul Islamcd789502017-04-19 14:37:55 -05001371
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001372 return defer.promise;
1373 },
1374 getPowerConsumption: function() {
1375 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001376 method: 'GET',
1377 url: DataService.getHost() +
1378 '/xyz/openbmc_project/sensors/power/total_power',
1379 headers: {
1380 'Accept': 'application/json',
1381 'Content-Type': 'application/json'
1382 },
1383 withCredentials: true
1384 })
1385 .then(
1386 function(response) {
1387 var json = JSON.stringify(response.data);
1388 var content = JSON.parse(json);
Iftekharul Islamcd789502017-04-19 14:37:55 -05001389
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001390 return getScaledValue(
1391 content.data.Value, content.data.Scale) +
1392 ' ' +
1393 Constants.POWER_CONSUMPTION_TEXT[content.data.Unit];
1394 },
1395 function(error) {
1396 if ('Not Found' == error.statusText) {
1397 return Constants.POWER_CONSUMPTION_TEXT.notavailable;
1398 } else {
1399 throw error;
1400 }
1401 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001402 },
1403 getPowerCap: function() {
1404 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001405 method: 'GET',
1406 url: DataService.getHost() +
1407 '/xyz/openbmc_project/control/host0/power_cap',
1408 headers: {
1409 'Accept': 'application/json',
1410 'Content-Type': 'application/json'
1411 },
1412 withCredentials: true
1413 })
1414 .then(function(response) {
1415 var json = JSON.stringify(response.data);
1416 var content = JSON.parse(json);
Michael Davisdf3bd122017-08-10 11:03:42 -05001417
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001418 return (false == content.data.PowerCapEnable) ?
1419 Constants.POWER_CAP_TEXT.disabled :
1420 content.data.PowerCap + ' ' + Constants.POWER_CAP_TEXT.unit;
1421 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001422 },
1423 setHostname: function(hostname) {
1424 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001425 method: 'PUT',
1426 url: DataService.getHost() +
1427 '/xyz/openbmc_project/network/config/attr/HostName',
1428 headers: {
1429 'Accept': 'application/json',
1430 'Content-Type': 'application/json'
1431 },
1432 withCredentials: true,
1433 data: JSON.stringify({'data': hostname})
1434 })
1435 .then(function(response) {
1436 return response.data;
1437 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001438 },
1439 };
1440 return SERVICE;
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001441 }
1442 ]);
Iftekharul Islamd2269e22017-05-02 09:32:45 -05001443
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001444})(window.angular);