blob: c17577c0a67fdd43ee6eab67a3e5d6545cbe00e7 [file] [log] [blame]
Iftekharul Islam99d199f2017-03-24 15:28:25 -05001/**
2 * API utilities service
3 *
4 * @module app/common/services/api-utils
5 * @exports APIUtils
6 * @name APIUtils
7 * @version 0.0.1
8 */
9
10window.angular && (function (angular) {
11 'use strict';
12 angular
13 .module('app.common.services')
Iftekharul Islam1acb4122017-11-02 13:20:32 -050014 .factory('APIUtils', ['$http', 'Constants', '$q', 'dataService',function($http, Constants, $q, DataService){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050015 var SERVICE = {
16 LOGIN_CREDENTIALS: Constants.LOGIN_CREDENTIALS,
17 API_CREDENTIALS: Constants.API_CREDENTIALS,
18 API_RESPONSE: Constants.API_RESPONSE,
19 CHASSIS_POWER_STATE: Constants.CHASSIS_POWER_STATE,
20 HOST_STATE_TEXT: Constants.HOST_STATE,
21 HOST_STATE: Constants.HOST_STATE,
Iftekharul Islamcd789502017-04-19 14:37:55 -050022 LED_STATE: Constants.LED_STATE,
23 LED_STATE_TEXT: Constants.LED_STATE_TEXT,
Iftekharul Islam1acb4122017-11-02 13:20:32 -050024 HOST_SESSION_STORAGE_KEY: Constants.API_CREDENTIALS.host_storage_key,
Iftekharul Islam99d199f2017-03-24 15:28:25 -050025 getChassisState: function(callback){
26 $http({
27 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -050028 url: DataService.getHost() + "/xyz/openbmc_project/state/chassis0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -050029 headers: {
30 'Accept': 'application/json',
31 'Content-Type': 'application/json'
32 },
33 withCredentials: true
34 }).success(function(response){
35 var json = JSON.stringify(response);
36 var content = JSON.parse(json);
37 callback(content.data.CurrentPowerState);
38 }).error(function(error){
39 console.log(error);
40 });
41 },
42 getHostState: function(callback){
43 $http({
44 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -050045 url: DataService.getHost() + "/xyz/openbmc_project/state/host0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -050046 headers: {
47 'Accept': 'application/json',
48 'Content-Type': 'application/json'
49 },
50 withCredentials: true
51 }).success(function(response){
52 var json = JSON.stringify(response);
53 var content = JSON.parse(json);
54 callback(content.data.CurrentHostState);
55 }).error(function(error){
56 console.log(error);
57 });
58 },
Iftekharul Islam171c6a12017-08-11 08:35:47 -050059 getNetworkInfo: function(){
60 var deferred = $q.defer();
61 $http({
62 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -050063 url: DataService.getHost() + "/xyz/openbmc_project/network/enumerate",
Iftekharul Islam171c6a12017-08-11 08:35:47 -050064 headers: {
65 'Accept': 'application/json',
66 'Content-Type': 'application/json'
67 },
68 withCredentials: true
69 }).success(function(response){
70 var json = JSON.stringify(response);
71 var content = JSON.parse(json);
72 var hostname = "";
73 var macAddress = "";
74
75 if(content.data.hasOwnProperty('/xyz/openbmc_project/network/config') &&
76 content.data['/xyz/openbmc_project/network/config'].hasOwnProperty('HostName')
77 ){
78 hostname = content.data['/xyz/openbmc_project/network/config'].HostName;
79 }
80
81 if(content.data.hasOwnProperty('/xyz/openbmc_project/network/eth0') &&
82 content.data['/xyz/openbmc_project/network/eth0'].hasOwnProperty('MACAddress')
83 ){
84 macAddress = content.data['/xyz/openbmc_project/network/eth0'].MACAddress;
85 }
86
87 deferred.resolve({
88 data: content.data,
89 hostname: hostname,
90 mac_address: macAddress,
91 });
92 }).error(function(error){
93 console.log(error);
94 deferred.reject(error);
95 });
96 return deferred.promise;
97 },
Michael Davisdf3bd122017-08-10 11:03:42 -050098 getLEDState: function(){
99 var deferred = $q.defer();
Iftekharul Islamcd789502017-04-19 14:37:55 -0500100 $http({
101 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500102 url: DataService.getHost() + "/xyz/openbmc_project/led/groups/enclosure_identify",
Iftekharul Islamcd789502017-04-19 14:37:55 -0500103 headers: {
104 'Accept': 'application/json',
105 'Content-Type': 'application/json'
106 },
107 withCredentials: true
108 }).success(function(response){
Michael Davisdf3bd122017-08-10 11:03:42 -0500109 var json = JSON.stringify(response);
110 var content = JSON.parse(json);
111 deferred.resolve(content.data.Asserted);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500112 }).error(function(error){
113 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500114 deferred.reject(error);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500115 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500116 return deferred.promise;
Iftekharul Islamcd789502017-04-19 14:37:55 -0500117 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500118 login: function(username, password, callback){
119 $http({
120 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500121 url: DataService.getHost() + "/login",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500122 headers: {
123 'Accept': 'application/json',
124 'Content-Type': 'application/json'
125 },
126 withCredentials: true,
127 data: JSON.stringify({"data": [username, password]})
128 }).success(function(response){
129 if(callback){
130 callback(response);
131 }
132 }).error(function(error){
133 if(callback){
Iftekharul Islamcd789502017-04-19 14:37:55 -0500134 if(error && error.status && error.status == 'error'){
135 callback(error);
136 }else{
137 callback(error, true);
138 }
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500139 }
140 console.log(error);
141 });
142 },
143 logout: function(callback){
144 $http({
145 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500146 url: DataService.getHost() + "/logout",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500147 headers: {
148 'Accept': 'application/json',
149 'Content-Type': 'application/json'
150 },
151 withCredentials: true,
152 data: JSON.stringify({"data": []})
153 }).success(function(response){
154 if(callback){
155 callback(response);
156 }
157 }).error(function(error){
158 if(callback){
159 callback(null, error);
160 }
161 console.log(error);
162 });
163 },
164 chassisPowerOn: function(callback){
165 $http({
166 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500167 url: DataService.getHost() + "/xyz/openbmc_project/state/host0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500168 headers: {
169 'Accept': 'application/json',
170 'Content-Type': 'application/json'
171 },
172 withCredentials: true,
173 data: JSON.stringify({"data": []})
174 }).success(function(response){
175 var json = JSON.stringify(response);
176 var content = JSON.parse(json);
177 if(callback){
178 return callback(content.data.CurrentPowerState);
179 }
180 }).error(function(error){
181 if(callback){
182 callback(error);
183 }else{
184 console.log(error);
185 }
186 });
187 },
188 chassisPowerOff: function(callback){
189 $http({
190 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500191 url: DataService.getHost() + "/xyz/openbmc_project/state/host0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500192 headers: {
193 'Accept': 'application/json',
194 'Content-Type': 'application/json'
195 },
196 withCredentials: true,
197 data: JSON.stringify({"data": []})
198 }).success(function(response){
199 var json = JSON.stringify(response);
200 var content = JSON.parse(json);
201 if(callback){
202 return callback(content.data.CurrentPowerState);
203 }
204 }).error(function(error){
205 if(callback){
206 callback(error);
207 }else{
208 console.log(error);
209 }
210 });
211 },
Iftekharul Islamcd789502017-04-19 14:37:55 -0500212 setLEDState: function(state, callback){
213 $http({
214 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500215 url: DataService.getHost() + "/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted",
Iftekharul Islamcd789502017-04-19 14:37:55 -0500216 headers: {
217 'Accept': 'application/json',
218 'Content-Type': 'application/json'
219 },
220 withCredentials: true,
221 data: JSON.stringify({"data": state})
222 }).success(function(response){
223 var json = JSON.stringify(response);
224 var content = JSON.parse(json);
225 if(callback){
226 return callback(content.status);
227 }
228 }).error(function(error){
229 if(callback){
230 callback(error);
231 }else{
232 console.log(error);
233 }
234 });
235 },
Iftekharul Islam55368122017-03-27 09:46:50 -0500236 bmcReboot: function(callback){
237 $http({
238 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500239 url: DataService.getHost() + "/xyz/openbmc_project/state/bmc0/attr/RequestedBmcTransition",
Iftekharul Islam55368122017-03-27 09:46:50 -0500240 headers: {
241 'Accept': 'application/json',
242 'Content-Type': 'application/json'
243 },
244 withCredentials: true,
245 data: JSON.stringify({"data": "xyz.openbmc_project.State.BMC.Transition.Reboot"})
246 }).success(function(response){
247 var json = JSON.stringify(response);
248 var content = JSON.parse(json);
249 if(callback){
250 return callback(content.status);
251 }
252 }).error(function(error){
253 if(callback){
254 callback(error);
255 }else{
256 console.log(error);
257 }
258 });
259 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500260 hostPowerOn: function(callback){
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500261 $http({
262 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500263 url: DataService.getHost() + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500264 headers: {
265 'Accept': 'application/json',
266 'Content-Type': 'application/json'
267 },
268 withCredentials: true,
269 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.On"})
270 }).success(function(response){
271 var json = JSON.stringify(response);
272 var content = JSON.parse(json);
273 if(callback){
274 return callback(content.status);
275 }
276 }).error(function(error){
277 if(callback){
278 callback(error);
279 }else{
280 console.log(error);
281 }
282 });
283 },
284 hostPowerOff: function(callback){
285 $http({
286 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500287 url: DataService.getHost() + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500288 headers: {
289 'Accept': 'application/json',
290 'Content-Type': 'application/json'
291 },
292 withCredentials: true,
293 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Off"})
294 }).success(function(response){
295 var json = JSON.stringify(response);
296 var content = JSON.parse(json);
297 if(callback){
298 return callback(content.status);
299 }
300 }).error(function(error){
301 if(callback){
302 callback(error);
303 }else{
304 console.log(error);
305 }
306 });
307 },
308 hostReboot: function(callback){
309 $http({
310 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500311 url: DataService.getHost() + "/xyz/openbmc_project/state/host0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500312 headers: {
313 'Accept': 'application/json',
314 'Content-Type': 'application/json'
315 },
316 withCredentials: true,
317 data: JSON.stringify({"data": []}),
318 }).success(function(response){
319 var json = JSON.stringify(response);
320 var content = JSON.parse(json);
321 if(callback){
322 return callback(content);
323 }
324 }).error(function(error){
325 if(callback){
326 callback(error);
327 }else{
328 console.log(error);
329 }
330 });
331 },
332 hostShutdown: function(callback){
333 $http({
334 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500335 url: DataService.getHost() + "/xyz/openbmc_project/state/host0",
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500336 headers: {
337 'Accept': 'application/json',
338 'Content-Type': 'application/json'
339 },
340 withCredentials: true,
341 data: JSON.stringify({"data": []})
342 }).success(function(response){
343 var json = JSON.stringify(response);
344 var content = JSON.parse(json);
345 if(callback){
346 return callback(content);
347 }
348 }).error(function(error){
349 if(callback){
350 callback(error);
351 }else{
352 console.log(error);
353 }
354 });
Iftekharul Islamcd789502017-04-19 14:37:55 -0500355 },
Michael Davisdf3bd122017-08-10 11:03:42 -0500356 getLogs: function(){
357 var deferred = $q.defer();
Iftekharul Islamcd789502017-04-19 14:37:55 -0500358 $http({
359 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500360 url: DataService.getHost() + "/xyz/openbmc_project/logging/enumerate",
Iftekharul Islamcd789502017-04-19 14:37:55 -0500361 headers: {
362 'Accept': 'application/json',
363 'Content-Type': 'application/json'
364 },
365 withCredentials: true
366 }).success(function(response){
367 var json = JSON.stringify(response);
368 var content = JSON.parse(json);
369 var dataClone = JSON.parse(JSON.stringify(content.data));
370 var data = [];
371 var severityCode = '';
372 var priority = '';
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500373 var health = '';
Iftekharul Islamcd789502017-04-19 14:37:55 -0500374 var relatedItems = [];
375
376 for(var key in content.data){
377 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Id')){
378 var severityFlags = {low: false, medium: false, high: false};
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500379 var healthFlags = {critical: false, warning: false, good: false};
Iftekharul Islamcd789502017-04-19 14:37:55 -0500380 severityCode = content.data[key].Severity.split(".").pop();
381 priority = Constants.SEVERITY_TO_PRIORITY_MAP[severityCode];
382 severityFlags[priority.toLowerCase()] = true;
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500383 health = Constants.SEVERITY_TO_HEALTH_MAP[severityCode];
384 healthFlags[health.toLowerCase()] = true;
Iftekharul Islamcd789502017-04-19 14:37:55 -0500385 relatedItems = [];
386 content.data[key].associations.forEach(function(item){
Iftekharul Islam532763f2017-04-25 09:44:40 -0500387 relatedItems.push(item[2]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500388 });
389
390 data.push(Object.assign({
391 path: key,
392 copied: false,
393 priority: priority,
394 severity_code: severityCode,
395 severity_flags: severityFlags,
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500396 health_flags: healthFlags,
Iftekharul Islamcd789502017-04-19 14:37:55 -0500397 additional_data: content.data[key].AdditionalData.join("\n"),
398 selected: false,
Iftekharul Islam8b4828a2017-04-19 14:37:55 -0500399 search_text: ("#" + content.data[key].Id + " " + severityCode + " " + content.data[key].Severity + " " + content.data[key].AdditionalData.join(" ")).toLowerCase(),
Iftekharul Islamcd789502017-04-19 14:37:55 -0500400 meta: false,
401 confirm: false,
402 related_items: relatedItems,
403 data: {key: key, value: content.data[key]}
404 }, content.data[key]));
405 }
406 }
Michael Davisdf3bd122017-08-10 11:03:42 -0500407 deferred.resolve({data: data, original: dataClone});
Iftekharul Islamcd789502017-04-19 14:37:55 -0500408 }).error(function(error){
409 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500410 deferred.reject(error);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500411 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500412
413 return deferred.promise;
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500414 },
415 getAllSensorStatus: function(callback){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500416 $http({
417 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500418 url: DataService.getHost() + "/xyz/openbmc_project/sensors/enumerate",
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500419 headers: {
420 'Accept': 'application/json',
421 'Content-Type': 'application/json'
422 },
423 withCredentials: true
424 }).success(function(response){
425 var json = JSON.stringify(response);
426 var content = JSON.parse(json);
427 var dataClone = JSON.parse(JSON.stringify(content.data));
428 var sensorData = [];
Iftekharul Islam8947e702017-07-27 10:28:07 -0500429 var severity = {};
430 var title = "";
431 var tempKeyParts = [];
432 var order = 0;
Iftekharul Islam8a122842017-09-11 10:58:16 -0500433 var customOrder = 0;
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500434
Iftekharul Islam96bbf312017-08-22 13:44:38 -0500435 function getScaledValue(value, scale){
436 scale = scale + "";
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500437 scale = parseInt(scale, 10);
438 var power = Math.abs(parseInt(scale,10));
Iftekharul Islam96bbf312017-08-22 13:44:38 -0500439
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500440 if(scale > 0){
Iftekharul Islam96bbf312017-08-22 13:44:38 -0500441 value = value * Math.pow(10, power);
Iftekharul Islamec6bcd12017-09-06 10:49:07 -0500442 }else if(scale < 0){
Iftekharul Islam96bbf312017-08-22 13:44:38 -0500443 value = value / Math.pow(10, power);
444 }
445 return value;
446 }
447
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500448 function getSensorStatus(reading){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500449 var severityFlags = {critical: false, warning: false, normal: false}, severityText = '', order = 0;
450
451 if(reading.hasOwnProperty('CriticalLow') &&
452 reading.Value < reading.CriticalLow
453 ){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500454 severityFlags.critical = true;
455 severityText = 'critical';
Iftekharul Islam8947e702017-07-27 10:28:07 -0500456 order = 2;
457 }else if(reading.hasOwnProperty('CriticalHigh') &&
458 reading.Value > reading.CriticalHigh
459 ){
460 severityFlags.critical = true;
461 severityText = 'critical';
462 order = 2;
463 }else if(reading.hasOwnProperty('CriticalLow') &&
464 reading.hasOwnProperty('WarningLow') &&
465 reading.Value >= reading.CriticalLow && reading.Value <= reading.WarningLow){
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500466 severityFlags.warning = true;
467 severityText = 'warning';
Iftekharul Islam8947e702017-07-27 10:28:07 -0500468 order = 1;
469 }else if(reading.hasOwnProperty('WarningHigh') &&
470 reading.hasOwnProperty('CriticalHigh') &&
471 reading.Value >= reading.WarningHigh && reading.Value <= reading.CriticalHigh){
472 severityFlags.warning = true;
473 severityText = 'warning';
474 order = 1;
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500475 }else{
476 severityFlags.normal = true;
477 severityText = 'normal';
478 }
Iftekharul Islam8947e702017-07-27 10:28:07 -0500479 return { flags: severityFlags, severityText: severityText, order: order};
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500480 }
481
482 for(var key in content.data){
483 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Unit')){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500484
485 severity = getSensorStatus(content.data[key]);
486
487 if(!content.data[key].hasOwnProperty('CriticalLow')){
488 content.data[key].CriticalLow = "--";
489 content.data[key].CriticalHigh = "--";
490 }
491
492 if(!content.data[key].hasOwnProperty('WarningLow')){
493 content.data[key].WarningLow = "--";
494 content.data[key].WarningHigh = "--";
495 }
496
497 tempKeyParts = key.split("/");
498 title = tempKeyParts.pop();
499 title = tempKeyParts.pop() + '_' + title;
500 title = title.split("_").map(function(item){
501 return item.toLowerCase().charAt(0).toUpperCase() + item.slice(1);
502 }).reduce(function(prev, el){
503 return prev + " " + el;
504 });
505
Iftekharul Islam96bbf312017-08-22 13:44:38 -0500506 content.data[key].Value = getScaledValue(content.data[key].Value, content.data[key].Scale);
507
Iftekharul Islam8a122842017-09-11 10:58:16 -0500508 if(Constants.SENSOR_SORT_ORDER.indexOf(content.data[key].Unit) > -1){
509 customOrder = Constants.SENSOR_SORT_ORDER.indexOf(content.data[key].Unit);
510 }else{
511 customOrder = Constants.SENSOR_SORT_ORDER_DEFAULT;
512 }
513
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500514 sensorData.push(Object.assign({
515 path: key,
516 selected: false,
517 confirm: false,
518 copied: false,
Iftekharul Islam8947e702017-07-27 10:28:07 -0500519 title: title,
520 unit: Constants.SENSOR_UNIT_MAP[content.data[key].Unit],
521 severity_flags: severity.flags,
522 status: severity.severityText,
523 order: severity.order,
Iftekharul Islam8a122842017-09-11 10:58:16 -0500524 custom_order: customOrder,
Iftekharul Islam8947e702017-07-27 10:28:07 -0500525 search_text: (title + " " + content.data[key].Value + " " +
526 Constants.SENSOR_UNIT_MAP[content.data[key].Unit] + " " +
527 severity.severityText + " " +
528 content.data[key].CriticalLow + " " +
529 content.data[key].CriticalHigh + " " +
530 content.data[key].WarningLow + " " +
531 content.data[key].WarningHigh + " "
532 ).toLowerCase(),
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500533 original_data: {key: key, value: content.data[key]}
534 }, content.data[key]));
535 }
536 }
537
Iftekharul Islam8947e702017-07-27 10:28:07 -0500538 callback(sensorData, dataClone);
Iftekharul Islamd2269e22017-05-02 09:32:45 -0500539 }).error(function(error){
540 console.log(error);
541 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500542 },
Michael Davisdf3bd122017-08-10 11:03:42 -0500543 getFirmwares: function(){
544 var deferred = $q.defer();
Iftekharul Islamc0161392017-06-14 15:46:15 -0500545 $http({
546 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500547 url: DataService.getHost() + "/xyz/openbmc_project/software/enumerate",
Iftekharul Islamc0161392017-06-14 15:46:15 -0500548 headers: {
549 'Accept': 'application/json',
550 'Content-Type': 'application/json'
551 },
552 withCredentials: true
553 }).success(function(response){
554 var json = JSON.stringify(response);
555 var content = JSON.parse(json);
556 var data = [];
557 var active = false;
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500558 var functional = false;
559 var ready = false;
560 var activationStatus = {active: false, ready: false, functional: false};
Iftekharul Islamc0161392017-06-14 15:46:15 -0500561 var isExtended = false;
562 var bmcActiveVersion = "";
563 var hostActiveVersion = "";
564 var imageType = "";
565 var extendedVersions = [];
566
567 function getFormatedExtendedVersions(extendedVersion){
568 var versions = [];
569 extendedVersion = extendedVersion.split(",");
570
571 extendedVersion.forEach(function(item){
572 var parts = item.split("-");
573 var numberIndex = 0;
574 for(var i = 0; i < parts.length; i++){
575 if(/[0-9]/.test(parts[i])){
576 numberIndex = i;
577 break;
578 }
579 }
580 var titlePart = parts.splice(0, numberIndex);
581 titlePart = titlePart.join("");
582 titlePart = titlePart[0].toUpperCase() + titlePart.substr(1, titlePart.length);
583 var versionPart = parts.join("-");
584 versions.push({
585 title: titlePart,
586 version: versionPart
587 });
588 });
589
590 return versions;
591 }
592
593 for(var key in content.data){
594 if(content.data.hasOwnProperty(key) && content.data[key].hasOwnProperty('Version')){
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500595
596 functional = (content.data[key].Priority == 0);
Iftekharul Islamc0161392017-06-14 15:46:15 -0500597 active = (/\.Active$/).test(content.data[key].Activation);
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500598 ready = (/\.Ready$/).test(content.data[key].Activation);
599 activationStatus = {functional: functional, active: active, ready: ready};
Iftekharul Islamc0161392017-06-14 15:46:15 -0500600 imageType = content.data[key].Purpose.split(".").pop();
601 isExtended = content.data[key].hasOwnProperty('ExtendedVersion') && content.data[key].ExtendedVersion != "";
602 if(isExtended){
603 extendedVersions = getFormatedExtendedVersions(content.data[key].ExtendedVersion);
604 }
605 data.push(Object.assign({
606 path: key,
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500607 functional: functional,
608 activationFlags: activationStatus,
Iftekharul Islamc0161392017-06-14 15:46:15 -0500609 imageId: key.split("/").pop(),
610 imageType: imageType,
611 isExtended: isExtended,
612 extended: {
613 show: false,
614 versions: extendedVersions
615 },
616 data: {key: key, value: content.data[key]}
617 }, content.data[key]));
618
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500619 if(functional && imageType == 'BMC'){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500620 bmcActiveVersion = content.data[key].Version;
621 }
622
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500623 if(functional && imageType == 'Host'){
Iftekharul Islamc0161392017-06-14 15:46:15 -0500624 hostActiveVersion = content.data[key].Version;
625 }
626 }
627 }
Michael Davisdf3bd122017-08-10 11:03:42 -0500628
629 deferred.resolve({
630 data: data,
631 bmcActiveVersion: bmcActiveVersion,
632 hostActiveVersion: hostActiveVersion
633 });
Iftekharul Islamc0161392017-06-14 15:46:15 -0500634 }).error(function(error){
635 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500636 deferred.reject(error);
Iftekharul Islamc0161392017-06-14 15:46:15 -0500637 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500638
639 return deferred.promise;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500640 },
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500641 changePriority: function(imageId, priority){
642 var deferred = $q.defer();
643 $http({
644 method: 'PUT',
645 url: DataService.getHost() + "/xyz/openbmc_project/software/" + imageId + "/attr/Priority",
646 headers: {
647 'Accept': 'application/octet-stream',
648 'Content-Type': 'application/octet-stream'
649 },
650 withCredentials: true,
651 data: JSON.stringify({"data": priority})
652 }).success(function(response){
653 var json = JSON.stringify(response);
654 var content = JSON.parse(json);
655 deferred.resolve(content);
656 }).error(function(error){
657 console.log(error);
658 deferred.reject(error);
659 });
660
661 return deferred.promise;
662 },
663 uploadImage: function(file){
664 var deferred = $q.defer();
Iftekharul Islamc0161392017-06-14 15:46:15 -0500665 $http({
666 method: 'PUT',
667 timeout: 5 * 60 * 1000,
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500668 url: DataService.getHost() + "/upload/image/",
Iftekharul Islamc0161392017-06-14 15:46:15 -0500669 headers: {
670 'Accept': 'application/octet-stream',
671 'Content-Type': 'application/octet-stream'
672 },
673 withCredentials: true,
674 data: file
675 }).success(function(response){
676 var json = JSON.stringify(response);
677 var content = JSON.parse(json);
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500678 deferred.resolve(content);
Iftekharul Islamc0161392017-06-14 15:46:15 -0500679 }).error(function(error){
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500680 console.log(error);
681 deferred.reject(error);
Iftekharul Islamc0161392017-06-14 15:46:15 -0500682 });
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500683
684 return deferred.promise;
685 },
686 downloadImage: function(host, filename){
687 var deferred = $q.defer();
688 $http({
689 method: 'POST',
690 url: DataService.getHost() + "/org/openbmc/control/flash/bmc/action/updateViaTftp",
691 headers: {
692 'Accept': 'application/json',
693 'Content-Type': 'application/json'
694 },
695 withCredentials: true,
696 data: JSON.stringify({"data": [host, filename]}),
697 responseType: 'arraybuffer'
698 }).success(function(response, status, headers){
699 deferred.resolve({
700 data: response,
701 status: status,
702 headers: headers
703 });
704 }).error(function(error){
705 console.log(error);
706 deferred.reject(error);
707 });
708
709 return deferred.promise;
Iftekharul Islamc0161392017-06-14 15:46:15 -0500710 },
Michael Davisdf3bd122017-08-10 11:03:42 -0500711 getBMCEthernetInfo: function(){
712 var deferred = $q.defer();
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500713 $http({
714 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500715 url: DataService.getHost() + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc/ethernet",
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500716 headers: {
717 'Accept': 'application/json',
718 'Content-Type': 'application/json'
719 },
720 withCredentials: true
721 }).success(function(response){
Michael Davisdf3bd122017-08-10 11:03:42 -0500722 var json = JSON.stringify(response);
723 var content = JSON.parse(json);
724 deferred.resolve(content.data);
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500725 }).error(function(error){
726 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500727 deferred.reject(error);
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500728 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500729
730 return deferred.promise;
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500731 },
732 getBMCInfo: function(callback){
Michael Davisdf3bd122017-08-10 11:03:42 -0500733 var deferred = $q.defer();
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500734 $http({
735 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500736 url: DataService.getHost() + "/xyz/openbmc_project/inventory/system/chassis/motherboard/boxelder/bmc",
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500737 headers: {
738 'Accept': 'application/json',
739 'Content-Type': 'application/json'
740 },
741 withCredentials: true
742 }).success(function(response){
Michael Davisdf3bd122017-08-10 11:03:42 -0500743 var json = JSON.stringify(response);
744 var content = JSON.parse(json);
745 deferred.resolve(content.data);
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500746 }).error(function(error){
747 console.log(error);
Michael Davisdf3bd122017-08-10 11:03:42 -0500748 deferred.reject(error);
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500749 });
Michael Davisdf3bd122017-08-10 11:03:42 -0500750 return deferred.promise;
Iftekharul Islam54c22e42017-06-28 11:06:16 -0500751 },
Iftekharul Islamee27d752017-07-05 15:54:31 -0500752 getHardwares: function(callback){
753 $http({
754 method: 'GET',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500755 url: DataService.getHost() + "/xyz/openbmc_project/inventory/enumerate",
Iftekharul Islamee27d752017-07-05 15:54:31 -0500756 headers: {
757 'Accept': 'application/json',
758 'Content-Type': 'application/json'
759 },
760 withCredentials: true
761 }).success(function(response){
762 var json = JSON.stringify(response);
763 var content = JSON.parse(json);
764 var hardwareData = [];
765 var keyIndexMap = {};
766 var title = "";
767 var data = [];
768 var searchText = "";
769 var componentIndex = -1;
770 var tempParts = [];
771
772
773 function isSubComponent(key){
774
775 for(var i = 0; i < Constants.HARDWARE.parent_components.length; i++){
776 if(key.split(Constants.HARDWARE.parent_components[i]).length == 2) return true;
777 }
778
779 return false;
780 }
781
782 function titlelize(title){
783 title = title.replace(/([A-Z0-9]+)/g, " $1").replace(/^\s+/, "");
784 for(var i = 0; i < Constants.HARDWARE.uppercase_titles.length; i++){
785 if(title.toLowerCase().indexOf((Constants.HARDWARE.uppercase_titles[i] + " ")) > -1){
786 return title.toUpperCase();
787 }
788 }
789
790 return title;
791 }
792
793 function camelcaseToLabel(obj){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500794 var transformed = [], label = "", value = "";
Iftekharul Islamee27d752017-07-05 15:54:31 -0500795 for(var key in obj){
796 label = key.replace(/([A-Z0-9]+)/g, " $1").replace(/^\s+/, "");
797 if(obj[key] !== ""){
Iftekharul Islam8947e702017-07-27 10:28:07 -0500798 value = obj[key];
799 if(value == 1 || value == 0){
800 value = (value == 1) ? 'Yes' : 'No';
801 }
802 transformed.push({key:label, value: value});
Iftekharul Islamee27d752017-07-05 15:54:31 -0500803 }
804 }
805
806 return transformed;
807 }
808
809 function getSearchText(data){
810 var searchText = "";
811 for(var i = 0; i < data.length; i++){
812 searchText += " " + data[i].key + " " + data[i].value;
813 }
814
815 return searchText;
816 }
817
818 for(var key in content.data){
819 if(content.data.hasOwnProperty(key) &&
820 key.indexOf(Constants.HARDWARE.component_key_filter) == 0){
821
822 data = camelcaseToLabel(content.data[key]);
823 searchText = getSearchText(data);
824 title = key.split("/").pop();
825
826 title = titlelize(title);
827
828 if(!isSubComponent(key)){
829 hardwareData.push(Object.assign({
830 path: key,
831 title: title,
832 selected: false,
833 expanded: false,
834 search_text: title.toLowerCase() + " " + searchText.toLowerCase(),
835 sub_components: [],
836 original_data: {key: key, value: content.data[key]}
837 }, {items: data}));
838
839 keyIndexMap[key] = hardwareData.length - 1;
840 }else{
841 var tempParts = key.split("/");
842 tempParts.pop();
843 tempParts = tempParts.join("/");
844 componentIndex = keyIndexMap[tempParts];
845 data = content.data[key];
846 data.title = title;
847 hardwareData[componentIndex].sub_components.push(data);
848 hardwareData[componentIndex].search_text += " " + title.toLowerCase();
849 }
850 }
851 }
852
853 if(callback){
854 callback(hardwareData, content.data);
855 }else{
856 return { data: hardwareData, original_data: content.data};
857 }
858 });
859 },
Iftekharul Islamf2d74642017-07-10 16:42:14 -0500860 deleteLogs: function(logs) {
861 var defer = $q.defer();
862 var promises = [];
863
864 function finished(){
865 defer.resolve();
866 }
867
868 logs.forEach(function(item){
869 promises.push($http({
870 method: 'POST',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500871 url: DataService.getHost() + "/xyz/openbmc_project/logging/entry/"+item.Id+"/action/Delete",
Iftekharul Islamf2d74642017-07-10 16:42:14 -0500872 headers: {
873 'Accept': 'application/json',
874 'Content-Type': 'application/json'
875 },
876 withCredentials: true,
877 data: JSON.stringify({"data": []})
878 }));
879 });
880
881 $q.all(promises).then(finished);
882
883 return defer.promise;
884 },
885 resolveLogs: function(logs) {
886 var defer = $q.defer();
887 var promises = [];
888
889 function finished(){
890 defer.resolve();
891 }
892
893 logs.forEach(function(item){
894 promises.push($http({
895 method: 'PUT',
Iftekharul Islam1acb4122017-11-02 13:20:32 -0500896 url: DataService.getHost() + "/xyz/openbmc_project/logging/entry/"+item.Id+"/attr/Resolved",
Iftekharul Islamf2d74642017-07-10 16:42:14 -0500897 headers: {
898 'Accept': 'application/json',
899 'Content-Type': 'application/json'
900 },
901 withCredentials: true,
902 data: JSON.stringify({"data": "1"})
903 }));
904 });
905
906 $q.all(promises).then(finished);
907
908 return defer.promise;
909 },
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500910 };
911 return SERVICE;
912 }]);
913
914 })(window.angular);