1/**
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')
14        .factory('APIUtils', ['$http', 'Constants', function($http, Constants){
15          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,
22              getChassisState: function(callback){
23                $http({
24                  method: 'GET',
25                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/chassis0",
26                  headers: {
27                      'Accept': 'application/json',
28                      'Content-Type': 'application/json'
29                  },
30                  withCredentials: true
31                }).success(function(response){
32                      var json = JSON.stringify(response);
33                      var content = JSON.parse(json);
34                      callback(content.data.CurrentPowerState);
35                }).error(function(error){
36                  console.log(error);
37                });
38              },
39              getHostState: function(callback){
40                $http({
41                  method: 'GET',
42                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
43                  headers: {
44                      'Accept': 'application/json',
45                      'Content-Type': 'application/json'
46                  },
47                  withCredentials: true
48                }).success(function(response){
49                      var json = JSON.stringify(response);
50                      var content = JSON.parse(json);
51                      callback(content.data.CurrentHostState);
52                }).error(function(error){
53                  console.log(error);
54                });
55              },
56              login: function(username, password, callback){
57                $http({
58                  method: 'POST',
59                  url: SERVICE.API_CREDENTIALS.host + "/login",
60                  headers: {
61                      'Accept': 'application/json',
62                      'Content-Type': 'application/json'
63                  },
64                  withCredentials: true,
65                  data: JSON.stringify({"data": [username, password]})
66                }).success(function(response){
67                  if(callback){
68                      callback(response);
69                  }
70                }).error(function(error){
71                  if(callback){
72                      callback(null, true);
73                  }
74                  console.log(error);
75                });
76              },
77              logout: function(callback){
78                $http({
79                  method: 'POST',
80                  url: SERVICE.API_CREDENTIALS.host + "/logout",
81                  headers: {
82                      'Accept': 'application/json',
83                      'Content-Type': 'application/json'
84                  },
85                  withCredentials: true,
86                  data: JSON.stringify({"data": []})
87                }).success(function(response){
88                  if(callback){
89                      callback(response);
90                  }
91                }).error(function(error){
92                  if(callback){
93                      callback(null, error);
94                  }
95                  console.log(error);
96                });
97              },
98              chassisPowerOn: function(callback){
99                $http({
100                  method: 'POST',
101                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
102                  headers: {
103                      'Accept': 'application/json',
104                      'Content-Type': 'application/json'
105                  },
106                  withCredentials: true,
107                  data: JSON.stringify({"data": []})
108                }).success(function(response){
109                      var json = JSON.stringify(response);
110                      var content = JSON.parse(json);
111                      if(callback){
112                          return callback(content.data.CurrentPowerState);
113                      }
114                }).error(function(error){
115                  if(callback){
116                      callback(error);
117                  }else{
118                      console.log(error);
119                  }
120                });
121              },
122              chassisPowerOff: function(callback){
123                $http({
124                  method: 'POST',
125                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
126                  headers: {
127                      'Accept': 'application/json',
128                      'Content-Type': 'application/json'
129                  },
130                  withCredentials: true,
131                  data: JSON.stringify({"data": []})
132                }).success(function(response){
133                      var json = JSON.stringify(response);
134                      var content = JSON.parse(json);
135                      if(callback){
136                          return callback(content.data.CurrentPowerState);
137                      }
138                }).error(function(error){
139                  if(callback){
140                      callback(error);
141                  }else{
142                      console.log(error);
143                  }
144                });
145              },
146              bmcReboot: function(callback){
147                $http({
148                  method: 'PUT',
149                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/bmc0/attr/RequestedBmcTransition",
150                  headers: {
151                      'Accept': 'application/json',
152                      'Content-Type': 'application/json'
153                  },
154                  withCredentials: true,
155                  data: JSON.stringify({"data": "xyz.openbmc_project.State.BMC.Transition.Reboot"})
156                }).success(function(response){
157                      var json = JSON.stringify(response);
158                      var content = JSON.parse(json);
159                      if(callback){
160                          return callback(content.status);
161                      }
162                }).error(function(error){
163                  if(callback){
164                      callback(error);
165                  }else{
166                      console.log(error);
167                  }
168                });
169              },
170              hostPowerOn: function(callback){
171                $http({
172                  method: 'PUT',
173                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
174                  headers: {
175                      'Accept': 'application/json',
176                      'Content-Type': 'application/json'
177                  },
178                  withCredentials: true,
179                  data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.On"})
180                }).success(function(response){
181                      var json = JSON.stringify(response);
182                      var content = JSON.parse(json);
183                      if(callback){
184                          return callback(content.status);
185                      }
186                }).error(function(error){
187                  if(callback){
188                      callback(error);
189                  }else{
190                      console.log(error);
191                  }
192                });
193              },
194              hostPowerOff: function(callback){
195                $http({
196                  method: 'PUT',
197                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0/attr/RequestedHostTransition",
198                  headers: {
199                      'Accept': 'application/json',
200                      'Content-Type': 'application/json'
201                  },
202                  withCredentials: true,
203                  data: JSON.stringify({"data": "xyz.openbmc_project.State.Host.Transition.Off"})
204                }).success(function(response){
205                      var json = JSON.stringify(response);
206                      var content = JSON.parse(json);
207                      if(callback){
208                          return callback(content.status);
209                      }
210                }).error(function(error){
211                  if(callback){
212                      callback(error);
213                  }else{
214                      console.log(error);
215                  }
216                });
217              },
218              hostReboot: function(callback){
219                $http({
220                  method: 'POST',
221                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
222                  headers: {
223                      'Accept': 'application/json',
224                      'Content-Type': 'application/json'
225                  },
226                  withCredentials: true,
227                  data: JSON.stringify({"data": []}),
228                }).success(function(response){
229                      var json = JSON.stringify(response);
230                      var content = JSON.parse(json);
231                      if(callback){
232                          return callback(content);
233                      }
234                }).error(function(error){
235                  if(callback){
236                      callback(error);
237                  }else{
238                      console.log(error);
239                  }
240                });
241              },
242              hostShutdown: function(callback){
243                $http({
244                  method: 'POST',
245                  url: SERVICE.API_CREDENTIALS.host + "/xyz/openbmc_project/state/host0",
246                  headers: {
247                      'Accept': 'application/json',
248                      'Content-Type': 'application/json'
249                  },
250                  withCredentials: true,
251                  data: JSON.stringify({"data": []})
252                }).success(function(response){
253                      var json = JSON.stringify(response);
254                      var content = JSON.parse(json);
255                      if(callback){
256                          return callback(content);
257                      }
258                }).error(function(error){
259                  if(callback){
260                      callback(error);
261                  }else{
262                      console.log(error);
263                  }
264                });
265              }
266          };
267          return SERVICE;
268        }]);
269
270        })(window.angular);
271