blob: 23bfffb5be777552a607fd3812a02ae066dc8b18 [file] [log] [blame]
Iftekharul Islamf157d372017-03-08 11:11:27 -06001/**
2chassis
3/org/openbmc/control/chassis0 —> PowerOn ..PowerOff
4
5host reboot
6/org/openbmc/control/host0 —>reboot
7
8shutdown
9/org/openbmc/control/host0 —> shutdown
10**/
11angular
12 .module('app.services', [])
Iftekharul Islam38f681f2017-03-21 13:22:59 -050013 .service('apiInterceptor', ['$q', '$rootScope', 'dataService', function($q, $rootScope, dataService){
14 return {
15 'request': function(config){
16 dataService.server_unreachable = false;
17 dataService.loading = true;
18 return config;
19 },
20 'response': function(response){
21 dataService.loading = false;
22 dataService.last_updated = new Date();
Iftekharul Islamf157d372017-03-08 11:11:27 -060023
Iftekharul Islam38f681f2017-03-21 13:22:59 -050024 if(response == null){
25 dataService.server_unreachable = true;
26 }
Iftekharul Islamccfe2452017-03-16 10:47:04 -050027
Iftekharul Islam38f681f2017-03-21 13:22:59 -050028 if(response && response.status == 'error' &&
29 dataService.path != '/login'){
30 $rootScope.$emit('timedout-user', {});
31 }
Iftekharul Islamccfe2452017-03-16 10:47:04 -050032
Iftekharul Islam38f681f2017-03-21 13:22:59 -050033 return response;
34 },
35 'responseError': function(rejection){
36 dataService.server_unreachable = true;
37 dataService.loading = false;
38 return $q.reject(rejection);
39 }
40 };
Iftekharul Islamccfe2452017-03-16 10:47:04 -050041 }])
Iftekharul Islam38f681f2017-03-21 13:22:59 -050042 .service('Constants', function(){
43 return {
Iftekharul Islamf157d372017-03-08 11:11:27 -060044 LOGIN_CREDENTIALS: {
Iftekharul Islam38f681f2017-03-21 13:22:59 -050045 username: "test",
46 password: "testpass",
Iftekharul Islamf157d372017-03-08 11:11:27 -060047 },
48 API_CREDENTIALS: {
Iftekharul Islamf157d372017-03-08 11:11:27 -060049 host: 'https://9.3.164.147'
50 },
Iftekharul Islam38f681f2017-03-21 13:22:59 -050051 API_RESPONSE: {
52 ERROR_STATUS: 'error',
53 ERROR_MESSAGE: '401 Unauthorized',
54 SUCCESS_STATUS: 'ok',
55 SUCCESS_MESSAGE: '200 OK'
56 },
Iftekharul Islamf157d372017-03-08 11:11:27 -060057 CHASSIS_POWER_STATE: {
58 on: 'On',
59 off: 'Off'
60 },
Iftekharul Islamccfe2452017-03-16 10:47:04 -050061 HOST_STATE_TEXT: {
Iftekharul Islamf157d372017-03-08 11:11:27 -060062 on: 'Running',
63 off: 'Off',
Iftekharul Islamccfe2452017-03-16 10:47:04 -050064 booting: 'Quiesced',
65 unreachable: 'Unreachable'
66 },
67 HOST_STATE: {
68 on: 1,
69 off: -1,
70 booting: 0,
71 unreachable: -2
Iftekharul Islam38f681f2017-03-21 13:22:59 -050072 }
73 };
74 })
75 .service('dataService', ['Constants', function(Constants){
76 this.app_version = "openBMC V.0.0.1";
77 this.server_health = 'Error';
78 this.server_state = 'Unreachable';
79 this.server_status = -2;
80 this.chassis_state = 'On';
81 this.server_id = "Server 9.3.164.147";
82 this.last_updated = new Date();
83
84 this.loading = false;
85 this.server_unreachable = false;
86 this.loading_message = "";
87 this.showNavigation = false;
88 this.bodyStyle = {};
89 this.path = '';
90
91 this.setPowerOnState = function(){
92 this.server_state = Constants.HOST_STATE_TEXT.on;
93 this.server_status = Constants.HOST_STATE.on;
94 },
95
96 this.setPowerOffState = function(){
97 this.server_state = Constants.HOST_STATE_TEXT.off;
98 this.server_status = Constants.HOST_STATE.off;
99 },
100
101 this.setBootingState = function(){
102 this.server_state = Constants.HOST_STATE_TEXT.booting;
103 this.server_status = Constants.HOST_STATE.booting;
104 },
105
106 this.setUnreachableState = function(){
107 this.server_state = Constants.HOST_STATE_TEXT.unreachable;
108 this.server_status = Constants.HOST_STATE.unreachable;
109 }
110 }])
111 .factory('APIUtils', ['$http', 'Constants', function($http, Constants){
112 var SERVICE = {
113 LOGIN_CREDENTIALS: Constants.LOGIN_CREDENTIALS,
114 API_CREDENTIALS: Constants.API_CREDENTIALS,
115 API_RESPONSE: Constants.API_RESPONSE,
116 CHASSIS_POWER_STATE: Constants.CHASSIS_POWER_STATE,
117 HOST_STATE_TEXT: Constants.HOST_STATE,
118 HOST_STATE: Constants.HOST_STATE,
Iftekharul Islamf157d372017-03-08 11:11:27 -0600119 getChassisState: function(callback){
120 $http({
121 method: 'GET',
122 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/chassis0",
123 headers: {
124 'Accept': 'application/json',
125 'Content-Type': 'application/json'
126 },
127 withCredentials: true
128 }).success(function(response){
129 var json = JSON.stringify(response);
130 var content = JSON.parse(json);
131 callback(content.data.CurrentPowerState);
132 }).error(function(error){
133 console.log(error);
134 });
135 },
136 getHostState: function(callback){
137 $http({
138 method: 'GET',
139 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
140 headers: {
141 'Accept': 'application/json',
142 'Content-Type': 'application/json'
143 },
144 withCredentials: true
145 }).success(function(response){
146 var json = JSON.stringify(response);
147 var content = JSON.parse(json);
148 callback(content.data.CurrentHostState);
149 }).error(function(error){
150 console.log(error);
151 });
152 },
Iftekharul Islam38f681f2017-03-21 13:22:59 -0500153 login: function(username, password, callback){
Iftekharul Islamf157d372017-03-08 11:11:27 -0600154 $http({
155 method: 'POST',
156 url: SERVICE.API_CREDENTIALS.host + "/login",
157 headers: {
158 'Accept': 'application/json',
159 'Content-Type': 'application/json'
160 },
161 withCredentials: true,
Iftekharul Islam38f681f2017-03-21 13:22:59 -0500162 data: JSON.stringify({"data": [username, password]})
Iftekharul Islamf157d372017-03-08 11:11:27 -0600163 }).success(function(response){
164 if(callback){
165 callback(response);
166 }
167 }).error(function(error){
Iftekharul Islam38f681f2017-03-21 13:22:59 -0500168 if(callback){
169 callback(null, true);
170 }
171 console.log(error);
172 });
173 },
174 logout: function(callback){
175 $http({
176 method: 'POST',
177 url: SERVICE.API_CREDENTIALS.host + "/logout",
178 headers: {
179 'Accept': 'application/json',
180 'Content-Type': 'application/json'
181 },
182 withCredentials: true,
183 data: JSON.stringify({"data": []})
184 }).success(function(response){
185 if(callback){
186 callback(response);
187 }
188 }).error(function(error){
189 if(callback){
190 callback(null, error);
191 }
Iftekharul Islamf157d372017-03-08 11:11:27 -0600192 console.log(error);
193 });
194 },
195 chassisPowerOn: function(callback){
196 $http({
197 method: 'POST',
198 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
199 headers: {
200 'Accept': 'application/json',
201 'Content-Type': 'application/json'
202 },
203 withCredentials: true,
204 data: JSON.stringify({"data": []})
205 }).success(function(response){
206 var json = JSON.stringify(response);
207 var content = JSON.parse(json);
208 if(callback){
209 return callback(content.data.CurrentPowerState);
210 }
211 }).error(function(error){
212 if(callback){
213 callback(error);
214 }else{
215 console.log(error);
216 }
217 });
218 },
219 chassisPowerOff: function(callback){
220 $http({
221 method: 'POST',
222 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
223 headers: {
224 'Accept': 'application/json',
225 'Content-Type': 'application/json'
226 },
227 withCredentials: true,
228 data: JSON.stringify({"data": []})
229 }).success(function(response){
230 var json = JSON.stringify(response);
231 var content = JSON.parse(json);
232 if(callback){
233 return callback(content.data.CurrentPowerState);
234 }
235 }).error(function(error){
236 if(callback){
237 callback(error);
238 }else{
239 console.log(error);
240 }
241 });
242 },
243 hostPowerOn: function(callback){
Iftekharul Islamccfe2452017-03-16 10:47:04 -0500244 /**
245 curl -c cjar -b cjar -k -H "Content-Type: application/json" -d
246 "{\"data\": \"xyz.openbmc_project.State.Host.Transition.Off\"}"
247 -X PUT
248 https://9.3.164.147/xyz/openbmc_project/state/host0/attr/RequestedHostTransition
249 **/
Iftekharul Islamf157d372017-03-08 11:11:27 -0600250 $http({
Iftekharul Islamccfe2452017-03-16 10:47:04 -0500251 method: 'PUT',
252 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
Iftekharul Islamf157d372017-03-08 11:11:27 -0600253 headers: {
254 'Accept': 'application/json',
255 'Content-Type': 'application/json'
256 },
257 withCredentials: true,
Iftekharul Islamccfe2452017-03-16 10:47:04 -0500258 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.On"})
Iftekharul Islamf157d372017-03-08 11:11:27 -0600259 }).success(function(response){
260 var json = JSON.stringify(response);
261 var content = JSON.parse(json);
262 if(callback){
Iftekharul Islamccfe2452017-03-16 10:47:04 -0500263 return callback(content.status);
Iftekharul Islamf157d372017-03-08 11:11:27 -0600264 }
265 }).error(function(error){
266 if(callback){
267 callback(error);
268 }else{
269 console.log(error);
270 }
271 });
272 },
273 hostPowerOff: function(callback){
274 $http({
Iftekharul Islamccfe2452017-03-16 10:47:04 -0500275 method: 'PUT',
276 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
Iftekharul Islamf157d372017-03-08 11:11:27 -0600277 headers: {
278 'Accept': 'application/json',
279 'Content-Type': 'application/json'
280 },
281 withCredentials: true,
Iftekharul Islamccfe2452017-03-16 10:47:04 -0500282 data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Off"})
Iftekharul Islamf157d372017-03-08 11:11:27 -0600283 }).success(function(response){
284 var json = JSON.stringify(response);
285 var content = JSON.parse(json);
286 if(callback){
Iftekharul Islamccfe2452017-03-16 10:47:04 -0500287 return callback(content.status);
Iftekharul Islamf157d372017-03-08 11:11:27 -0600288 }
289 }).error(function(error){
290 if(callback){
291 callback(error);
292 }else{
293 console.log(error);
294 }
295 });
296 },
297 hostReboot: function(callback){
298 $http({
299 method: 'POST',
300 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
301 headers: {
302 'Accept': 'application/json',
303 'Content-Type': 'application/json'
304 },
305 withCredentials: true,
306 data: JSON.stringify({"data": []}),
307 }).success(function(response){
308 var json = JSON.stringify(response);
309 var content = JSON.parse(json);
310 if(callback){
311 return callback(content);
312 }
313 }).error(function(error){
314 if(callback){
315 callback(error);
316 }else{
317 console.log(error);
318 }
319 });
320 },
321 hostShutdown: function(callback){
322 $http({
323 method: 'POST',
324 url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
325 headers: {
326 'Accept': 'application/json',
327 'Content-Type': 'application/json'
328 },
329 withCredentials: true,
330 data: JSON.stringify({"data": []})
331 }).success(function(response){
332 var json = JSON.stringify(response);
333 var content = JSON.parse(json);
334 if(callback){
335 return callback(content);
336 }
337 }).error(function(error){
338 if(callback){
339 callback(error);
340 }else{
341 console.log(error);
342 }
343 });
344 }
345 };
346 return SERVICE;
Iftekharul Islam115984a2017-03-09 09:47:35 -0600347 }])
348 .factory('userModel',['APIUtils',function(APIUtils){
349 return {
Iftekharul Islam38f681f2017-03-21 13:22:59 -0500350 login : function(username, password, callback){
351 APIUtils.login(username, password, function(response, error){
352 if(response &&
353 response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS){
354 sessionStorage.setItem('LOGIN_ID', username);
355 callback(true);
356 }else{
357 callback(false, error);
358 }
359 });
Iftekharul Islam115984a2017-03-09 09:47:35 -0600360 },
361 isLoggedIn : function(){
362 if(sessionStorage.getItem('LOGIN_ID') === null){
363 return false;
364 }
Iftekharul Islam115984a2017-03-09 09:47:35 -0600365 return true;
366 },
Iftekharul Islam38f681f2017-03-21 13:22:59 -0500367 logout : function(callback){
368 APIUtils.logout(function(response, error){
369 if(response &&
370 response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS){
371 sessionStorage.removeItem('LOGIN_ID');
372 callback(true);
373 }else{
374 callback(false, error);
375 }
376 });
Iftekharul Islam115984a2017-03-09 09:47:35 -0600377 }
378 };
Iftekharul Islamf157d372017-03-08 11:11:27 -0600379 }]);