blob: 0b1aa46f096a35c5c8e66f80af3fd36af4f2c6e5 [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', [
beccabroekc3abaa92018-08-14 13:47:18 -050012 '$http', 'Constants', '$q', 'dataService', '$interval',
13 function($http, Constants, $q, DataService, $interval) {
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,
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070029 HOST_STATE_TEXT: Constants.HOST_STATE,
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070030 LED_STATE: Constants.LED_STATE,
31 LED_STATE_TEXT: Constants.LED_STATE_TEXT,
32 HOST_SESSION_STORAGE_KEY: Constants.API_CREDENTIALS.host_storage_key,
Gunnar Mills65491142018-06-04 14:23:33 -050033 validIPV4IP: function(ip) {
34 // Checks for [0-255].[0-255].[0-255].[0-255]
35 return ip.match(
36 /\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/);
37 },
AppaRao Pulib1289ec2018-11-14 20:33:30 +053038 getRedfishSysName: function() {
39 return $http({
40 method: 'GET',
41 url: DataService.getHost() + '/redfish/v1/Systems',
42 withCredentials: true
43 })
44 .then(
45 function(response) {
46 var sysUrl = response.data['Members'][0]['@odata.id'];
47 return sysUrl.split('/').pop(-1);
48 },
49 function(error) {
50 console.log(JSON.stringify(error));
51 });
52 },
53 getSystemLogs: function(recordType) {
54 var uri = '/redfish/v1/Systems/' + DataService.systemName +
55 '/LogServices/EventLog/Entries';
56 return $http({
57 method: 'GET',
58 url: DataService.getHost() + uri,
59 withCredentials: true
60 })
61 .then(
62 function(response) {
63 var logEntries = [];
64 angular.forEach(response.data['Members'], function(log) {
65 if (log.hasOwnProperty('EntryType')) {
66 if (log['EntryType'] == recordType) {
67 logEntries.push(log);
68 }
69 }
70 });
71 return logEntries;
72 },
73 function(error) {
74 console.log(JSON.stringify(error));
75 });
76 },
77 clearSystemLogs: function() {
78 var uri = '/redfish/v1/Systems/' + DataService.systemName +
79 '/LogServices/EventLog/Actions/LogService.ClearLog';
80 return $http({
81 method: 'POST',
82 url: DataService.getHost() + uri,
83 withCredentials: true
84 });
85 },
Gunnar Mills854fbba2018-09-13 15:41:30 -050086 deleteObject: function(path) {
87 return $http({
88 method: 'POST',
89 url: DataService.getHost() + path + '/action/Delete',
90 withCredentials: true,
91 data: JSON.stringify({'data': []})
92 })
93 .then(function(response) {
94 return response.data;
95 });
96 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070097 getHostState: function() {
98 var deferred = $q.defer();
99 $http({
100 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700101 url: DataService.getHost() +
102 '/xyz/openbmc_project/state/host0/attr/CurrentHostState',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700103 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700104 })
105 .then(
106 function(response) {
107 var json = JSON.stringify(response.data);
108 var content = JSON.parse(json);
109 deferred.resolve(content.data);
110 },
111 function(error) {
112 console.log(error);
113 deferred.reject(error);
114 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700115 return deferred.promise;
116 },
Gunnar Millsff64c542018-08-24 15:49:09 -0500117 getSNMPManagers: function() {
118 return $http({
119 method: 'GET',
120 url: DataService.getHost() +
121 '/xyz/openbmc_project/network/snmp/manager/enumerate',
Gunnar Millsff64c542018-08-24 15:49:09 -0500122 withCredentials: true
123 })
124 .then(function(response) {
125 return response.data;
126 });
127 },
beccabroekc3abaa92018-08-14 13:47:18 -0500128 pollHostStatusTillOn: function() {
129 var deferred = $q.defer();
130 var hostOnTimeout = setTimeout(function() {
131 ws.close();
132 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_ON_TIMEOUT));
133 }, Constants.TIMEOUT.HOST_ON);
134
135 var ws =
136 new WebSocket('wss://' + DataService.server_id + '/subscribe');
137 var data = JSON.stringify({
138 'paths': ['/xyz/openbmc_project/state/host0'],
139 'interfaces': ['xyz.openbmc_project.State.Host']
140 });
141 ws.onopen = function() {
142 ws.send(data);
143 };
144 ws.onmessage = function(evt) {
145 var content = JSON.parse(evt.data);
146 var hostState = content.properties.CurrentHostState;
147 if (hostState === Constants.HOST_STATE_TEXT.on_code) {
148 clearTimeout(hostOnTimeout);
149 ws.close();
150 deferred.resolve();
151 } else if (hostState === Constants.HOST_STATE_TEXT.error_code) {
152 clearTimeout(hostOnTimeout);
153 ws.close();
154 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_QUIESCED));
155 }
156 };
157 },
158
159 pollHostStatusTilReboot: function() {
160 var deferred = $q.defer();
161 var onState = Constants.HOST_STATE_TEXT.on_code;
162 var offState = Constants.HOST_STATE_TEXT.on_code;
163 var hostTimeout;
164 var setHostTimeout = function(message, timeout) {
165 hostTimeout = setTimeout(function() {
166 ws.close();
167 deferred.reject(new Error(message));
168 }, timeout);
169 };
170 var ws =
171 new WebSocket('wss://' + DataService.server_id + '/subscribe');
172 var data = JSON.stringify({
173 'paths': ['/xyz/openbmc_project/state/host0'],
174 'interfaces': ['xyz.openbmc_project.State.Host']
175 });
176 ws.onopen = function() {
177 ws.send(data);
178 };
179 setHostTimeout(
180 Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT,
181 Constants.TIMEOUT.HOST_OFF);
182 var pollState = offState;
183 ws.onmessage = function(evt) {
184 var content = JSON.parse(evt.data);
185 var hostState = content.properties.CurrentHostState;
186 if (hostState === pollState) {
187 if (pollState === offState) {
188 clearTimeout(hostTimeout);
189 pollState = onState;
190 setHostTimeout(
191 Constants.MESSAGES.POLL.HOST_ON_TIMEOUT,
192 Constants.TIMEOUT.HOST_ON);
193 }
194 if (pollState === onState) {
195 clearTimeout(hostTimeout);
196 ws.close();
197 deferred.resolve();
198 }
199 } else if (hostState === Constants.HOST_STATE_TEXT.error_code) {
200 clearTimeout(hostTimeout);
201 ws.close();
202 deferred.reject(new Error(Constants.MESSAGES.POLL.HOST_QUIESCED));
203 }
204 };
205 },
206
207 pollHostStatusTillOff: function() {
208 var deferred = $q.defer();
209 var hostOffTimeout = setTimeout(function() {
210 ws.close();
211 deferred.reject(
212 new Error(Constants.MESSAGES.POLL.HOST_OFF_TIMEOUT));
213 }, Constants.TIMEOUT.HOST_OFF);
214
215 var ws =
216 new WebSocket('wss://' + DataService.server_id + '/subscribe');
217 var data = JSON.stringify({
218 'paths': ['/xyz/openbmc_project/state/host0'],
219 'interfaces': ['xyz.openbmc_project.State.Host']
220 });
221 ws.onopen = function() {
222 ws.send(data);
223 };
224 ws.onmessage = function(evt) {
225 var content = JSON.parse(evt.data);
226 var hostState = content.properties.CurrentHostState;
227 if (hostState === Constants.HOST_STATE_TEXT.off_code) {
228 clearTimeout(hostOffTimeout);
229 ws.close();
230 deferred.resolve();
231 }
232 };
233 },
Gunnar Mills854fbba2018-09-13 15:41:30 -0500234 addSNMPManager: function(address, port) {
235 return $http({
236 method: 'POST',
237 url: DataService.getHost() +
238 '/xyz/openbmc_project/network/snmp/manager/action/Client',
239 withCredentials: true,
240 data: JSON.stringify({'data': [address, +port]})
241 })
242 .then(function(response) {
243 return response.data;
244 });
245 },
246 setSNMPManagerPort: function(snmpManagerPath, port) {
247 return $http({
248 method: 'PUT',
249 url: DataService.getHost() + snmpManagerPath + '/attr/Port',
250 withCredentials: true,
251 data: JSON.stringify({'data': +port})
252 })
253 .then(function(response) {
254 return response.data;
255 });
256 },
257 setSNMPManagerAddress: function(snmpManagerPath, address) {
258 return $http({
259 method: 'PUT',
260 url: DataService.getHost() + snmpManagerPath +
261 '/attr/Address',
262 withCredentials: true,
263 data: JSON.stringify({'data': address})
264 })
265 .then(function(response) {
266 return response.data;
267 });
268 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700269 getNetworkInfo: function() {
270 var deferred = $q.defer();
271 $http({
272 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700273 url: DataService.getHost() +
274 '/xyz/openbmc_project/network/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700275 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700276 })
277 .then(
278 function(response) {
279 var json = JSON.stringify(response.data);
280 var content = JSON.parse(json);
281 var hostname = '';
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500282 var defaultgateway = '';
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700283 var macAddress = '';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700284
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700285 function parseNetworkData(content) {
286 var data = {
287 interface_ids: [],
288 interfaces: {},
289 ip_addresses: {ipv4: [], ipv6: []},
290 };
291 var interfaceId = '', keyParts = [], interfaceHash = '',
292 interfaceType = '';
293 for (var key in content.data) {
Gunnar Mills3efbe2d2018-06-07 14:47:19 -0500294 if (key.match(/network\/eth\d+(_\d+)?$/ig)) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700295 interfaceId = key.split('/').pop();
296 if (data.interface_ids.indexOf(interfaceId) == -1) {
297 data.interface_ids.push(interfaceId);
298 data.interfaces[interfaceId] = {
299 interfaceIname: '',
300 DomainName: '',
301 MACAddress: '',
302 Nameservers: [],
303 DHCPEnabled: 0,
304 ipv4: {ids: [], values: []},
305 ipv6: {ids: [], values: []}
306 };
307 data.interfaces[interfaceId].MACAddress =
308 content.data[key].MACAddress;
309 data.interfaces[interfaceId].DomainName =
310 content.data[key].DomainName.join(' ');
311 data.interfaces[interfaceId].Nameservers =
312 content.data[key].Nameservers;
313 data.interfaces[interfaceId].DHCPEnabled =
314 content.data[key].DHCPEnabled;
315 }
316 } else if (
317 key.match(
Gunnar Mills3efbe2d2018-06-07 14:47:19 -0500318 /network\/eth\d+(_\d+)?\/ipv[4|6]\/[a-z0-9]+$/ig)) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700319 keyParts = key.split('/');
320 interfaceHash = keyParts.pop();
321 interfaceType = keyParts.pop();
322 interfaceId = keyParts.pop();
323
324 if (data.interfaces[interfaceId][interfaceType]
325 .ids.indexOf(interfaceHash) == -1) {
326 data.interfaces[interfaceId][interfaceType]
327 .ids.push(interfaceHash);
328 data.interfaces[interfaceId][interfaceType]
329 .values.push(content.data[key]);
330 data.ip_addresses[interfaceType].push(
331 content.data[key]['Address']);
332 }
333 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700334 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700335 return data;
336 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700337
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700338 if (content.data.hasOwnProperty(
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500339 '/xyz/openbmc_project/network/config')) {
340 if (content.data['/xyz/openbmc_project/network/config']
341 .hasOwnProperty('HostName')) {
342 hostname =
343 content.data['/xyz/openbmc_project/network/config']
344 .HostName;
345 }
346 if (content.data['/xyz/openbmc_project/network/config']
347 .hasOwnProperty('DefaultGateway')) {
348 defaultgateway =
349 content.data['/xyz/openbmc_project/network/config']
350 .DefaultGateway;
351 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700352 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700353
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700354 if (content.data.hasOwnProperty(
355 '/xyz/openbmc_project/network/eth0') &&
356 content.data['/xyz/openbmc_project/network/eth0']
357 .hasOwnProperty('MACAddress')) {
358 macAddress =
359 content.data['/xyz/openbmc_project/network/eth0']
360 .MACAddress;
361 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700362
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700363 deferred.resolve({
364 data: content.data,
365 hostname: hostname,
Gunnar Millse9f5fe72018-05-04 13:43:10 -0500366 defaultgateway: defaultgateway,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700367 mac_address: macAddress,
368 formatted_data: parseNetworkData(content)
369 });
370 },
371 function(error) {
372 console.log(error);
373 deferred.reject(error);
374 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700375 return deferred.promise;
376 },
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500377 setMACAddress: function(interface_name, mac_address) {
378 return $http({
379 method: 'PUT',
380 url: DataService.getHost() +
381 '/xyz/openbmc_project/network/' + interface_name +
382 '/attr/MACAddress',
Gunnar Mills7ddc7272018-04-12 16:12:03 -0500383 withCredentials: true,
384 data: JSON.stringify({'data': mac_address})
385 })
386 .then(function(response) {
387 return response.data;
388 });
389 },
Gunnar Millsdca79d72018-05-30 13:07:01 -0500390 setDefaultGateway: function(defaultGateway) {
391 return $http({
392 method: 'PUT',
393 url: DataService.getHost() +
394 '/xyz/openbmc_project/network/config/attr/DefaultGateway',
Gunnar Millsdca79d72018-05-30 13:07:01 -0500395 withCredentials: true,
396 data: JSON.stringify({'data': defaultGateway})
397 })
398 .then(function(response) {
399 return response.data;
400 });
401 },
Gunnar Millscb2c3062018-05-31 13:13:30 -0500402 setDHCPEnabled: function(interfaceName, dhcpEnabled) {
403 return $http({
404 method: 'PUT',
405 url: DataService.getHost() +
406 '/xyz/openbmc_project/network/' + interfaceName +
407 '/attr/DHCPEnabled',
Gunnar Millscb2c3062018-05-31 13:13:30 -0500408 withCredentials: true,
409 data: JSON.stringify({'data': dhcpEnabled})
410 })
411 .then(function(response) {
412 return response.data;
413 });
414 },
Gunnar Mills06467822018-06-06 15:43:18 -0500415 setNameservers: function(interfaceName, dnsServers) {
416 return $http({
417 method: 'PUT',
418 url: DataService.getHost() +
419 '/xyz/openbmc_project/network/' + interfaceName +
420 '/attr/Nameservers',
Gunnar Mills06467822018-06-06 15:43:18 -0500421 withCredentials: true,
422 data: JSON.stringify({'data': dnsServers})
423 })
424 .then(function(response) {
425 return response.data;
426 });
427 },
Gunnar Millsa45c3852018-05-30 16:18:45 -0500428 deleteIPV4: function(interfaceName, networkID) {
429 return $http({
430 method: 'POST',
431 url: DataService.getHost() +
432 '/xyz/openbmc_project/network/' + interfaceName +
433 '/ipv4/' + networkID + '/action/Delete',
Gunnar Millsa45c3852018-05-30 16:18:45 -0500434 withCredentials: true,
435 data: JSON.stringify({'data': []})
436 })
437 .then(function(response) {
438 return response.data;
439 });
440 },
441 addIPV4: function(
442 interfaceName, ipAddress, netmaskPrefixLength, gateway) {
443 return $http({
444 method: 'POST',
445 url: DataService.getHost() +
446 '/xyz/openbmc_project/network/' + interfaceName +
447 '/action/IP',
Gunnar Millsa45c3852018-05-30 16:18:45 -0500448 withCredentials: true,
449 data: JSON.stringify({
450 'data': [
451 'xyz.openbmc_project.Network.IP.Protocol.IPv4',
452 ipAddress, +netmaskPrefixLength, gateway
453 ]
454 })
455 })
456 .then(function(response) {
457 return response.data;
458 });
459 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700460 getLEDState: function() {
461 var deferred = $q.defer();
462 $http({
463 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700464 url: DataService.getHost() +
465 '/xyz/openbmc_project/led/groups/enclosure_identify',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700466 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700467 })
468 .then(
469 function(response) {
470 var json = JSON.stringify(response.data);
471 var content = JSON.parse(json);
472 deferred.resolve(content.data.Asserted);
473 },
474 function(error) {
475 console.log(error);
476 deferred.reject(error);
477 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700478 return deferred.promise;
479 },
480 login: function(username, password, callback) {
481 $http({
482 method: 'POST',
483 url: DataService.getHost() + '/login',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700484 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700485 data: JSON.stringify({'data': [username, password]})
486 })
487 .then(
488 function(response) {
489 if (callback) {
490 callback(response.data);
491 }
492 },
493 function(error) {
494 if (callback) {
495 if (error && error.status && error.status == 'error') {
496 callback(error);
497 } else {
498 callback(error, true);
499 }
500 }
501 console.log(error);
502 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700503 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700504 logout: function(callback) {
505 $http({
506 method: 'POST',
507 url: DataService.getHost() + '/logout',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700508 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700509 data: JSON.stringify({'data': []})
510 })
511 .then(
512 function(response) {
513 if (callback) {
514 callback(response.data);
515 }
516 },
517 function(error) {
518 if (callback) {
519 callback(null, error);
520 }
521 console.log(error);
522 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700523 },
AppaRao Pulicf7219c2018-12-27 16:17:46 +0530524 getAccountServiceRoles: function() {
525 var roles = [];
526
Gunnar Mills4693ddb2019-03-27 15:49:40 -0500527 return $http({
528 method: 'GET',
529 url: DataService.getHost() +
530 '/redfish/v1/AccountService/Roles',
531 withCredentials: true
532 })
533 .then(
534 function(response) {
535 var members = response.data['Members'];
536 angular.forEach(members, function(member) {
537 roles.push(member['@odata.id'].split('/').pop());
AppaRao Pulicf7219c2018-12-27 16:17:46 +0530538 });
Gunnar Mills4693ddb2019-03-27 15:49:40 -0500539 return roles;
540 },
541 function(error) {
542 console.log(error);
543 });
AppaRao Pulicf7219c2018-12-27 16:17:46 +0530544 },
545 getAllUserAccounts: function() {
AppaRao Puli28711a62018-10-17 16:07:55 +0530546 var deferred = $q.defer();
547 var promises = [];
548 var users = [];
549
Gunnar Mills4693ddb2019-03-27 15:49:40 -0500550 $http({
551 method: 'GET',
552 url: DataService.getHost() + '/redfish/v1/AccountService/Accounts',
553 withCredentials: true
554 })
555 .then(
556 function(response) {
557 var members = response.data['Members'];
558 angular.forEach(members, function(member) {
559 promises.push(
560 $http({
561 method: 'GET',
562 url: DataService.getHost() + member['@odata.id'],
563 withCredentials: true
564 }).then(function(res) {
565 return res.data;
AppaRao Puli28711a62018-10-17 16:07:55 +0530566 }));
AppaRao Puli28711a62018-10-17 16:07:55 +0530567 });
Gunnar Mills4693ddb2019-03-27 15:49:40 -0500568
569 $q.all(promises).then(
570 function(results) {
571 deferred.resolve(results);
572 },
573 function(errors) {
574 deferred.reject(errors);
575 });
576 },
577 function(error) {
578 console.log(error);
579 deferred.reject(error);
580 });
AppaRao Puli28711a62018-10-17 16:07:55 +0530581 return deferred.promise;
582 },
AppaRao Pulib1e7c862019-03-12 14:56:40 +0530583
584 getAllUserAccountProperties: function(callback) {
585 return $http({
586 method: 'GET',
587 url: DataService.getHost() + '/redfish/v1/AccountService',
588 withCredentials: true
589 })
590 .then(
591 function(response) {
592 return response.data;
593 },
594 function(error) {
595 console.log(error);
596 });
597 },
598
599 saveUserAccountProperties: function(lockoutduration, lockoutthreshold) {
600 var data = {};
601 if (lockoutduration != undefined) {
602 data['AccountLockoutDuration'] = lockoutduration;
603 }
604 if (lockoutthreshold != undefined) {
605 data['AccountLockoutThreshold'] = lockoutthreshold;
606 }
607
608 return $http({
609 method: 'PATCH',
610 url: DataService.getHost() + '/redfish/v1/AccountService',
611 withCredentials: true,
612 data: data
613 });
614 },
615
AppaRao Puli28711a62018-10-17 16:07:55 +0530616 createUser: function(user, passwd, role, enabled) {
Gunnar Mills4693ddb2019-03-27 15:49:40 -0500617 var data = {};
618 data['UserName'] = user;
619 data['Password'] = passwd;
620 data['RoleId'] = role;
621 data['Enabled'] = enabled;
AppaRao Puli28711a62018-10-17 16:07:55 +0530622
Gunnar Mills4693ddb2019-03-27 15:49:40 -0500623 return $http({
624 method: 'POST',
625 url: DataService.getHost() + '/redfish/v1/AccountService/Accounts',
626 withCredentials: true,
627 data: data
628 });
AppaRao Puli28711a62018-10-17 16:07:55 +0530629 },
630 updateUser: function(user, newUser, passwd, role, enabled) {
Gunnar Mills4693ddb2019-03-27 15:49:40 -0500631 var data = {};
632 if ((newUser !== undefined) && (newUser != null)) {
633 data['UserName'] = newUser;
AppaRao Puli28711a62018-10-17 16:07:55 +0530634 }
Gunnar Mills4693ddb2019-03-27 15:49:40 -0500635 if ((role !== undefined) && (role != null)) {
636 data['RoleId'] = role;
637 }
638 if ((enabled !== undefined) && (enabled != null)) {
639 data['Enabled'] = enabled;
640 }
641 if ((passwd !== undefined) && (passwd != null)) {
642 data['Password'] = passwd;
643 }
644 return $http({
645 method: 'PATCH',
646 url: DataService.getHost() +
647 '/redfish/v1/AccountService/Accounts/' + user,
648 withCredentials: true,
649 data: data
650 });
AppaRao Puli28711a62018-10-17 16:07:55 +0530651 },
652 deleteUser: function(user) {
Gunnar Mills4693ddb2019-03-27 15:49:40 -0500653 return $http({
654 method: 'DELETE',
655 url: DataService.getHost() +
656 '/redfish/v1/AccountService/Accounts/' + user,
657 withCredentials: true,
658 });
AppaRao Puli28711a62018-10-17 16:07:55 +0530659 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700660 chassisPowerOff: function() {
661 var deferred = $q.defer();
662 $http({
663 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700664 url: DataService.getHost() +
665 '/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700666 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700667 data: JSON.stringify(
668 {'data': 'xyz.openbmc_project.State.Chassis.Transition.Off'})
669 })
670 .then(
671 function(response) {
672 var json = JSON.stringify(response.data);
673 var content = JSON.parse(json);
674 deferred.resolve(content.status);
675 },
676 function(error) {
677 console.log(error);
678 deferred.reject(error);
679 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700680 return deferred.promise;
681 },
beccabroekb7f0ee12019-01-10 11:15:58 -0600682 setLEDState: function(state) {
683 return $http({
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700684 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700685 url: DataService.getHost() +
686 '/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700687 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700688 data: JSON.stringify({'data': state})
689 })
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700690 },
Dixsie Wolmersc57ec322019-04-26 12:58:51 -0500691 bmcReboot: function() {
692 return $http({
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700693 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700694 url: DataService.getHost() +
Gunnar Mills70086982019-01-04 15:10:04 -0600695 '/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700696 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700697 data: JSON.stringify(
698 {'data': 'xyz.openbmc_project.State.BMC.Transition.Reboot'})
Dixsie Wolmersc57ec322019-04-26 12:58:51 -0500699 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700700 },
beccabroekbfc99902018-07-24 15:36:33 -0500701 getLastRebootTime: function() {
702 return $http({
703 method: 'GET',
704 url: DataService.getHost() +
705 '/xyz/openbmc_project/state/bmc0/attr/LastRebootTime',
beccabroekbfc99902018-07-24 15:36:33 -0500706 withCredentials: true
707 })
708 .then(function(response) {
709 return response.data;
710 });
711 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700712 hostPowerOn: function() {
713 var deferred = $q.defer();
714 $http({
715 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700716 url: DataService.getHost() +
717 '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700718 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700719 data: JSON.stringify(
720 {'data': 'xyz.openbmc_project.State.Host.Transition.On'})
721 })
722 .then(
723 function(response) {
724 var json = JSON.stringify(response.data);
725 var content = JSON.parse(json);
726 deferred.resolve(content.status);
727 },
728 function(error) {
729 console.log(error);
730 deferred.reject(error);
731 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700732 return deferred.promise;
733 },
734 hostPowerOff: function() {
735 var deferred = $q.defer();
736 $http({
737 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700738 url: DataService.getHost() +
739 '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700740 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700741 data: JSON.stringify(
742 {'data': 'xyz.openbmc_project.State.Host.Transition.Off'})
743 })
744 .then(
745 function(response) {
746 var json = JSON.stringify(response.data);
747 var content = JSON.parse(json);
748 deferred.resolve(content.status);
749 },
750 function(error) {
751 console.log(error);
752 deferred.reject(error);
753 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700754 return deferred.promise;
755 },
756 hostReboot: function() {
757 var deferred = $q.defer();
758 $http({
759 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700760 url: DataService.getHost() +
761 '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700762 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700763 data: JSON.stringify(
764 {'data': 'xyz.openbmc_project.State.Host.Transition.Reboot'})
765 })
766 .then(
767 function(response) {
768 var json = JSON.stringify(response.data);
769 var content = JSON.parse(json);
770 deferred.resolve(content.status);
771 },
772 function(error) {
773 console.log(error);
774 deferred.reject(error);
775 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700776
777 return deferred.promise;
778 },
beccabroek56744252018-08-03 11:25:11 -0500779 getLastPowerTime: function() {
780 return $http({
781 method: 'GET',
782 url: DataService.getHost() +
783 '/xyz/openbmc_project/state/chassis0/attr/LastStateChangeTime',
beccabroek56744252018-08-03 11:25:11 -0500784 withCredentials: true
785 })
786 .then(function(response) {
787 return response.data;
788 });
789 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700790 getLogs: function() {
791 var deferred = $q.defer();
792 $http({
793 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700794 url: DataService.getHost() +
795 '/xyz/openbmc_project/logging/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700796 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700797 })
798 .then(
799 function(response) {
800 var json = JSON.stringify(response.data);
801 var content = JSON.parse(json);
802 var dataClone = JSON.parse(JSON.stringify(content.data));
803 var data = [];
804 var severityCode = '';
805 var priority = '';
806 var health = '';
807 var relatedItems = [];
Matt Spinler845acdc2018-05-01 16:41:28 -0500808 var eventID = 'None';
809 var description = 'None';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700810
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700811 for (var key in content.data) {
812 if (content.data.hasOwnProperty(key) &&
813 content.data[key].hasOwnProperty('Id')) {
814 var severityFlags = {
815 low: false,
816 medium: false,
817 high: false
818 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700819 severityCode =
820 content.data[key].Severity.split('.').pop();
821 priority =
822 Constants.SEVERITY_TO_PRIORITY_MAP[severityCode];
823 severityFlags[priority.toLowerCase()] = true;
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700824 relatedItems = [];
825 content.data[key].associations.forEach(function(item) {
826 relatedItems.push(item[2]);
827 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700828
Matt Spinler845acdc2018-05-01 16:41:28 -0500829 if (content.data[key].hasOwnProperty(['EventID'])) {
830 eventID = content.data[key].EventID;
831 }
832
833 if (content.data[key].hasOwnProperty(['Description'])) {
834 description = content.data[key].Description;
835 }
836
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700837 data.push(Object.assign(
838 {
839 path: key,
840 copied: false,
841 priority: priority,
842 severity_code: severityCode,
843 severity_flags: severityFlags,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700844 additional_data:
845 content.data[key].AdditionalData.join('\n'),
846 type: content.data[key].Message,
847 selected: false,
848 search_text:
849 ('#' + content.data[key].Id + ' ' +
850 severityCode + ' ' +
Gunnar Mills07876ff2018-06-28 15:21:22 -0500851 content.data[key].Message + ' ' +
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700852 content.data[key].Severity + ' ' +
853 content.data[key].AdditionalData.join(' '))
854 .toLowerCase(),
855 meta: false,
856 confirm: false,
857 related_items: relatedItems,
Matt Spinler845acdc2018-05-01 16:41:28 -0500858 eventID: eventID,
859 description: description,
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700860 data: {key: key, value: content.data[key]}
861 },
862 content.data[key]));
863 }
864 }
865 deferred.resolve({data: data, original: dataClone});
866 },
867 function(error) {
868 console.log(error);
869 deferred.reject(error);
870 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700871
872 return deferred.promise;
873 },
Gunnar Mills615a2f82019-04-17 14:30:39 -0500874 getAllSensorStatus: function(callback) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700875 $http({
876 method: 'GET',
Gunnar Mills615a2f82019-04-17 14:30:39 -0500877 url: DataService.getHost() +
878 '/xyz/openbmc_project/sensors/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -0700879 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700880 })
881 .then(
882 function(response) {
Gunnar Mills615a2f82019-04-17 14:30:39 -0500883 var json = JSON.stringify(response.data);
884 var content = JSON.parse(json);
885 var dataClone = JSON.parse(JSON.stringify(content.data));
886 var sensorData = [];
887 var severity = {};
888 var title = '';
889 var tempKeyParts = [];
890 var order = 0;
891 var customOrder = 0;
892
893 function getSensorStatus(reading) {
894 var severityFlags = {
895 critical: false,
896 warning: false,
897 normal: false
898 },
899 severityText = '', order = 0;
900
901 if (reading.hasOwnProperty('CriticalLow') &&
902 reading.Value < reading.CriticalLow) {
903 severityFlags.critical = true;
904 severityText = 'critical';
905 order = 2;
906 } else if (
907 reading.hasOwnProperty('CriticalHigh') &&
908 reading.Value > reading.CriticalHigh) {
909 severityFlags.critical = true;
910 severityText = 'critical';
911 order = 2;
912 } else if (
913 reading.hasOwnProperty('CriticalLow') &&
914 reading.hasOwnProperty('WarningLow') &&
915 reading.Value >= reading.CriticalLow &&
916 reading.Value <= reading.WarningLow) {
917 severityFlags.warning = true;
918 severityText = 'warning';
919 order = 1;
920 } else if (
921 reading.hasOwnProperty('WarningHigh') &&
922 reading.hasOwnProperty('CriticalHigh') &&
923 reading.Value >= reading.WarningHigh &&
924 reading.Value <= reading.CriticalHigh) {
925 severityFlags.warning = true;
926 severityText = 'warning';
927 order = 1;
928 } else {
929 severityFlags.normal = true;
930 severityText = 'normal';
931 }
932 return {
933 flags: severityFlags,
934 severityText: severityText,
935 order: order
936 };
937 }
938
939 for (var key in content.data) {
940 if (content.data.hasOwnProperty(key) &&
941 content.data[key].hasOwnProperty('Unit')) {
942 severity = getSensorStatus(content.data[key]);
943
944 if (!content.data[key].hasOwnProperty('CriticalLow')) {
945 content.data[key].CriticalLow = '--';
946 content.data[key].CriticalHigh = '--';
947 }
948
949 if (!content.data[key].hasOwnProperty('WarningLow')) {
950 content.data[key].WarningLow = '--';
951 content.data[key].WarningHigh = '--';
952 }
953
954 tempKeyParts = key.split('/');
955 title = tempKeyParts.pop();
956 title = tempKeyParts.pop() + '_' + title;
957 title = title.split('_')
958 .map(function(item) {
959 return item.toLowerCase()
960 .charAt(0)
961 .toUpperCase() +
962 item.slice(1);
963 })
964 .reduce(function(prev, el) {
965 return prev + ' ' + el;
966 });
967
968 content.data[key].Value = getScaledValue(
969 content.data[key].Value, content.data[key].Scale);
970 content.data[key].CriticalLow = getScaledValue(
971 content.data[key].CriticalLow,
972 content.data[key].Scale);
973 content.data[key].CriticalHigh = getScaledValue(
974 content.data[key].CriticalHigh,
975 content.data[key].Scale);
976 content.data[key].WarningLow = getScaledValue(
977 content.data[key].WarningLow,
978 content.data[key].Scale);
979 content.data[key].WarningHigh = getScaledValue(
980 content.data[key].WarningHigh,
981 content.data[key].Scale);
982 if (Constants.SENSOR_SORT_ORDER.indexOf(
983 content.data[key].Unit) > -1) {
984 customOrder = Constants.SENSOR_SORT_ORDER.indexOf(
985 content.data[key].Unit);
986 } else {
987 customOrder = Constants.SENSOR_SORT_ORDER_DEFAULT;
988 }
989
990 sensorData.push(Object.assign(
991 {
992 path: key,
993 selected: false,
994 confirm: false,
995 copied: false,
996 title: title,
997 unit:
998 Constants
999 .SENSOR_UNIT_MAP[content.data[key].Unit],
1000 severity_flags: severity.flags,
1001 status: severity.severityText,
1002 order: severity.order,
1003 custom_order: customOrder,
1004 search_text:
1005 (title + ' ' + content.data[key].Value + ' ' +
1006 Constants.SENSOR_UNIT_MAP[content.data[key]
1007 .Unit] +
1008 ' ' + severity.severityText + ' ' +
1009 content.data[key].CriticalLow + ' ' +
1010 content.data[key].CriticalHigh + ' ' +
1011 content.data[key].WarningLow + ' ' +
1012 content.data[key].WarningHigh + ' ')
1013 .toLowerCase(),
1014 original_data:
1015 {key: key, value: content.data[key]}
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001016 },
Gunnar Mills615a2f82019-04-17 14:30:39 -05001017 content.data[key]));
1018 }
1019 }
1020
Alexander Filippove4ae8542019-06-10 15:42:48 +03001021 sensorData.sort(function(a, b) {
1022 return a.title.localeCompare(
1023 b.title, 'en', {numeric: true});
1024 });
1025
Gunnar Mills615a2f82019-04-17 14:30:39 -05001026 callback(sensorData, dataClone);
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001027 },
1028 function(error) {
Gunnar Mills615a2f82019-04-17 14:30:39 -05001029 console.log(error);
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001030 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001031 },
1032 getActivation: function(imageId) {
1033 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001034 method: 'GET',
1035 url: DataService.getHost() +
1036 '/xyz/openbmc_project/software/' + imageId +
1037 '/attr/Activation',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001038 withCredentials: true
1039 })
1040 .then(function(response) {
1041 return response.data;
1042 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001043 },
1044 getFirmwares: function() {
1045 var deferred = $q.defer();
1046 $http({
1047 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001048 url: DataService.getHost() +
1049 '/xyz/openbmc_project/software/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001050 withCredentials: true
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001051 })
1052 .then(
1053 function(response) {
1054 var json = JSON.stringify(response.data);
1055 var content = JSON.parse(json);
1056 var data = [];
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001057 var isExtended = false;
1058 var bmcActiveVersion = '';
1059 var hostActiveVersion = '';
1060 var imageType = '';
1061 var extendedVersions = [];
1062 var functionalImages = [];
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001063
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001064 function getFormatedExtendedVersions(extendedVersion) {
1065 var versions = [];
1066 extendedVersion = extendedVersion.split(',');
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001067
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001068 extendedVersion.forEach(function(item) {
1069 var parts = item.split('-');
1070 var numberIndex = 0;
1071 for (var i = 0; i < parts.length; i++) {
1072 if (/[0-9]/.test(parts[i])) {
1073 numberIndex = i;
1074 break;
1075 }
1076 }
George Liu22d78222019-07-19 10:35:40 +08001077 if (numberIndex > 0) {
1078 var titlePart = parts.splice(0, numberIndex);
1079 titlePart = titlePart.join('');
1080 titlePart = titlePart[0].toUpperCase() +
1081 titlePart.substr(1, titlePart.length);
1082 var versionPart = parts.join('-');
1083 versions.push(
1084 {title: titlePart, version: versionPart});
1085 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001086 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001087
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001088 return versions;
1089 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001090
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001091 // Get the list of functional images so we can compare
1092 // later if an image is functional
1093 if (content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH]) {
1094 functionalImages =
1095 content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH]
1096 .endpoints;
1097 }
1098 for (var key in content.data) {
1099 if (content.data.hasOwnProperty(key) &&
1100 content.data[key].hasOwnProperty('Version')) {
Gunnar Millsac9131e2018-07-13 15:52:18 -05001101 var activationStatus = '';
1102
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001103 // If the image is "Functional" use that for the
AppaRao Puli28711a62018-10-17 16:07:55 +05301104 // activation status, else use the value of
1105 // "Activation"
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001106 // github.com/openbmc/phosphor-dbus-interfaces/blob/master/xyz/openbmc_project/Software/Activation.interface.yaml
Gunnar Millsac9131e2018-07-13 15:52:18 -05001107 if (content.data[key].Activation) {
1108 activationStatus =
1109 content.data[key].Activation.split('.').pop();
1110 }
1111
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001112 if (functionalImages.includes(key)) {
1113 activationStatus = 'Functional';
1114 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001115
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001116 imageType = content.data[key].Purpose.split('.').pop();
1117 isExtended = content.data[key].hasOwnProperty(
1118 'ExtendedVersion') &&
1119 content.data[key].ExtendedVersion != '';
1120 if (isExtended) {
1121 extendedVersions = getFormatedExtendedVersions(
1122 content.data[key].ExtendedVersion);
1123 }
1124 data.push(Object.assign(
1125 {
1126 path: key,
1127 activationStatus: activationStatus,
1128 imageId: key.split('/').pop(),
1129 imageType: imageType,
1130 isExtended: isExtended,
1131 extended:
1132 {show: false, versions: extendedVersions},
1133 data: {key: key, value: content.data[key]}
1134 },
1135 content.data[key]));
1136
1137 if (activationStatus == 'Functional' &&
1138 imageType == 'BMC') {
1139 bmcActiveVersion = content.data[key].Version;
1140 }
1141
1142 if (activationStatus == 'Functional' &&
1143 imageType == 'Host') {
1144 hostActiveVersion = content.data[key].Version;
1145 }
1146 }
1147 }
1148
1149 deferred.resolve({
1150 data: data,
1151 bmcActiveVersion: bmcActiveVersion,
1152 hostActiveVersion: hostActiveVersion
1153 });
Iftekharul Islam171c6a12017-08-11 08:35:47 -05001154 },
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001155 function(error) {
1156 console.log(error);
1157 deferred.reject(error);
1158 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001159
1160 return deferred.promise;
1161 },
1162 changePriority: function(imageId, priority) {
Gunnar Millsf7f59462018-09-20 16:02:08 -05001163 return $http({
1164 method: 'PUT',
1165 url: DataService.getHost() +
1166 '/xyz/openbmc_project/software/' + imageId +
1167 '/attr/Priority',
1168 withCredentials: true,
1169 data: JSON.stringify({'data': priority})
1170 })
1171 .then(function(response) {
1172 return response.data;
1173 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001174 },
1175 deleteImage: function(imageId) {
Gunnar Millsf7f59462018-09-20 16:02:08 -05001176 return $http({
1177 method: 'POST',
1178 url: DataService.getHost() +
1179 '/xyz/openbmc_project/software/' + imageId +
1180 '/action/Delete',
1181 withCredentials: true,
1182 data: JSON.stringify({'data': []})
1183 })
1184 .then(function(response) {
1185 return response.data;
1186 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001187 },
1188 activateImage: function(imageId) {
Gunnar Millsf7f59462018-09-20 16:02:08 -05001189 return $http({
1190 method: 'PUT',
1191 url: DataService.getHost() +
1192 '/xyz/openbmc_project/software/' + imageId +
1193 '/attr/RequestedActivation',
1194 withCredentials: true,
1195 data: JSON.stringify(
1196 {'data': Constants.FIRMWARE.ACTIVATE_FIRMWARE})
1197 })
1198 .then(function(response) {
1199 return response.data;
1200 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001201 },
1202 uploadImage: function(file) {
1203 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001204 method: 'POST',
1205 timeout: 5 * 60 * 1000,
1206 url: DataService.getHost() + '/upload/image',
Gunnar Millsdd9d4c32018-09-10 12:59:34 -05001207 // Overwrite the default 'application/json' Content-Type
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001208 headers: {'Content-Type': 'application/octet-stream'},
1209 withCredentials: true,
1210 data: file
1211 })
1212 .then(function(response) {
1213 return response.data;
1214 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001215 },
1216 downloadImage: function(host, filename) {
1217 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001218 method: 'POST',
1219 url: DataService.getHost() +
1220 '/xyz/openbmc_project/software/action/DownloadViaTFTP',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001221 withCredentials: true,
1222 data: JSON.stringify({'data': [filename, host]}),
1223 responseType: 'arraybuffer'
1224 })
1225 .then(function(response) {
1226 return response.data;
1227 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001228 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001229 getServerInfo: function() {
1230 // TODO: openbmc/openbmc#3117 Need a way via REST to get
1231 // interfaces so we can get the system object(s) by the looking
1232 // for the system interface.
1233 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001234 method: 'GET',
1235 url: DataService.getHost() +
1236 '/xyz/openbmc_project/inventory/system',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001237 withCredentials: true
1238 })
1239 .then(function(response) {
1240 return response.data;
1241 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001242 },
1243 getBMCTime: function() {
1244 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001245 method: 'GET',
1246 url: DataService.getHost() + '/xyz/openbmc_project/time/bmc',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001247 withCredentials: true
1248 })
1249 .then(function(response) {
1250 return response.data;
1251 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001252 },
Gunnar Millsc74d4342018-07-18 14:52:02 -05001253 getTime: function() {
1254 return $http({
1255 method: 'GET',
1256 url: DataService.getHost() +
1257 '/xyz/openbmc_project/time/enumerate',
Gunnar Millsc74d4342018-07-18 14:52:02 -05001258 withCredentials: true
1259 })
1260 .then(function(response) {
1261 return response.data;
1262 });
1263 },
Gunnar Millsb7ea2792018-07-18 13:01:48 -05001264 // Even though NTPServers is a network interface specific path
1265 // (e.g. /xyz/openbmc_project/network/eth0/attr/NTPServers) it acts
1266 // like a global setting. Just use eth0 for setting and getting the
1267 // NTP Servers until it is moved to a non-network interface specific
1268 // path like it is in Redfish. TODO: openbmc/phosphor-time-manager#4
1269 getNTPServers: function() {
1270 return $http({
1271 method: 'GET',
1272 url: DataService.getHost() +
1273 '/xyz/openbmc_project/network/eth0/attr/NTPServers',
Gunnar Millsb7ea2792018-07-18 13:01:48 -05001274 withCredentials: true
1275 })
1276 .then(function(response) {
1277 return response.data;
1278 });
1279 },
1280 setNTPServers: function(ntpServers) {
1281 return $http({
1282 method: 'PUT',
1283 url: DataService.getHost() +
1284 '/xyz/openbmc_project/network/eth0/attr/NTPServers',
Gunnar Millsb7ea2792018-07-18 13:01:48 -05001285 withCredentials: true,
1286 data: JSON.stringify({'data': ntpServers})
1287 })
1288 .then(function(response) {
1289 return response.data;
1290 });
1291 },
1292 setTimeMode: function(timeMode) {
1293 return $http({
1294 method: 'PUT',
1295 url: DataService.getHost() +
1296 '/xyz/openbmc_project/time/sync_method/attr/TimeSyncMethod',
Gunnar Millsb7ea2792018-07-18 13:01:48 -05001297 withCredentials: true,
1298 data: JSON.stringify({'data': timeMode})
1299 })
1300 .then(function(response) {
1301 return response.data;
1302 });
1303 },
1304 setTimeOwner: function(timeOwner) {
1305 return $http({
1306 method: 'PUT',
1307 url: DataService.getHost() +
1308 '/xyz/openbmc_project/time/owner/attr/TimeOwner',
Gunnar Millsb7ea2792018-07-18 13:01:48 -05001309 withCredentials: true,
1310 data: JSON.stringify({'data': timeOwner})
1311 })
1312 .then(function(response) {
1313 return response.data;
1314 });
1315 },
1316 setBMCTime: function(time) {
1317 return $http({
1318 method: 'PUT',
1319 url: DataService.getHost() +
1320 '/xyz/openbmc_project/time/bmc/attr/Elapsed',
Gunnar Millsb7ea2792018-07-18 13:01:48 -05001321 withCredentials: true,
1322 data: JSON.stringify({'data': time})
1323 })
1324 .then(function(response) {
1325 return response.data;
1326 });
1327 },
1328 setHostTime: function(time) {
1329 return $http({
1330 method: 'PUT',
1331 url: DataService.getHost() +
1332 '/xyz/openbmc_project/time/host/attr/Elapsed',
Gunnar Millsb7ea2792018-07-18 13:01:48 -05001333 withCredentials: true,
1334 data: JSON.stringify({'data': time})
1335 })
1336 .then(function(response) {
1337 return response.data;
1338 });
1339 },
beccabroek309b5da2018-11-07 12:22:31 -06001340 getCertificateLocations: function() {
1341 return $http({
1342 method: 'GET',
1343 url: DataService.getHost() +
1344 '/redfish/v1/CertificateService/CertificateLocations',
1345 withCredentials: true
1346 })
1347 .then(function(response) {
1348 return response.data;
1349 });
1350 },
1351 getCertificate: function(location) {
1352 return $http({
1353 method: 'GET',
1354 url: DataService.getHost() + location,
1355 withCredentials: true
1356 })
1357 .then(function(response) {
1358 return response.data;
1359 });
1360 },
1361 addNewCertificate: function(file, type) {
1362 return $http({
1363 method: 'POST',
1364 url: DataService.getHost() + type.location,
1365 headers: {'Content-Type': 'application/x-pem-file'},
1366 withCredentials: true,
1367 data: file
1368 })
1369 .then(function(response) {
1370 return response.data;
1371 });
1372 },
1373 replaceCertificate: function(data) {
1374 return $http({
1375 method: 'POST',
1376 url: DataService.getHost() +
1377 '/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate',
1378 withCredentials: true,
1379 data: data
1380 })
1381 .then(function(response) {
1382 return response.data;
1383 });
1384 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001385 getHardwares: function(callback) {
1386 $http({
1387 method: 'GET',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001388 url: DataService.getHost() +
1389 '/xyz/openbmc_project/inventory/enumerate',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001390 withCredentials: true
1391 }).then(function(response) {
1392 var json = JSON.stringify(response.data);
1393 var content = JSON.parse(json);
1394 var hardwareData = [];
1395 var keyIndexMap = {};
1396 var title = '';
beccabroek628ba8b2018-10-01 14:12:25 -05001397 var depth = '';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001398 var data = [];
1399 var searchText = '';
1400 var componentIndex = -1;
beccabroek628ba8b2018-10-01 14:12:25 -05001401 var parent = '';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001402
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001403 function isSubComponent(key) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001404 for (var i = 0; i < Constants.HARDWARE.parent_components.length;
1405 i++) {
1406 if (key.split(Constants.HARDWARE.parent_components[i]).length ==
1407 2)
1408 return true;
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001409 }
1410
1411 return false;
1412 }
1413
1414 function titlelize(title) {
1415 title = title.replace(/([A-Z0-9]+)/g, ' $1').replace(/^\s+/, '');
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001416 for (var i = 0; i < Constants.HARDWARE.uppercase_titles.length;
1417 i++) {
1418 if (title.toLowerCase().indexOf(
1419 (Constants.HARDWARE.uppercase_titles[i] + ' ')) > -1) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001420 return title.toUpperCase();
1421 }
1422 }
1423
1424 return title;
1425 }
1426
1427 function camelcaseToLabel(obj) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001428 var transformed = [], label = '', value = '';
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001429 for (var key in obj) {
1430 label = key.replace(/([A-Z0-9]+)/g, ' $1').replace(/^\s+/, '');
1431 if (obj[key] !== '') {
1432 value = obj[key];
1433 if (value == 1 || value == 0) {
1434 value = (value == 1) ? 'Yes' : 'No';
1435 }
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001436 transformed.push({key: label, value: value});
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001437 }
1438 }
1439
1440 return transformed;
1441 }
1442
beccabroek628ba8b2018-10-01 14:12:25 -05001443 function determineParent(key) {
1444 var levels = key.split('/');
1445 levels.pop();
1446 return levels.join('/');
1447 }
1448
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001449 function getSearchText(data) {
1450 var searchText = '';
1451 for (var i = 0; i < data.length; i++) {
1452 searchText += ' ' + data[i].key + ' ' + data[i].value;
1453 }
1454
1455 return searchText;
1456 }
1457
1458 for (var key in content.data) {
1459 if (content.data.hasOwnProperty(key) &&
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001460 key.indexOf(Constants.HARDWARE.component_key_filter) == 0) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001461 data = camelcaseToLabel(content.data[key]);
1462 searchText = getSearchText(data);
1463 title = key.split('/').pop();
Gunnar Millseedf0b92019-02-27 10:52:01 -06001464 // All and only associations have the property "endpoints".
1465 // We don't want to show associations on the hardware page.
1466 // Example: An association from the BMC inventory item to the
1467 // BMC firmware images.
1468 if (content.data[key].hasOwnProperty('endpoints')) {
1469 continue;
1470 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001471
1472 title = titlelize(title);
beccabroek628ba8b2018-10-01 14:12:25 -05001473 // e.g. /xyz/openbmc_project/inventory/system and
1474 // /xyz/openbmc_project/inventory/system/chassis are depths of 5
1475 // and 6.
1476 depth = key.split('/').length;
1477 parent = determineParent(key);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001478
1479 if (!isSubComponent(key)) {
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001480 hardwareData.push(Object.assign(
1481 {
1482 path: key,
1483 title: title,
beccabroek628ba8b2018-10-01 14:12:25 -05001484 depth: depth,
1485 parent: parent,
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001486 selected: false,
1487 expanded: false,
1488 search_text: title.toLowerCase() + ' ' +
1489 searchText.toLowerCase(),
1490 sub_components: [],
1491 original_data: {key: key, value: content.data[key]}
1492 },
1493 {items: data}));
Iftekharul Islam2a489552017-11-02 13:23:08 -05001494
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001495 keyIndexMap[key] = hardwareData.length - 1;
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001496 } else {
beccabroek628ba8b2018-10-01 14:12:25 -05001497 parent = determineParent(key)
1498 componentIndex = keyIndexMap[parent];
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001499 data = content.data[key];
1500 data.title = title;
1501 hardwareData[componentIndex].sub_components.push(data);
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001502 hardwareData[componentIndex].search_text +=
1503 ' ' + title.toLowerCase();
Iftekharul Islam171c6a12017-08-11 08:35:47 -05001504
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001505 // Sort the subcomponents alphanumeric so they are displayed
1506 // on the inventory page in order (e.g. core 0, core 1, core
1507 // 2, ... core 12, core 13)
1508 hardwareData[componentIndex].sub_components.sort(function(
1509 a, b) {
1510 return a.title.localeCompare(
1511 b.title, 'en', {numeric: true});
Gunnar Mills55a8e392018-03-14 15:37:44 -05001512 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001513 }
1514 }
1515 }
beccabroek628ba8b2018-10-01 14:12:25 -05001516 // First, order the components by depth and then place the child
1517 // components beneath their parent component alphanumerically. Can
1518 // be removed with completion of
1519 // https://github.com/openbmc/openbmc/issues/3401
1520 // TODO: Remove this once implemented in back end
1521 hardwareData.sort(function(a, b) {
1522 if (a.depth < b.depth) return -1;
1523 if (a.depth > b.depth) return 1;
1524 return b.title.localeCompare(a.title, 'en', {numeric: true});
1525 });
1526
1527 var orderedComponents = [];
1528
1529 for (var i = 0; i < hardwareData.length; i++) {
1530 if (!keyIndexMap[hardwareData[i].parent]) {
1531 orderedComponents.push(hardwareData[i]);
1532 } else {
1533 for (var j = 0; j < orderedComponents.length; j++) {
1534 if (orderedComponents[j].path === hardwareData[i].parent) {
1535 var child = hardwareData[i];
1536 orderedComponents.splice(j + 1, 0, child);
1537 }
1538 }
1539 }
1540 }
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001541
1542 if (callback) {
beccabroek628ba8b2018-10-01 14:12:25 -05001543 callback(orderedComponents, content.data);
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001544 } else {
beccabroek628ba8b2018-10-01 14:12:25 -05001545 return {data: orderedComponents, original_data: content.data};
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001546 }
1547 });
1548 },
1549 deleteLogs: function(logs) {
1550 var defer = $q.defer();
1551 var promises = [];
1552
1553 function finished() {
1554 defer.resolve();
1555 }
1556
1557 logs.forEach(function(item) {
1558 promises.push($http({
1559 method: 'POST',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001560 url: DataService.getHost() +
1561 '/xyz/openbmc_project/logging/entry/' + item.Id +
1562 '/action/Delete',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001563 withCredentials: true,
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001564 data: JSON.stringify({'data': []})
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001565 }));
1566 });
1567
1568 $q.all(promises).then(finished);
1569
1570 return defer.promise;
1571 },
1572 resolveLogs: function(logs) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001573 var promises = [];
1574
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001575 logs.forEach(function(item) {
1576 promises.push($http({
1577 method: 'PUT',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001578 url: DataService.getHost() +
1579 '/xyz/openbmc_project/logging/entry/' + item.Id +
1580 '/attr/Resolved',
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001581 withCredentials: true,
Gunnar Mills7e48d082019-01-23 16:02:03 -06001582 data: JSON.stringify({'data': true})
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001583 }));
1584 });
Gunnar Mills7e48d082019-01-23 16:02:03 -06001585 return $q.all(promises);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001586 },
Yoshie Muranakae4194ce2019-05-24 14:33:56 -05001587 setRemoteLoggingServer: (data) => {
1588 const ip = data.hostname;
1589 const port = data.port;
1590 const setIPRequest = $http({
1591 method: 'PUT',
1592 url: DataService.getHost() +
1593 '/xyz/openbmc_project/logging/config/remote/attr/Address',
1594 withCredentials: true,
1595 data: {'data': ip}
1596 });
1597 const setPortRequest = $http({
1598 method: 'PUT',
1599 url: DataService.getHost() +
1600 '/xyz/openbmc_project/logging/config/remote/attr/Port',
1601 withCredentials: true,
1602 data: {'data': port}
1603 });
1604 const promises = [setIPRequest, setPortRequest];
1605 return $q.all(promises);
1606 },
1607 getRemoteLoggingServer: () => {
1608 return $http({
1609 method: 'GET',
1610 url: DataService.getHost() +
1611 '/xyz/openbmc_project/logging/config/remote',
1612 withCredentials: true
1613 })
1614 .then((response) => {
1615 const remoteServer = response.data.data;
1616 if (remoteServer === undefined) {
1617 return undefined;
1618 }
1619 const hostname = remoteServer.Address;
1620 const port = remoteServer.Port;
1621 if (hostname === '') {
1622 return undefined;
1623 } else {
1624 return {
1625 hostname, port
1626 }
1627 }
1628 });
1629 },
1630 disableRemoteLoggingServer: () => {
1631 return SERVICE.setRemoteLoggingServer({hostname: '', port: 0});
1632 },
1633 updateRemoteLoggingServer: (data) => {
1634 // Recommended to disable existing configuration
1635 // before updating config to new server
1636 // https://github.com/openbmc/phosphor-logging#changing-the-rsyslog-server
1637 return SERVICE.disableRemoteLoggingServer()
1638 .then(() => {
1639 return SERVICE.setRemoteLoggingServer(data);
1640 })
1641 .catch(() => {
1642 // try updating server even if initial disable attempt fails
1643 return SERVICE.setRemoteLoggingServer(data);
1644 });
1645 },
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001646 getPowerConsumption: function() {
1647 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001648 method: 'GET',
1649 url: DataService.getHost() +
1650 '/xyz/openbmc_project/sensors/power/total_power',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001651 withCredentials: true
1652 })
1653 .then(
1654 function(response) {
1655 var json = JSON.stringify(response.data);
1656 var content = JSON.parse(json);
Iftekharul Islamcd789502017-04-19 14:37:55 -05001657
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001658 return getScaledValue(
1659 content.data.Value, content.data.Scale) +
1660 ' ' +
1661 Constants.POWER_CONSUMPTION_TEXT[content.data.Unit];
1662 },
1663 function(error) {
1664 if ('Not Found' == error.statusText) {
1665 return Constants.POWER_CONSUMPTION_TEXT.notavailable;
1666 } else {
1667 throw error;
1668 }
1669 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001670 },
1671 getPowerCap: function() {
1672 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001673 method: 'GET',
1674 url: DataService.getHost() +
1675 '/xyz/openbmc_project/control/host0/power_cap',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001676 withCredentials: true
1677 })
1678 .then(function(response) {
Gunnar Mills006aaa02018-06-26 16:08:59 -05001679 return response.data;
1680 });
1681 },
1682 setPowerCapEnable: function(powerCapEnable) {
1683 return $http({
1684 method: 'PUT',
1685 url: DataService.getHost() +
1686 '/xyz/openbmc_project/control/host0/power_cap/attr/PowerCapEnable',
Gunnar Mills006aaa02018-06-26 16:08:59 -05001687 withCredentials: true,
1688 data: JSON.stringify({'data': powerCapEnable})
1689 })
1690 .then(function(response) {
1691 return response.data;
1692 });
1693 },
1694 setPowerCap: function(powerCap) {
1695 return $http({
1696 method: 'PUT',
1697 url: DataService.getHost() +
1698 '/xyz/openbmc_project/control/host0/power_cap/attr/PowerCap',
Gunnar Mills006aaa02018-06-26 16:08:59 -05001699 withCredentials: true,
1700 data: JSON.stringify({'data': powerCap})
1701 })
1702 .then(function(response) {
1703 return response.data;
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001704 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001705 },
1706 setHostname: function(hostname) {
1707 return $http({
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001708 method: 'PUT',
1709 url: DataService.getHost() +
1710 '/xyz/openbmc_project/network/config/attr/HostName',
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001711 withCredentials: true,
1712 data: JSON.stringify({'data': hostname})
1713 })
1714 .then(function(response) {
1715 return response.data;
1716 });
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001717 },
1718 };
1719 return SERVICE;
Andrew Geisslerd27bb132018-05-24 11:07:27 -07001720 }
1721 ]);
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07001722})(window.angular);