Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 1 | /** |
| 2 | * API utilities service |
| 3 | * |
| 4 | * @module app/common/services/api-utils |
| 5 | * @exports APIUtils |
| 6 | * @name APIUtils |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 9 | window.angular && (function(angular) { |
| 10 | 'use strict'; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 11 | angular.module('app.common.services').factory('APIUtils', [ |
| 12 | '$http', 'Constants', '$q', 'dataService', |
| 13 | function($http, Constants, $q, DataService) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 14 | var getScaledValue = function(value, scale) { |
| 15 | scale = scale + ''; |
| 16 | scale = parseInt(scale, 10); |
| 17 | var power = Math.abs(parseInt(scale, 10)); |
CamVan Nguyen | 3327583 | 2018-03-13 18:38:37 -0500 | [diff] [blame] | 18 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 19 | if (scale > 0) { |
| 20 | value = value * Math.pow(10, power); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 21 | } else if (scale < 0) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 22 | value = value / Math.pow(10, power); |
| 23 | } |
| 24 | return value; |
| 25 | }; |
| 26 | var SERVICE = { |
| 27 | API_CREDENTIALS: Constants.API_CREDENTIALS, |
| 28 | API_RESPONSE: Constants.API_RESPONSE, |
| 29 | CHASSIS_POWER_STATE: Constants.CHASSIS_POWER_STATE, |
| 30 | HOST_STATE_TEXT: Constants.HOST_STATE, |
| 31 | HOST_STATE: Constants.HOST_STATE, |
| 32 | LED_STATE: Constants.LED_STATE, |
| 33 | LED_STATE_TEXT: Constants.LED_STATE_TEXT, |
| 34 | HOST_SESSION_STORAGE_KEY: Constants.API_CREDENTIALS.host_storage_key, |
| 35 | getChassisState: function() { |
| 36 | var deferred = $q.defer(); |
| 37 | $http({ |
| 38 | method: 'GET', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 39 | url: DataService.getHost() + |
| 40 | '/xyz/openbmc_project/state/chassis0/attr/CurrentPowerState', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 41 | withCredentials: true |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 42 | }) |
| 43 | .then( |
| 44 | function(response) { |
| 45 | var json = JSON.stringify(response.data); |
| 46 | var content = JSON.parse(json); |
| 47 | deferred.resolve(content.data); |
| 48 | }, |
| 49 | function(error) { |
| 50 | console.log(error); |
| 51 | deferred.reject(error); |
| 52 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 53 | return deferred.promise; |
| 54 | }, |
Gunnar Mills | 6549114 | 2018-06-04 14:23:33 -0500 | [diff] [blame] | 55 | validIPV4IP: function(ip) { |
| 56 | // Checks for [0-255].[0-255].[0-255].[0-255] |
| 57 | return ip.match( |
| 58 | /\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/); |
| 59 | }, |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 60 | getHostState: function() { |
| 61 | var deferred = $q.defer(); |
| 62 | $http({ |
| 63 | method: 'GET', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 64 | url: DataService.getHost() + |
| 65 | '/xyz/openbmc_project/state/host0/attr/CurrentHostState', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 66 | withCredentials: true |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 67 | }) |
| 68 | .then( |
| 69 | function(response) { |
| 70 | var json = JSON.stringify(response.data); |
| 71 | var content = JSON.parse(json); |
| 72 | deferred.resolve(content.data); |
| 73 | }, |
| 74 | function(error) { |
| 75 | console.log(error); |
| 76 | deferred.reject(error); |
| 77 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 78 | return deferred.promise; |
| 79 | }, |
Gunnar Mills | ff64c54 | 2018-08-24 15:49:09 -0500 | [diff] [blame] | 80 | getSNMPManagers: function() { |
| 81 | return $http({ |
| 82 | method: 'GET', |
| 83 | url: DataService.getHost() + |
| 84 | '/xyz/openbmc_project/network/snmp/manager/enumerate', |
Gunnar Mills | ff64c54 | 2018-08-24 15:49:09 -0500 | [diff] [blame] | 85 | withCredentials: true |
| 86 | }) |
| 87 | .then(function(response) { |
| 88 | return response.data; |
| 89 | }); |
| 90 | }, |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 91 | getNetworkInfo: function() { |
| 92 | var deferred = $q.defer(); |
| 93 | $http({ |
| 94 | method: 'GET', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 95 | url: DataService.getHost() + |
| 96 | '/xyz/openbmc_project/network/enumerate', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 97 | withCredentials: true |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 98 | }) |
| 99 | .then( |
| 100 | function(response) { |
| 101 | var json = JSON.stringify(response.data); |
| 102 | var content = JSON.parse(json); |
| 103 | var hostname = ''; |
Gunnar Mills | e9f5fe7 | 2018-05-04 13:43:10 -0500 | [diff] [blame] | 104 | var defaultgateway = ''; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 105 | var macAddress = ''; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 106 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 107 | function parseNetworkData(content) { |
| 108 | var data = { |
| 109 | interface_ids: [], |
| 110 | interfaces: {}, |
| 111 | ip_addresses: {ipv4: [], ipv6: []}, |
| 112 | }; |
| 113 | var interfaceId = '', keyParts = [], interfaceHash = '', |
| 114 | interfaceType = ''; |
| 115 | for (var key in content.data) { |
Gunnar Mills | 3efbe2d | 2018-06-07 14:47:19 -0500 | [diff] [blame] | 116 | if (key.match(/network\/eth\d+(_\d+)?$/ig)) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 117 | interfaceId = key.split('/').pop(); |
| 118 | if (data.interface_ids.indexOf(interfaceId) == -1) { |
| 119 | data.interface_ids.push(interfaceId); |
| 120 | data.interfaces[interfaceId] = { |
| 121 | interfaceIname: '', |
| 122 | DomainName: '', |
| 123 | MACAddress: '', |
| 124 | Nameservers: [], |
| 125 | DHCPEnabled: 0, |
| 126 | ipv4: {ids: [], values: []}, |
| 127 | ipv6: {ids: [], values: []} |
| 128 | }; |
| 129 | data.interfaces[interfaceId].MACAddress = |
| 130 | content.data[key].MACAddress; |
| 131 | data.interfaces[interfaceId].DomainName = |
| 132 | content.data[key].DomainName.join(' '); |
| 133 | data.interfaces[interfaceId].Nameservers = |
| 134 | content.data[key].Nameservers; |
| 135 | data.interfaces[interfaceId].DHCPEnabled = |
| 136 | content.data[key].DHCPEnabled; |
| 137 | } |
| 138 | } else if ( |
| 139 | key.match( |
Gunnar Mills | 3efbe2d | 2018-06-07 14:47:19 -0500 | [diff] [blame] | 140 | /network\/eth\d+(_\d+)?\/ipv[4|6]\/[a-z0-9]+$/ig)) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 141 | keyParts = key.split('/'); |
| 142 | interfaceHash = keyParts.pop(); |
| 143 | interfaceType = keyParts.pop(); |
| 144 | interfaceId = keyParts.pop(); |
| 145 | |
| 146 | if (data.interfaces[interfaceId][interfaceType] |
| 147 | .ids.indexOf(interfaceHash) == -1) { |
| 148 | data.interfaces[interfaceId][interfaceType] |
| 149 | .ids.push(interfaceHash); |
| 150 | data.interfaces[interfaceId][interfaceType] |
| 151 | .values.push(content.data[key]); |
| 152 | data.ip_addresses[interfaceType].push( |
| 153 | content.data[key]['Address']); |
| 154 | } |
| 155 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 156 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 157 | return data; |
| 158 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 159 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 160 | if (content.data.hasOwnProperty( |
Gunnar Mills | e9f5fe7 | 2018-05-04 13:43:10 -0500 | [diff] [blame] | 161 | '/xyz/openbmc_project/network/config')) { |
| 162 | if (content.data['/xyz/openbmc_project/network/config'] |
| 163 | .hasOwnProperty('HostName')) { |
| 164 | hostname = |
| 165 | content.data['/xyz/openbmc_project/network/config'] |
| 166 | .HostName; |
| 167 | } |
| 168 | if (content.data['/xyz/openbmc_project/network/config'] |
| 169 | .hasOwnProperty('DefaultGateway')) { |
| 170 | defaultgateway = |
| 171 | content.data['/xyz/openbmc_project/network/config'] |
| 172 | .DefaultGateway; |
| 173 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 174 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 175 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 176 | if (content.data.hasOwnProperty( |
| 177 | '/xyz/openbmc_project/network/eth0') && |
| 178 | content.data['/xyz/openbmc_project/network/eth0'] |
| 179 | .hasOwnProperty('MACAddress')) { |
| 180 | macAddress = |
| 181 | content.data['/xyz/openbmc_project/network/eth0'] |
| 182 | .MACAddress; |
| 183 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 184 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 185 | deferred.resolve({ |
| 186 | data: content.data, |
| 187 | hostname: hostname, |
Gunnar Mills | e9f5fe7 | 2018-05-04 13:43:10 -0500 | [diff] [blame] | 188 | defaultgateway: defaultgateway, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 189 | mac_address: macAddress, |
| 190 | formatted_data: parseNetworkData(content) |
| 191 | }); |
| 192 | }, |
| 193 | function(error) { |
| 194 | console.log(error); |
| 195 | deferred.reject(error); |
| 196 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 197 | return deferred.promise; |
| 198 | }, |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 199 | setMACAddress: function(interface_name, mac_address) { |
| 200 | return $http({ |
| 201 | method: 'PUT', |
| 202 | url: DataService.getHost() + |
| 203 | '/xyz/openbmc_project/network/' + interface_name + |
| 204 | '/attr/MACAddress', |
Gunnar Mills | 7ddc727 | 2018-04-12 16:12:03 -0500 | [diff] [blame] | 205 | withCredentials: true, |
| 206 | data: JSON.stringify({'data': mac_address}) |
| 207 | }) |
| 208 | .then(function(response) { |
| 209 | return response.data; |
| 210 | }); |
| 211 | }, |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 212 | setDefaultGateway: function(defaultGateway) { |
| 213 | return $http({ |
| 214 | method: 'PUT', |
| 215 | url: DataService.getHost() + |
| 216 | '/xyz/openbmc_project/network/config/attr/DefaultGateway', |
Gunnar Mills | dca79d7 | 2018-05-30 13:07:01 -0500 | [diff] [blame] | 217 | withCredentials: true, |
| 218 | data: JSON.stringify({'data': defaultGateway}) |
| 219 | }) |
| 220 | .then(function(response) { |
| 221 | return response.data; |
| 222 | }); |
| 223 | }, |
Gunnar Mills | cb2c306 | 2018-05-31 13:13:30 -0500 | [diff] [blame] | 224 | setDHCPEnabled: function(interfaceName, dhcpEnabled) { |
| 225 | return $http({ |
| 226 | method: 'PUT', |
| 227 | url: DataService.getHost() + |
| 228 | '/xyz/openbmc_project/network/' + interfaceName + |
| 229 | '/attr/DHCPEnabled', |
Gunnar Mills | cb2c306 | 2018-05-31 13:13:30 -0500 | [diff] [blame] | 230 | withCredentials: true, |
| 231 | data: JSON.stringify({'data': dhcpEnabled}) |
| 232 | }) |
| 233 | .then(function(response) { |
| 234 | return response.data; |
| 235 | }); |
| 236 | }, |
Gunnar Mills | 0646782 | 2018-06-06 15:43:18 -0500 | [diff] [blame] | 237 | setNameservers: function(interfaceName, dnsServers) { |
| 238 | return $http({ |
| 239 | method: 'PUT', |
| 240 | url: DataService.getHost() + |
| 241 | '/xyz/openbmc_project/network/' + interfaceName + |
| 242 | '/attr/Nameservers', |
Gunnar Mills | 0646782 | 2018-06-06 15:43:18 -0500 | [diff] [blame] | 243 | withCredentials: true, |
| 244 | data: JSON.stringify({'data': dnsServers}) |
| 245 | }) |
| 246 | .then(function(response) { |
| 247 | return response.data; |
| 248 | }); |
| 249 | }, |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 250 | deleteIPV4: function(interfaceName, networkID) { |
| 251 | return $http({ |
| 252 | method: 'POST', |
| 253 | url: DataService.getHost() + |
| 254 | '/xyz/openbmc_project/network/' + interfaceName + |
| 255 | '/ipv4/' + networkID + '/action/Delete', |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 256 | withCredentials: true, |
| 257 | data: JSON.stringify({'data': []}) |
| 258 | }) |
| 259 | .then(function(response) { |
| 260 | return response.data; |
| 261 | }); |
| 262 | }, |
| 263 | addIPV4: function( |
| 264 | interfaceName, ipAddress, netmaskPrefixLength, gateway) { |
| 265 | return $http({ |
| 266 | method: 'POST', |
| 267 | url: DataService.getHost() + |
| 268 | '/xyz/openbmc_project/network/' + interfaceName + |
| 269 | '/action/IP', |
Gunnar Mills | a45c385 | 2018-05-30 16:18:45 -0500 | [diff] [blame] | 270 | withCredentials: true, |
| 271 | data: JSON.stringify({ |
| 272 | 'data': [ |
| 273 | 'xyz.openbmc_project.Network.IP.Protocol.IPv4', |
| 274 | ipAddress, +netmaskPrefixLength, gateway |
| 275 | ] |
| 276 | }) |
| 277 | }) |
| 278 | .then(function(response) { |
| 279 | return response.data; |
| 280 | }); |
| 281 | }, |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 282 | getLEDState: function() { |
| 283 | var deferred = $q.defer(); |
| 284 | $http({ |
| 285 | method: 'GET', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 286 | url: DataService.getHost() + |
| 287 | '/xyz/openbmc_project/led/groups/enclosure_identify', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 288 | withCredentials: true |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 289 | }) |
| 290 | .then( |
| 291 | function(response) { |
| 292 | var json = JSON.stringify(response.data); |
| 293 | var content = JSON.parse(json); |
| 294 | deferred.resolve(content.data.Asserted); |
| 295 | }, |
| 296 | function(error) { |
| 297 | console.log(error); |
| 298 | deferred.reject(error); |
| 299 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 300 | return deferred.promise; |
| 301 | }, |
| 302 | login: function(username, password, callback) { |
| 303 | $http({ |
| 304 | method: 'POST', |
| 305 | url: DataService.getHost() + '/login', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 306 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 307 | data: JSON.stringify({'data': [username, password]}) |
| 308 | }) |
| 309 | .then( |
| 310 | function(response) { |
| 311 | if (callback) { |
| 312 | callback(response.data); |
| 313 | } |
| 314 | }, |
| 315 | function(error) { |
| 316 | if (callback) { |
| 317 | if (error && error.status && error.status == 'error') { |
| 318 | callback(error); |
| 319 | } else { |
| 320 | callback(error, true); |
| 321 | } |
| 322 | } |
| 323 | console.log(error); |
| 324 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 325 | }, |
| 326 | testPassword: function(username, password) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 327 | // Calls /login without the current session to verify the given |
| 328 | // password is correct ignore the interceptor logout on a bad password |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 329 | return $http({ |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 330 | method: 'POST', |
| 331 | url: DataService.getHost() + '/login', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 332 | withCredentials: false, |
| 333 | data: JSON.stringify({'data': [username, password]}) |
| 334 | }) |
| 335 | .then(function(response) { |
| 336 | return response.data; |
| 337 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 338 | }, |
| 339 | logout: function(callback) { |
| 340 | $http({ |
| 341 | method: 'POST', |
| 342 | url: DataService.getHost() + '/logout', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 343 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 344 | data: JSON.stringify({'data': []}) |
| 345 | }) |
| 346 | .then( |
| 347 | function(response) { |
| 348 | if (callback) { |
| 349 | callback(response.data); |
| 350 | } |
| 351 | }, |
| 352 | function(error) { |
| 353 | if (callback) { |
| 354 | callback(null, error); |
| 355 | } |
| 356 | console.log(error); |
| 357 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 358 | }, |
| 359 | changePassword: function(user, newPassword) { |
| 360 | var deferred = $q.defer(); |
| 361 | $http({ |
| 362 | method: 'POST', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 363 | url: DataService.getHost() + '/xyz/openbmc_project/user/' + user + |
| 364 | '/action/SetPassword', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 365 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 366 | data: JSON.stringify({'data': [newPassword]}), |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 367 | responseType: 'arraybuffer' |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 368 | }) |
| 369 | .then( |
| 370 | function(response, status, headers) { |
| 371 | deferred.resolve( |
| 372 | {data: response, status: status, headers: headers}); |
| 373 | }, |
| 374 | function(error) { |
| 375 | console.log(error); |
| 376 | deferred.reject(error); |
| 377 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 378 | return deferred.promise; |
| 379 | }, |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 380 | chassisPowerOff: function() { |
| 381 | var deferred = $q.defer(); |
| 382 | $http({ |
| 383 | method: 'PUT', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 384 | url: DataService.getHost() + |
| 385 | '/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 386 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 387 | data: JSON.stringify( |
| 388 | {'data': 'xyz.openbmc_project.State.Chassis.Transition.Off'}) |
| 389 | }) |
| 390 | .then( |
| 391 | function(response) { |
| 392 | var json = JSON.stringify(response.data); |
| 393 | var content = JSON.parse(json); |
| 394 | deferred.resolve(content.status); |
| 395 | }, |
| 396 | function(error) { |
| 397 | console.log(error); |
| 398 | deferred.reject(error); |
| 399 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 400 | return deferred.promise; |
| 401 | }, |
| 402 | setLEDState: function(state, callback) { |
| 403 | $http({ |
| 404 | method: 'PUT', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 405 | url: DataService.getHost() + |
| 406 | '/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 407 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 408 | data: JSON.stringify({'data': state}) |
| 409 | }) |
| 410 | .then( |
| 411 | function(response) { |
| 412 | var json = JSON.stringify(response.data); |
| 413 | var content = JSON.parse(json); |
| 414 | if (callback) { |
| 415 | return callback(content.status); |
| 416 | } |
| 417 | }, |
| 418 | function(error) { |
| 419 | if (callback) { |
| 420 | callback(error); |
| 421 | } else { |
| 422 | console.log(error); |
| 423 | } |
| 424 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 425 | }, |
| 426 | bmcReboot: function(callback) { |
| 427 | $http({ |
| 428 | method: 'PUT', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 429 | url: DataService.getHost() + |
| 430 | '/xyz/openbmc_project/state/bmc0/attr/RequestedBmcTransition', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 431 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 432 | data: JSON.stringify( |
| 433 | {'data': 'xyz.openbmc_project.State.BMC.Transition.Reboot'}) |
| 434 | }) |
| 435 | .then( |
| 436 | function(response) { |
| 437 | var json = JSON.stringify(response.data); |
| 438 | var content = JSON.parse(json); |
| 439 | if (callback) { |
| 440 | return callback(content.status); |
| 441 | } |
| 442 | }, |
| 443 | function(error) { |
| 444 | if (callback) { |
| 445 | callback(error); |
| 446 | } else { |
| 447 | console.log(error); |
| 448 | } |
| 449 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 450 | }, |
beccabroek | bfc9990 | 2018-07-24 15:36:33 -0500 | [diff] [blame] | 451 | getLastRebootTime: function() { |
| 452 | return $http({ |
| 453 | method: 'GET', |
| 454 | url: DataService.getHost() + |
| 455 | '/xyz/openbmc_project/state/bmc0/attr/LastRebootTime', |
beccabroek | bfc9990 | 2018-07-24 15:36:33 -0500 | [diff] [blame] | 456 | withCredentials: true |
| 457 | }) |
| 458 | .then(function(response) { |
| 459 | return response.data; |
| 460 | }); |
| 461 | }, |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 462 | hostPowerOn: function() { |
| 463 | var deferred = $q.defer(); |
| 464 | $http({ |
| 465 | method: 'PUT', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 466 | url: DataService.getHost() + |
| 467 | '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 468 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 469 | data: JSON.stringify( |
| 470 | {'data': 'xyz.openbmc_project.State.Host.Transition.On'}) |
| 471 | }) |
| 472 | .then( |
| 473 | function(response) { |
| 474 | var json = JSON.stringify(response.data); |
| 475 | var content = JSON.parse(json); |
| 476 | deferred.resolve(content.status); |
| 477 | }, |
| 478 | function(error) { |
| 479 | console.log(error); |
| 480 | deferred.reject(error); |
| 481 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 482 | return deferred.promise; |
| 483 | }, |
| 484 | hostPowerOff: function() { |
| 485 | var deferred = $q.defer(); |
| 486 | $http({ |
| 487 | method: 'PUT', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 488 | url: DataService.getHost() + |
| 489 | '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 490 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 491 | data: JSON.stringify( |
| 492 | {'data': 'xyz.openbmc_project.State.Host.Transition.Off'}) |
| 493 | }) |
| 494 | .then( |
| 495 | function(response) { |
| 496 | var json = JSON.stringify(response.data); |
| 497 | var content = JSON.parse(json); |
| 498 | deferred.resolve(content.status); |
| 499 | }, |
| 500 | function(error) { |
| 501 | console.log(error); |
| 502 | deferred.reject(error); |
| 503 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 504 | return deferred.promise; |
| 505 | }, |
| 506 | hostReboot: function() { |
| 507 | var deferred = $q.defer(); |
| 508 | $http({ |
| 509 | method: 'PUT', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 510 | url: DataService.getHost() + |
| 511 | '/xyz/openbmc_project/state/host0/attr/RequestedHostTransition', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 512 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 513 | data: JSON.stringify( |
| 514 | {'data': 'xyz.openbmc_project.State.Host.Transition.Reboot'}) |
| 515 | }) |
| 516 | .then( |
| 517 | function(response) { |
| 518 | var json = JSON.stringify(response.data); |
| 519 | var content = JSON.parse(json); |
| 520 | deferred.resolve(content.status); |
| 521 | }, |
| 522 | function(error) { |
| 523 | console.log(error); |
| 524 | deferred.reject(error); |
| 525 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 526 | |
| 527 | return deferred.promise; |
| 528 | }, |
| 529 | hostShutdown: function(callback) { |
| 530 | $http({ |
| 531 | method: 'POST', |
| 532 | url: DataService.getHost() + '/xyz/openbmc_project/state/host0', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 533 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 534 | data: JSON.stringify({'data': []}) |
| 535 | }) |
| 536 | .then( |
| 537 | function(response) { |
| 538 | var json = JSON.stringify(response.data); |
| 539 | var content = JSON.parse(json); |
| 540 | if (callback) { |
| 541 | return callback(content); |
| 542 | } |
| 543 | }, |
| 544 | function(error) { |
| 545 | if (callback) { |
| 546 | callback(error); |
| 547 | } else { |
| 548 | console.log(error); |
| 549 | } |
| 550 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 551 | }, |
beccabroek | 5674425 | 2018-08-03 11:25:11 -0500 | [diff] [blame] | 552 | getLastPowerTime: function() { |
| 553 | return $http({ |
| 554 | method: 'GET', |
| 555 | url: DataService.getHost() + |
| 556 | '/xyz/openbmc_project/state/chassis0/attr/LastStateChangeTime', |
beccabroek | 5674425 | 2018-08-03 11:25:11 -0500 | [diff] [blame] | 557 | withCredentials: true |
| 558 | }) |
| 559 | .then(function(response) { |
| 560 | return response.data; |
| 561 | }); |
| 562 | }, |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 563 | getLogs: function() { |
| 564 | var deferred = $q.defer(); |
| 565 | $http({ |
| 566 | method: 'GET', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 567 | url: DataService.getHost() + |
| 568 | '/xyz/openbmc_project/logging/enumerate', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 569 | withCredentials: true |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 570 | }) |
| 571 | .then( |
| 572 | function(response) { |
| 573 | var json = JSON.stringify(response.data); |
| 574 | var content = JSON.parse(json); |
| 575 | var dataClone = JSON.parse(JSON.stringify(content.data)); |
| 576 | var data = []; |
| 577 | var severityCode = ''; |
| 578 | var priority = ''; |
| 579 | var health = ''; |
| 580 | var relatedItems = []; |
Matt Spinler | 845acdc | 2018-05-01 16:41:28 -0500 | [diff] [blame] | 581 | var eventID = 'None'; |
| 582 | var description = 'None'; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 583 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 584 | for (var key in content.data) { |
| 585 | if (content.data.hasOwnProperty(key) && |
| 586 | content.data[key].hasOwnProperty('Id')) { |
| 587 | var severityFlags = { |
| 588 | low: false, |
| 589 | medium: false, |
| 590 | high: false |
| 591 | }; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 592 | severityCode = |
| 593 | content.data[key].Severity.split('.').pop(); |
| 594 | priority = |
| 595 | Constants.SEVERITY_TO_PRIORITY_MAP[severityCode]; |
| 596 | severityFlags[priority.toLowerCase()] = true; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 597 | relatedItems = []; |
| 598 | content.data[key].associations.forEach(function(item) { |
| 599 | relatedItems.push(item[2]); |
| 600 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 601 | |
Matt Spinler | 845acdc | 2018-05-01 16:41:28 -0500 | [diff] [blame] | 602 | if (content.data[key].hasOwnProperty(['EventID'])) { |
| 603 | eventID = content.data[key].EventID; |
| 604 | } |
| 605 | |
| 606 | if (content.data[key].hasOwnProperty(['Description'])) { |
| 607 | description = content.data[key].Description; |
| 608 | } |
| 609 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 610 | data.push(Object.assign( |
| 611 | { |
| 612 | path: key, |
| 613 | copied: false, |
| 614 | priority: priority, |
| 615 | severity_code: severityCode, |
| 616 | severity_flags: severityFlags, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 617 | additional_data: |
| 618 | content.data[key].AdditionalData.join('\n'), |
| 619 | type: content.data[key].Message, |
| 620 | selected: false, |
| 621 | search_text: |
| 622 | ('#' + content.data[key].Id + ' ' + |
| 623 | severityCode + ' ' + |
Gunnar Mills | 07876ff | 2018-06-28 15:21:22 -0500 | [diff] [blame] | 624 | content.data[key].Message + ' ' + |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 625 | content.data[key].Severity + ' ' + |
| 626 | content.data[key].AdditionalData.join(' ')) |
| 627 | .toLowerCase(), |
| 628 | meta: false, |
| 629 | confirm: false, |
| 630 | related_items: relatedItems, |
Matt Spinler | 845acdc | 2018-05-01 16:41:28 -0500 | [diff] [blame] | 631 | eventID: eventID, |
| 632 | description: description, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 633 | data: {key: key, value: content.data[key]} |
| 634 | }, |
| 635 | content.data[key])); |
| 636 | } |
| 637 | } |
| 638 | deferred.resolve({data: data, original: dataClone}); |
| 639 | }, |
| 640 | function(error) { |
| 641 | console.log(error); |
| 642 | deferred.reject(error); |
| 643 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 644 | |
| 645 | return deferred.promise; |
| 646 | }, |
| 647 | getAllSensorStatus: function(callback) { |
| 648 | $http({ |
| 649 | method: 'GET', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 650 | url: DataService.getHost() + |
| 651 | '/xyz/openbmc_project/sensors/enumerate', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 652 | withCredentials: true |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 653 | }) |
| 654 | .then( |
| 655 | function(response) { |
| 656 | var json = JSON.stringify(response.data); |
| 657 | var content = JSON.parse(json); |
| 658 | var dataClone = JSON.parse(JSON.stringify(content.data)); |
| 659 | var sensorData = []; |
| 660 | var severity = {}; |
| 661 | var title = ''; |
| 662 | var tempKeyParts = []; |
| 663 | var order = 0; |
| 664 | var customOrder = 0; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 665 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 666 | function getSensorStatus(reading) { |
| 667 | var severityFlags = { |
| 668 | critical: false, |
| 669 | warning: false, |
| 670 | normal: false |
| 671 | }, |
| 672 | severityText = '', order = 0; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 673 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 674 | if (reading.hasOwnProperty('CriticalLow') && |
| 675 | reading.Value < reading.CriticalLow) { |
| 676 | severityFlags.critical = true; |
| 677 | severityText = 'critical'; |
| 678 | order = 2; |
| 679 | } else if ( |
| 680 | reading.hasOwnProperty('CriticalHigh') && |
| 681 | reading.Value > reading.CriticalHigh) { |
| 682 | severityFlags.critical = true; |
| 683 | severityText = 'critical'; |
| 684 | order = 2; |
| 685 | } else if ( |
| 686 | reading.hasOwnProperty('CriticalLow') && |
| 687 | reading.hasOwnProperty('WarningLow') && |
| 688 | reading.Value >= reading.CriticalLow && |
| 689 | reading.Value <= reading.WarningLow) { |
| 690 | severityFlags.warning = true; |
| 691 | severityText = 'warning'; |
| 692 | order = 1; |
| 693 | } else if ( |
| 694 | reading.hasOwnProperty('WarningHigh') && |
| 695 | reading.hasOwnProperty('CriticalHigh') && |
| 696 | reading.Value >= reading.WarningHigh && |
| 697 | reading.Value <= reading.CriticalHigh) { |
| 698 | severityFlags.warning = true; |
| 699 | severityText = 'warning'; |
| 700 | order = 1; |
| 701 | } else { |
| 702 | severityFlags.normal = true; |
| 703 | severityText = 'normal'; |
| 704 | } |
| 705 | return { |
| 706 | flags: severityFlags, |
| 707 | severityText: severityText, |
| 708 | order: order |
| 709 | }; |
| 710 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 711 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 712 | for (var key in content.data) { |
| 713 | if (content.data.hasOwnProperty(key) && |
| 714 | content.data[key].hasOwnProperty('Unit')) { |
| 715 | severity = getSensorStatus(content.data[key]); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 716 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 717 | if (!content.data[key].hasOwnProperty('CriticalLow')) { |
| 718 | content.data[key].CriticalLow = '--'; |
| 719 | content.data[key].CriticalHigh = '--'; |
| 720 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 721 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 722 | if (!content.data[key].hasOwnProperty('WarningLow')) { |
| 723 | content.data[key].WarningLow = '--'; |
| 724 | content.data[key].WarningHigh = '--'; |
| 725 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 726 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 727 | tempKeyParts = key.split('/'); |
| 728 | title = tempKeyParts.pop(); |
| 729 | title = tempKeyParts.pop() + '_' + title; |
| 730 | title = title.split('_') |
| 731 | .map(function(item) { |
| 732 | return item.toLowerCase() |
| 733 | .charAt(0) |
| 734 | .toUpperCase() + |
| 735 | item.slice(1); |
| 736 | }) |
| 737 | .reduce(function(prev, el) { |
| 738 | return prev + ' ' + el; |
| 739 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 740 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 741 | content.data[key].Value = getScaledValue( |
| 742 | content.data[key].Value, content.data[key].Scale); |
| 743 | content.data[key].CriticalLow = getScaledValue( |
| 744 | content.data[key].CriticalLow, |
| 745 | content.data[key].Scale); |
| 746 | content.data[key].CriticalHigh = getScaledValue( |
| 747 | content.data[key].CriticalHigh, |
| 748 | content.data[key].Scale); |
| 749 | content.data[key].WarningLow = getScaledValue( |
| 750 | content.data[key].WarningLow, |
| 751 | content.data[key].Scale); |
| 752 | content.data[key].WarningHigh = getScaledValue( |
| 753 | content.data[key].WarningHigh, |
| 754 | content.data[key].Scale); |
| 755 | if (Constants.SENSOR_SORT_ORDER.indexOf( |
| 756 | content.data[key].Unit) > -1) { |
| 757 | customOrder = Constants.SENSOR_SORT_ORDER.indexOf( |
| 758 | content.data[key].Unit); |
| 759 | } else { |
| 760 | customOrder = Constants.SENSOR_SORT_ORDER_DEFAULT; |
| 761 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 762 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 763 | sensorData.push(Object.assign( |
| 764 | { |
| 765 | path: key, |
| 766 | selected: false, |
| 767 | confirm: false, |
| 768 | copied: false, |
| 769 | title: title, |
| 770 | unit: |
| 771 | Constants |
| 772 | .SENSOR_UNIT_MAP[content.data[key].Unit], |
| 773 | severity_flags: severity.flags, |
| 774 | status: severity.severityText, |
| 775 | order: severity.order, |
| 776 | custom_order: customOrder, |
| 777 | search_text: |
| 778 | (title + ' ' + content.data[key].Value + ' ' + |
| 779 | Constants.SENSOR_UNIT_MAP[content.data[key] |
| 780 | .Unit] + |
| 781 | ' ' + severity.severityText + ' ' + |
| 782 | content.data[key].CriticalLow + ' ' + |
| 783 | content.data[key].CriticalHigh + ' ' + |
| 784 | content.data[key].WarningLow + ' ' + |
| 785 | content.data[key].WarningHigh + ' ') |
| 786 | .toLowerCase(), |
| 787 | original_data: |
| 788 | {key: key, value: content.data[key]} |
| 789 | }, |
| 790 | content.data[key])); |
| 791 | } |
| 792 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 793 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 794 | callback(sensorData, dataClone); |
| 795 | }, |
| 796 | function(error) { |
| 797 | console.log(error); |
| 798 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 799 | }, |
| 800 | getActivation: function(imageId) { |
| 801 | return $http({ |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 802 | method: 'GET', |
| 803 | url: DataService.getHost() + |
| 804 | '/xyz/openbmc_project/software/' + imageId + |
| 805 | '/attr/Activation', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 806 | withCredentials: true |
| 807 | }) |
| 808 | .then(function(response) { |
| 809 | return response.data; |
| 810 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 811 | }, |
| 812 | getFirmwares: function() { |
| 813 | var deferred = $q.defer(); |
| 814 | $http({ |
| 815 | method: 'GET', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 816 | url: DataService.getHost() + |
| 817 | '/xyz/openbmc_project/software/enumerate', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 818 | withCredentials: true |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 819 | }) |
| 820 | .then( |
| 821 | function(response) { |
| 822 | var json = JSON.stringify(response.data); |
| 823 | var content = JSON.parse(json); |
| 824 | var data = []; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 825 | var isExtended = false; |
| 826 | var bmcActiveVersion = ''; |
| 827 | var hostActiveVersion = ''; |
| 828 | var imageType = ''; |
| 829 | var extendedVersions = []; |
| 830 | var functionalImages = []; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 831 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 832 | function getFormatedExtendedVersions(extendedVersion) { |
| 833 | var versions = []; |
| 834 | extendedVersion = extendedVersion.split(','); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 835 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 836 | extendedVersion.forEach(function(item) { |
| 837 | var parts = item.split('-'); |
| 838 | var numberIndex = 0; |
| 839 | for (var i = 0; i < parts.length; i++) { |
| 840 | if (/[0-9]/.test(parts[i])) { |
| 841 | numberIndex = i; |
| 842 | break; |
| 843 | } |
| 844 | } |
| 845 | var titlePart = parts.splice(0, numberIndex); |
| 846 | titlePart = titlePart.join(''); |
| 847 | titlePart = titlePart[0].toUpperCase() + |
| 848 | titlePart.substr(1, titlePart.length); |
| 849 | var versionPart = parts.join('-'); |
| 850 | versions.push({title: titlePart, version: versionPart}); |
| 851 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 852 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 853 | return versions; |
| 854 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 855 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 856 | // Get the list of functional images so we can compare |
| 857 | // later if an image is functional |
| 858 | if (content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH]) { |
| 859 | functionalImages = |
| 860 | content.data[Constants.FIRMWARE.FUNCTIONAL_OBJPATH] |
| 861 | .endpoints; |
| 862 | } |
| 863 | for (var key in content.data) { |
| 864 | if (content.data.hasOwnProperty(key) && |
| 865 | content.data[key].hasOwnProperty('Version')) { |
Gunnar Mills | ac9131e | 2018-07-13 15:52:18 -0500 | [diff] [blame] | 866 | var activationStatus = ''; |
| 867 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 868 | // If the image is "Functional" use that for the |
| 869 | // activation status, else use the value of "Activation" |
| 870 | // github.com/openbmc/phosphor-dbus-interfaces/blob/master/xyz/openbmc_project/Software/Activation.interface.yaml |
Gunnar Mills | ac9131e | 2018-07-13 15:52:18 -0500 | [diff] [blame] | 871 | if (content.data[key].Activation) { |
| 872 | activationStatus = |
| 873 | content.data[key].Activation.split('.').pop(); |
| 874 | } |
| 875 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 876 | if (functionalImages.includes(key)) { |
| 877 | activationStatus = 'Functional'; |
| 878 | } |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 879 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 880 | imageType = content.data[key].Purpose.split('.').pop(); |
| 881 | isExtended = content.data[key].hasOwnProperty( |
| 882 | 'ExtendedVersion') && |
| 883 | content.data[key].ExtendedVersion != ''; |
| 884 | if (isExtended) { |
| 885 | extendedVersions = getFormatedExtendedVersions( |
| 886 | content.data[key].ExtendedVersion); |
| 887 | } |
| 888 | data.push(Object.assign( |
| 889 | { |
| 890 | path: key, |
| 891 | activationStatus: activationStatus, |
| 892 | imageId: key.split('/').pop(), |
| 893 | imageType: imageType, |
| 894 | isExtended: isExtended, |
| 895 | extended: |
| 896 | {show: false, versions: extendedVersions}, |
| 897 | data: {key: key, value: content.data[key]} |
| 898 | }, |
| 899 | content.data[key])); |
| 900 | |
| 901 | if (activationStatus == 'Functional' && |
| 902 | imageType == 'BMC') { |
| 903 | bmcActiveVersion = content.data[key].Version; |
| 904 | } |
| 905 | |
| 906 | if (activationStatus == 'Functional' && |
| 907 | imageType == 'Host') { |
| 908 | hostActiveVersion = content.data[key].Version; |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | deferred.resolve({ |
| 914 | data: data, |
| 915 | bmcActiveVersion: bmcActiveVersion, |
| 916 | hostActiveVersion: hostActiveVersion |
| 917 | }); |
Iftekharul Islam | 171c6a1 | 2017-08-11 08:35:47 -0500 | [diff] [blame] | 918 | }, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 919 | function(error) { |
| 920 | console.log(error); |
| 921 | deferred.reject(error); |
| 922 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 923 | |
| 924 | return deferred.promise; |
| 925 | }, |
| 926 | changePriority: function(imageId, priority) { |
| 927 | var deferred = $q.defer(); |
| 928 | $http({ |
| 929 | method: 'PUT', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 930 | url: DataService.getHost() + '/xyz/openbmc_project/software/' + |
| 931 | imageId + '/attr/Priority', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 932 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 933 | data: JSON.stringify({'data': priority}) |
| 934 | }) |
| 935 | .then( |
| 936 | function(response) { |
| 937 | var json = JSON.stringify(response.data); |
| 938 | var content = JSON.parse(json); |
| 939 | deferred.resolve(content); |
| 940 | }, |
| 941 | function(error) { |
| 942 | console.log(error); |
| 943 | deferred.reject(error); |
| 944 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 945 | |
| 946 | return deferred.promise; |
| 947 | }, |
| 948 | deleteImage: function(imageId) { |
| 949 | var deferred = $q.defer(); |
| 950 | $http({ |
| 951 | method: 'POST', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 952 | url: DataService.getHost() + '/xyz/openbmc_project/software/' + |
| 953 | imageId + '/action/Delete', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 954 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 955 | data: JSON.stringify({'data': []}) |
| 956 | }) |
| 957 | .then( |
| 958 | function(response) { |
| 959 | var json = JSON.stringify(response.data); |
| 960 | var content = JSON.parse(json); |
| 961 | deferred.resolve(content); |
| 962 | }, |
| 963 | function(error) { |
| 964 | console.log(error); |
| 965 | deferred.reject(error); |
| 966 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 967 | |
| 968 | return deferred.promise; |
| 969 | }, |
| 970 | activateImage: function(imageId) { |
| 971 | var deferred = $q.defer(); |
| 972 | $http({ |
| 973 | method: 'PUT', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 974 | url: DataService.getHost() + '/xyz/openbmc_project/software/' + |
| 975 | imageId + '/attr/RequestedActivation', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 976 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 977 | data: |
| 978 | JSON.stringify({'data': Constants.FIRMWARE.ACTIVATE_FIRMWARE}) |
| 979 | }) |
| 980 | .then( |
| 981 | function(response) { |
| 982 | var json = JSON.stringify(response.data); |
| 983 | var content = JSON.parse(json); |
| 984 | deferred.resolve(content); |
| 985 | }, |
| 986 | function(error) { |
| 987 | console.log(error); |
| 988 | deferred.reject(error); |
| 989 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 990 | |
| 991 | return deferred.promise; |
| 992 | }, |
| 993 | uploadImage: function(file) { |
| 994 | return $http({ |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 995 | method: 'POST', |
| 996 | timeout: 5 * 60 * 1000, |
| 997 | url: DataService.getHost() + '/upload/image', |
Gunnar Mills | dd9d4c3 | 2018-09-10 12:59:34 -0500 | [diff] [blame^] | 998 | // Overwrite the default 'application/json' Content-Type |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 999 | headers: {'Content-Type': 'application/octet-stream'}, |
| 1000 | withCredentials: true, |
| 1001 | data: file |
| 1002 | }) |
| 1003 | .then(function(response) { |
| 1004 | return response.data; |
| 1005 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1006 | }, |
| 1007 | downloadImage: function(host, filename) { |
| 1008 | return $http({ |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1009 | method: 'POST', |
| 1010 | url: DataService.getHost() + |
| 1011 | '/xyz/openbmc_project/software/action/DownloadViaTFTP', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1012 | withCredentials: true, |
| 1013 | data: JSON.stringify({'data': [filename, host]}), |
| 1014 | responseType: 'arraybuffer' |
| 1015 | }) |
| 1016 | .then(function(response) { |
| 1017 | return response.data; |
| 1018 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1019 | }, |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1020 | getServerInfo: function() { |
| 1021 | // TODO: openbmc/openbmc#3117 Need a way via REST to get |
| 1022 | // interfaces so we can get the system object(s) by the looking |
| 1023 | // for the system interface. |
| 1024 | return $http({ |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1025 | method: 'GET', |
| 1026 | url: DataService.getHost() + |
| 1027 | '/xyz/openbmc_project/inventory/system', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1028 | withCredentials: true |
| 1029 | }) |
| 1030 | .then(function(response) { |
| 1031 | return response.data; |
| 1032 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1033 | }, |
| 1034 | getBMCTime: function() { |
| 1035 | return $http({ |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1036 | method: 'GET', |
| 1037 | url: DataService.getHost() + '/xyz/openbmc_project/time/bmc', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1038 | withCredentials: true |
| 1039 | }) |
| 1040 | .then(function(response) { |
| 1041 | return response.data; |
| 1042 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1043 | }, |
Gunnar Mills | c74d434 | 2018-07-18 14:52:02 -0500 | [diff] [blame] | 1044 | getTime: function() { |
| 1045 | return $http({ |
| 1046 | method: 'GET', |
| 1047 | url: DataService.getHost() + |
| 1048 | '/xyz/openbmc_project/time/enumerate', |
Gunnar Mills | c74d434 | 2018-07-18 14:52:02 -0500 | [diff] [blame] | 1049 | withCredentials: true |
| 1050 | }) |
| 1051 | .then(function(response) { |
| 1052 | return response.data; |
| 1053 | }); |
| 1054 | }, |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1055 | getHardwares: function(callback) { |
| 1056 | $http({ |
| 1057 | method: 'GET', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1058 | url: DataService.getHost() + |
| 1059 | '/xyz/openbmc_project/inventory/enumerate', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1060 | withCredentials: true |
| 1061 | }).then(function(response) { |
| 1062 | var json = JSON.stringify(response.data); |
| 1063 | var content = JSON.parse(json); |
| 1064 | var hardwareData = []; |
| 1065 | var keyIndexMap = {}; |
| 1066 | var title = ''; |
| 1067 | var data = []; |
| 1068 | var searchText = ''; |
| 1069 | var componentIndex = -1; |
| 1070 | var tempParts = []; |
| 1071 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1072 | function isSubComponent(key) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1073 | for (var i = 0; i < Constants.HARDWARE.parent_components.length; |
| 1074 | i++) { |
| 1075 | if (key.split(Constants.HARDWARE.parent_components[i]).length == |
| 1076 | 2) |
| 1077 | return true; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | return false; |
| 1081 | } |
| 1082 | |
| 1083 | function titlelize(title) { |
| 1084 | title = title.replace(/([A-Z0-9]+)/g, ' $1').replace(/^\s+/, ''); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1085 | for (var i = 0; i < Constants.HARDWARE.uppercase_titles.length; |
| 1086 | i++) { |
| 1087 | if (title.toLowerCase().indexOf( |
| 1088 | (Constants.HARDWARE.uppercase_titles[i] + ' ')) > -1) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1089 | return title.toUpperCase(); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | return title; |
| 1094 | } |
| 1095 | |
| 1096 | function camelcaseToLabel(obj) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1097 | var transformed = [], label = '', value = ''; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1098 | for (var key in obj) { |
| 1099 | label = key.replace(/([A-Z0-9]+)/g, ' $1').replace(/^\s+/, ''); |
| 1100 | if (obj[key] !== '') { |
| 1101 | value = obj[key]; |
| 1102 | if (value == 1 || value == 0) { |
| 1103 | value = (value == 1) ? 'Yes' : 'No'; |
| 1104 | } |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1105 | transformed.push({key: label, value: value}); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | return transformed; |
| 1110 | } |
| 1111 | |
| 1112 | function getSearchText(data) { |
| 1113 | var searchText = ''; |
| 1114 | for (var i = 0; i < data.length; i++) { |
| 1115 | searchText += ' ' + data[i].key + ' ' + data[i].value; |
| 1116 | } |
| 1117 | |
| 1118 | return searchText; |
| 1119 | } |
| 1120 | |
| 1121 | for (var key in content.data) { |
| 1122 | if (content.data.hasOwnProperty(key) && |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1123 | key.indexOf(Constants.HARDWARE.component_key_filter) == 0) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1124 | data = camelcaseToLabel(content.data[key]); |
| 1125 | searchText = getSearchText(data); |
| 1126 | title = key.split('/').pop(); |
| 1127 | |
| 1128 | title = titlelize(title); |
| 1129 | |
| 1130 | if (!isSubComponent(key)) { |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1131 | hardwareData.push(Object.assign( |
| 1132 | { |
| 1133 | path: key, |
| 1134 | title: title, |
| 1135 | selected: false, |
| 1136 | expanded: false, |
| 1137 | search_text: title.toLowerCase() + ' ' + |
| 1138 | searchText.toLowerCase(), |
| 1139 | sub_components: [], |
| 1140 | original_data: {key: key, value: content.data[key]} |
| 1141 | }, |
| 1142 | {items: data})); |
Iftekharul Islam | 2a48955 | 2017-11-02 13:23:08 -0500 | [diff] [blame] | 1143 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1144 | keyIndexMap[key] = hardwareData.length - 1; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1145 | } else { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1146 | var tempParts = key.split('/'); |
| 1147 | tempParts.pop(); |
| 1148 | tempParts = tempParts.join('/'); |
| 1149 | componentIndex = keyIndexMap[tempParts]; |
| 1150 | data = content.data[key]; |
| 1151 | data.title = title; |
| 1152 | hardwareData[componentIndex].sub_components.push(data); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1153 | hardwareData[componentIndex].search_text += |
| 1154 | ' ' + title.toLowerCase(); |
Iftekharul Islam | 171c6a1 | 2017-08-11 08:35:47 -0500 | [diff] [blame] | 1155 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1156 | // Sort the subcomponents alphanumeric so they are displayed |
| 1157 | // on the inventory page in order (e.g. core 0, core 1, core |
| 1158 | // 2, ... core 12, core 13) |
| 1159 | hardwareData[componentIndex].sub_components.sort(function( |
| 1160 | a, b) { |
| 1161 | return a.title.localeCompare( |
| 1162 | b.title, 'en', {numeric: true}); |
Gunnar Mills | 55a8e39 | 2018-03-14 15:37:44 -0500 | [diff] [blame] | 1163 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | if (callback) { |
| 1169 | callback(hardwareData, content.data); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1170 | } else { |
| 1171 | return {data: hardwareData, original_data: content.data}; |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1172 | } |
| 1173 | }); |
| 1174 | }, |
| 1175 | deleteLogs: function(logs) { |
| 1176 | var defer = $q.defer(); |
| 1177 | var promises = []; |
| 1178 | |
| 1179 | function finished() { |
| 1180 | defer.resolve(); |
| 1181 | } |
| 1182 | |
| 1183 | logs.forEach(function(item) { |
| 1184 | promises.push($http({ |
| 1185 | method: 'POST', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1186 | url: DataService.getHost() + |
| 1187 | '/xyz/openbmc_project/logging/entry/' + item.Id + |
| 1188 | '/action/Delete', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1189 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1190 | data: JSON.stringify({'data': []}) |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1191 | })); |
| 1192 | }); |
| 1193 | |
| 1194 | $q.all(promises).then(finished); |
| 1195 | |
| 1196 | return defer.promise; |
| 1197 | }, |
| 1198 | resolveLogs: function(logs) { |
| 1199 | var defer = $q.defer(); |
| 1200 | var promises = []; |
| 1201 | |
| 1202 | function finished() { |
| 1203 | defer.resolve(); |
| 1204 | } |
| 1205 | |
| 1206 | logs.forEach(function(item) { |
| 1207 | promises.push($http({ |
| 1208 | method: 'PUT', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1209 | url: DataService.getHost() + |
| 1210 | '/xyz/openbmc_project/logging/entry/' + item.Id + |
| 1211 | '/attr/Resolved', |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1212 | withCredentials: true, |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1213 | data: JSON.stringify({'data': '1'}) |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1214 | })); |
| 1215 | }); |
Iftekharul Islam | a1d238f | 2018-02-26 12:29:45 -0600 | [diff] [blame] | 1216 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1217 | $q.all(promises).then(finished); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 1218 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1219 | return defer.promise; |
| 1220 | }, |
| 1221 | getPowerConsumption: function() { |
| 1222 | return $http({ |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1223 | method: 'GET', |
| 1224 | url: DataService.getHost() + |
| 1225 | '/xyz/openbmc_project/sensors/power/total_power', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1226 | withCredentials: true |
| 1227 | }) |
| 1228 | .then( |
| 1229 | function(response) { |
| 1230 | var json = JSON.stringify(response.data); |
| 1231 | var content = JSON.parse(json); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 1232 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1233 | return getScaledValue( |
| 1234 | content.data.Value, content.data.Scale) + |
| 1235 | ' ' + |
| 1236 | Constants.POWER_CONSUMPTION_TEXT[content.data.Unit]; |
| 1237 | }, |
| 1238 | function(error) { |
| 1239 | if ('Not Found' == error.statusText) { |
| 1240 | return Constants.POWER_CONSUMPTION_TEXT.notavailable; |
| 1241 | } else { |
| 1242 | throw error; |
| 1243 | } |
| 1244 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1245 | }, |
| 1246 | getPowerCap: function() { |
| 1247 | return $http({ |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1248 | method: 'GET', |
| 1249 | url: DataService.getHost() + |
| 1250 | '/xyz/openbmc_project/control/host0/power_cap', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1251 | withCredentials: true |
| 1252 | }) |
| 1253 | .then(function(response) { |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 1254 | return response.data; |
| 1255 | }); |
| 1256 | }, |
| 1257 | setPowerCapEnable: function(powerCapEnable) { |
| 1258 | return $http({ |
| 1259 | method: 'PUT', |
| 1260 | url: DataService.getHost() + |
| 1261 | '/xyz/openbmc_project/control/host0/power_cap/attr/PowerCapEnable', |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 1262 | withCredentials: true, |
| 1263 | data: JSON.stringify({'data': powerCapEnable}) |
| 1264 | }) |
| 1265 | .then(function(response) { |
| 1266 | return response.data; |
| 1267 | }); |
| 1268 | }, |
| 1269 | setPowerCap: function(powerCap) { |
| 1270 | return $http({ |
| 1271 | method: 'PUT', |
| 1272 | url: DataService.getHost() + |
| 1273 | '/xyz/openbmc_project/control/host0/power_cap/attr/PowerCap', |
Gunnar Mills | 006aaa0 | 2018-06-26 16:08:59 -0500 | [diff] [blame] | 1274 | withCredentials: true, |
| 1275 | data: JSON.stringify({'data': powerCap}) |
| 1276 | }) |
| 1277 | .then(function(response) { |
| 1278 | return response.data; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1279 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1280 | }, |
| 1281 | setHostname: function(hostname) { |
| 1282 | return $http({ |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1283 | method: 'PUT', |
| 1284 | url: DataService.getHost() + |
| 1285 | '/xyz/openbmc_project/network/config/attr/HostName', |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1286 | withCredentials: true, |
| 1287 | data: JSON.stringify({'data': hostname}) |
| 1288 | }) |
| 1289 | .then(function(response) { |
| 1290 | return response.data; |
| 1291 | }); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1292 | }, |
| 1293 | }; |
| 1294 | return SERVICE; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 1295 | } |
| 1296 | ]); |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 1297 | })(window.angular); |