2 * Copyright 2016-2017 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
16 var templateParameters = {
\r
18 // the create params used for ui
\r
22 var lcmHandler = function() {
\r
23 this._addOwnEvents();
\r
24 jQuery.i18n.properties({
\r
26 name : 'lcm-template-parameters-i18n',
\r
32 lcmHandler.prototype = {
\r
33 _addOwnEvents : function() {
\r
34 $('#createNS').click(this.okAction);
\r
36 okAction : function() {
\r
37 if (!checkInputs('create', templateParameters.paramJsonObj)) {
\r
40 var sengMsgObj = collectCreateParamfromUI('', 'create', templateParameters.paramJsonObj);
\r
41 var gatewayService = '/openoapi/servicegateway/v1/services';
\r
42 $.when(createServiceInstance(sengMsgObj))
\r
43 .then(function(response) {
\r
44 if (response.status === 'finished') {
\r
45 $.when(queryService()).then(function(serviceInstance){
\r
46 $('#sai').bootstrapTable("append", serviceInstance);
\r
48 $('#vmAppDialog').removeClass('in').css('display','none');
\r
50 showErrorMessage('Create service failed',response);
\r
57 * init parameter tab
\r
60 function initParameterTab() {
\r
61 // Service template was not changed. Do not re-initiate the parameter tab.
\r
62 if (!templateParameters.changed) {
\r
65 var templateId = $("#svcTempl").val();
\r
66 if ('select' === templateId) {
\r
67 document.getElementById("templateParameterTab").innerHTML = '';
\r
70 $.when(fetchCreateParameters(templateId))
\r
71 .then(function(createParam) {
\r
72 // set the create param object
\r
73 templateParameters.paramJsonObj = createParam.parameters;
\r
74 // convert the create param to UI.
\r
75 var components = convertCreateParamsToUI('create', createParam.parameters);
\r
76 document.getElementById("templateParameterTab").innerHTML = components;
\r
77 templateParameters.changed = false;
\r
82 * generate the template to create parameters object
\r
88 function fetchCreateParameters(templateId) {
\r
89 //return $.getJSON("./conf/queryCreateParams.json");
\r
90 var uri = '/openoapi/servicegateway/v1/createparameters/' + templateId;
\r
98 * convert the template params obj to html UI string
\r
100 * @param identify the object identify, it should be '' when called
\r
101 * @return the html component string
\r
103 function convertCreateParamsToUI(identify, createParam) {
\r
104 var components = '';
\r
105 // convert host to UI
\r
106 if (undefined != createParam.domainHost && 'enum' === createParam.domainHost.type) {
\r
107 components = components
\r
108 + generateParamComponent(createParam.nodeType, identify,
\r
109 createParam.domainHost, false);
\r
111 // convert own param to UI
\r
112 createParam.additionalParamForNs
\r
113 .forEach(function(param) {
\r
114 components = components
\r
115 + generateParamComponent(createParam.nodeType,
\r
116 identify, param, false);
\r
118 // convert segments to UI
\r
119 createParam.segments.forEach(function(segment) {
\r
120 // each segment in a field set.
\r
121 components = components + '<fieldset style="margin-left:25px;"><legend>'
\r
122 + segment.nodeTemplateName + '</legend>';
\r
123 // the identify for segment
\r
124 var segmentIdentify = '' == identify ? segment.nodeTemplateName
\r
125 : identify + '_' + segment.nodeTemplateName;
\r
126 // convert segment to UI
\r
127 components = components
\r
128 + convertCreateParamsToUI(segmentIdentify, segment);
\r
129 components = components + '</fieldset>';
\r
136 * for each required parameter it could not be empty
\r
137 * @param identify the identify of a segment
\r
138 * @param createParam the create param object
\r
139 * @returns the check result
\r
141 function checkInputs(identify, createParam) {
\r
142 //check domain host
\r
143 if (undefined != createParam.domainHost && 'enum' === createParam.domainHost.type) {
\r
144 var value = collectParamValue(identify, createParam.domainHost);
\r
145 if ('select' == value) {
\r
146 var name = getParamLabel(createParam.nodeType, createParam.domainHost);
\r
147 alert( name + ' is required.')
\r
151 // check parameters
\r
152 for(var i= 0; i < createParam.additionalParamForNs.length; i++){
\r
153 var param = createParam.additionalParamForNs[i];
\r
154 var value = collectParamValue(identify, param);
\r
155 if(param.required && ('' === value || ('enum' == param.type && 'select' == value))){
\r
156 // the param resource key is nodeType.paramName
\r
157 var name = getParamLabel(createParam.nodeType, param);
\r
158 alert(name + ' is required.')
\r
162 // get segments param value from UI
\r
163 var segmentcheckResult = true;
\r
164 for(var i= 0; i < createParam.segments.length; i++){
\r
165 var segment = createParam.segments[i];
\r
166 // the identify for segment
\r
167 var segmentIdentify = '' == identify ? segment.nodeTemplateName
\r
168 : identify + '_' + segment.nodeTemplateName;
\r
169 segmentcheckResult = checkInputs(segmentIdentify, segment);
\r
170 if (!segmentcheckResult) {
\r
174 return segmentcheckResult;
\r
179 * convert the template params obj to html UI string
\r
181 * @param identify the object identify, it should be different every time
\r
182 * @return the html component string
\r
184 function collectCreateParamfromUI(parentHost,identify, createParam) {
\r
185 // the create params used for create msg
\r
186 var paramSentObj = {
\r
188 nodeTemplateName:'',
\r
191 additionalParamForNs:{}
\r
193 // get the domain value
\r
194 if (undefined != createParam.domainHost && 'enum' === createParam.domainHost.type) {
\r
195 var domain = collectParamValue(identify, createParam.domainHost);
\r
196 paramSentObj.domainHost = collectParamValue(identify, createParam.domainHost)
\r
198 //if parent domainHost is not '' and local domain host is'', it should be setted as parent
\r
199 if('' != parentHost && '' == paramSentObj.domainHost){
\r
200 paramSentObj.domainHost = parentHost;
\r
202 paramSentObj.nodeTemplateName = createParam.nodeTemplateName;
\r
203 paramSentObj.nodeType = createParam.nodeType;
\r
205 // get own param value from UI
\r
206 createParam.additionalParamForNs.forEach(function(param) {
\r
207 paramSentObj.additionalParamForNs[param.name] = collectParamValue(identify, param);
\r
209 // get segments param value from UI
\r
210 createParam.segments.forEach(function(segment) {
\r
211 // the identify for segment
\r
212 var segmentIdentify = '' == identify ? segment.nodeTemplateName
\r
213 : identify + '_' + segment.nodeTemplateName;
\r
214 var segmentObj = collectCreateParamfromUI(paramSentObj.domainHost, segmentIdentify, segment);
\r
215 paramSentObj.segments.push(segmentObj);
\r
217 return paramSentObj;
\r
221 * get a param value
\r
222 * @param identify the identify of a segment
\r
223 * @param param the param object
\r
224 * @returns the value of the param
\r
226 function collectParamValue(identify, param) {
\r
227 var value = $('#' + getParamId(identify, param)).val();
\r
232 * get the param id in ui
\r
237 function getParamId(identify, param) {
\r
238 return '' ===identify ? param.name : identify + '_' + param.name;
\r
242 * get the resource string of a param.
\r
243 * @param nodeType node type
\r
244 * @param param param object
\r
245 * @returns resource string
\r
247 function getParamLabel(nodeType, param) {
\r
248 var name = $.i18n.prop(nodeType + '.' + param.name);
\r
249 if (name.length === 0 || name.slice(0, 1) === '[') {
\r
255 * convert combox component
\r
259 * @param stringReadOnly
\r
262 function generateParamComponent(nodeType, identify, param, strReadOnly) {
\r
263 // the param resource key is nodeType.paramName
\r
264 var name = getParamLabel(nodeType, param);
\r
265 var id = getParamId(identify,param);
\r
266 var component = '';
\r
267 if (param.type === 'string') {
\r
268 component = '<div class="mT15 form-group" style="margin-left:0px;">'
\r
269 + '<label class="col-sm-5 control-label">'
\r
270 + '<span>' + name + '</span>' + generateRequiredLabel(param)
\r
272 + '<div class="col-sm-5"><input type="text" id="' + id
\r
273 + '" name="parameter description" class="form-control" placeholder="'
\r
274 + name + '" value="' + param.defaultValue;
\r
276 component = component + '" readonly="' + strReadOnly + '"/>'+ '</div></div>';
\r
278 component = component + '"/>'+ '</div></div>';
\r
281 } else if (param.type === 'enum') {
\r
282 component = '<div class="form-group" style="margin-left:0px;margin-bottom:5px;">'
\r
283 + '<label class="col-sm-5 control-label">'
\r
284 + '<span>' + name + '</span>'
\r
285 + '<span class="required">*</span>'
\r
287 + '<div class="col-sm-5">'
\r
288 + '<select class="form-control" style ="padding-top: 0px;padding-bottom: 0px;"'
\r
289 + ' id="' + id + '" name="' + param.name + '" value="' + param.defaultValue
\r
291 + this.transformToOptions(param.range)
\r
292 + '</select></div></div>';
\r
298 * transfer the enum range to html body
\r
299 * @param items the map of the range
\r
300 * @returns the html string
\r
302 function transformToOptions(items) {
\r
303 var options = '<option value="select">--select--</option>';
\r
305 for ( var key in items) {
\r
306 var option = '<option value="' + key + '">' + items[key] + '</option>';
\r
307 options = options + option;
\r
313 * generate required identify to html string
\r
314 * @param parameter the parameter object
\r
315 * @returns the html string
\r
317 function generateRequiredLabel(parameter) {
\r
318 var requiredLabel = '';
\r
319 if (parameter.required === 'true') {
\r
320 requiredLabel = '<span class="required">*</span>';
\r
322 return requiredLabel;
\r
327 * @param sengMsgObj the parameters
\r
330 function createServiceInstance(sengMsgObj) {
\r
331 var defer = $.Deferred();
\r
334 'name' : $('#svcName').val(),
\r
335 'description' : $('#svcDesc').val(),
\r
336 'serviceDefId' : '', //no need now, reserved
\r
337 'templateId' : $("#svcTempl").val(),
\r
338 'parameters' : sengMsgObj
\r
341 var serviceGatewayUri = '/openoapi/servicegateway/v1/services';
\r
344 url : serviceGatewayUri,
\r
345 contentType : "application/json",
\r
347 data : JSON.stringify(parameter)
\r
349 .then(function(response) {
\r
350 return queryProgress('create service', response.service.serviceId,response.service.operationId);
\r
351 }).then(function(result){
\r
352 defer.resolve(result);
\r
363 function deleteService(rowId, row) {
\r
364 var deleteHandle = function(result) {
\r
366 var serviceId = row.serviceId;
\r
367 var remove = function() {
\r
368 $('#sai').bootstrapTable('remove', {
\r
369 field : 'serviceId',
\r
370 values : [ serviceId ]
\r
373 var failFun = function(responseDesc) {
\r
374 $.isLoading("hide");
\r
375 showErrorMessage("Delete service failed", responseDesc);
\r
377 $.when(deleteServiceInstance(serviceId))
\r
378 .then(function(response) {
\r
379 if (response.status === 'finished') {
\r
382 showErrorMessage('Create service failed',response);
\r
388 bootbox.confirm("Do you confirm to delete service?", deleteHandle);
\r
392 * sent delete instance msg
\r
393 * @param serviceId the service id
\r
396 function deleteServiceInstance(serviceId) {
\r
397 var defer = $.Deferred();
\r
398 var deleteUrl = '/openoapi/servicegateway/v1/services/' + serviceId;
\r
402 contentType : "application/json",
\r
404 data : JSON.stringify(parameter)
\r
406 .then(function(response) {
\r
407 return queryProgress('delete service', serviceId,response.operationId);
\r
408 }).then(function(result){
\r
409 defer.resolve(result);
\r
415 * query progress of the operation
\r
416 * @param operation the operation string
\r
417 * @param serviceId the service id
\r
418 * @param operationId the operation id
\r
421 function queryProgress(operation, serviceId, operationId) {
\r
422 //show the progress dialog
\r
423 $( "#idProgressTitle" ).text(operation);
\r
424 $( "#progressContent" ).text('status:');
\r
425 $( "#progressbar" ).attr("style","width: 0%");
\r
426 $( "#progressDialog" ).modal({backdrop:'static', keyboard:false});
\r
427 //set a timer for query operation
\r
428 var defer = $.Deferred();
\r
429 var queryProgressUril = '/openoapi/servicegateway/v1/services/' + serviceId + '/operations/' + operationId;
\r
430 var timerDefer = $.Deferred();
\r
431 var timeout = 3600000;
\r
432 var fun = function() {
\r
433 if (timeout === 0) {
\r
434 timerDefer.resolve({
\r
436 reason : operation + ' timeout!',
\r
440 timeout = timeout - 1000;
\r
443 url : gsoServiceUri
\r
445 .then(function(response) {
\r
447 $( "#progressbar" ).attr("style","width: " + response.operation.progress.toString() + "%");
\r
448 $( "#progressValue" ).text(response.operation.progress.toString() + '%');
\r
449 $( "#progressContent" ).text('status: ' + response.operation.operationContent);
\r
450 if (response.operation.result == 'finished' || response.operation.result == 'error') {
\r
451 timerDefer.resolve({
\r
452 status : response.operation.result ,
\r
453 reason : response.operation.reason
\r
458 var timerId = setInterval(fun, 1000);
\r
460 .then(function(responseDesc) {
\r
461 clearInterval(timerId);
\r
462 $('#progressDialog').modal('hide');
\r
464 status : responseDesc.status,
\r
465 reason : responseDesc.reason,
\r
466 serviceId:serviceId
\r
475 * convert the input parameters to ui
\r
476 * @param identify the identify of a segment
\r
477 * @param createParam the create param object
\r
478 * @returns the check result
\r
480 function convertInputsToUI(parentHost, identify, serviceParam) {
\r
481 var components = '';
\r
482 // convert host to UI
\r
483 if (undefined != serviceParam.domainHost && '' != serviceParam.domainHost && parentHost != serviceParam.domainHost) {
\r
487 defaultValue:getShowVal('domainHost', serviceParam.domainHost),
\r
490 components = components + generateParamComponent(serviceParam.nodeType, identify,
\r
493 // convert own param to UI
\r
494 for(var key in serviceParam.additionalParamForNs ){
\r
498 defaultValue:getShowVal(key, serviceParam.additionalParamForNs[key]),
\r
501 components = components + generateParamComponent(serviceParam.nodeType,
\r
502 identify, param, true);
\r
504 // convert segments to UI
\r
505 serviceParam.segments.forEach(function(segment) {
\r
506 // each segment in a field set.
\r
507 components = components + '<div class="mT15 form-group"><fieldset style="margin-left:25px;"><legend>'
\r
508 + segment.nodeTemplateName + '</legend>';
\r
509 // the identify for segment
\r
510 var segmentIdentify = '' == identify ? segment.nodeTemplateName
\r
511 : identify + '_' + segment.nodeTemplateName;
\r
512 // convert segment to UI
\r
513 components = components
\r
514 + convertInputsToUI(serviceParam.domainHost, segmentIdentify, segment);
\r
515 components = components + '</fieldset></div>';
\r
520 function getShowVal(paramName, paramValue){
\r
521 if(paramName == 'domainHost'){
\r
522 return getHostNameByVal(paramValue);
\r
524 else if(paramName == 'location'){
\r
525 return getVimNameById(paramValue);
\r
527 else if(paramName == 'sdncontroller'){
\r
528 return getSdnControllerNameById(paramValue);
\r
535 function getHostNameByVal(hostDomain){
\r
536 var requestUrl ="/openoapi/servicegateway/v1/domains";
\r
537 var returnObj = '';
\r
542 contentType : "application/json",
\r
543 success : function(jsonobj) {
\r
544 jsonobj.forEach(function(host){
\r
545 if(host.host == hostDomain){
\r
546 returnObj = host.name;
\r
550 error : function(xhr, ajaxOptions, thrownError) {
\r
551 alert("Error on getting link data : " + xhr.responseText);
\r
557 //get the vim name by id.
\r
558 function getVimNameById(vimId){
\r
559 var requestUrl ="/openoapi/extsys/v1/vims/" + vimId;
\r
566 contentType : "application/json",
\r
567 success : function(jsonobj) {
\r
568 // TODO return according to the json data received.
\r
569 returnObj = jsonobj;
\r
571 error : function(xhr, ajaxOptions, thrownError) {
\r
572 alert("Error on getting link data : " + xhr.responseText);
\r
578 //get the sdn controller name by id.
\r
579 function getSdnControllerNameById(sdnControllerId){
\r
580 var requestUrl ="/openoapi/extsys/v1/sdncontrollers/" + sdnControllerId;
\r
587 contentType : "application/json",
\r
588 success : function(jsonobj) {
\r
589 // TODO return according to the json data received.
\r
590 returnObj = jsonobj;
\r
592 error : function(xhr, ajaxOptions, thrownError) {
\r
593 alert("Error on getting link data : " + xhr.responseText);
\r
602 * show error dialog
\r
603 * @param title the title
\r
604 * @param result the result
\r
607 function showErrorMessage(title, result) {
\r
608 //show the error dialog
\r
609 $( "#errorDialogTitle" ).text(title);
\r
610 $( "#errorDialogReason" ).text(result.reason);
\r
611 $( "#errorDialog" ).modal({backdrop:'static', keyboard:false});
\r
615 * generate the template to create parameters object
\r
617 * @param templateId the template id
\r
620 function queryService(serviceId) {
\r
621 var uri = '/openoapi/servicegateway/v1/services/' + serviceId;
\r