blob: 8fa7c7f4bc405768e2170a231e92fa88fdf40d39 [file] [log] [blame]
Luji7fb663ae2016-09-14 22:47:09 +08001/*
2 * Copyright (C) 2015 ZTE, Inc. and others. All rights reserved. (ZTE)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Luji7a5777c52016-09-15 00:07:33 +080016var templateParameters = {
Luji756c24392016-09-15 00:37:15 +080017 templateName: '',
18 parameters: []
19};
Luji7988a5092016-09-15 10:44:36 +080020
21var service_instance_insert_index = 0;
22
Luji756c24392016-09-15 00:37:15 +080023var lcmHandler = function(){
24 this._addOwnEvents();
25};
26
27lcmHandler.prototype = {
28 _addOwnEvents : function () {
29 $('a[data-toggle="tab"]').on('show.bs.tab', this.beforeParameterTabShow);
Luji717050c32016-09-15 01:04:10 +080030 $('#createNS').click(this.okAction);
Luji756c24392016-09-15 00:37:15 +080031 },
32 beforeParameterTabShow : function (event) {
33 renderTemplateParametersTab();
Luji717050c32016-09-15 01:04:10 +080034 },
35 okAction : function (event) {
36 var serviceInstance = {
37 serviceTemplateId: $('#svcTempl').val(),
38 serviceName: $('#svcName').val(),
39 serviceDescription: $('#svcDesc').val(),
40 serviceParameters: collectServiceParameters(templateParameters)
41 }
Luji7c4e8e732016-09-15 11:28:45 +080042 var gatewayService = '/openoapi/servicegateway/v1/services';
Luji717050c32016-09-15 01:04:10 +080043 var serviceTemplate = fetchServiceTemplateBy(serviceInstance.serviceTemplateId);
44 if(serviceTemplate === undefined) {
45 return;
46 }
47 if(serviceTemplate.csarType === 'GSAR') {
Luji7c4e8e732016-09-15 11:28:45 +080048 serviceInstance.serviceInstanceId = createGsoServiceInstance(gatewayService, serviceInstance);
Luji717050c32016-09-15 01:04:10 +080049 }else if(serviceTemplate.csarType === 'NSAR' || serviceTemplate.csarType === 'NFAR') {
Luji7c4e8e732016-09-15 11:28:45 +080050 serviceInstance.serviceInstanceId = createNfvoServiceInstance(gatewayService, serviceInstance);
Luji717050c32016-09-15 01:04:10 +080051 }else if(serviceTemplate.csarType === 'SSAR') {
Luji7c4e8e732016-09-15 11:28:45 +080052 serviceInstance.serviceInstanceId = createSdnoServiceInstance(gatewayService, serviceInstance);
Luji717050c32016-09-15 01:04:10 +080053 }
Luji7988a5092016-09-15 10:44:36 +080054 if(serviceInstance.serviceInstanceId === undefined) {
55 return;
56 }
57 updateTable(serviceInstance);
Luji756c24392016-09-15 00:37:15 +080058 }
Luji7a5777c52016-09-15 00:07:33 +080059};
60
Luji717050c32016-09-15 01:04:10 +080061function collectServiceParameters(parameters) {
62 var serviceParameters = {};
63 var i;
64 for( i = 0; i < parameters.length; i += 1) {
65 serviceParameters[parameters.name] = $('#' + parameters[i].id).val();
66 }
67 return serviceParameters;
68}
69
70function fetchServiceTemplateBy(templateId) {
Luji7988a5092016-09-15 10:44:36 +080071 var serviceTemplateUri = '/openoapi/catalog/v1/servicetemplates/'+ templateId;
72 var template;
73 $.ajax({
74 type : "GET",
75 async: false,
76 url : serviceTemplateUri,
77 contentType : "application/json",
78 dataType : "json",
79 success : function(jsonResp) {
80 template = {
81 name: jsonResp.templateName,
82 gsarId: jsonResp.csarId
83 }
84 },
85 error : function(xhr, ajaxOptions, thrownError) {
86 alert("Error on page : " + xhr.responseText);
Luji717050c32016-09-15 01:04:10 +080087 }
Luji7988a5092016-09-15 10:44:36 +080088 });
89 if(template === undefined) {
Luji717050c32016-09-15 01:04:10 +080090 return template;
Luji7988a5092016-09-15 10:44:36 +080091 }
92 var queryCsarUri = '/openoapi/catalog/v1/csars/' + template.gsarId;
93 $.ajax({
94 type : "GET",
95 async: false,
96 url : queryCsarUri,
97 contentType : "application/json",
98 dataType : "json",
99 success : function(jsonResp) {
100 template.csarType = jsonResp.type
101 },
102 error : function(xhr, ajaxOptions, thrownError) {
103 alert("Error on page : " + xhr.responseText);
104 }
105 });
106 return template;
Luji717050c32016-09-15 01:04:10 +0800107}
108
Luji7a5777c52016-09-15 00:07:33 +0800109function renderTemplateParametersTab() {
110 templateParameters = fetchTemplateParameterDefinitions(templateParameters);
111 var components = transfromToComponents(templateParameters.parameters);
112 document.getElementById("parameterTab").innerHTML = components;
113}
114
115function fetchTemplateParameterDefinitions(parameters) {
Luji756c24392016-09-15 00:37:15 +0800116 var serviceTemplate = parameters.templateName;
Luji7a5777c52016-09-15 00:07:33 +0800117 var currentServiceTemplate = $("#svcTempl").val();
118 // Do not need to fetch template parameters if template do not change in UI.
119 if(serviceTemplate === currentServiceTemplate) {
120 return;
121 }
122 var queryParametersUri = '/openoapi/catalog/v1/servicetemplates/' + currentServiceTemplate + '/parameters';
123 var inputParameters = [];
124 $.ajax({
125 type : "GET",
126 async: false,
127 url : queryParametersUri,
128 contentType : "application/json",
129 dataType : "json",
130 success : function(jsonResp) {
131 var inputs = jsonResp.inputs;
132 var i;
133 for( i = 0; i < inputs.length; i += 1) {
134 inputParameters[i] = {
135 name: inputs[i].name,
136 type: inputs[i].type,
137 description: inputs[i].description,
138 defaultValue: inputs[i].defaultValue,
139 required: inputs[i].required,
140 id: 'parameter_' + i,
141 value: inputs[i].defaultValue
142 };
143 }
144 },
145 error : function(xhr, ajaxOptions, thrownError) {
146 alert("Error on page : " + xhr.responseText);
147 }
148 });
149 return { name: currentServiceTemplate, parameters: inputParameters };
150}
151
152function transfromToComponents(parameters) {
153 var components = '';
154 var i;
155 for( i = 0; i < parameters.length; i += 1) {
156 var component = '<div class="form-group">' +
Luji7988a5092016-09-15 10:44:36 +0800157 '<label class="col-sm-3 control-label">' +
158 '<span>' + parameters[i].description + '</span>' + generateRequiredLabel(parameters[i]) +
159 '</label>' +
160 '<div class="col-sm-7">' +
161 '<input type="text" id="'+ parameters[i].id +'" name="parameter description" class="form-control" placeholder="' +
162 parameters[i].description + '" value="'+ parameters[i].value +'" />' +
163 '</div></div>';
164 components = components + component;
165 }
166 return components;
Luji7a5777c52016-09-15 00:07:33 +0800167}
168
Luji717050c32016-09-15 01:04:10 +0800169function generateRequiredLabel(parameter) {
170 var requiredLabel = '';
171 if(parameter.required === 'true') {
172 requiredLabel = '<span class="required">*</span>';
173 }
174 return requiredLabel;
175}
176
Luji7c4e8e732016-09-15 11:28:45 +0800177function createGsoServiceInstance(gatewayService, serviceInstance) {
Luji7988a5092016-09-15 10:44:36 +0800178 var gsoLcmUri = '/openoapi/lifecyclemgr/v1/services';
179 var parameter = {
180 'name': serviceInstance.serviceName,
181 'description': serviceInstance.serviceDescription,
182 'serviceDefId': serviceTemplate.gsarId,
183 'templatedId': serviceInstance.serviceTemplateId,
184 'templateName': serviceTemplate.templateName,
185 'getewayUri': gsoLcmUrl,
186 'parameters': serviceInstance.serviceParameters
187 };
188 var serviceInstanceId;
189 $.ajax({
190 type : "POST",
191 async: false,
Luji7c4e8e732016-09-15 11:28:45 +0800192 url : gatewayService,
Luji7988a5092016-09-15 10:44:36 +0800193 contentType : "application/json",
194 dataType : "json",
195 data : JSON.stringify(parameter),
196 success : function(jsonResp) {
197 if(jsonResp.result.errorCode != '200') {
198 alert("Create service instance Error!");
199 return;
200 }
201 serviceInstanceId = jsonResp.serviceId;
202 },
203 error : function(xhr, ajaxOptions, thrownError) {
204 alert("Error on page : " + xhr.responseText);
205 }
206 });
207 return serviceInstanceId;
Luji7fb663ae2016-09-14 22:47:09 +0800208}
Luji7e5e08842016-09-14 23:10:07 +0800209
Luji7c4e8e732016-09-15 11:28:45 +0800210function createNfvoServiceInstance(gatewayService, serviceInstance) {
Luji7988a5092016-09-15 10:44:36 +0800211 var nfvoLcmNsUrl = '/openoapi/nslcm/v1.0/ns';
Luji7c4e8e732016-09-15 11:28:45 +0800212 createServiceInstance(gatewayService, nfvoLcmNsUrl, serviceInstance);
Luji7e5e08842016-09-14 23:10:07 +0800213}
214
Luji7c4e8e732016-09-15 11:28:45 +0800215function createServiceInstance(gatewayService, gatewayUri, serviceInstance) {
216 var nsInstanceId = createNetworkService(gatewayService, gatewayUri, serviceInstance);
Luji7988a5092016-09-15 10:44:36 +0800217 if(nsInstanceId === undefined) {
218 return;
219 }
220 instantiateNetworkService(gatewayUri, nsInstanceId, serviceInstance);
Luji7e5e08842016-09-14 23:10:07 +0800221}
222
Luji7c4e8e732016-09-15 11:28:45 +0800223function createNetworkService(gatewayService, gatewayUri, serviceInstance) {
Luji7988a5092016-09-15 10:44:36 +0800224 var parameter = {
225 'nsdId': serviceInstance.serviceTemplateId,
226 'nsName': serviceInstance.serviceName,
227 'description': serviceInstance.serviceDescription,
228 'gatewayUri': gatewayUri,
229 'parameters': serviceInstance.serviceParameters
230 };
231 var nsInstanceId;
232 $.ajax({
233 type : "POST",
234 async: false,
Luji7c4e8e732016-09-15 11:28:45 +0800235 url : gatewayService,
Luji7988a5092016-09-15 10:44:36 +0800236 contentType : "application/json",
237 dataType : "json",
238 data : JSON.stringify(parameter),
239 success : function(jsonResp) {
240 nsInstanceId = jsonResp.nsInstanceId;
241 },
242 error : function(xhr, ajaxOptions, thrownError) {
243 alert("Error on page : " + xhr.responseText);
244 }
245 });
246 return nsInstanceId;
Luji7e5e08842016-09-14 23:10:07 +0800247}
248
249function instantiateNetworkService(gatewayUri, nsInstanceId, serviceInstance) {
Luji7988a5092016-09-15 10:44:36 +0800250 var initNsUrl = gatewayUri + '/' + nsInstanceId + '/Instantiate'
251 var parameter = {
252 'gatewayUri': initNsUrl,
253 'nsInstanceId': nsInstanceId,
254 'additionalParamForNs': serviceInstance.serviceParameters
255 };
256 var result = false;
257 $.ajax({
258 type : "POST",
259 async: false,
Luji7c4e8e732016-09-15 11:28:45 +0800260 url : gatewayService,
Luji7988a5092016-09-15 10:44:36 +0800261 contentType : "application/json",
262 dataType : "json",
263 data : JSON.stringify(parameter),
264 success : function(jsonResp) {
265 result = true;
266 },
267 error : function(xhr, ajaxOptions, thrownError) {
268 alert("Error on page : " + xhr.responseText);
269 }
270 });
271 return result;
Luji7e5e08842016-09-14 23:10:07 +0800272}
Luji755c653d2016-09-14 23:36:30 +0800273
Luji7c4e8e732016-09-15 11:28:45 +0800274function createSdnoServiceInstance(gatewayService, serviceInstance) {
Luji7988a5092016-09-15 10:44:36 +0800275 var sdnoLcmNsUrl = '/openoapi/sdnonslcm/v1.0/ns';
Luji7c4e8e732016-09-15 11:28:45 +0800276 createServiceInstance(gatewayService, sdnoLcmNsUrl, serviceInstance);
Luji7988a5092016-09-15 10:44:36 +0800277}
278
279function updateTable(serviceInstance) {
Luji7f7f15a62016-09-15 11:05:07 +0800280 appendOenRow();
281 addDeleteEventRegistration();
282}
283
284function appendOenRow() {
Luji7988a5092016-09-15 10:44:36 +0800285 var index = service_instance_insert_index;
286 var creator = '';
287 $('#sai').append('<tr id="service_instance_' + index + '"></tr>');
288 $("#service_instance_" + index).html('<td><div class="DataTables_sort_wrapper openo-ellipsis "><span id="service_name" class="openo-table-th-sorticon overflow_elip leftHeaderAlign openo-table-disable-element ">'+ serviceInstance.serviceName + '</span></td>' +
289 '<td><span class="openo-table-th-sorticon overflow_elip leftHeaderAlign openo-table-disable-element ">'+ serviceInstance.templateName + '</span></td>' +
290 '<td class="service_template_id"><span class="openo-table-th-sorticon overflow_elip leftHeaderAlign openo-table-disable-element ">'+ serviceInstance.serviceTemplateId + '</span><input type="hidden" value="'+serviceInstance.serviceInstanceId+'"/></td>' +
291 '<td><span class="openo-table-th-sorticon overflow_elip leftHeaderAlign openo-table-disable-element ">'+ formatDate(new Date()) + '</span></td>' +
292 '<td><span class="openo-table-th-sorticon overflow_elip leftHeaderAlign openo-table-disable-element ">'+ creator + '</span></td>' +
293 '<td><button class="data_delete_action"><img id="delete_action" class="openo-table-th-sorticon overflow_elip leftHeaderAlign openo-table-disable-element " src="images/delete.png"></img></button></td>');
294 service_instance_insert_index += 1;
295}
296
297function formatDate(date) {
298 var year = date.getFullYear();
299 var month = date.getMonth() + 1;
300 var day = date.getDate();
301 var hh = date.getHours();
302 var mm = date.getMinutes();
303 var ss = date.getSeconds();
304 return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
Luji755c653d2016-09-14 23:36:30 +0800305}
Luji7f7f15a62016-09-15 11:05:07 +0800306
307function addDeleteEventRegistration() {
308 $(".data_delete_action").click(function(event) {
309 var trElement = $(this).parents("tr")[0];
310 var tdElement = $(trElement).children("td.service_template_id")[0];
311 var spanElement = $(tdElement).children("span")[0];
312 var templateId = $(spanElement).text();
313 var inputElement = $(tdElement).children("input")[0];
314 var instanceId = $(inputElement).val();
315 var result = deleteServiceInstance(templateId, instanceId);
316 if(result) {
317 trElement.remove();
318 alert("Service instance deleted successfully!");
319 }
320 });
321}
322
323function deleteServiceInstance(templateId, instanceId) {
324 var serviceTemplate = fetchServiceTemplateBy(templateId);
325 if(serviceTemplate === undefined) {
326 return;
327 }
Luji7c4e8e732016-09-15 11:28:45 +0800328 var gatewayService = '/openoapi/servicegateway/v1/services';
Luji7f7f15a62016-09-15 11:05:07 +0800329 var result = false;
330 if(serviceTemplate.csarType === 'GSAR') {
Luji7c4e8e732016-09-15 11:28:45 +0800331 result = deleteGsoServiceInstance(gatewayService, instanceId);
Luji7f7f15a62016-09-15 11:05:07 +0800332 }else if(serviceTemplate.csarType === 'NSAR' || serviceTemplate.csarType === 'NFAR') {
Luji7c4e8e732016-09-15 11:28:45 +0800333 result = deleteNfvoServiceInstance(gatewayService, instanceId);
Luji7f7f15a62016-09-15 11:05:07 +0800334 }else if(serviceTemplate.csarType === 'SSAR') {
Luji7c4e8e732016-09-15 11:28:45 +0800335 result = deleteSdnoServiceInstance(gatewayService, instanceId);
Luji7f7f15a62016-09-15 11:05:07 +0800336 }
337 return result;
338}
339
Luji7c4e8e732016-09-15 11:28:45 +0800340function deleteGsoServiceInstance(gatewayService, instanceId) {
Luji7f7f15a62016-09-15 11:05:07 +0800341 var gsoLcmUrl = '/openoapi/lifecyclemgr/v1/services/'+ instanceId;
Luji7c4e8e732016-09-15 11:28:45 +0800342 return sendDeleteRequest(gatewayService, gsoLcmUrl);
Luji7f7f15a62016-09-15 11:05:07 +0800343}
344
Luji7c4e8e732016-09-15 11:28:45 +0800345function deleteNfvoServiceInstance(gatewayService, instanceId) {
Luji7f7f15a62016-09-15 11:05:07 +0800346 var nfvoNsUrl = '/openoapi/nslcm/v1.0/ns/' + instanceId;
347 var nfvoNsTerminateUrl = nfvoNsUrl +'/terminate';
Luji7c4e8e732016-09-15 11:28:45 +0800348 var result = sendDeleteRequest(gatewayService, nfvoNsTerminateUrl);
Luji7f7f15a62016-09-15 11:05:07 +0800349 if(result) {
Luji7c4e8e732016-09-15 11:28:45 +0800350 result = sendDeleteRequest(gatewayService, nfvoNsUrl);
Luji7f7f15a62016-09-15 11:05:07 +0800351 }
352 return result;
353}
354
Luji7c4e8e732016-09-15 11:28:45 +0800355function deleteSdnoServiceInstance(gatewayService, instanceId) {
Luji7f7f15a62016-09-15 11:05:07 +0800356 var sdnoNsUrl = '/openoapi/sdnonslcm/v1.0/ns/' + instanceId;
357 var sdnoNsTerminateUrl = sdnoNsUrl + '/terminate';
Luji7c4e8e732016-09-15 11:28:45 +0800358 var result = sendDeleteRequest(gatewayService, sdnoNsTerminateUrl);
Luji7f7f15a62016-09-15 11:05:07 +0800359 if(result) {
Luji7c4e8e732016-09-15 11:28:45 +0800360 result = sendDeleteRequest(gatewayService, sdnoNsUrl);
Luji7f7f15a62016-09-15 11:05:07 +0800361 }
362 return result;
363}
364
Luji7c4e8e732016-09-15 11:28:45 +0800365function sendDeleteRequest(gatewayService, url) {
Luji7f7f15a62016-09-15 11:05:07 +0800366 var parameter = {
367 URL: url
368 };
369 var result = false;
370 $.ajax({
371 type : "DELETE",
372 async: false,
Luji7c4e8e732016-09-15 11:28:45 +0800373 url : gatewayService,
Luji7f7f15a62016-09-15 11:05:07 +0800374 contentType : "application/json",
375 dataType : "json",
376 data : JSON.stringify(parameter),
377 success : function(jsonResp) {
378 result = true;
379 },
380 error : function(xhr, ajaxOptions, thrownError) {
381 alert("Error on page : " + xhr.responseText);
382 }
383 });
384 return result;
385}