blob: 71756d4282365f16e0ec1c162360064f20687dee [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 },
59 getHostState: function() {
60 var deferred = $q.defer();
61 $http({
62 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -070063 url: DataService.getHost() +
64 '/xyz/openbmc_project/state/host0/attr/CurrentHostState',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070065 headers: {
66 'Accept': 'application/json',
67 'Content-Type': 'application/json'
68 },
69 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -070070 })
71 .then(
72 function(response) {
73 var json = JSON.stringify(response.data);
74 var content = JSON.parse(json);
75 deferred.resolve(content.data);
76 },
77 function(error) {
78 console.log(error);
79 deferred.reject(error);
80 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070081 return deferred.promise;
82 },
83 getNetworkInfo: function() {
84 var deferred = $q.defer();
85 $http({
86 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -070087 url: DataService.getHost() +
88 '/xyz/openbmc_project/network/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070089 headers: {
90 'Accept': 'application/json',
91 'Content-Type': 'application/json'
92 },
93 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -070094 })
95 .then(
96 function(response) {
97 var json = JSON.stringify(response.data);
98 var content = JSON.parse(json);
99 var hostname = '';
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500100 var defaultgateway = '';
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700101 var macAddress = '';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700102
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700103 function parseNetworkData(content) {
104 var data = {
105 interface_ids: [],
106 interfaces: {},
107 ip_addresses: {ipv4: [], ipv6: []},
108 };
109 var interfaceId = '', keyParts = [], interfaceHash = '',
110 interfaceType = '';
111 for (var key in content.data) {
112 if (key.match(/network\/eth\d+$/ig)) {
113 interfaceId = key.split('/').pop();
114 if (data.interface_ids.indexOf(interfaceId) == -1) {
115 data.interface_ids.push(interfaceId);
116 data.interfaces[interfaceId] = {
117 interfaceIname: '',
118 DomainName: '',
119 MACAddress: '',
120 Nameservers: [],
121 DHCPEnabled: 0,
122 ipv4: {ids: [], values: []},
123 ipv6: {ids: [], values: []}
124 };
125 data.interfaces[interfaceId].MACAddress =
126 content.data[key].MACAddress;
127 data.interfaces[interfaceId].DomainName =
128 content.data[key].DomainName.join(' ');
129 data.interfaces[interfaceId].Nameservers =
130 content.data[key].Nameservers;
131 data.interfaces[interfaceId].DHCPEnabled =
132 content.data[key].DHCPEnabled;
133 }
134 } else if (
135 key.match(
136 /network\/eth\d+\/ipv[4|6]\/[a-z0-9]+$/ig)) {
137 keyParts = key.split('/');
138 interfaceHash = keyParts.pop();
139 interfaceType = keyParts.pop();
140 interfaceId = keyParts.pop();
141
142 if (data.interfaces[interfaceId][interfaceType]
143 .ids.indexOf(interfaceHash) == -1) {
144 data.interfaces[interfaceId][interfaceType]
145 .ids.push(interfaceHash);
146 data.interfaces[interfaceId][interfaceType]
147 .values.push(content.data[key]);
148 data.ip_addresses[interfaceType].push(
149 content.data[key]['Address']);
150 }
151 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700152 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700153 return data;
154 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700155
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700156 if (content.data.hasOwnProperty(
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500157 '/xyz/openbmc_project/network/config')) {
158 if (content.data['/xyz/openbmc_project/network/config']
159 .hasOwnProperty('HostName')) {
160 hostname =
161 content.data['/xyz/openbmc_project/network/config']
162 .HostName;
163 }
164 if (content.data['/xyz/openbmc_project/network/config']
165 .hasOwnProperty('DefaultGateway')) {
166 defaultgateway =
167 content.data['/xyz/openbmc_project/network/config']
168 .DefaultGateway;
169 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700170 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700171
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700172 if (content.data.hasOwnProperty(
173 '/xyz/openbmc_project/network/eth0') &&
174 content.data['/xyz/openbmc_project/network/eth0']
175 .hasOwnProperty('MACAddress')) {
176 macAddress =
177 content.data['/xyz/openbmc_project/network/eth0']
178 .MACAddress;
179 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700180
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700181 deferred.resolve({
182 data: content.data,
183 hostname: hostname,
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500184 defaultgateway: defaultgateway,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700185 mac_address: macAddress,
186 formatted_data: parseNetworkData(content)
187 });
188 },
189 function(error) {
190 console.log(error);
191 deferred.reject(error);
192 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700193 return deferred.promise;
194 },
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500195 setMACAddress: function(interface_name, mac_address) {
196 return $http({
197 method: 'PUT',
198 url: DataService.getHost() +
199 '/xyz/openbmc_project/network/' + interface_name +
200 '/attr/MACAddress',
201 headers: {
202 'Accept': 'application/json',
203 'Content-Type': 'application/json'
204 },
205 withCredentials: true,
206 data: JSON.stringify({'data': mac_address})
207 })
208 .then(function(response) {
209 return response.data;
210 });
211 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700212 getLEDState: function() {
213 var deferred = $q.defer();
214 $http({
215 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700216 url: DataService.getHost() +
217 '/xyz/openbmc_project/led/groups/enclosure_identify',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700218 headers: {
219 'Accept': 'application/json',
220 'Content-Type': 'application/json'
221 },
222 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700223 })
224 .then(
225 function(response) {
226 var json = JSON.stringify(response.data);
227 var content = JSON.parse(json);
228 deferred.resolve(content.data.Asserted);
229 },
230 function(error) {
231 console.log(error);
232 deferred.reject(error);
233 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700234 return deferred.promise;
235 },
236 login: function(username, password, callback) {
237 $http({
238 method: 'POST',
239 url: DataService.getHost() + '/login',
240 headers: {
241 'Accept': 'application/json',
242 'Content-Type': 'application/json'
243 },
244 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700245 data: JSON.stringify({'data': [username, password]})
246 })
247 .then(
248 function(response) {
249 if (callback) {
250 callback(response.data);
251 }
252 },
253 function(error) {
254 if (callback) {
255 if (error && error.status && error.status == 'error') {
256 callback(error);
257 } else {
258 callback(error, true);
259 }
260 }
261 console.log(error);
262 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700263 },
264 testPassword: function(username, password) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700265 // Calls /login without the current session to verify the given
266 // password is correct ignore the interceptor logout on a bad password
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700267 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700268 method: 'POST',
269 url: DataService.getHost() + '/login',
270 headers: {
271 'Accept': 'application/json',
272 'Content-Type': 'application/json'
273 },
274 withCredentials: false,
275 data: JSON.stringify({'data': [username, password]})
276 })
277 .then(function(response) {
278 return response.data;
279 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700280 },
281 logout: function(callback) {
282 $http({
283 method: 'POST',
284 url: DataService.getHost() + '/logout',
285 headers: {
286 'Accept': 'application/json',
287 'Content-Type': 'application/json'
288 },
289 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700290 data: JSON.stringify({'data': []})
291 })
292 .then(
293 function(response) {
294 if (callback) {
295 callback(response.data);
296 }
297 },
298 function(error) {
299 if (callback) {
300 callback(null, error);
301 }
302 console.log(error);
303 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700304 },
305 changePassword: function(user, newPassword) {
306 var deferred = $q.defer();
307 $http({
308 method: 'POST',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700309 url: DataService.getHost() + '/xyz/openbmc_project/user/' + user +
310 '/action/SetPassword',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700311 headers: {
312 'Accept': 'application/json',
313 'Content-Type': 'application/json'
314 },
315 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700316 data: JSON.stringify({'data': [newPassword]}),
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700317 responseType: 'arraybuffer'
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700318 })
319 .then(
320 function(response, status, headers) {
321 deferred.resolve(
322 {data: response, status: status, headers: headers});
323 },
324 function(error) {
325 console.log(error);
326 deferred.reject(error);
327 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700328 return deferred.promise;
329 },
330 chassisPowerOn: function(callback) {
331 $http({
332 method: 'POST',
333 url: DataService.getHost() + '/xyz/openbmc_project/state/host0',
334 headers: {
335 'Accept': 'application/json',
336 'Content-Type': 'application/json'
337 },
338 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700339 data: JSON.stringify({'data': []})
340 })
341 .then(
342 function(response) {
343 var json = JSON.stringify(response.data);
344 var content = JSON.parse(json);
345 if (callback) {
346 return callback(content.data.CurrentPowerState);
347 }
348 },
349 function(error) {
350 if (callback) {
351 callback(error);
352 } else {
353 console.log(error);
354 }
355 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700356 },
357 chassisPowerOff: function() {
358 var deferred = $q.defer();
359 $http({
360 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700361 url: DataService.getHost() +
362 '/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700363 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(
369 {'data': 'xyz.openbmc_project.State.Chassis.Transition.Off'})
370 })
371 .then(
372 function(response) {
373 var json = JSON.stringify(response.data);
374 var content = JSON.parse(json);
375 deferred.resolve(content.status);
376 },
377 function(error) {
378 console.log(error);
379 deferred.reject(error);
380 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700381 return deferred.promise;
382 },
383 setLEDState: function(state, callback) {
384 $http({
385 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700386 url: DataService.getHost() +
387 '/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700388 headers: {
389 'Accept': 'application/json',
390 'Content-Type': 'application/json'
391 },
392 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700393 data: JSON.stringify({'data': state})
394 })
395 .then(
396 function(response) {
397 var json = JSON.stringify(response.data);
398 var content = JSON.parse(json);
399 if (callback) {
400 return callback(content.status);
401 }
402 },
403 function(error) {
404 if (callback) {
405 callback(error);
406 } else {
407 console.log(error);
408 }
409 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700410 },
411 bmcReboot: function(callback) {
412 $http({
413 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700414 url: DataService.getHost() +
415 '/xyz/openbmc_project/state/bmc0/attr/RequestedBmcTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700416 headers: {
417 'Accept': 'application/json',
418 'Content-Type': 'application/json'
419 },
420 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700421 data: JSON.stringify(
422 {'data': 'xyz.openbmc_project.State.BMC.Transition.Reboot'})
423 })
424 .then(
425 function(response) {
426 var json = JSON.stringify(response.data);
427 var content = JSON.parse(json);
428 if (callback) {
429 return callback(content.status);
430 }
431 },
432 function(error) {
433 if (callback) {
434 callback(error);
435 } else {
436 console.log(error);
437 }
438 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700439 },
440 hostPowerOn: function() {
441 var deferred = $q.defer();
442 $http({
443 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700444 url: DataService.getHost() +
445 '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700446 headers: {
447 'Accept': 'application/json',
448 'Content-Type': 'application/json'
449 },
450 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700451 data: JSON.stringify(
452 {'data': 'xyz.openbmc_project.State.Host.Transition.On'})
453 })
454 .then(
455 function(response) {
456 var json = JSON.stringify(response.data);
457 var content = JSON.parse(json);
458 deferred.resolve(content.status);
459 },
460 function(error) {
461 console.log(error);
462 deferred.reject(error);
463 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700464 return deferred.promise;
465 },
466 hostPowerOff: function() {
467 var deferred = $q.defer();
468 $http({
469 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700470 url: DataService.getHost() +
471 '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700472 headers: {
473 'Accept': 'application/json',
474 'Content-Type': 'application/json'
475 },
476 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700477 data: JSON.stringify(
478 {'data': 'xyz.openbmc_project.State.Host.Transition.Off'})
479 })
480 .then(
481 function(response) {
482 var json = JSON.stringify(response.data);
483 var content = JSON.parse(json);
484 deferred.resolve(content.status);
485 },
486 function(error) {
487 console.log(error);
488 deferred.reject(error);
489 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700490 return deferred.promise;
491 },
492 hostReboot: function() {
493 var deferred = $q.defer();
494 $http({
495 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700496 url: DataService.getHost() +
497 '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700498 headers: {
499 'Accept': 'application/json',
500 'Content-Type': 'application/json'
501 },
502 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700503 data: JSON.stringify(
504 {'data': 'xyz.openbmc_project.State.Host.Transition.Reboot'})
505 })
506 .then(
507 function(response) {
508 var json = JSON.stringify(response.data);
509 var content = JSON.parse(json);
510 deferred.resolve(content.status);
511 },
512 function(error) {
513 console.log(error);
514 deferred.reject(error);
515 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700516
517 return deferred.promise;
518 },
519 hostShutdown: function(callback) {
520 $http({
521 method: 'POST',
522 url: DataService.getHost() + '/xyz/openbmc_project/state/host0',
523 headers: {
524 'Accept': 'application/json',
525 'Content-Type': 'application/json'
526 },
527 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700528 data: JSON.stringify({'data': []})
529 })
530 .then(
531 function(response) {
532 var json = JSON.stringify(response.data);
533 var content = JSON.parse(json);
534 if (callback) {
535 return callback(content);
536 }
537 },
538 function(error) {
539 if (callback) {
540 callback(error);
541 } else {
542 console.log(error);
543 }
544 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700545 },
546 getLogs: function() {
547 var deferred = $q.defer();
548 $http({
549 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700550 url: DataService.getHost() +
551 '/xyz/openbmc_project/logging/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700552 headers: {
553 'Accept': 'application/json',
554 'Content-Type': 'application/json'
555 },
556 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700557 })
558 .then(
559 function(response) {
560 var json = JSON.stringify(response.data);
561 var content = JSON.parse(json);
562 var dataClone = JSON.parse(JSON.stringify(content.data));
563 var data = [];
564 var severityCode = '';
565 var priority = '';
566 var health = '';
567 var relatedItems = [];
Matt Spinler845acdc2018-05-01 16:41:28 -0500568 var eventID = 'None';
569 var description = 'None';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700570
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700571 for (var key in content.data) {
572 if (content.data.hasOwnProperty(key) &&
573 content.data[key].hasOwnProperty('Id')) {
574 var severityFlags = {
575 low: false,
576 medium: false,
577 high: false
578 };
579 var healthFlags = {
580 critical: false,
581 warning: false,
582 good: false
583 };
584 severityCode =
585 content.data[key].Severity.split('.').pop();
586 priority =
587 Constants.SEVERITY_TO_PRIORITY_MAP[severityCode];
588 severityFlags[priority.toLowerCase()] = true;
589 health = Constants.SEVERITY_TO_HEALTH_MAP[severityCode];
590 healthFlags[health.toLowerCase()] = true;
591 relatedItems = [];
592 content.data[key].associations.forEach(function(item) {
593 relatedItems.push(item[2]);
594 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700595
Matt Spinler845acdc2018-05-01 16:41:28 -0500596 if (content.data[key].hasOwnProperty(['EventID'])) {
597 eventID = content.data[key].EventID;
598 }
599
600 if (content.data[key].hasOwnProperty(['Description'])) {
601 description = content.data[key].Description;
602 }
603
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700604 data.push(Object.assign(
605 {
606 path: key,
607 copied: false,
608 priority: priority,
609 severity_code: severityCode,
610 severity_flags: severityFlags,
611 health_flags: healthFlags,
612 additional_data:
613 content.data[key].AdditionalData.join('\n'),
614 type: content.data[key].Message,
615 selected: false,
616 search_text:
617 ('#' + content.data[key].Id + ' ' +
618 severityCode + ' ' +
619 content.data[key].Severity + ' ' +
620 content.data[key].AdditionalData.join(' '))
621 .toLowerCase(),
622 meta: false,
623 confirm: false,
624 related_items: relatedItems,
Matt Spinler845acdc2018-05-01 16:41:28 -0500625 eventID: eventID,
626 description: description,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700627 data: {key: key, value: content.data[key]}
628 },
629 content.data[key]));
630 }
631 }
632 deferred.resolve({data: data, original: dataClone});
633 },
634 function(error) {
635 console.log(error);
636 deferred.reject(error);
637 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700638
639 return deferred.promise;
640 },
641 getAllSensorStatus: function(callback) {
642 $http({
643 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700644 url: DataService.getHost() +
645 '/xyz/openbmc_project/sensors/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700646 headers: {
647 'Accept': 'application/json',
648 'Content-Type': 'application/json'
649 },
650 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700651 })
652 .then(
653 function(response) {
654 var json = JSON.stringify(response.data);
655 var content = JSON.parse(json);
656 var dataClone = JSON.parse(JSON.stringify(content.data));
657 var sensorData = [];
658 var severity = {};
659 var title = '';
660 var tempKeyParts = [];
661 var order = 0;
662 var customOrder = 0;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700663
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700664 function getSensorStatus(reading) {
665 var severityFlags = {
666 critical: false,
667 warning: false,
668 normal: false
669 },
670 severityText = '', order = 0;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700671
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700672 if (reading.hasOwnProperty('CriticalLow') &&
673 reading.Value < reading.CriticalLow) {
674 severityFlags.critical = true;
675 severityText = 'critical';
676 order = 2;
677 } else if (
678 reading.hasOwnProperty('CriticalHigh') &&
679 reading.Value > reading.CriticalHigh) {
680 severityFlags.critical = true;
681 severityText = 'critical';
682 order = 2;
683 } else if (
684 reading.hasOwnProperty('CriticalLow') &&
685 reading.hasOwnProperty('WarningLow') &&
686 reading.Value >= reading.CriticalLow &&
687 reading.Value <= reading.WarningLow) {
688 severityFlags.warning = true;
689 severityText = 'warning';
690 order = 1;
691 } else if (
692 reading.hasOwnProperty('WarningHigh') &&
693 reading.hasOwnProperty('CriticalHigh') &&
694 reading.Value >= reading.WarningHigh &&
695 reading.Value <= reading.CriticalHigh) {
696 severityFlags.warning = true;
697 severityText = 'warning';
698 order = 1;
699 } else {
700 severityFlags.normal = true;
701 severityText = 'normal';
702 }
703 return {
704 flags: severityFlags,
705 severityText: severityText,
706 order: order
707 };
708 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700709
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700710 for (var key in content.data) {
711 if (content.data.hasOwnProperty(key) &&
712 content.data[key].hasOwnProperty('Unit')) {
713 severity = getSensorStatus(content.data[key]);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700714
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700715 if (!content.data[key].hasOwnProperty('CriticalLow')) {
716 content.data[key].CriticalLow = '--';
717 content.data[key].CriticalHigh = '--';
718 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700719
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700720 if (!content.data[key].hasOwnProperty('WarningLow')) {
721 content.data[key].WarningLow = '--';
722 content.data[key].WarningHigh = '--';
723 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700724
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700725 tempKeyParts = key.split('/');
726 title = tempKeyParts.pop();
727 title = tempKeyParts.pop() + '_' + title;
728 title = title.split('_')
729 .map(function(item) {
730 return item.toLowerCase()
731 .charAt(0)
732 .toUpperCase() +
733 item.slice(1);
734 })
735 .reduce(function(prev, el) {
736 return prev + ' ' + el;
737 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700738
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700739 content.data[key].Value = getScaledValue(
740 content.data[key].Value, content.data[key].Scale);
741 content.data[key].CriticalLow = getScaledValue(
742 content.data[key].CriticalLow,
743 content.data[key].Scale);
744 content.data[key].CriticalHigh = getScaledValue(
745 content.data[key].CriticalHigh,
746 content.data[key].Scale);
747 content.data[key].WarningLow = getScaledValue(
748 content.data[key].WarningLow,
749 content.data[key].Scale);
750 content.data[key].WarningHigh = getScaledValue(
751 content.data[key].WarningHigh,
752 content.data[key].Scale);
753 if (Constants.SENSOR_SORT_ORDER.indexOf(
754 content.data[key].Unit) > -1) {
755 customOrder = Constants.SENSOR_SORT_ORDER.indexOf(
756 content.data[key].Unit);
757 } else {
758 customOrder = Constants.SENSOR_SORT_ORDER_DEFAULT;
759 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700760
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700761 sensorData.push(Object.assign(
762 {
763 path: key,
764 selected: false,
765 confirm: false,
766 copied: false,
767 title: title,
768 unit:
769 Constants
770 .SENSOR_UNIT_MAP[content.data[key].Unit],
771 severity_flags: severity.flags,
772 status: severity.severityText,
773 order: severity.order,
774 custom_order: customOrder,
775 search_text:
776 (title + ' ' + content.data[key].Value + ' ' +
777 Constants.SENSOR_UNIT_MAP[content.data[key]
778 .Unit] +
779 ' ' + severity.severityText + ' ' +
780 content.data[key].CriticalLow + ' ' +
781 content.data[key].CriticalHigh + ' ' +
782 content.data[key].WarningLow + ' ' +
783 content.data[key].WarningHigh + ' ')
784 .toLowerCase(),
785 original_data:
786 {key: key, value: content.data[key]}
787 },
788 content.data[key]));
789 }
790 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700791
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700792 callback(sensorData, dataClone);
793 },
794 function(error) {
795 console.log(error);
796 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700797 },
798 getActivation: function(imageId) {
799 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700800 method: 'GET',
801 url: DataService.getHost() +
802 '/xyz/openbmc_project/software/' + imageId +
803 '/attr/Activation',
804 headers: {
805 'Accept': 'application/json',
806 'Content-Type': 'application/json'
807 },
808 withCredentials: true
809 })
810 .then(function(response) {
811 return response.data;
812 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700813 },
814 getFirmwares: function() {
815 var deferred = $q.defer();
816 $http({
817 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700818 url: DataService.getHost() +
819 '/xyz/openbmc_project/software/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700820 headers: {
821 'Accept': 'application/json',
822 'Content-Type': 'application/json'
823 },
824 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700825 })
826 .then(
827 function(response) {
828 var json = JSON.stringify(response.data);
829 var content = JSON.parse(json);
830 var data = [];
831 var activationStatus = '';
832 var isExtended = false;
833 var bmcActiveVersion = '';
834 var hostActiveVersion = '';
835 var imageType = '';
836 var extendedVersions = [];
837 var functionalImages = [];
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700838
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700839 function getFormatedExtendedVersions(extendedVersion) {
840 var versions = [];
841 extendedVersion = extendedVersion.split(',');
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700842
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700843 extendedVersion.forEach(function(item) {
844 var parts = item.split('-');
845 var numberIndex = 0;
846 for (var i = 0; i < parts.length; i++) {
847 if (/[0-9]/.test(parts[i])) {
848 numberIndex = i;
849 break;
850 }
851 }
852 var titlePart = parts.splice(0, numberIndex);
853 titlePart = titlePart.join('');
854 titlePart = titlePart[0].toUpperCase() +
855 titlePart.substr(1, titlePart.length);
856 var versionPart = parts.join('-');
857 versions.push({title: titlePart, version: versionPart});
858 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700859
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700860 return versions;
861 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700862
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700863 // Get the list of functional images so we can compare
864 // later if an image is functional
865 if (content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH]) {
866 functionalImages =
867 content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH]
868 .endpoints;
869 }
870 for (var key in content.data) {
871 if (content.data.hasOwnProperty(key) &&
872 content.data[key].hasOwnProperty('Version')) {
873 // If the image is "Functional" use that for the
874 // activation status, else use the value of "Activation"
875 // github.com/openbmc/phosphor-dbus-interfaces/blob/master/xyz/openbmc_project/Software/Activation.interface.yaml
876 activationStatus =
877 content.data[key].Activation.split('.').pop();
878 if (functionalImages.includes(key)) {
879 activationStatus = 'Functional';
880 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700881
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700882 imageType = content.data[key].Purpose.split('.').pop();
883 isExtended = content.data[key].hasOwnProperty(
884 'ExtendedVersion') &&
885 content.data[key].ExtendedVersion != '';
886 if (isExtended) {
887 extendedVersions = getFormatedExtendedVersions(
888 content.data[key].ExtendedVersion);
889 }
890 data.push(Object.assign(
891 {
892 path: key,
893 activationStatus: activationStatus,
894 imageId: key.split('/').pop(),
895 imageType: imageType,
896 isExtended: isExtended,
897 extended:
898 {show: false, versions: extendedVersions},
899 data: {key: key, value: content.data[key]}
900 },
901 content.data[key]));
902
903 if (activationStatus == 'Functional' &&
904 imageType == 'BMC') {
905 bmcActiveVersion = content.data[key].Version;
906 }
907
908 if (activationStatus == 'Functional' &&
909 imageType == 'Host') {
910 hostActiveVersion = content.data[key].Version;
911 }
912 }
913 }
914
915 deferred.resolve({
916 data: data,
917 bmcActiveVersion: bmcActiveVersion,
918 hostActiveVersion: hostActiveVersion
919 });
Iftekharul Islam171c6a12017-08-11 08:35:47 -0500920 },
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700921 function(error) {
922 console.log(error);
923 deferred.reject(error);
924 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700925
926 return deferred.promise;
927 },
928 changePriority: function(imageId, priority) {
929 var deferred = $q.defer();
930 $http({
931 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700932 url: DataService.getHost() + '/xyz/openbmc_project/software/' +
933 imageId + '/attr/Priority',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700934 headers: {
935 'Accept': 'application/json',
936 'Content-Type': 'application/json'
937 },
938 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700939 data: JSON.stringify({'data': priority})
940 })
941 .then(
942 function(response) {
943 var json = JSON.stringify(response.data);
944 var content = JSON.parse(json);
945 deferred.resolve(content);
946 },
947 function(error) {
948 console.log(error);
949 deferred.reject(error);
950 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700951
952 return deferred.promise;
953 },
954 deleteImage: function(imageId) {
955 var deferred = $q.defer();
956 $http({
957 method: 'POST',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700958 url: DataService.getHost() + '/xyz/openbmc_project/software/' +
959 imageId + '/action/Delete',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700960 headers: {
961 'Accept': 'application/json',
962 'Content-Type': 'application/json'
963 },
964 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700965 data: JSON.stringify({'data': []})
966 })
967 .then(
968 function(response) {
969 var json = JSON.stringify(response.data);
970 var content = JSON.parse(json);
971 deferred.resolve(content);
972 },
973 function(error) {
974 console.log(error);
975 deferred.reject(error);
976 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700977
978 return deferred.promise;
979 },
980 activateImage: function(imageId) {
981 var deferred = $q.defer();
982 $http({
983 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700984 url: DataService.getHost() + '/xyz/openbmc_project/software/' +
985 imageId + '/attr/RequestedActivation',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700986 headers: {
987 'Accept': 'application/json',
988 'Content-Type': 'application/json'
989 },
990 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700991 data:
992 JSON.stringify({'data': Constants.FIRMWARE.ACTIVATE_FIRMWARE})
993 })
994 .then(
995 function(response) {
996 var json = JSON.stringify(response.data);
997 var content = JSON.parse(json);
998 deferred.resolve(content);
999 },
1000 function(error) {
1001 console.log(error);
1002 deferred.reject(error);
1003 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001004
1005 return deferred.promise;
1006 },
1007 uploadImage: function(file) {
1008 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001009 method: 'POST',
1010 timeout: 5 * 60 * 1000,
1011 url: DataService.getHost() + '/upload/image',
1012 headers: {'Content-Type': 'application/octet-stream'},
1013 withCredentials: true,
1014 data: file
1015 })
1016 .then(function(response) {
1017 return response.data;
1018 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001019 },
1020 downloadImage: function(host, filename) {
1021 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001022 method: 'POST',
1023 url: DataService.getHost() +
1024 '/xyz/openbmc_project/software/action/DownloadViaTFTP',
1025 headers: {
1026 'Accept': 'application/json',
1027 'Content-Type': 'application/json'
1028 },
1029 withCredentials: true,
1030 data: JSON.stringify({'data': [filename, host]}),
1031 responseType: 'arraybuffer'
1032 })
1033 .then(function(response) {
1034 return response.data;
1035 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001036 },
1037 getBMCEthernetInfo: function() {
1038 var deferred = $q.defer();
1039 $http({
1040 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001041 url: DataService.getHost() +
1042 '/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc/ethernet',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001043 headers: {
1044 'Accept': 'application/json',
1045 'Content-Type': 'application/json'
1046 },
1047 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001048 })
1049 .then(
1050 function(response) {
1051 var json = JSON.stringify(response.data);
1052 var content = JSON.parse(json);
1053 deferred.resolve(content.data);
1054 },
1055 function(error) {
1056 console.log(error);
1057 deferred.reject(error);
1058 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001059
1060 return deferred.promise;
1061 },
1062 getBMCInfo: function(callback) {
1063 var deferred = $q.defer();
1064 $http({
1065 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001066 url: DataService.getHost() +
1067 '/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001068 headers: {
1069 'Accept': 'application/json',
1070 'Content-Type': 'application/json'
1071 },
1072 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001073 })
1074 .then(
1075 function(response) {
1076 var json = JSON.stringify(response.data);
1077 var content = JSON.parse(json);
1078 deferred.resolve(content.data);
1079 },
1080 function(error) {
1081 console.log(error);
1082 deferred.reject(error);
1083 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001084 return deferred.promise;
1085 },
1086 getServerInfo: function() {
1087 // TODO: openbmc/openbmc#3117 Need a way via REST to get
1088 // interfaces so we can get the system object(s) by the looking
1089 // for the system interface.
1090 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001091 method: 'GET',
1092 url: DataService.getHost() +
1093 '/xyz/openbmc_project/inventory/system',
1094 headers: {
1095 'Accept': 'application/json',
1096 'Content-Type': 'application/json'
1097 },
1098 withCredentials: true
1099 })
1100 .then(function(response) {
1101 return response.data;
1102 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001103 },
1104 getBMCTime: function() {
1105 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001106 method: 'GET',
1107 url: DataService.getHost() + '/xyz/openbmc_project/time/bmc',
1108 headers: {
1109 'Accept': 'application/json',
1110 'Content-Type': 'application/json'
1111 },
1112 withCredentials: true
1113 })
1114 .then(function(response) {
1115 return response.data;
1116 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001117 },
1118 getHardwares: function(callback) {
1119 $http({
1120 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001121 url: DataService.getHost() +
1122 '/xyz/openbmc_project/inventory/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001123 headers: {
1124 'Accept': 'application/json',
1125 'Content-Type': 'application/json'
1126 },
1127 withCredentials: true
1128 }).then(function(response) {
1129 var json = JSON.stringify(response.data);
1130 var content = JSON.parse(json);
1131 var hardwareData = [];
1132 var keyIndexMap = {};
1133 var title = '';
1134 var data = [];
1135 var searchText = '';
1136 var componentIndex = -1;
1137 var tempParts = [];
1138
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001139 function isSubComponent(key) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001140 for (var i = 0; i < Constants.HARDWARE.parent_components.length;
1141 i++) {
1142 if (key.split(Constants.HARDWARE.parent_components[i]).length ==
1143 2)
1144 return true;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001145 }
1146
1147 return false;
1148 }
1149
1150 function titlelize(title) {
1151 title = title.replace(/([A-Z0-9]+)/g, ' $1').replace(/^\s+/, '');
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001152 for (var i = 0; i < Constants.HARDWARE.uppercase_titles.length;
1153 i++) {
1154 if (title.toLowerCase().indexOf(
1155 (Constants.HARDWARE.uppercase_titles[i] + ' ')) > -1) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001156 return title.toUpperCase();
1157 }
1158 }
1159
1160 return title;
1161 }
1162
1163 function camelcaseToLabel(obj) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001164 var transformed = [], label = '', value = '';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001165 for (var key in obj) {
1166 label = key.replace(/([A-Z0-9]+)/g, ' $1').replace(/^\s+/, '');
1167 if (obj[key] !== '') {
1168 value = obj[key];
1169 if (value == 1 || value == 0) {
1170 value = (value == 1) ? 'Yes' : 'No';
1171 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001172 transformed.push({key: label, value: value});
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001173 }
1174 }
1175
1176 return transformed;
1177 }
1178
1179 function getSearchText(data) {
1180 var searchText = '';
1181 for (var i = 0; i < data.length; i++) {
1182 searchText += ' ' + data[i].key + ' ' + data[i].value;
1183 }
1184
1185 return searchText;
1186 }
1187
1188 for (var key in content.data) {
1189 if (content.data.hasOwnProperty(key) &&
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001190 key.indexOf(Constants.HARDWARE.component_key_filter) == 0) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001191 data = camelcaseToLabel(content.data[key]);
1192 searchText = getSearchText(data);
1193 title = key.split('/').pop();
1194
1195 title = titlelize(title);
1196
1197 if (!isSubComponent(key)) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001198 hardwareData.push(Object.assign(
1199 {
1200 path: key,
1201 title: title,
1202 selected: false,
1203 expanded: false,
1204 search_text: title.toLowerCase() + ' ' +
1205 searchText.toLowerCase(),
1206 sub_components: [],
1207 original_data: {key: key, value: content.data[key]}
1208 },
1209 {items: data}));
Iftekharul Islam2a489552017-11-02 13:23:08 -05001210
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001211 keyIndexMap[key] = hardwareData.length - 1;
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001212 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001213 var tempParts = key.split('/');
1214 tempParts.pop();
1215 tempParts = tempParts.join('/');
1216 componentIndex = keyIndexMap[tempParts];
1217 data = content.data[key];
1218 data.title = title;
1219 hardwareData[componentIndex].sub_components.push(data);
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001220 hardwareData[componentIndex].search_text +=
1221 ' ' + title.toLowerCase();
Iftekharul Islam171c6a12017-08-11 08:35:47 -05001222
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001223 // Sort the subcomponents alphanumeric so they are displayed
1224 // on the inventory page in order (e.g. core 0, core 1, core
1225 // 2, ... core 12, core 13)
1226 hardwareData[componentIndex].sub_components.sort(function(
1227 a, b) {
1228 return a.title.localeCompare(
1229 b.title, 'en', {numeric: true});
Gunnar Mills55a8e392018-03-14 15:37:44 -05001230 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001231 }
1232 }
1233 }
1234
1235 if (callback) {
1236 callback(hardwareData, content.data);
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001237 } else {
1238 return {data: hardwareData, original_data: content.data};
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001239 }
1240 });
1241 },
1242 deleteLogs: function(logs) {
1243 var defer = $q.defer();
1244 var promises = [];
1245
1246 function finished() {
1247 defer.resolve();
1248 }
1249
1250 logs.forEach(function(item) {
1251 promises.push($http({
1252 method: 'POST',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001253 url: DataService.getHost() +
1254 '/xyz/openbmc_project/logging/entry/' + item.Id +
1255 '/action/Delete',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001256 headers: {
1257 'Accept': 'application/json',
1258 'Content-Type': 'application/json'
Gunnar Mills55a8e392018-03-14 15:37:44 -05001259 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001260 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001261 data: JSON.stringify({'data': []})
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001262 }));
1263 });
1264
1265 $q.all(promises).then(finished);
1266
1267 return defer.promise;
1268 },
1269 resolveLogs: function(logs) {
1270 var defer = $q.defer();
1271 var promises = [];
1272
1273 function finished() {
1274 defer.resolve();
1275 }
1276
1277 logs.forEach(function(item) {
1278 promises.push($http({
1279 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001280 url: DataService.getHost() +
1281 '/xyz/openbmc_project/logging/entry/' + item.Id +
1282 '/attr/Resolved',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001283 headers: {
1284 'Accept': 'application/json',
1285 'Content-Type': 'application/json'
Iftekharul Islam99d199f2017-03-24 15:28:25 -05001286 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001287 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001288 data: JSON.stringify({'data': '1'})
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001289 }));
1290 });
Iftekharul Islama1d238f2018-02-26 12:29:45 -06001291
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001292 $q.all(promises).then(finished);
Iftekharul Islamcd789502017-04-19 14:37:55 -05001293
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001294 return defer.promise;
1295 },
1296 getPowerConsumption: function() {
1297 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001298 method: 'GET',
1299 url: DataService.getHost() +
1300 '/xyz/openbmc_project/sensors/power/total_power',
1301 headers: {
1302 'Accept': 'application/json',
1303 'Content-Type': 'application/json'
1304 },
1305 withCredentials: true
1306 })
1307 .then(
1308 function(response) {
1309 var json = JSON.stringify(response.data);
1310 var content = JSON.parse(json);
Iftekharul Islamcd789502017-04-19 14:37:55 -05001311
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001312 return getScaledValue(
1313 content.data.Value, content.data.Scale) +
1314 ' ' +
1315 Constants.POWER_CONSUMPTION_TEXT[content.data.Unit];
1316 },
1317 function(error) {
1318 if ('Not Found' == error.statusText) {
1319 return Constants.POWER_CONSUMPTION_TEXT.notavailable;
1320 } else {
1321 throw error;
1322 }
1323 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001324 },
1325 getPowerCap: function() {
1326 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001327 method: 'GET',
1328 url: DataService.getHost() +
1329 '/xyz/openbmc_project/control/host0/power_cap',
1330 headers: {
1331 'Accept': 'application/json',
1332 'Content-Type': 'application/json'
1333 },
1334 withCredentials: true
1335 })
1336 .then(function(response) {
1337 var json = JSON.stringify(response.data);
1338 var content = JSON.parse(json);
Michael Davisdf3bd122017-08-10 11:03:42 -05001339
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001340 return (false == content.data.PowerCapEnable) ?
1341 Constants.POWER_CAP_TEXT.disabled :
1342 content.data.PowerCap + ' ' + Constants.POWER_CAP_TEXT.unit;
1343 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001344 },
1345 setHostname: function(hostname) {
1346 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001347 method: 'PUT',
1348 url: DataService.getHost() +
1349 '/xyz/openbmc_project/network/config/attr/HostName',
1350 headers: {
1351 'Accept': 'application/json',
1352 'Content-Type': 'application/json'
1353 },
1354 withCredentials: true,
1355 data: JSON.stringify({'data': hostname})
1356 })
1357 .then(function(response) {
1358 return response.data;
1359 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001360 },
1361 };
1362 return SERVICE;
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001363 }
1364 ]);
Iftekharul Islamd2269e22017-05-02 09:32:45 -05001365
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001366})(window.angular);