Merge branch 'master' of https://osm.etsi.org/gerrit/osm/UI
[osm/UI.git] / skyquake / plugins / launchpad / api / launchpad.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 APIVersion = '/v1';
25 var _ = require('underscore');
26 var epa_aggregator = require('./epa_aggregator.js');
27 var transforms = require('./transforms.js');
28 var uuid = require('node-uuid');
29
30 // Revealing module pattern objects
31 var Catalog = {};
32 var Config = {};
33 var NSR = {};
34 var VNFR = {};
35 var VLR = {};
36 var RIFT = {};
37 var ComputeTopology = {};
38 var NetworkTopology = {};
39 var VDUR = {};
40 var CloudAccount = {};
41 var ConfigAgentAccount = {};
42 var RPC = {};
43 var SSHkey = {};
44 // API Configuration Info
45 var APIConfig = {}
46 APIConfig.NfviMetrics = ['vcpu', 'memory'];
47
48 RPC.executeNSServicePrimitive = function(req) {
49 var api_server = req.query['api_server'];
50 return new Promise(function(resolve, reject) {
51 var jsonData = {
52 "input": req.body
53 };
54
55 var headers = _.extend({},
56 constants.HTTP_HEADERS.accept.data,
57 constants.HTTP_HEADERS.content_type.data, {
58 'Authorization': req.get('Authorization')
59 }
60 );
61 request({
62 url: utils.confdPort(api_server) + APIVersion + '/api/operations/exec-ns-service-primitive',
63 method: 'POST',
64 headers: headers,
65 forever: constants.FOREVER_ON,
66 rejectUnauthorized: false,
67 json: jsonData
68 }, function(error, response, body) {
69 if (utils.validateResponse('RPC.executeNSServicePrimitive', error, response, body, resolve, reject)) {
70 return resolve({
71 statusCode: response.statusCode,
72 data: JSON.stringify(response.body)
73 });
74 }
75 })
76 });
77 };
78
79 RPC.getNSServicePrimitiveValues = function(req) {
80 var api_server = req.query['api_server'];
81 // var nsr_id = req.body['nsr_id_ref'];
82 // var nsConfigPrimitiveName = req.body['name'];
83 return new Promise(function(resolve, reject) {
84 var jsonData = {
85 "input": req.body
86 };
87
88 var headers = _.extend({},
89 constants.HTTP_HEADERS.accept.data,
90 constants.HTTP_HEADERS.content_type.data, {
91 'Authorization': req.get('Authorization')
92 }
93 );
94 request({
95 uri: utils.confdPort(api_server) + APIVersion + '/api/operations/get-ns-service-primitive-values',
96 method: 'POST',
97 headers: headers,
98 forever: constants.FOREVER_ON,
99 rejectUnauthorized: false,
100 json: jsonData
101 }, function(error, response, body) {
102 if (utils.validateResponse('RPC.getNSServicePrimitiveValues', error, response, body, resolve, reject)) {
103
104 resolve({
105 statusCode: response.statusCode,
106 data: JSON.parse(body)
107 });
108 }
109 });
110 }).catch(function(error) {
111 console.log('error getting primitive values');
112 });
113 };
114 RPC.refreshAccountConnectionStatus = function(req) {
115 var api_server = req.query['api_server'];
116 var Name = req.params.name;
117 var Type = req.params.type;
118 var jsonData = {
119 input: {}
120 };
121 var rpcInfo = {
122 sdn: {
123 label: 'sdn-account',
124 rpc: 'update-sdn-status'
125 },
126 config: {
127 label: 'cfg-agent-account',
128 rpc: 'update-cfg-agent-status'
129 },
130 cloud: {
131 label: 'cloud-account',
132 rpc: 'update-cloud-status'
133 }
134 }
135 jsonData.input[rpcInfo[Type].label] = Name;
136 var headers = _.extend({},
137 constants.HTTP_HEADERS.accept.data,
138 constants.HTTP_HEADERS.content_type.data, {
139 'Authorization': req.get('Authorization')
140 }
141 );
142 return new Promise(function(resolve, reject) {
143
144 request({
145 uri: utils.confdPort(api_server) + APIVersion + '/api/operations/' + rpcInfo[Type].rpc,
146 method: 'POST',
147 headers: headers,
148 forever: constants.FOREVER_ON,
149 rejectUnauthorized: false,
150 json: jsonData
151 }, function(error, response, body) {
152 if (utils.validateResponse('RPC.refreshAccountConnectionStatus', error, response, body, resolve, reject)) {
153
154 resolve({
155 statusCode: response.statusCode,
156 data: body
157 });
158 }
159 });
160 }).catch(function(error) {
161 console.log('Error refreshing account info');
162 });
163 };
164
165
166 var DataCenters = {};
167 // Catalog module methods
168 Catalog.get = function(req) {
169 var api_server = req.query['api_server'];
170 var results = {}
171 return new Promise(function(resolve, reject) {
172 Promise.all([
173 rp({
174 uri: utils.confdPort(api_server) + APIVersion + '/api/config/nsd-catalog/nsd?deep',
175 method: 'GET',
176 headers: _.extend({}, constants.HTTP_HEADERS.accept.collection, {
177 'Authorization': req.get('Authorization')
178 }),
179 forever: constants.FOREVER_ON,
180 rejectUnauthorized: false,
181 resolveWithFullResponse: true
182 }),
183 rp({
184 uri: utils.confdPort(api_server) + APIVersion + '/api/config/vnfd-catalog/vnfd?deep',
185 method: 'GET',
186 headers: _.extend({}, constants.HTTP_HEADERS.accept.collection, {
187 'Authorization': req.get('Authorization')
188 }),
189 forever: constants.FOREVER_ON,
190 rejectUnauthorized: false,
191 resolveWithFullResponse: true
192 }),
193 rp({
194 uri: utils.confdPort(api_server) + APIVersion + '/api/operational/ns-instance-opdata?deep',
195 method: 'GET',
196 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
197 'Authorization': req.get('Authorization')
198 }),
199 forever: constants.FOREVER_ON,
200 rejectUnauthorized: false,
201 resolveWithFullResponse: true
202 })
203 // Not enabled for now
204 // rp({
205 // uri: utils.confdPort(api_server) + APIVersion + '/api/config/pnfd-catalog/pnfd?deep',
206 // method: 'GET',
207 // headers: _.extend({},
208 // constants.HTTP_HEADERS.accept.collection,
209 // {
210 // 'Authorization': req.get('Authorization')
211 // }),
212 // forever: constants.FOREVER_ON,
213 // rejectUnauthorized: false,
214 // resolveWithFullResponse: true
215 // })
216 ]).then(function(result) {
217 console.log('Resolved all request promises (NSD, VNFD) successfully');
218 var response = [{
219 "id": "GUID-1",
220 "name": "RIFT.wareâ„¢ NS Descriptors Catalog",
221 "short-name": "rift.ware-nsd-cat",
222 "description": "RIFT.wareâ„¢, an open source NFV development and deployment software platform that makes it simple to create, deploy and manage hyper-scale Virtual network functions and applications.",
223 "vendor": "RIFT.io",
224 "version": "",
225 "created-on": "",
226 "type": "nsd",
227 "meta": {
228 "icon-svg": "data:image/svg+xml,%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22iso-8859-1%22%3F%3E%0A%3C!--%20Generator%3A%20Adobe%20Illustrator%2018.0.0%2C%20SVG%20Export%20Plug-In%20.%20SVG%20Version%3A%206.00%20Build%200)%20%20--%3E%0A%3C!DOCTYPE%20svg%20PUBLIC%20%22-%2F%2FW3C%2F%2FDTD%20SVG%201.1%2F%2FEN%22%20%22http%3A%2F%2Fwww.w3.org%2FGraphics%2FSVG%2F1.1%2FDTD%2Fsvg11.dtd%22%3E%0A%3Csvg%20version%3D%221.1%22%20id%3D%22connection-icon-1%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20x%3D%220px%22%20y%3D%220px%22%0A%09%20viewBox%3D%220%200%2050%2050%22%20style%3D%22enable-background%3Anew%200%200%2050%2050%3B%22%20xml%3Aspace%3D%22preserve%22%3E%0A%09%3Cpath%20d%3D%22M15%2030c-2.8%200-5-2.2-5-5s2.2-5%205-5%205%202.2%205%205-2.2%205-5%205zm0-8c-1.7%200-3%201.3-3%203s1.3%203%203%203%203-1.3%203-3-1.3-3-3-3z%22%2F%3E%3Cpath%20d%3D%22M35%2020c-2.8%200-5-2.2-5-5s2.2-5%205-5%205%202.2%205%205-2.2%205-5%205zm0-8c-1.7%200-3%201.3-3%203s1.3%203%203%203%203-1.3%203-3-1.3-3-3-3z%22%2F%3E%3Cpath%20d%3D%22M35%2040c-2.8%200-5-2.2-5-5s2.2-5%205-5%205%202.2%205%205-2.2%205-5%205zm0-8c-1.7%200-3%201.3-3%203s1.3%203%203%203%203-1.3%203-3-1.3-3-3-3z%22%2F%3E%3Cpath%20d%3D%22M19.007%2025.885l12.88%206.44-.895%201.788-12.88-6.44z%22%2F%3E%3Cpath%20d%3D%22M30.993%2015.885l.894%201.79-12.88%206.438-.894-1.79z%22%2F%3E%3C%2Fsvg%3E"
229 },
230 "descriptors": []
231 }, {
232 "id": "GUID-2",
233 "name": "RIFT.wareâ„¢ VNF Descriptors Catalog",
234 "short-name": "rift.ware-vnfd-cat",
235 "description": "RIFT.wareâ„¢, an open source NFV development and deployment software platform that makes it simple to create, deploy and manage hyper-scale Virtual network functions and applications.",
236 "vendor": "RIFT.io",
237 "version": "",
238 "created-on": "",
239 "type": "vnfd",
240 "meta": {
241 "icon-svg": "data:image/svg+xml,<?xml version=\"1.0\" encoding=\"utf-8\"?> <!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"> <svg version=\"1.1\" id=\"Layer_3\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\" width=\"100px\" height=\"100px\" viewBox=\"0 0 100 100\" enable-background=\"new 0 0 100 100\" xml:space=\"preserve\"> <g> <path d=\"M58.852,62.447l-4.662-1.033c-0.047-3.138-0.719-6.168-1.996-9.007l3.606-2.92c0.858-0.695,0.99-1.954,0.296-2.813 l-4.521-5.584c-0.334-0.413-0.818-0.675-1.346-0.731c-0.525-0.057-1.056,0.102-1.468,0.435L45.25,43.64v0 c-2.486-1.907-5.277-3.259-8.297-4.019v-4.458c0-1.104-0.896-2-2-2H27.77c-1.104,0-2,0.896-2,2v4.461 c-3.08,0.777-5.922,2.171-8.447,4.144l-3.545-2.82c-0.415-0.33-0.94-0.479-1.472-0.422c-0.527,0.06-1.009,0.327-1.339,0.743 l-4.472,5.623c-0.688,0.864-0.544,2.123,0.32,2.81l3.642,2.896v0c-1.25,2.848-1.895,5.88-1.916,9.011l-4.666,1.078 c-1.076,0.249-1.747,1.322-1.499,2.398l1.616,7.001c0.249,1.077,1.325,1.747,2.399,1.499l4.813-1.111v0 c1.429,2.681,3.344,5.017,5.691,6.943l-2.17,4.55c-0.476,0.997-0.054,2.19,0.943,2.666l6.484,3.094 c0.271,0.129,0.566,0.195,0.861,0.195c0.226,0,0.451-0.038,0.668-0.115c0.5-0.177,0.909-0.545,1.138-1.024l2.198-4.611 c2.923,0.563,5.966,0.554,8.879-0.033l2.236,4.585c0.484,0.994,1.685,1.403,2.675,0.921l6.456-3.148 c0.992-0.484,1.405-1.682,0.921-2.674l-2.206-4.524c2.335-1.946,4.231-4.301,5.639-6.999l4.812,1.067 c1.076,0.237,2.146-0.441,2.385-1.52l1.556-7.014c0.115-0.518,0.02-1.06-0.266-1.508C59.82,62.878,59.369,62.562,58.852,62.447z M40.18,61.761c0,4.859-3.953,8.812-8.813,8.812c-4.858,0-8.811-3.953-8.811-8.812s3.952-8.812,8.811-8.812 C36.227,52.949,40.18,56.902,40.18,61.761z\"/> <path d=\"M64.268,45.324c0.746,0,1.463-0.42,1.806-1.139l1.054-2.208c1.826,0.353,3.736,0.345,5.551-0.021l1.07,2.195 c0.484,0.992,1.682,1.405,2.675,0.921l2.691-1.313c0.477-0.233,0.842-0.646,1.015-1.147c0.172-0.501,0.139-1.051-0.095-1.528 l-1.052-2.155c1.458-1.214,2.645-2.686,3.527-4.377l2.278,0.504c1.075,0.238,2.146-0.442,2.386-1.52l0.647-2.923 c0.238-1.078-0.442-2.146-1.521-2.385l-2.184-0.484c-0.028-1.962-0.449-3.857-1.248-5.632l1.673-1.355 c0.412-0.334,0.675-0.818,0.73-1.345s-0.102-1.056-0.436-1.468l-1.884-2.327c-0.697-0.859-1.957-0.99-2.813-0.295l-1.614,1.307 c-1.554-1.193-3.299-2.038-5.188-2.513v-2.039c0-1.104-0.896-2-2-2h-2.994c-1.104,0-2,0.896-2,2v2.04 c-1.927,0.486-3.703,1.358-5.28,2.592l-1.634-1.298c-0.862-0.687-2.12-0.543-2.81,0.32l-1.864,2.344 c-0.33,0.416-0.481,0.945-0.422,1.472c0.061,0.527,0.327,1.009,0.743,1.339l1.69,1.345c-0.78,1.779-1.184,3.676-1.197,5.636 l-2.189,0.505c-0.517,0.119-0.965,0.439-1.246,0.889c-0.281,0.45-0.372,0.993-0.252,1.51l0.675,2.918 c0.249,1.076,1.323,1.747,2.398,1.498l2.28-0.527c0.892,1.676,2.089,3.137,3.559,4.343l-1.035,2.17 c-0.228,0.479-0.257,1.028-0.08,1.528c0.178,0.5,0.546,0.91,1.024,1.138l2.703,1.289C63.686,45.261,63.979,45.324,64.268,45.324z M64.334,27.961c0-3.039,2.473-5.51,5.512-5.51c3.038,0,5.51,2.472,5.51,5.51c0,3.039-2.472,5.511-5.51,5.511 C66.807,33.472,64.334,31,64.334,27.961z\"/> <path d=\"M96.107,66.441l-2.182-0.484c-0.028-1.961-0.449-3.856-1.25-5.632l1.675-1.355c0.412-0.334,0.675-0.818,0.73-1.346 c0.056-0.527-0.102-1.056-0.436-1.468l-1.885-2.327c-0.695-0.859-1.956-0.99-2.813-0.295l-1.614,1.307 c-1.555-1.193-3.3-2.038-5.188-2.513v-2.039c0-1.104-0.896-2-2-2h-2.994c-1.104,0-2,0.896-2,2v2.041 c-1.929,0.486-3.706,1.358-5.282,2.592l-0.001,0l-1.631-1.298c-0.415-0.331-0.938-0.482-1.472-0.422 c-0.527,0.06-1.009,0.327-1.339,0.742l-1.863,2.343c-0.688,0.865-0.544,2.123,0.32,2.811l1.691,1.345 c-0.782,1.784-1.186,3.68-1.199,5.636l-2.188,0.505c-0.517,0.12-0.965,0.439-1.246,0.889c-0.281,0.45-0.372,0.993-0.252,1.51 l0.675,2.918c0.249,1.076,1.327,1.744,2.397,1.498l2.281-0.526c0.893,1.677,2.09,3.138,3.558,4.343h0.001l-1.035,2.168 c-0.229,0.479-0.258,1.029-0.081,1.529c0.178,0.5,0.546,0.909,1.024,1.138l2.702,1.289c0.278,0.132,0.571,0.195,0.86,0.195 c0.746,0,1.463-0.42,1.806-1.139l1.054-2.208c1.828,0.353,3.739,0.347,5.552-0.021l1.071,2.194 c0.484,0.992,1.682,1.405,2.675,0.921l2.69-1.312c0.477-0.233,0.842-0.645,1.014-1.147c0.173-0.501,0.14-1.051-0.093-1.528 l-1.052-2.155c1.459-1.215,2.645-2.688,3.525-4.377l2.278,0.505c0.52,0.116,1.061,0.02,1.508-0.266 c0.447-0.285,0.763-0.736,0.878-1.254l0.647-2.923C97.866,67.748,97.186,66.681,96.107,66.441z M85.162,66.174 c0,3.039-2.471,5.511-5.508,5.511c-3.039,0-5.512-2.472-5.512-5.511c0-3.039,2.473-5.511,5.512-5.511 C82.691,60.664,85.162,63.136,85.162,66.174z\"/> </g> </svg> "
242 },
243 "descriptors": []
244 }, {
245 "id": "GUID-3",
246 "name": "RIFT.wareâ„¢ PNF Descriptors Catalog",
247 "short-name": "rift.ware-pnfd-cat",
248 "description": "RIFT.wareâ„¢, an open source NFV development and deployment software platform that makes it simple to create, deploy and manage hyper-scale Virtual network functions and applications.",
249 "vendor": "RIFT.io",
250 "version": "",
251 "created-on": "",
252 "type": "pnfd",
253 "meta": {
254 "icon-svg": "data:image/svg+xml,<?xml version=\"1.0\" encoding=\"utf-8\"?> <!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"> <svg version=\"1.1\" id=\"Layer_4\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\" width=\"100px\" height=\"100px\" viewBox=\"0 0 100 100\" enable-background=\"new 0 0 100 100\" xml:space=\"preserve\"> <path d=\"M86.334,47.444V35.759H13.666v11.686h3.561v5.111h-3.561v11.686h72.668V52.556h-4.108v-5.111H86.334z M26.628,59.454h-5.051 v-4.941h5.051V59.454z M26.628,52.404h-5.051v-4.941h5.051V52.404z M26.628,45.486h-5.051v-4.941h5.051V45.486z M34.094,59.454 h-5.051v-4.941h5.051V59.454z M34.094,52.404h-5.051v-4.941h5.051V52.404z M34.094,45.486h-5.051v-4.941h5.051V45.486z M41.452,59.454h-5.051v-4.941h5.051V59.454z M41.452,52.404h-5.051v-4.941h5.051V52.404z M41.452,45.486h-5.051v-4.941h5.051 V45.486z M48.733,59.454h-5.051v-4.941h5.051V59.454z M48.733,52.404h-5.051v-4.941h5.051V52.404z M48.733,45.486h-5.051v-4.941 h5.051V45.486z M56.2,59.454h-5.051v-4.941H56.2V59.454z M56.2,52.404h-5.051v-4.941H56.2V52.404z M56.2,45.486h-5.051v-4.941H56.2 V45.486z M63.558,59.454h-5.05v-4.941h5.05V59.454z M63.558,52.404h-5.05v-4.941h5.05V52.404z M63.558,45.486h-5.05v-4.941h5.05 V45.486z M74.858,59.312h-6.521v-3.013h6.521V59.312z M71.572,50.854c-2.875,0-5.204-2.33-5.204-5.203s2.329-5.203,5.204-5.203 s5.204,2.33,5.204,5.203S74.446,50.854,71.572,50.854z M74.858,45.618c0,1.801-1.46,3.261-3.261,3.261 c-1.8,0-3.261-1.46-3.261-3.261s1.46-3.26,3.261-3.26C73.398,42.358,74.858,43.817,74.858,45.618z\"/> </svg>"
255 },
256 "descriptors": []
257 }];
258 var vnfdCatalog = null;
259 var vnfdDict = {};
260 if (result[1].body) {
261 vnfdCatalog = JSON.parse(result[1].body).collection['vnfd:vnfd'].map(function(v, i) {
262 vnfdDict[v.id] = v['short-name'] || v.name;
263 })
264 }
265 if (result[0].body) {
266 response[0].descriptors = JSON.parse(result[0].body).collection['nsd:nsd'];
267 if (result[2].body) {
268 var data = JSON.parse(result[2].body);
269 if (data && data["nsr:ns-instance-opdata"] && data["nsr:ns-instance-opdata"]["rw-nsr:nsd-ref-count"]) {
270 var nsdRefCountCollection = data["nsr:ns-instance-opdata"]["rw-nsr:nsd-ref-count"];
271 response[0].descriptors.map(function(nsd) {
272 if (!nsd["meta"]) {
273 nsd["meta"] = {};
274 }
275 if (typeof nsd['meta'] == 'string') {
276 nsd['meta'] = JSON.parse(nsd['meta']);
277 }
278 nsd["meta"]["instance-ref-count"] = _.findWhere(nsdRefCountCollection, {
279 "nsd-id-ref": nsd.id
280 })["instance-ref-count"];
281 nsd["constituent-vnfd"] && nsd["constituent-vnfd"].map(function(v) {
282 v.name = vnfdDict[v["vnfd-id-ref"]];
283 })
284 });
285 }
286 }
287 };
288 if (result[1].body) {
289 response[1].descriptors = JSON.parse(result[1].body).collection['vnfd:vnfd'];
290 };
291 // if (result[2].body) {
292 // response[2].descriptors = JSON.parse(result[2].body).collection['pnfd:pnfd'];
293 // };
294 resolve({
295 statusCode: response.statusCode || 200,
296 data: JSON.stringify(response)
297 });
298 }).catch(function(error) {
299 // Todo: Need better logic than all or nothing.
300 // Right now even if one of the southbound APIs fails - all fail
301 var res = {};
302 console.log('Problem with Catalog.get', error);
303 res.statusCode = error.statusCode || 500;
304 res.errorMessage = {
305 error: 'Failed to get catalogs' + error
306 };
307 reject(res);
308 });
309 });
310 };
311 Catalog.delete = function(req) {
312 var api_server = req.query['api_server'];
313 var catalogType = req.params.catalogType;
314 var id = req.params.id;
315 console.log('Deleting', catalogType, id, 'from', api_server);
316 return new Promise(function(resolve, reject) {
317 request({
318 uri: utils.confdPort(api_server) + APIVersion + '/api/config/' + catalogType + '-catalog/' + catalogType + '/' + id,
319 method: 'DELETE',
320 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
321 'Authorization': req.get('Authorization')
322 }),
323 forever: constants.FOREVER_ON,
324 rejectUnauthorized: false,
325 }, function(error, response, body) {
326 if (utils.validateResponse('Catalog.delete', error, response, body, resolve, reject)) {
327 resolve({
328 statusCode: response.statusCode
329 });
330 }
331 });
332 });
333 };
334 Catalog.getVNFD = function(req) {
335 var api_server = req.query['api_server'];
336 var vnfdID = req.body.data;
337 var authorization = req.get('Authorization');
338 var VNFDs = [];
339 if (typeof(vnfdID) == "object" && vnfdID.constructor.name == "Array") {
340 vnfdID.map(function(id) {
341 VNFDs.push(requestVNFD(id));
342 });
343 } else {
344 VNFDs.push(requestVNFD(vnfdID));
345 }
346 return new Promise(function(resolve, reject) {
347 Promise.all(VNFDs).then(function(data) {
348 resolve(data)
349 }).catch(function(error) {
350 // Todo: Need better logic than all or nothing.
351 // Right now even if one of the southbound APIs fails - all fail
352 var res = {};
353 console.log('Problem with Catalog.getVNFD', error);
354 res.statusCode = 404;
355 res.errorMessage = {
356 error: 'Failed to get VNFDs' + error
357 };
358 reject(res);
359 });
360 });
361
362 function requestVNFD(id) {
363 return new Promise(function(resolve, reject) {
364 var url = utils.confdPort(api_server) + APIVersion + '/api/config/vnfd-catalog/vnfd' + (id ? '/' + id : '') + '?deep';
365 request({
366 uri: url,
367 method: 'GET',
368 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
369 'Authorization': authorization
370 }),
371 forever: constants.FOREVER_ON,
372 rejectUnauthorized: false,
373 }, function(error, response, body) {
374 if (utils.validateResponse('Catalog.getVNFD', error, response, body, resolve, reject)) {
375 var data;
376 //Is this still needed?
377 try {
378 data = JSON.parse(response.body)
379 } catch (e) {
380 reject({
381 statusCode: response ? response.statusCode : 400,
382 errorMessage: 'Issue parsing VNFD ' + id + 'from ' + utils.confdPort(api_server) + APIVersion + '/api/config/vnfd-catalog/vnfd/' + id + '?deep'
383 });
384 }
385 resolve(data);
386 }
387 });
388 });
389 }
390 };
391 Catalog.create = function(req) {
392 var api_server = req.query['api_server'];
393 var catalogType = req.params.catalogType;
394 var data = req.body;
395 console.log('Creating', catalogType, 'on', api_server);
396 var jsonData = {};
397 jsonData[catalogType] = [];
398 jsonData[catalogType].push(data);
399 return new Promise(function(resolve, reject) {
400 var requestHeaders = {};
401 _.extend(requestHeaders, constants.HTTP_HEADERS.accept.data, constants.HTTP_HEADERS.content_type.data, {
402 'Authorization': req.get('Authorization')
403 });
404 request({
405 uri: utils.confdPort(api_server) + APIVersion + '/api/config/' + catalogType + '-catalog',
406 method: 'POST',
407 headers: requestHeaders,
408 forever: constants.FOREVER_ON,
409 rejectUnauthorized: false,
410 json: jsonData
411 }, function(error, response, body) {
412 if (utils.validateResponse('Catalog.create', error, response, body, resolve, reject)) {
413 resolve({
414 statusCode: response.statusCode
415 });
416 }
417 });
418 });
419 };
420 Catalog.update = function(req) {
421 var api_server = req.query['api_server'];
422 var catalogType = req.params.catalogType;
423 var id = req.params.id;
424 var data = req.body;
425 console.log('Updating', catalogType, 'id', id, 'on', api_server);
426 var jsonData = {};
427 jsonData[catalogType] = {};
428 jsonData[catalogType] = data;
429 return new Promise(function(resolve, reject) {
430 var requestHeaders = {};
431 _.extend(requestHeaders, constants.HTTP_HEADERS.accept.data, constants.HTTP_HEADERS.content_type.data, {
432 'Authorization': req.get('Authorization')
433 });
434 request({
435 uri: utils.confdPort(api_server) + APIVersion + '/api/config/' + catalogType + '-catalog' + '/' + catalogType + '/' + id,
436 method: 'PUT',
437 headers: requestHeaders,
438 forever: constants.FOREVER_ON,
439 rejectUnauthorized: false,
440 json: jsonData
441 }, function(error, response, body) {
442 if (utils.validateResponse('Catalog.update', error, response, body, resolve, reject)) {
443 resolve({
444 statusCode: response.statusCode
445 });
446 }
447 });
448 });
449 };
450
451 Catalog.decorateNsdCatalogWithPlacementGroups = function decorateNsdCatalogWithPlacementGroups(catalog) {
452 var newData = catalog;
453 var parsedCatalog = JSON.parse(catalog.data);
454 var nsds = parsedCatalog[0].descriptors;
455 var vnfds = parsedCatalog[1].descriptors;
456 var vnfdDict = (function(){
457 var dict = {};
458 vnfds.map(function(v, i) {
459 dict[v.id] = v;
460 })
461 return dict;
462 })(vnfds);
463
464 nsds.map(function(c, i) {
465 //Rename and decorate NSD placement groups
466 c['ns-placement-groups'] = c['placement-groups'] && c['placement-groups'].map(function(p, i) {
467 //Adds vnfd name to member-vnfd entry
468 p['member-vnfd'] = p['member-vnfd'].map(function(v) {
469 v.name = vnfdDict[v['vnfd-id-ref']].name;
470 return v;
471 });
472 p['host-aggregate'] = [];
473 return p;
474 });
475
476 //Adds vnf placement groups to nsd object for UI
477 c['vnf-placement-groups'] = [];
478 c['constituent-vnfd'] && c['constituent-vnfd'].map(function(v) {
479 var vnf = vnfdDict[v['vnfd-id-ref']];
480 // var vnfPg = {
481 // name: vnf.name,
482 // 'placement-groups': vnf['placement-groups'].map(function(vp){
483 // vp['host-aggregate'] = [{}];
484 // return vp;
485 // })
486 // };
487 v['vnf-name'] = vnf.name;
488 vnf['placement-groups'] && vnf['placement-groups'].map(function(vp) {
489 vp['host-aggregate'] = [];
490 vp['vnf-name'] = vnf.name;
491 vp['vnfd-id-ref'] = v['vnfd-id-ref'];
492 vp['member-vnf-index'] = v['member-vnf-index'];
493 c['vnf-placement-groups'].push(vp);
494 })
495 })
496 return c;
497 })
498 // parsedCatalog[0].descriptors = nsds;
499 newData.data = JSON.stringify(parsedCatalog);
500 return newData;
501 }
502
503 // NSR module methods
504 // Spend some time refactoring this
505 // refactor to accept only request object
506 NSR.get = function(req) {
507 var self = this;
508 var nsrPromises = [];
509 var api_server = req.query["api_server"];
510 var id = req.params.id;
511 var nsdInfo = new Promise(function(resolve, reject) {
512 request({
513 uri: utils.confdPort(api_server) + APIVersion + '/api/config/nsd-catalog/nsd?deep',
514 method: 'GET',
515 headers: _.extend({}, constants.HTTP_HEADERS.accept.collection, {
516 'Authorization': req.get('Authorization')
517 }),
518 forever: constants.FOREVER_ON,
519 rejectUnauthorized: false,
520 }, function(error, response, body) {
521 if (utils.validateResponse('NSR.get nsd-catalog', error, response, body, resolve, reject)) {
522 var data;
523 var isString = typeof(response.body) == "string";
524 if (isString && response.body == '') return resolve('empty');
525 data = isString ? JSON.parse(response.body) : response.body;
526 var nsdData = data.collection["nsd:nsd"];
527 if (nsdData.constructor.name == "Object") {
528 nsdData = [nsdData];
529 }
530 resolve(nsdData);
531 };
532 })
533 })
534 var config = new Promise(function(resolve, reject) {
535 request({
536 uri: utils.confdPort(api_server) + APIVersion + '/api/operational/ns-instance-config/nsr' + (id ? '/' + id : '') + '?deep',
537 method: 'GET',
538 headers: _.extend({}, id ? constants.HTTP_HEADERS.accept.data : constants.HTTP_HEADERS.accept.collection, {
539 'Authorization': req.get('Authorization')
540 }),
541 forever: constants.FOREVER_ON,
542 rejectUnauthorized: false,
543 }, function(error, response, body) {
544 if (utils.validateResponse('NSR.get ns-instance-config', error, response, body, resolve, reject)) {
545 var data;
546 var isString = typeof(response.body) == "string";
547 if (isString && response.body == '') return resolve();
548 data = isString ? JSON.parse(response.body) : response.body;
549 data = id ? data : data.collection;
550 var nsrData = data["nsr:nsr"];
551 if (nsrData.constructor.name == "Object") {
552 nsrData = [nsrData];
553 }
554 resolve(nsrData);
555 };
556 });
557 });
558 var opData = new Promise(function(resolve, reject) {
559 request({
560 uri: utils.confdPort(api_server) + APIVersion + '/api/operational/ns-instance-opdata/nsr' + (id ? '/' + id : '') + '?deep',
561 method: 'GET',
562 headers: _.extend({}, id ? constants.HTTP_HEADERS.accept.data : constants.HTTP_HEADERS.accept.collection, {
563 'Authorization': req.get('Authorization')
564 }),
565 forever: constants.FOREVER_ON,
566 rejectUnauthorized: false,
567 }, function(error, response, body) {
568 if (utils.validateResponse('NSR.get ns-instance-opdata', error, response, body, resolve, reject)) {
569 var data;
570 var isString = typeof(response.body) == "string";
571 if (isString && response.body == '') return resolve();
572 data = isString ? JSON.parse(response.body) : response.body;
573 data = id ? data : data.collection;
574 var nsrData = data["nsr:nsr"];
575 if (nsrData.constructor.name == "Object") {
576 nsrData = [nsrData];
577 }
578 nsrData.forEach(self.decorateWithScalingGroupDict);
579 nsrData.forEach(self.decorateAndTransformNFVI);
580 nsrData.forEach(self.decorateAndTransformWithControls);
581 Promise.all(self.addVnfrDataPromise(req, nsrData)).then(function() {
582 Promise.all(self.addVlrDataPromise(req, nsrData)).then(function() {
583 resolve(nsrData);
584 });
585 });
586 };
587 });
588 }).catch(function(error) {
589 console.log('error getting aggregated NS opdata', error)
590 //note this will actually trigger the success callback
591 });
592 return new Promise(function(resolve, reject) {
593 //Need smarter error handling here
594 Promise.all([config, opData]).then(function(resolves) {
595 var aggregate = {};
596 // resolves[0] ==> ns-instance-config
597 // resolves[1] ==> ns-instance-opdata
598
599 var nsInstanceConfig = resolves[0] && resolves[0];
600 var nsInstanceOpdata = resolves[1] && resolves[1];
601
602 if (!nsInstanceConfig && !nsInstanceOpdata) {
603 return resolve({
604 nsrs: []
605 });
606 }
607
608 nsInstanceConfig.forEach(function(v, k) {
609 v.nsd_name = v['nsd'] && v['nsd']['name'];
610 var scaling_group_descriptor = null;
611
612 scaling_group_descriptor = v['nsd'] && v['nsd']['scaling-group-descriptor'];
613
614 if (scaling_group_descriptor) {
615 scaling_group_descriptor.map(function(sgd, sgdi) {
616 sgd['vnfd-member'] && sgd['vnfd-member'].map(function(vnfd, vnfdi) {
617 var vnfrObj = _.findWhere(_.findWhere(nsInstanceOpdata, {
618 'ns-instance-config-ref': v.id
619 }).vnfrs, {
620 'member-vnf-index-ref': vnfd['member-vnf-index-ref']
621 });
622 if (vnfrObj) {
623 vnfd['short-name'] = vnfrObj['short-name'];
624 }
625 })
626 })
627 v['scaling-group-descriptor'] = scaling_group_descriptor;
628 }
629
630 if (nsInstanceOpdata && nsInstanceOpdata.constructor.name == "Array") {
631 nsInstanceOpdata.forEach(function(w, l) {
632 if (v.id == w["ns-instance-config-ref"]) {
633 for (prop in w) {
634 if (prop != "ns-instance-config-ref" && !v.hasOwnProperty(prop)) {
635 v[prop] = w[prop];
636 }
637 }
638 }
639 });
640 }
641
642 v['scaling-group-record'] && v['scaling-group-record'].map(function(sgr) {
643 var scalingGroupName = sgr['scaling-group-name-ref'];
644 sgr['instance'] && sgr['instance'].map(function(instance) {
645 var scalingGroupInstanceId = instance['instance-id'];
646 instance['vnfrs'] && instance['vnfrs'].map(function(vnfr) {
647 var vnfrObj = _.findWhere(v['vnfrs'], {id: vnfr});
648 if (vnfrObj) {
649 vnfrObj['scaling-group-name'] = scalingGroupName;
650 vnfrObj['scaling-group-instance-id'] = scalingGroupInstanceId;
651 }
652 });
653 });
654 })
655 });
656 var nsrsData = nsInstanceConfig;
657 nsrsData.sort(function(a, b) {
658 return a["create-time"] - b["create-time"];
659 });
660 resolve({
661 nsrs: nsrsData
662 });
663 }).catch(function(error) {
664 reject({
665 statusCode: 404,
666 errorMessage: error
667 })
668 })
669 });
670 };
671 // Static VNFR Cache bu VNFR ID
672 var staticVNFRCache = {};
673
674 /**
675 * [decorateWithScalingGroupDict description]
676 * @param {[type]} nsr [description]
677 * @return {[type]}
678 {vnfr-id} : {
679 "scaling-group-name-ref": "sg1",
680 "instance-id": 0,
681 "op-status": "running",
682 "is-default": "true",
683 "create-time": 1463593760,
684 "config-status": "configuring",
685 "vnfrs": [
686 "432154e3-164e-4c05-83ee-3b56e4c898e7"
687 ]
688 }
689 */
690 NSR.decorateWithScalingGroupDict = function(nsr) {
691 var sg = nsr["scaling-group-record"];
692 var dict = {};
693 if(sg) {
694 sg.map(function(s) {
695 var sgRef = s['scaling-group-name-ref'];
696 s.instance && s.instance.map(function(si) {
697 si.vnfrs && si.vnfrs.map(function(v) {
698 dict[v] = si;
699 dict[v]["scaling-group-name-ref"] = sgRef;
700 })
701 })
702 })
703 }
704 return nsr['vnfr-scaling-groups'] = dict;
705 }
706
707
708 NSR.addVlrDataPromise = function(req, nsrs) {
709 var api_server = req.query['api_server'];
710 var promises = [];
711 nsrs.map(function(nsr) {
712 var vlrPromises = [];
713 var vlr = nsr['vlr'];
714 nsr['decorated-vlrs'] = [];
715 if (!vlr) {
716 console.log('No VL\'s found in NS');
717 }
718 vlr && vlr.map(function(vlrObject) {
719 req.params.id = vlrObject['vlr-ref'];
720 var vlrPromise = VLR.get(req).then(function(vlr) {
721 try {
722 var vlrItem = vlr['data'][0];
723 decorateNSRWithVLR(nsr, vlrObject, vlrItem);
724 } catch (e) {
725 console.log('Expection caught getting VLRs and adding to NSR:', e);
726 }
727 })
728 vlrPromises.push(vlrPromise);
729 });
730 var NSR_Promise = new Promise(function(resolve, reject) {
731 Promise.all(vlrPromises).then(function() {
732 resolve();
733 })
734 });
735 promises.push(NSR_Promise);
736 });
737 return promises;
738
739 function decorateNSRWithVLR(nsr, nsrVLRObject, vlr) {
740 var vlrObject = _.extend(nsrVLRObject, vlr);
741 vlrObject['vnfr-connection-point-ref'] && vlrObject['vnfr-connection-point-ref'].map(function(vnfrCP) {
742 var vnfrName = nsr['vnfrs'] && _.find(nsr['vnfrs'], {id: vnfrCP['vnfr-id']})['name'];
743 vnfrName && (vnfrCP['vnfr-name'] = vnfrName);
744 });
745 nsr['decorated-vlrs'].splice(_.sortedIndex(nsr['decorated-vlrs'], vlrObject, 'name'), 0, vlrObject);
746 // nsr['decorated-vlrs'].splice(_.sortedIndex(nsr['decorated-vlrs'], vlrObject, 'create-time'), 0, vlrObject);
747 }
748 }
749
750
751 NSR.addVnfrDataPromise = function(req, nsrs) {
752 var api_server = req.query['api_server'];
753 var promises = [];
754 nsrs.map(function(nsr) {
755 var epa_params = {};
756 var constituent_vnfr_ref = nsr["constituent-vnfr-ref"];
757 var vnfrPromises = [];
758 nsr["vnfrs"] = [];
759 nsr["dashboard-urls"] = [];
760 nsr['nfvi-metrics'] = [];
761 if (!constituent_vnfr_ref) {
762 console.log('Something is wrong, there are no constituent-vnfr-refs');
763 constituent_vnfr_ref = [];
764 }
765 //Get VNFR Static Data
766 constituent_vnfr_ref && constituent_vnfr_ref.map(function(constituentVnfrObj) {
767 req.params.id = constituentVnfrObj['vnfr-id'];
768 var vnfrPromise;
769 vnfrPromise = VNFR.get(req).then(function(vnfr) {
770 try {
771 var vnfrItem = vnfr[0];
772 decorateNSRWithVNFR(nsr, vnfrItem)
773 staticVNFRCache[vnfrItem.id] = vnfrItem;
774 } catch (e) {
775 console.log('Exception caught:', e);
776 }
777 });
778 vnfrPromises.push(vnfrPromise);
779 });
780 var NSR_Promise = new Promise(function(resolve, reject) {
781 Promise.all(vnfrPromises).then(function() {
782 var vnfrs = staticVNFRCache;
783 //Aggregate EPA Params
784 constituent_vnfr_ref && constituent_vnfr_ref.map(function(k) {
785 if (vnfrs[k['vnfr-id']]) {
786 epa_params = epa_aggregator(vnfrs[k['vnfr-id']].vdur, epa_params);
787 }
788 })
789 //Add VNFR Name to monitoring params
790 try {
791 if (nsr["monitoring-param"]) {
792 nsr["monitoring-param"].map(function(m) {
793 // var vnfr = vnfrs[m["vnfr-id"]] || {};
794 // m["vnfr-name"] = vnfr['name'] ? vnfr['name'] : (vnfr['short-name'] ? vnfr['short-name'] : 'VNFR');
795 var groupTag = m['group-tag'];
796 var vnfrId = m['vnfr-mon-param-ref'] && m['vnfr-mon-param-ref'][0] && m['vnfr-mon-param-ref'][0]['vnfr-id-ref'];
797 var vnfr = vnfrs[vnfrId] || {};
798 m["vnfr-name"] = vnfr['name'] ? vnfr['name'] : (vnfr['short-name'] ? vnfr['short-name'] : 'VNFR');
799 m['group-tag'] = (groupTag ? (groupTag + ' - ') : '') + m['vnfr-name'] + (vnfrId ? ' (' + vnfrId.substring(1,8) + '...)' : '');
800
801 });
802 }
803 } catch (e) {
804 console.log('Exception caught:', e);
805 }
806 resolve();
807 })
808 })
809 nsr["epa-params"] = epa_params;
810 promises.push(NSR_Promise);
811 })
812 return promises;
813
814 function decorateNSRWithVDURConsoleUrls(nsr, vnfr) {
815 nsr['console-urls'] = nsr['console-urls'] ? nsr['console-urls'] : [];
816
817 vnfr && vnfr['vdur'] && vnfr['vdur'].map(function(vdur) {
818 // This console-url is what front-end will hit to generate a real console-url
819 vdur['console-url'] = 'api/vnfr/' + vnfr.id + '/vdur/' + vdur.id + '/console-url';
820 nsr['console-urls'].push({
821 id: vdur.id,
822 name: vnfr.name,
823 'console-url': vdur['console-url']
824 });
825 });
826 }
827
828 function decorateNSRWithVNFR(nsr, vnfr) {
829 var vnfrObj = {
830 id: vnfr.id,
831 "member-vnf-index-ref": vnfr["member-vnf-index-ref"],
832 "short-name": vnfr["short-name"],
833 "vnf-configuration": vnfr["vnf-configuration"],
834 "nsr-id": nsr['ns-instance-config-ref'],
835 "name": vnfr['name'],
836 "vdur": vnfr["vdur"],
837 "cloud-account": vnfr["cloud-account"]
838 };
839 var vnfrSg = nsr['vnfr-scaling-groups'];
840 var vnfrName = vnfr["name"];
841 if(vnfrSg) {
842 if(vnfrSg[vnfr.id]) {
843 vnfrName = vnfrSg[vnfr.id]["scaling-group-name-ref"] + ':' + vnfrSg[vnfr.id][ "instance-id"] + ':' + vnfrName;
844 }
845 }
846 var vnfrNfviMetrics = buildNfviGraphs(vnfr.vdur, vnfrName);
847 if (vnfr['vnf-configuration'] && vnfr['vnf-configuration']['service-primitive'] && vnfr['vnf-configuration']['service-primitive'].length > 0) {
848 vnfrObj['service-primitives-present'] = true;
849 } else {
850 vnfrObj['service-primitives-present'] = false;
851 }
852 transforms.mergeVnfrNfviMetrics(vnfrNfviMetrics, nsr["nfvi-metrics"]);
853 //TODO: Should be sorted by create-time when it becomes available instead of id
854 // nsr["vnfrs"].splice(_.sortedIndex(nsr['vnfrs'], vnfrObj, 'create-time'), 0, vnfrObj);
855 nsr["vnfrs"].splice(_.sortedIndex(nsr['vnfrs'], vnfrObj, 'id'), 0, vnfrObj);
856 vnfrObj["dashboard-url"] = vnfr["dashboard-url"];
857 nsr["dashboard-urls"].push(vnfrObj);
858
859 decorateNSRWithVDURConsoleUrls(nsr, vnfr);
860 }
861 }
862 NSR.create = function(req) {
863 var api_server = req.query['api_server'];
864 var data = req.body.data;
865 console.log('Instantiating NSR on ', api_server);
866 return new Promise(function(resolve, reject) {
867 var requestHeaders = {};
868 _.extend(requestHeaders, constants.HTTP_HEADERS.accept.data, constants.HTTP_HEADERS.content_type.data, {
869 'Authorization': req.get('Authorization')
870 });
871 request({
872 uri: utils.confdPort(api_server) + APIVersion + '/api/config/ns-instance-config',
873 method: 'POST',
874 headers: requestHeaders,
875 forever: constants.FOREVER_ON,
876 rejectUnauthorized: false,
877 json: data
878 }, function(error, response, body) {
879 if (utils.validateResponse('NSR.create', error, response, body, resolve, reject)) {
880 var nsr_id = null;
881 try {
882 nsr_id = data.nsr[0].id;
883 } catch (e) {
884 console.log("NSR.create unable to get nsr_id. Error: %s",
885 e.toString());
886 }
887 resolve({
888 statusCode: response.statusCode,
889 data: { nsr_id: nsr_id }
890 });
891 };
892 });
893 });
894 };
895 NSR.delete = function(req) {
896 var api_server = req.query["api_server"];
897 var id = req.params.id;
898 if (!id || !api_server) {
899 return new Promise(function(resolve, reject) {
900 console.log('Must specifiy api_server and id to delete NSR');
901 return reject({
902 statusCode: 500,
903 errorMessage: {
904 error: 'Must specifiy api_server and id to delete NSR'
905 }
906 });
907 });
908 };
909 console.log('Deleting NSR with id: ' + id + 'on server: ' + api_server);
910 return new Promise(function(resolve, reject) {
911 var requestHeaders = {};
912 _.extend(requestHeaders, constants.HTTP_HEADERS.accept.data, {
913 'Authorization': req.get('Authorization')
914 });
915 request({
916 uri: utils.confdPort(api_server) + APIVersion + '/api/config/ns-instance-config/nsr/' + id,
917 method: 'DELETE',
918 headers: requestHeaders,
919 forever: constants.FOREVER_ON,
920 rejectUnauthorized: false,
921 }, function(error, response, body) {
922 if (utils.validateResponse('NSR.delete', error, response, body, resolve, reject)) {
923 resolve({
924 statusCode: response.statusCode,
925 data: JSON.stringify(response.body)
926 });
927 };
928 });
929 });
930 };
931 NSR.decorateAndTransformNFVI = function(nsr) {
932 var toDecorate = [];
933 // var metricsToUse = ["vcpu", "memory", "storage", "network"];
934 var metricsToUse = ["vcpu", "memory"];
935 try {
936 var nfviMetrics = nsr["rw-nsr:nfvi-metrics"];
937 if (nfviMetrics) {
938 metricsToUse.map(function(name) {
939 toDecorate.push(nfviMetrics[name])
940 });
941 }
942 nsr["nfvi-metrics"] = toDecorate;
943 delete nsr["rw-nsr:nfvi-metrics"];
944 } catch (e) {}
945 return nsr;
946 }
947 //Not a great pattern, Need a better way of handling logging;
948 //Refactor and move to the logging/logging.js
949 var logCache = {
950 decorateAndTransformWithControls: {}
951 }
952 NSR.decorateAndTransformWithControls = function(nsr) {
953 var controlTypes = ["action-param", "control-param"];
954 var nsControls = [];
955 var Groups = {};
956 controlTypes.map(function(control) {
957 try {
958 var controls = nsr["rw-nsr:" + control];
959 // nsControls.push(controls);
960 controls.map(function(item) {
961 if (!Groups[item["group-tag"]]) {
962 Groups[item["group-tag"]] = {};
963 Groups[item["group-tag"]]["action-param"] = []
964 Groups[item["group-tag"]]["control-param"] = []
965 }
966 Groups[item["group-tag"]][control].push(item);
967 });
968 delete nsr["rw-nsr:" + control];
969 } catch (e) {
970 var id = nsr["ns-instance-config-ref"];
971 if (!logCache.decorateAndTransformWithControls[id]) {
972 logCache.decorateAndTransformWithControls[id] = {};
973 }
974 var log = logCache.decorateAndTransformWithControls[id];
975 if (!log[control]) {
976 log[control] = true;
977 console.log('No controls exist for ' + control + ' at ' + nsr["ns-instance-config-ref"]);
978 }
979 }
980 });
981 for (k in Groups) {
982 var obj = {}
983 obj[k] = Groups[k];
984 nsControls.push(obj)
985 }
986 nsr.nsControls = nsControls;
987 return nsr;
988 };
989 NSR.setStatus = function(req) {
990 var api_server = req.query['api_server'];
991 var id = req.params.id;
992 var status = req.body.status;
993 console.log('Setting NSR (id: ' + id + ') status, on ' + api_server + ', to be: ' + status);
994 return new Promise(function(resolve, reject) {
995 var command;
996 if (typeof(status) != "string") {
997 reject({
998 'ERROR': 'NSR.setStatus Error: status is not a string type'
999 });
1000 }
1001 command = status.toUpperCase();
1002 if (command != "ENABLED" && command != "DISABLED") {
1003 reject({
1004 'ERROR': 'NSR.setStatus Error: status is: ' + command + '. It should be ENABLED or DISABLED'
1005 });
1006 }
1007 var requestHeaders = {};
1008 _.extend(requestHeaders, constants.HTTP_HEADERS.accept.data, constants.HTTP_HEADERS.content_type.data, {
1009 'Authorization': req.get('Authorization')
1010 });
1011 request({
1012 uri: utils.confdPort(api_server) + APIVersion + '/api/config/ns-instance-config/nsr/' + id + '/admin-status/',
1013 method: 'PUT',
1014 headers: requestHeaders,
1015 json: {
1016 "nsr:admin-status": command
1017 },
1018 forever: constants.FOREVER_ON,
1019 rejectUnauthorized: false,
1020 }, function(error, response, body) {
1021 if (utils.validateResponse('NSR.setStatus', error, response, body, resolve, reject)) {
1022 resolve({
1023 statusCode: response.statusCode
1024 });
1025 };
1026 });
1027 });
1028 };
1029
1030 NSR.createScalingGroupInstance = function(req) {
1031 var api_server = req.query['api_server'];
1032 var id = req.params.id;
1033 var scaling_group_id = req.params.scaling_group_id;
1034 if (!api_server || !id || !scaling_group_id) {
1035 return new Promise(function(resolve, reject) {
1036 return reject({
1037 statusCode: 500,
1038 errorMessage: {
1039 error: 'API server/NSR id/Scaling group not provided'
1040 }
1041 });
1042 });
1043 }
1044
1045 var instance_id = Math.floor(Math.random() * 65535);
1046
1047 var jsonData = {
1048 instance: [{
1049 // id: uuid.v1()
1050 id: instance_id
1051 }]
1052 };
1053
1054 console.log('Creating scaling group instance for NSR ', id, ', scaling group ', scaling_group_id, ' with instance id ', instance_id);
1055
1056 return new Promise(function(resolve, reject) {
1057 var requestHeaders = {};
1058 _.extend(requestHeaders,
1059 constants.HTTP_HEADERS.accept.data,
1060 constants.HTTP_HEADERS.content_type.data,
1061 {
1062 'Authorization': req.get('Authorization')
1063 }
1064 );
1065
1066 request({
1067 uri: utils.confdPort(api_server) + APIVersion + '/api/config/ns-instance-config/nsr/' + id + '/scaling-group/' + scaling_group_id + '/instance',
1068 method: 'POST',
1069 headers: requestHeaders,
1070 json: jsonData,
1071 forever: constants.FOREVER_ON,
1072 rejectUnauthorized: false
1073 }, function (error, response, body) {
1074 if (utils.validateResponse('NSR.createScalingGroupInstance', error, response, body, resolve, reject)) {
1075 resolve({
1076 statusCode: response.statusCode,
1077 data: typeof response.body == 'string' ? JSON.parse(response.body):response.body
1078 })
1079 }
1080 });
1081 });
1082 };
1083
1084 NSR.deleteScalingGroupInstance = function(req) {
1085 var api_server=req.query['api_server'];
1086 var id = req.params.id;
1087 var scaling_group_id = req.params.scaling_group_id;
1088 var scaling_instance_id = req.params.scaling_instance_id;
1089
1090 if (!api_server || !id || !scaling_group_id || !scaling_instance_id) {
1091 return new Promise(function(resolve, reject) {
1092 return reject({
1093 statusCode: 500,
1094 errorMessage: {
1095 error: 'API server/NSR id/Scaling group/Scaling instance id not provided'
1096 }
1097 });
1098 });
1099 }
1100
1101 console.log('Deleting scaling group instance id ', scaling_instance_id,
1102 ' for scaling group ', scaling_group_id,
1103 ', under NSR ', id);
1104
1105 return new Promise(function(resolve, reject) {
1106 var requestHeaders = {};
1107 _.extend(requestHeaders,
1108 constants.HTTP_HEADERS.accept.data,
1109 constants.HTTP_HEADERS.content_type.data,
1110 {
1111 'Authorization': req.get('Authorization')
1112 }
1113 );
1114
1115 request({
1116 uri: utils.confdPort(api_server) + APIVersion + '/api/config/ns-instance-config/nsr/' + id + '/scaling-group/' + scaling_group_id + '/instance/' + scaling_instance_id,
1117 method: 'DELETE',
1118 headers: requestHeaders,
1119 forever: constants.FOREVER_ON,
1120 rejectUnauthorized: false
1121 }, function (error, response, body) {
1122 if (utils.validateResponse('NSR.deleteScalingGroupInstance', error, response, body, resolve, reject)) {
1123 resolve({
1124 statusCode: response.statusCode,
1125 data: typeof response.body == 'string' ? JSON.parse(response.body):response.body
1126 })
1127 }
1128 });
1129 });
1130 };
1131
1132 NSR.nsd = {};
1133 NSR.nsd.vld = {};
1134
1135 NSR.nsd.vld.get = function(req) {
1136 var api_server = req.query['api_server'];
1137 var nsr_id = req.params.nsr_id;
1138 var vld_id = req.params.vld_id;
1139
1140 if (!api_server || !nsr_id) {
1141 return new Promise(function(resolve, reject) {
1142 return reject({
1143 statusCode: constants.HTTPS_RESPONSE_CODES.ERROR.INTERNAL_SERVER_ERROR,
1144 errorMessage: 'API server/NSR id not provided'
1145 });
1146 })
1147 }
1148 console.log('Getting VLD', vld_id ? (' ' + vld_id) : ('\'s'), ' for NSR id', nsr_id);
1149
1150 return new Promise(function(resolve, reject) {
1151 var requestHeaders = {};
1152 _.extend(requestHeaders,
1153 vld_id ? constants.HTTP_HEADERS.accept.data : constants.HTTP_HEADERS.accept.collection,
1154 {
1155 'Authorization': req.get('Authorization')
1156 }
1157 );
1158
1159 request({
1160 uri: utils.confdPort(api_server) + APIVersion + '/api/config/ns-instance-config/nsr/' + nsr_id + '/nsd/vld' + (vld_id ? '/' + vld_id : '') +'?deep',
1161 method: 'GET',
1162 headers: requestHeaders,
1163 forever: constants.FOREVER_ON,
1164 rejectUnauthorized: false
1165 }, function (error, response, body) {
1166 if (utils.validateResponse('NSR.nsd.vld.get', error, response, body, resolve, reject)) {
1167 resolve({
1168 statusCode: response.statusCode,
1169 data: typeof response.body == 'string' ? JSON.parse(response.body):response.body
1170 });
1171 }
1172 });
1173 });
1174 };
1175
1176 NSR.nsd.vld.create = function(req) {
1177 var api_server = req.query['api_server'];
1178 var nsr_id = req.params.nsr_id;
1179 var vld_id = req.params.vld_id;
1180 var data = req.body;
1181
1182 if (!api_server || !nsr_id) {
1183 return new Promise(function(resolve, reject) {
1184 return reject({
1185 statusCode: constants.HTTPS_RESPONSE_CODES.ERROR.INTERNAL_SERVER_ERROR,
1186 errorMessage: 'API server/NSR id not provided'
1187 });
1188 });
1189 }
1190
1191 console.log((vld_id ? 'Updating VLD ' + vld_id : 'Creating VLD') + ' under NSR', nsr_id);
1192
1193 var jsonData = {
1194 vld: typeof(data) == 'string' ? JSON.parse(data) : data
1195 };
1196
1197 return new Promise(function(resolve, reject) {
1198 var requestHeaders = {};
1199 _.extend(requestHeaders, constants.HTTP_HEADERS.accept.data, constants.HTTP_HEADERS.content_type.data, {
1200 'Authorization': req.get('Authorization')
1201 });
1202 request({
1203 uri: utils.confdPort(api_server) + '/api/config/ns-instance-config/nsr/' + nsr_id + '/nsd/vld' + (vld_id ? '/' + vld_id : ''),
1204 method: vld_id ? 'PUT' : 'POST',
1205 headers: requestHeaders,
1206 forever: constants.FOREVER_ON,
1207 rejectUnauthorized: false,
1208 json: jsonData
1209 }, function(error, response, body) {
1210 if (utils.validateResponse('NSR.nsd.vld.create/update', error, response, body, resolve, reject)) {
1211 resolve({
1212 statusCode: response.statusCode,
1213 data: (typeof(response.body) == 'string') ? JSON.parse(response.body) : response.body
1214 });
1215 }
1216 });
1217 });
1218 };
1219
1220 NSR.nsd.vld.update = NSR.nsd.vld.create;
1221
1222 NSR.nsd.vld.delete = function(req) {
1223 var api_server = req.query['api_server'];
1224 var nsr_id = req.params.nsr_id;
1225 var vld_id = req.params.vld_id;
1226
1227 if (!api_server || !nsr_id || !vld_id) {
1228 return new Promise(function(resolve, reject) {
1229 return reject({
1230 statusCode: constants.HTTPS_RESPONSE_CODES.ERROR.INTERNAL_SERVER_ERROR,
1231 errorMessage: 'API server/NSR id/VLD id not provided'
1232 });
1233 })
1234 }
1235 console.log('Deleting VLD', vld_id, 'for NSR id', nsr_id);
1236
1237 return new Promise(function(resolve, reject) {
1238 var requestHeaders = {};
1239 _.extend(requestHeaders,
1240 constants.HTTP_HEADERS.accept.data,
1241 {
1242 'Authorization': req.get('Authorization')
1243 }
1244 );
1245
1246 request({
1247 uri: utils.confdPort(api_server) + APIVersion + '/api/config/ns-instance-config/nsr/' + nsr_id + '/nsd/vld/' + vld_id,
1248 method: 'DELETE',
1249 headers: requestHeaders,
1250 forever: constants.FOREVER_ON,
1251 rejectUnauthorized: false
1252 }, function (error, response, body) {
1253 if (utils.validateResponse('NSR.nsd.vld.delete', error, response, body, resolve, reject)) {
1254 resolve({
1255 statusCode: response.statusCode,
1256 data: typeof response.body == 'string' ? JSON.parse(response.body):response.body
1257 });
1258 }
1259 });
1260 });
1261 }
1262
1263 VNFR.get = function(req) {
1264 var api_server = req.query["api_server"];
1265 var id = req.params.id;
1266 var uri = utils.confdPort(api_server);
1267 uri += APIVersion + '/api/operational/vnfr-catalog/vnfr' + (id ? '/' + id : '') + '?deep';
1268 var headers = _.extend({}, id ? constants.HTTP_HEADERS.accept.data : constants.HTTP_HEADERS.accept.collection, {
1269 'Authorization': req.get('Authorization')
1270 });
1271 return new Promise(function(resolve, reject) {
1272 request({
1273 url: uri,
1274 method: 'GET',
1275 headers: headers,
1276 forever: constants.FOREVER_ON,
1277 rejectUnauthorized: false,
1278 }, function(error, response, body) {
1279 if (utils.validateResponse('VNFR.get', error, response, body, resolve, reject)) {
1280 var data = JSON.parse(response.body);
1281 var returnData = id ? [data["vnfr:vnfr"]] : data.collection["vnfr:vnfr"];
1282 returnData.forEach(function(vnfr) {
1283 vnfr['nfvi-metrics'] = buildNfviGraphs(vnfr.vdur);
1284 vnfr['epa-params'] = epa_aggregator(vnfr.vdur);
1285 vnfr['service-primitives-present'] = (vnfr['vnf-configuration'] && vnfr['vnf-configuration']['service-primitive'] && vnfr['vnf-configuration']['service-primitive'].length > 0) ? true : false;
1286 vnfr['vdur'] && vnfr['vdur'].map(function(vdur, vdurIndex) {
1287 // This console-url is what front-end will hit to generate a real console-url
1288 vdur['console-url'] = 'api/vnfr/' + vnfr.id + '/vdur/' + vdur.id + '/console-url';
1289 });
1290 });
1291 return resolve(returnData);
1292 };
1293 });
1294 });
1295 }
1296
1297 function buildNfviGraphs(VDURs, vnfrName){
1298 var temp = {};
1299 var toReturn = [];
1300 APIConfig.NfviMetrics.map(function(k) {
1301
1302 VDURs && VDURs.map(function(v,i) {
1303 //Check for RIFT-12699: VDUR NFVI Metrics not fully populated
1304 if (v["rw-vnfr:nfvi-metrics"] && v["rw-vnfr:nfvi-metrics"][k] && v["rw-vnfr:nfvi-metrics"][k].hasOwnProperty('utilization')) {
1305 if(!temp[k]) {
1306 temp[k] = {
1307 title: '',
1308 data: []
1309 };
1310 };
1311 try {
1312 var data = v["rw-vnfr:nfvi-metrics"][k];
1313 var newData = {};
1314 newData.name = v.name ? v.name : v.id.substring(0,6);
1315 newData.name = vnfrName ? vnfrName + ': ' + newData.name : newData.name;
1316 newData.id = v.id;
1317 //converts to perentage
1318 newData.utilization = data.utilization * 0.01;
1319 temp[k].data.push(newData);
1320 temp[k].title = v["rw-vnfr:nfvi-metrics"][k].label;
1321 } catch (e) {
1322 console.log('Something went wrong with the VNFR NFVI Metrics. Check that the data is being properly returned. ERROR: ', e);
1323 }
1324 }
1325 });
1326 if(temp[k]) {
1327 toReturn.push(temp[k]);
1328 }
1329 });
1330 return toReturn;
1331 }
1332
1333
1334 //Cache NSR reference for VNFR
1335 VNFR.cachedNSR = {};
1336 VNFR.getByNSR = function(req) {
1337 var api_server = req.query["api_server"];
1338 var id = req.params.nsr_id;
1339 var uri = utils.confdPort(api_server);
1340 var reqClone = _.clone(req);
1341 delete reqClone.params.id;
1342 uri += APIVersion + '/api/operational/ns-instance-opdata/nsr/' + id + '?deep';
1343 var headers = _.extend({}, id ? constants.HTTP_HEADERS.accept.data : constants.HTTP_HEADERS.accept.collection, {
1344 'Authorization': req.get('Authorization')
1345 });
1346 return new Promise(function(resolve, reject) {
1347 if (VNFR.cachedNSR[id]) {
1348 var data = VNFR.cachedNSR[id];
1349 var vnfrList = _.pluck(data["constituent-vnfr-ref"], 'vnfr-id');
1350 VNFR.get(reqClone).then(function(vnfrData) {
1351 resolve(filterVnfrByList(vnfrList, vnfrData));
1352 });
1353 } else {
1354 request({
1355 url: uri,
1356 method: 'GET',
1357 headers: headers,
1358 forever: constants.FOREVER_ON,
1359 rejectUnauthorized: false,
1360 }, function(error, response, body) {
1361 if (utils.validateResponse('VNFR.getByNSR', error, response, body, resolve, reject)) {
1362 var data = JSON.parse(response.body);
1363 data = data["nsr:nsr"];
1364 //Cache NSR data with NSR-ID as
1365 VNFR.cachedNSR[id] = data;
1366 var vnfrList = _.pluck(data["constituent-vnfr-ref"], 'vnfr-id');
1367 var returnData = [];
1368 VNFR.get(reqClone).then(function(vnfrData) {
1369 resolve(filterVnfrByList(vnfrList, vnfrData));
1370 });
1371 };
1372 });
1373 }
1374 });
1375 };
1376
1377 function filterVnfrByList(vnfrList, vnfrData) {
1378 return vnfrData.map(function(vnfr) {
1379 if (vnfrList.indexOf(vnfr.id) > -1) {
1380 return vnfr;
1381 }
1382 })
1383 };
1384
1385 VLR.get = function(req) {
1386 var api_server = req.query["api_server"];
1387 var id = req.params.id;
1388 var uri = utils.confdPort(api_server);
1389 uri += APIVersion + '/api/operational/vlr-catalog/vlr' + (id ? '/' + id : '') + '?deep';
1390 var headers = _.extend({}, id ? constants.HTTP_HEADERS.accept.data : constants.HTTP_HEADERS.accept.collection, {
1391 'Authorization': req.get('Authorization')
1392 });
1393 return new Promise(function(resolve, reject) {
1394 request({
1395 url: uri,
1396 method: 'GET',
1397 headers: headers,
1398 forever: constants.FOREVER_ON,
1399 rejectUnauthorized: false,
1400 }, function(error, response, body) {
1401 if (utils.validateResponse('VLR.get', error, response, body, resolve, reject)) {
1402 var data = JSON.parse(response.body);
1403 var returnData = id ? [data["vlr:vlr"]] : data.collection["vlr:vlr"];
1404 return resolve({
1405 data: returnData,
1406 statusCode: response.statusCode
1407 });
1408 };
1409 });
1410 });
1411 }
1412
1413 RIFT.api = function(req) {
1414 var api_server = req.query["api_server"];
1415 var uri = utils.confdPort(api_server);
1416 var url = req.path;
1417 return new Promise(function(resolve, reject) {
1418 request({
1419 url: uri + url + '?deep',
1420 method: 'GET',
1421 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
1422 'Authorization': req.get('Authorization')
1423 }),
1424 forever: constants.FOREVER_ON,
1425 rejectUnauthorized: false,
1426 }, function(error, response, body) {
1427 if (utils.validateResponse('RIFT.api', error, response, body, resolve, reject)) {
1428 resolve(JSON.parse(response.body))
1429 };
1430 })
1431 })
1432 };
1433
1434 ComputeTopology.get = function(req) {
1435 var api_server = req.query['api_server'];
1436 var nsr_id = req.params.id;
1437 var result = {
1438 id: nsr_id, // node id
1439 name: nsr_id, // node name to display
1440 parameters: {}, // the parameters that can be used to determine size/color, etc. for the node
1441 type: 'nsr',
1442 children: [] // children for the node
1443 };
1444 return new Promise(function(resolve, reject) {
1445 var nsrPromise = new Promise(function(success, failure) {
1446 request({
1447 uri: utils.confdPort(api_server) + APIVersion + '/api/operational/ns-instance-opdata/nsr/' + nsr_id + '?deep',
1448 method: 'GET',
1449 headers: _.extend({},
1450 constants.HTTP_HEADERS.accept.data, {
1451 'Authorization': req.get('Authorization')
1452 }),
1453 forever: constants.FOREVER_ON,
1454 rejectUnauthorized: false,
1455 }, function(error, response, body) {
1456 if (utils.validateResponse('ComputeTopology.get ns-instance-opdata/nsr/:id', error, response, body, success, failure)) {
1457 var data;
1458 var isString = typeof(response.body) == "string";
1459 if (isString && response.body == '') {
1460 return success({});
1461 }
1462 try {
1463 data = isString ? JSON.parse(response.body) : response.body;
1464
1465 var nsrNFVIMetricData = data["nsr:nsr"]["rw-nsr:nfvi-metrics"];
1466 result.parameters = nsrNFVIMetricData;
1467
1468 result.name = data["nsr:nsr"]["name-ref"];
1469
1470 var nsrData = data["nsr:nsr"]["constituent-vnfr-ref"];
1471 success(nsrData);
1472 } catch (e) {
1473 console.log('Error parsing ns-instance-opdata for NSR ID', nsr_id, 'Exception:', e);
1474 return failure()
1475 }
1476 };
1477 });
1478 }).then(function(data) {
1479
1480 try {
1481 // got NSR data
1482 // now get VNFR data and populate the structure
1483 var vnfrPromises = [];
1484
1485 // Run separately to confirm that primary structure is populated before promise resolution takes over
1486 // and starts modifying the data
1487 data.forEach(function(vnfrObj) {
1488
1489 var vnfrId = vnfrObj['vnfr-id'];
1490
1491 // If anything needs to be added to result for each vnfrId, do it here
1492
1493 vnfrPromises.push(
1494 new Promise(function(success, failure) {
1495 rp({
1496 uri: utils.confdPort(api_server) + APIVersion + '/api/operational/vnfr-catalog/vnfr/' + vnfrId + '?deep',
1497 method: 'GET',
1498 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
1499 'Authorization': req.get('Authorization')
1500 }),
1501 forever: constants.FOREVER_ON,
1502 rejectUnauthorized: false,
1503 resolveWithFullResponse: true
1504 }, function(error, response, body) {
1505 if (utils.validateResponse('ComputeTopology.get vnfr-catalaog/vnfr/:id', error, response, body, success, failure)) {
1506 try {
1507 var data = JSON.parse(response.body);
1508 var returnData = data["vnfr:vnfr"];
1509
1510 // Push VNFRs in result
1511 result.children.push({
1512 id: vnfrId,
1513 name: returnData.name,
1514 parameters: {}, // nfvi metrics here
1515 children: [],
1516 type: 'vnfr'
1517 });
1518
1519 // Push VDURs in result
1520 returnData.vdur.forEach(function(vdur) {
1521 result.children[result.children.length - 1].children.push({
1522 id: vdur.id,
1523 name: vdur.id,
1524 parameters: {},
1525 type: 'vdur'
1526 // children: []
1527 });
1528 });
1529
1530 return success(returnData.vdur);
1531 } catch (e) {
1532 console.log('Error parsing vnfr-catalog for VNFR ID', vnfrId, 'Exception:', e);
1533 return failure();
1534 }
1535 };
1536 });
1537 })
1538 );
1539 });
1540
1541 Promise.all(vnfrPromises).then(function(output) {
1542 console.log('Resolved all VNFR requests successfully');
1543 // By now result must be completely populated. output is moot
1544
1545 // Sort the results as there's no order to them from RIFT-REST
1546 result.children.sort(sortByName);
1547
1548 result.children.forEach(function(vnfr) {
1549 vnfr.children.sort(sortByName);
1550 });
1551
1552 resolve({
1553 statusCode: 200,
1554 data: result
1555 });
1556 }).catch(function(error) {
1557 // Todo: Can this be made better?
1558 // Right now if one of the southbound APIs fails - we just return what's populated so far in result
1559 console.log('Problem with ComputeTopology.get vnfr-catalog/vnfr/:id', error, 'Resolving with partial data', result);
1560 resolve({
1561 statusCode: 200,
1562 data: result
1563 });
1564 });
1565 } catch (e) {
1566 // API came back with empty ns-instance-opdata response for NSR ID
1567 // bail
1568 console.log('Error iterating through ns-instance-opdata response for NSR ID', nsr_id, 'Exception:', e);
1569 resolve({
1570 statusCode: 200,
1571 data: result
1572 })
1573 }
1574 }, function(error) {
1575 // failed to get NSR data.
1576 // bail
1577 resolve({
1578 statusCode: 200,
1579 data: result
1580 });
1581 });
1582 });
1583 };
1584
1585 NetworkTopology.get = function(req) {
1586 var api_server = req.query["api_server"];
1587 var uri = utils.confdPort(api_server);
1588 uri += APIVersion + '/api/operational/network?deep';
1589 var headers = _.extend({}, constants.HTTP_HEADERS.accept.data, {
1590 'Authorization': req.get('Authorization')
1591 });
1592 return new Promise(function(resolve, reject) {
1593 request({
1594 url: uri,
1595 method: 'GET',
1596 headers: headers,
1597 forever: constants.FOREVER_ON,
1598 rejectUnauthorized: false
1599 }, function(error, response, body) {
1600 if (utils.validateResponse('NetworkTopology.get', error, response, body, resolve, reject)) {
1601 var data = JSON.parse(response.body);
1602 var returnData = transforms.transformNetworkTopology(
1603 data["ietf-network:network"]
1604 );
1605 resolve({
1606 statusCode: 200,
1607 data: returnData
1608 });
1609 };
1610 });
1611 })
1612 }
1613
1614 VDUR.get = function(req) {
1615 var api_server = req.query["api_server"];
1616 var vnfrID = req.params.vnfr_id;
1617 var vdurID = req.params.vdur_id;
1618 var uri = utils.confdPort(api_server);
1619 uri += APIVersion + '/api/operational/vnfr-catalog/vnfr/' + vnfrID + '/vdur/' + vdurID + '?deep';
1620 var headers = _.extend({}, constants.HTTP_HEADERS.accept.data, {
1621 'Authorization': req.get('Authorization')
1622 });
1623 return new Promise(function(resolve, reject) {
1624 request({
1625 url: uri,
1626 method: 'GET',
1627 headers: headers,
1628 forever: constants.FOREVER_ON,
1629 rejectUnauthorized: false,
1630 }, function(error, response, body) {
1631 if (utils.validateResponse('VDUR.get', error, response, body, resolve, reject)) {
1632 var data = JSON.parse(response.body);
1633 var returnData = data["vdur:vdur"];
1634 return resolve(returnData);
1635 };
1636 });
1637 })
1638 }
1639
1640 VDUR.consoleUrl = {};
1641 VDUR.consoleUrl.get = function(req) {
1642 var api_server = req.query["api_server"];
1643 var vnfrID = req.params.vnfr_id;
1644 var vdurID = req.params.vdur_id;
1645 var uri = utils.confdPort(api_server);
1646 uri += APIVersion + '/api/operational/vnfr-console/vnfr/' + vnfrID + '/vdur/' + vdurID + '/console-url' + '?deep';
1647 var headers = _.extend({}, constants.HTTP_HEADERS.accept.data, {
1648 'Authorization': req.get('Authorization')
1649 });
1650 return new Promise(function(resolve, reject) {
1651 request({
1652 url: uri,
1653 method: 'GET',
1654 headers: headers,
1655 forever: constants.FOREVER_ON,
1656 rejectUnauthorized: false,
1657 }, function(error, response, body) {
1658 if (utils.validateResponse('VDUR.consoleUrl.get', error, response, body, resolve, reject)) {
1659 var data = JSON.parse(response.body);
1660 var returnData = data;
1661 return resolve({
1662 data: returnData,
1663 statusCode: response.statusCode
1664 });
1665 };
1666 });
1667 })
1668 }
1669
1670 CloudAccount.get = function(req) {
1671 var api_server = req.query["api_server"];
1672 var uri = utils.confdPort(api_server);
1673 uri += APIVersion + '/api/operational/cloud/account?deep';
1674 var headers = _.extend({}, constants.HTTP_HEADERS.accept.collection, {
1675 'Authorization': req.get('Authorization')
1676 });
1677 return new Promise(function(resolve, reject) {
1678 request({
1679 url: uri,
1680 method: 'GET',
1681 headers: headers,
1682 forever: constants.FOREVER_ON,
1683 rejectUnauthorized: false,
1684 }, function(error, response, body) {
1685 if (utils.validateResponse('CloudAccount.get', error, response, body, resolve, reject)) {
1686 var data = JSON.parse(response.body);
1687 var returnData = data["collection"]["rw-cloud:account"];
1688 resolve({
1689 statusCode: 200,
1690 data: returnData
1691 });
1692 };
1693 });
1694 });
1695 }
1696
1697
1698 // Config-Agent Account APIs
1699 ConfigAgentAccount.get = function(req) {
1700 var self = this;
1701
1702 var api_server = req.query["api_server"];
1703 var id = req.params.id;
1704
1705 if (!id) {
1706 // Get all config accounts
1707 return new Promise(function(resolve, reject) {
1708
1709 var requestHeaders = {};
1710 _.extend(requestHeaders,
1711 constants.HTTP_HEADERS.accept.collection, {
1712 'Authorization': req.get('Authorization')
1713 });
1714
1715 request({
1716 url: utils.confdPort(api_server) + APIVersion + '/api/operational/config-agent/account',
1717 type: 'GET',
1718 headers: requestHeaders,
1719 forever: constants.FOREVER_ON,
1720 rejectUnauthorized: false,
1721 },
1722 function(error, response, body) {
1723 var data;
1724 var statusCode;
1725 if (utils.validateResponse('ConfigAgentAccount.get', error, response, body, resolve, reject)) {
1726 try {
1727 data = JSON.parse(response.body).collection['rw-config-agent:account'];
1728 statusCode = response.statusCode;
1729 } catch (e) {
1730 console.log('Problem with "ConfigAgentAccount.get"', e);
1731 var err = {};
1732 err.statusCode = 500;
1733 err.errorMessage = {
1734 error: 'Problem with "ConfigAgentAccount.get": ' + e.toString()
1735 }
1736 return reject(err);
1737 }
1738
1739 return resolve({
1740 statusCode: statusCode,
1741 data: data
1742 });
1743 };
1744 });
1745 });
1746 } else {
1747 //Get a specific config account
1748 return new Promise(function(resolve, reject) {
1749 var requestHeaders = {};
1750 _.extend(requestHeaders,
1751 constants.HTTP_HEADERS.accept.data, {
1752 'Authorization': req.get('Authorization')
1753 });
1754
1755 request({
1756 url: utils.confdPort(api_server) + APIVersion + '/api/operational/config-agent/account/' + id,
1757 type: 'GET',
1758 headers: requestHeaders,
1759 forever: constants.FOREVER_ON,
1760 rejectUnauthorized: false,
1761 },
1762 function(error, response, body) {
1763 var data;
1764 var statusCode;
1765 if (utils.validateResponse('ConfigAgentAccount.get', error, response, body, resolve, reject)) {
1766 try {
1767 data = JSON.parse(response.body)['rw-config-agent:account'];
1768 statusCode = response.statusCode;
1769 } catch (e) {
1770 console.log('Problem with "ConfigAgentAccount.get"', e);
1771 var err = {};
1772 err.statusCode = 500;
1773 err.errorMessage = {
1774 error: 'Problem with "ConfigAgentAccount.get": ' + e.toString()
1775 }
1776 return reject(err);
1777 }
1778
1779 return resolve({
1780 statusCode: statusCode,
1781 data: data
1782 });
1783 }
1784 });
1785 });
1786 }
1787 };
1788
1789 ConfigAgentAccount.create = function(req) {
1790
1791 var api_server = req.query["api_server"];
1792 var data = req.body;
1793
1794 return new Promise(function(resolve, reject) {
1795 var jsonData = {
1796 "account": Array.isArray(data) ? data : [data]
1797 };
1798
1799 console.log('Creating with', JSON.stringify(jsonData));
1800
1801 var requestHeaders = {};
1802 _.extend(requestHeaders,
1803 constants.HTTP_HEADERS.accept.data,
1804 constants.HTTP_HEADERS.content_type.data, {
1805 'Authorization': req.get('Authorization')
1806 });
1807
1808 request({
1809 url: utils.confdPort(api_server) + APIVersion + '/api/config/config-agent',
1810 method: 'POST',
1811 headers: requestHeaders,
1812 forever: constants.FOREVER_ON,
1813 rejectUnauthorized: false,
1814 json: jsonData,
1815 }, function(error, response, body) {
1816 if (utils.validateResponse('ConfigAgentAccount.create', error, response, body, resolve, reject)) {
1817 return resolve({
1818 statusCode: response.statusCode,
1819 data: JSON.stringify(response.body),
1820 body:response.body.body
1821 });
1822 };
1823 });
1824 });
1825 };
1826
1827 ConfigAgentAccount.update = function(req) {
1828
1829 var api_server = req.query["api_server"];
1830 var id = req.params.id;
1831 var data = req.body;
1832
1833 return new Promise(function(resolve, reject) {
1834 var jsonData = {
1835 "rw-config-agent:account": data
1836 };
1837
1838 console.log('Updating config-agent', id, ' with', JSON.stringify(jsonData));
1839
1840 var requestHeaders = {};
1841 _.extend(requestHeaders,
1842 constants.HTTP_HEADERS.accept.data,
1843 constants.HTTP_HEADERS.content_type.data, {
1844 'Authorization': req.get('Authorization')
1845 });
1846
1847 request({
1848 url: utils.confdPort(api_server) + APIVersion + '/api/config/config-agent/account/' + id,
1849 method: 'PUT',
1850 headers: requestHeaders,
1851 forever: constants.FOREVER_ON,
1852 rejectUnauthorized: false,
1853 json: jsonData,
1854 }, function(error, response, body) {
1855 if (utils.validateResponse('ConfigAgentAccount.update', error, response, body, resolve, reject)) {
1856 return resolve({
1857 statusCode: response.statusCode,
1858 data: JSON.stringify(response.body)
1859 });
1860 };
1861 });
1862 });
1863 };
1864
1865 ConfigAgentAccount.delete = function(req) {
1866
1867 var api_server = req.query["api_server"];
1868 var id = req.params.id;
1869
1870 if (!id || !api_server) {
1871 return new Promise(function(resolve, reject) {
1872 console.log('Must specifiy api_server and id to delete config-agent account');
1873 return reject({
1874 statusCode: 500,
1875 errorMessage: {
1876 error: 'Must specifiy api_server and id to delete config agent account'
1877 }
1878 });
1879 });
1880 };
1881
1882 return new Promise(function(resolve, reject) {
1883 var requestHeaders = {};
1884 _.extend(requestHeaders,
1885 constants.HTTP_HEADERS.accept.data, {
1886 'Authorization': req.get('Authorization')
1887 });
1888 request({
1889 url: utils.confdPort(api_server) + APIVersion + '/api/config/config-agent/account/' + id,
1890 method: 'DELETE',
1891 headers: requestHeaders,
1892 forever: constants.FOREVER_ON,
1893 rejectUnauthorized: false,
1894 }, function(error, response, body) {
1895 if (utils.validateResponse('ConfigAgentAccount.delete', error, response, body, resolve, reject)) {
1896 return resolve({
1897 statusCode: response.statusCode,
1898 data: JSON.stringify(response.body)
1899 });
1900 };
1901 });
1902 });
1903 };
1904
1905
1906 DataCenters.get = function(req) {
1907 var api_server = req.query["api_server"];
1908 return new Promise(function(resolve, reject) {
1909 var requestHeaders = {};
1910 _.extend(requestHeaders,
1911 constants.HTTP_HEADERS.accept.data, {
1912 'Authorization': req.get('Authorization')
1913 });
1914 request({
1915 url: utils.confdPort(api_server) + APIVersion + '/api/operational/datacenters?deep',
1916 method: 'GET',
1917 headers: requestHeaders,
1918 forever: constants.FOREVER_ON,
1919 rejectUnauthorized: false,
1920 }, function(error, response, body) {
1921 if (utils.validateResponse('DataCenters.get', error, response, body, resolve, reject)) {
1922 var returnData = {};
1923 try {
1924 data = JSON.parse(response.body)["rw-launchpad:datacenters"]["ro-accounts"];
1925 data.map(function(c) {
1926 returnData[c.name] = c.datacenters;
1927 })
1928 statusCode = response.statusCode;
1929 } catch (e) {
1930 console.log('Problem with "DataCenters.get"', e);
1931 var err = {};
1932 err.statusCode = 500;
1933 err.errorMessage = {
1934 error: 'Problem with "DataCenters.get": ' + e.toString()
1935 }
1936 return reject(err);
1937 }
1938 return resolve({
1939 statusCode: response.statusCode,
1940 data: returnData
1941 });
1942 };
1943 });
1944 });
1945 }
1946
1947 SSHkey.get = function(req) {
1948 var api_server = req.query["api_server"];
1949 return new Promise(function(resolve, reject) {
1950 var requestHeaders = {};
1951 _.extend(requestHeaders,
1952 constants.HTTP_HEADERS.accept.data, {
1953 'Authorization': req.get('Authorization')
1954 });
1955 request({
1956 url: utils.confdPort(api_server) + APIVersion + '/api/config/key-pair?deep',
1957 method: 'GET',
1958 headers: requestHeaders,
1959 forever: constants.FOREVER_ON,
1960 rejectUnauthorized: false,
1961 }, function(error, response, body) {
1962 if (utils.validateResponse('SSHkey.get', error, response, body, resolve, reject)) {
1963 var returnData = {};
1964 try {
1965 returnData = JSON.parse(response.body)['nsr:key-pair'];
1966 statusCode = response.statusCode;
1967 } catch (e) {
1968 console.log('Problem with "SSHkey.get"', e);
1969 var err = {};
1970 err.statusCode = 500;
1971 err.errorMessage = {
1972 error: 'Problem with "SSHkey.get": ' + e.toString()
1973 }
1974 return reject(err);
1975 }
1976 return resolve({
1977 statusCode: response.statusCode,
1978 data: returnData
1979 });
1980 };
1981 });
1982 });
1983 }
1984 SSHkey.delete = function(req) {
1985 var api_server = req.query['api_server'];
1986 var id = decodeURI(req.params.name);
1987 console.log('Deleting ssk-key', id);
1988 return new Promise(function(resolve, reject) {
1989 request({
1990 uri: utils.confdPort(api_server) + APIVersion + '/api/config/key-pair/' + id,
1991 method: 'DELETE',
1992 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
1993 'Authorization': req.get('Authorization')
1994 }),
1995 forever: constants.FOREVER_ON,
1996 rejectUnauthorized: false,
1997 }, function(error, response, body) {
1998 if (utils.validateResponse('SSHkey.delete', error, response, body, resolve, reject)) {
1999 resolve({
2000 statusCode: response.statusCode
2001 });
2002 }
2003 });
2004 });
2005 };
2006 SSHkey.post = function(req) {
2007 var api_server = req.query['api_server'];
2008 var data = req.body;
2009 return new Promise(function(resolve, reject) {
2010 request({
2011 uri: utils.confdPort(api_server) + APIVersion + '/api/config/key-pair/',
2012 method: 'POST',
2013 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
2014 'Authorization': req.get('Authorization')
2015 }),
2016 json: data,
2017 forever: constants.FOREVER_ON,
2018 rejectUnauthorized: false,
2019 }, function(error, response, body) {
2020 if (utils.validateResponse('SSHkey.post', error, response, body, resolve, reject)) {
2021 resolve({
2022 data: 'success',
2023 statusCode: response.statusCode
2024 });
2025 }
2026 });
2027 });
2028 };
2029 SSHkey.put = function(req) {
2030 var api_server = req.query['api_server'];
2031 var data = req.body;
2032 return new Promise(function(resolve, reject) {
2033 request({
2034 uri: utils.confdPort(api_server) + APIVersion + '/api/config/key-pair/',
2035 method: 'PUT',
2036 headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
2037 'Authorization': req.get('Authorization')
2038 }),
2039 json: data,
2040 forever: constants.FOREVER_ON,
2041 rejectUnauthorized: false,
2042 }, function(error, response, body) {
2043 if (utils.validateResponse('SSHkey.put', error, response, body, resolve, reject)) {
2044 resolve({
2045 statusCode: response.statusCode
2046 });
2047 }
2048 });
2049 });
2050 };
2051
2052 function sortByName(a, b) {
2053 return a.name > b.name;
2054 }
2055
2056 module.exports.catalog = Catalog;
2057 module.exports.nsr = NSR;
2058 module.exports.vnfr = VNFR;
2059 module.exports.vlr = VLR;
2060 module.exports.vdur = VDUR;
2061 module.exports.rift = RIFT;
2062 module.exports.computeTopology = ComputeTopology;
2063 module.exports.networkTopology = NetworkTopology;
2064 module.exports.config = Config;
2065 module.exports.cloud_account = CloudAccount;
2066 module.exports['config-agent-account'] = ConfigAgentAccount;
2067 module.exports.rpc = RPC;
2068 module.exports.data_centers = DataCenters;
2069 module.exports.SSHkey = SSHkey;