RIFT-15007: fixed disappearing uint64 values
[osm/UI.git] / skyquake / plugins / accounts / api / accounts.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 var request = require('request');
20 var Promise = require('bluebird');
21 var rp = require('request-promise');
22 var utils = require('../../../framework/core/api_utils/utils.js');
23 var constants = require('../../../framework/core/api_utils/constants.js');
24 var _ = require('underscore');
25 var Cloud = require('./cloud_account/cloudAccount')
26 var Sdn = require('./sdn_account/sdnAccount')
27 var ConfigAgent = require('./config_agent/configAgent')
28 var Accounts = {};
29 var nameSpace = {
30 cloud: 'cloud',
31 sdn: 'sdn-account',
32 'config-agent': 'config-agent'
33 };
34 Accounts.get = function(req) {
35 return new Promise(function(resolve, reject) {
36 if (req.params.type || req.params.name) {
37 getAccount(req)
38 .then(function(data) {
39 resolve({
40 statusCode: 200,
41 data: Object.assign(data, {
42 type: req.params.type
43 })
44 });
45 })
46 } else {
47 getAll(req, resolve, reject);
48 }
49 });
50
51 function getAll(req, resolve, reject) {
52 Promise.all([
53 Cloud.get(req),
54 Sdn.get(req),
55 ConfigAgent.get(req)
56 ]).then(function(result) {
57 var ReturnData = {
58 cloud: result[0],
59 sdn: result[1],
60 'config-agent': result[2]
61 };
62 ReturnData.cloud.type = 'cloud';
63 ReturnData.sdn.type = 'sdn';
64 ReturnData['config-agent'].type = 'config';
65 resolve({
66 statusCode: 200,
67 data: ReturnData
68 });
69 })
70 }
71 }
72
73 Accounts.update = updateAccount;
74 Accounts.create = updateAccount;
75 Accounts.delete = deleteAccount;
76 Accounts.refreshAccountConnectionStatus = refreshAccountConnectionStatus
77 function getAccount(req) {
78 return new Promise(function(resolve, reject) {
79 var self = this;
80 var api_server = req.query["api_server"];
81 var id = req.params.id || req.params.name;
82 var requestHeaders = {};
83 var type = nameSpace[req.params.type];
84 var url = utils.confdPort(api_server) + '/api/operational/' + type;
85 //SDN model doesn't follow convention
86 if (type != 'sdn-account') {
87 url += '/account';
88 }
89 if (id) {
90 url += '/' + id;
91 }
92
93 _.extend(
94 requestHeaders,
95 id ? constants.HTTP_HEADERS.accept.data : constants.HTTP_HEADERS.accept.collection, {
96 'Authorization': req.get('Authorization')
97 }
98 );
99
100 request({
101 url: url + '?deep',
102 type: 'GET',
103 headers: requestHeaders,
104 forever: constants.FOREVER_ON,
105 rejectUnauthorized: false
106 },
107 function(error, response, body) {
108 var data;
109 var objKey = 'rw-' + type + ':account';
110 //SDN model doesn't follow convention
111 if (type == 'sdn-account') {
112 objKey = 'rw-sdn:sdn-account';
113 }
114 if (utils.validateResponse(type.toUpperCase() + '.get', error, response, body, resolve, reject)) {
115 try {
116 data = JSON.parse(response.body);
117 if (!id) {
118 data = data.collection;
119 }
120
121 data = data[objKey]
122 } catch (e) {
123 console.log('Problem with "' + type.toUpperCase() + '.get"', e);
124 var err = {};
125 err.statusCode = 500;
126 err.errorMessage = {
127 error: 'Problem with "' + type.toUpperCase() + '.get": ' + e
128 }
129 return reject(err);
130 }
131 return resolve({
132 statusCode: response.statusCode,
133 data: data
134 });
135 };
136 });
137 });
138 }
139
140 function updateAccount(req) {
141 var self = this;
142 var id = req.params.id || req.params.name;
143 var api_server = req.query["api_server"];
144 var type = nameSpace[req.params.type];
145 var data = req.body;
146 var requestHeaders = {};
147 var createData = {};
148 var url = utils.confdPort(api_server) + '/api/config/' + type;
149 var method = 'POST'
150 if (!id) {
151 if (type == 'sdn-account') {
152 createData = {
153 'sdn-account': Array.isArray(data) ? data : [data]
154 }
155 } else {
156 createData = {
157 'account': Array.isArray(data) ? data : [data]
158 }
159 }
160 console.log('Creating ' + type + ' account: ', createData);
161 } else {
162 method = 'PUT';
163 if (type == 'sdn-account') {
164 url += '/' + id;
165 createData['rw-sdn:sdn-account'] = Array.isArray(data) ? data : [data];
166
167 } else {
168 url += '/account/' + id;
169 createData['rw-' + type + ':account'] = Array.isArray(data) ? data : [data];
170 }
171 //createData = createData[0];
172 }
173
174
175
176 return new Promise(function(resolve, reject) {
177 _.extend(requestHeaders,
178 constants.HTTP_HEADERS.accept.data,
179 constants.HTTP_HEADERS.content_type.data, {
180 'Authorization': req.get('Authorization')
181 });
182 request({
183 url: url,
184 method: method,
185 headers: requestHeaders,
186 forever: constants.FOREVER_ON,
187 rejectUnauthorized: false,
188 json: createData,
189 }, function(error, response, body) {
190 if (utils.validateResponse(type.toUpperCase() + '.' + method, error, response, body, resolve, reject)) {
191 return resolve({
192 statusCode: response.statusCode,
193 data: JSON.stringify(response.body)
194 });
195 };
196 });
197 })
198 }
199
200 function deleteAccount(req) {
201 var self = this;
202 var id = req.params.id || req.params.name;
203 var api_server = req.query["api_server"];
204 var type = nameSpace[req.params.type];
205 var data = req.body;
206 var requestHeaders = {};
207 var createData = {};
208 var url = utils.confdPort(api_server) + '/api/config/' + type;
209 if (type == 'sdn-account') {
210 url += '/' + id;
211 } else {
212 url += '/account/' + id;
213 }
214 return new Promise(function(resolve, reject) {
215 _.extend(requestHeaders,
216 constants.HTTP_HEADERS.accept.data,
217 constants.HTTP_HEADERS.content_type.data, {
218 'Authorization': req.get('Authorization')
219 });
220 request({
221 url: url,
222 method: 'DELETE',
223 headers: requestHeaders,
224 forever: constants.FOREVER_ON,
225 rejectUnauthorized: false,
226 }, function(error, response, body) {
227 if (utils.validateResponse(type.toUpperCase() + '.DELETE', error, response, body, resolve, reject)) {
228 return resolve({
229 statusCode: response.statusCode,
230 data: JSON.stringify(response.body)
231 });
232 };
233 });
234 })
235 }
236
237 function refreshAccountConnectionStatus (req) {
238 var api_server = req.query['api_server'];
239 var Name = req.params.name;
240 var Type = req.params.type;
241 var jsonData = {
242 input: {}
243 };
244 var rpcInfo = {
245 sdn: {
246 label: 'sdn-account',
247 rpc: 'update-sdn-status'
248 },
249 'config': {
250 label: 'cfg-agent-account',
251 rpc: 'update-cfg-agent-status'
252 },
253 cloud: {
254 label: 'cloud-account',
255 rpc: 'update-cloud-status'
256 }
257 }
258 jsonData.input[rpcInfo[Type].label] = Name;
259 var headers = _.extend({},
260 constants.HTTP_HEADERS.accept.data,
261 constants.HTTP_HEADERS.content_type.data, {
262 'Authorization': req.get('Authorization')
263 }
264 );
265 return new Promise(function(resolve, reject) {
266
267 request({
268 uri: utils.confdPort(api_server) + '/api/operations/' + rpcInfo[Type].rpc,
269 method: 'POST',
270 headers: headers,
271 forever: constants.FOREVER_ON,
272 rejectUnauthorized: false,
273 json: jsonData
274 }, function(error, response, body) {
275 if (utils.validateResponse('RPC.refreshAccountConnectionStatus', error, response, body, resolve, reject)) {
276
277 resolve({
278 statusCode: response.statusCode,
279 data: body
280 });
281 }
282 });
283 }).catch(function(error) {
284 console.log('Error refreshing account info');
285 });
286 };
287
288 module.exports = Accounts;