/*
* Copyright 2016 ZTE Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var templateParameters = {
changed: true,
parameters: [],
vimInfos: []
};
var lcmHandler = function () {
this._addOwnEvents();
jQuery.i18n.properties({
language:'en-US',
name:'lcm-template-parameters-i18n',
path:'i18n/',
mode:'map'
});
};
lcmHandler.prototype = {
_addOwnEvents: function () {
$('#createNS').click(this.okAction);
},
okAction: function () {
if(!checkLocation(templateParameters.parameters)) {
alert('Location must be selected in Template Parameters');
return;
}
$.isLoading({ text: "Creating service..." });
var serviceInstance = {
serviceTemplateId: $("#svcTempl").val(),
serviceName: $('#svcName').val(),
description: $('#svcDesc').val(),
inputParameters: collectServiceParameters(templateParameters.parameters)
};
var gatewayService = '/openoapi/servicegateway/v1/services';
$.when(
fetchServiceTemplateBy(serviceInstance.serviceTemplateId)
).then(
function(template) {
serviceInstance.templateName = template.name;
serviceInstance.serviceType = template.serviceType;
return createNetworkServiceInstance(template, serviceInstance, gatewayService);
}
).then(
function(response) {
$.isLoading('hide');
if(response.status === 'success') {
updateTable(response.instance);
$('#vmAppDialog').removeClass('in').css('display', 'none');
} else {
showErrorMessage('Create service failed', response.errorResult);
}
}
);
}
};
function showErrorMessage(title, result) {
var info = ' ' + '
' + title + '
';
info = info + '
Status: ' + result.status + '
';
info = info + '
Description:
';
if(result.statusDescription.forEach === undefined) {
info = info + '
' + result.statusDescription + '
';
} else {
result.statusDescription.forEach(function(message) {
info = info + '
';
return component;
}
function transformToOptions(items) {
var options = '';
var i;
for (i = 0; i < items.length; i += 1) {
var option = '';
options = options + option;
}
return options;
}
function generateComponent(inputPara) {
var component = '
' +
'' +
'
' +
'' +
'
';
return component;
}
function showName(inputPara) {
var name = $.i18n.prop(inputPara.name)
if(name.length === 0 || name.slice(0, 1) === '[') {
name = inputPara.showName;
}
return name;
}
function generateRequiredLabel(parameter) {
var requiredLabel = '';
if (parameter.required === 'true') {
requiredLabel = '*';
}
return requiredLabel;
}
function createNetworkServiceInstance(template, serviceInstance, gatewayService) {
if (template.serviceType === 'GSO') {
return createGsoServiceInstance(gatewayService, serviceInstance, template);
} else if (template.serviceType === 'NFVO') {
return createNfvoServiceInstance(gatewayService, serviceInstance, template);
} else if (template.serviceType === 'SDNO') {
return createSdnoServiceInstance(gatewayService, serviceInstance);
}
}
function createGsoServiceInstance(gatewayService, serviceInstance, serviceTemplate) {
var defer = $.Deferred();
var gsoLcmUri = '/openoapi/gso/v1/services';
var parameter = {
'service': {
'name': serviceInstance.serviceName,
'description': serviceInstance.description,
'serviceDefId': serviceTemplate.gsarId,
'templateId': serviceInstance.serviceTemplateId,
'templateName': serviceTemplate.name,
'gatewayUri': gsoLcmUri,
'parameters': serviceInstance.inputParameters
}
};
$.when($.ajax({
type: "POST",
url: gatewayService,
contentType: "application/json",
dataType: "json",
data: JSON.stringify(parameter)
})).then(function(response) {
if(response.result.status === 'success') {
serviceInstance.serviceId = response.serviceId;
var gsoServiceUri = '/openoapi/gso/v1/services/' + response.serviceId;
var timerDefer = $.Deferred();
var timeout = 3600000;
var fun = function() {
if(timeout === 0) {
timerDefer.resolve({
status: 'fail',
statusDescription: 'Operation is timeout!',
errorCode: ''
});
return;
}
timeout = timeout - 1000;
$.when(
$.ajax({
type: "GET",
url: gsoServiceUri
})
).then(
function(response) {
if(response.result === 'success' || response.result === 'failed') {
timerDefer.resolve(response);
}
}
);
};
var timerId = setInterval(fun, 1000);
$.when(timerDefer).then(
function(responseDesc) {
clearInterval(timerId);
if(responseDesc.result === 'success') {
defer.resolve({status: 'success', instance: serviceInstance});
} else {
defer.resolve({
status: 'fail',
errorResult: {
status: responseDesc.result,
statusDescription: 'fail to create the service',
errorCode: ''
}});
}
}
);
} else {
defer.resolve({status: 'fail', errorResult: {status:'fail', statusDescription: 'fail to create the service', errorCode: ''}});
}
});
return defer;
}
function createNfvoServiceInstance(gatewayService, serviceInstance, template) {
var nfvoLcmUri = '/openoapi/nslcm/v1';
serviceInstance.nsdId = template.id;
return createServiceInstance(gatewayService, nfvoLcmUri, serviceInstance);
}
function createSdnoServiceInstance(gatewayService, serviceInstance) {
var sdnoLcmUri = '/openoapi/sdnonslcm/v1';
serviceInstance.nsdId = serviceInstance.serviceTemplateId;
return createServiceInstance(gatewayService, sdnoLcmUri, serviceInstance);
}
function createServiceInstance(gatewayService, lcmUri, serviceInstance) {
var nsUri = lcmUri + '/ns';
var defer = $.Deferred();
var sParameter = {
'nsdId': serviceInstance.nsdId,
'nsName': serviceInstance.serviceName,
'description': serviceInstance.description,
'gatewayUri': nsUri
};
$.when($.ajax({
type: "POST",
url: gatewayService,
contentType: "application/json",
dataType: "json",
data: JSON.stringify(sParameter)
})).then(function(response) {
if(response.result.status === 'success') {
var nsInstanceId = response.serviceId;
serviceInstance.serviceId = nsInstanceId;
var initNsUrl = nsUri + '/' + nsInstanceId + '/instantiate';
var parameter = {
'gatewayUri': initNsUrl,
'nsInstanceId': nsInstanceId,
'additionalParamForNs': serviceInstance.inputParameters
};
return $.ajax({
type: "POST",
url: gatewayService,
contentType: "application/json",
dataType: "json",
data: JSON.stringify(parameter)
});
} else {
return response;
}
}).then(function(response) {
if(response.result.status === 'success') {
var jobId = response.serviceId;
var jobStatusUri = lcmUri + '/jobs/' + jobId;
var timerDefer = $.Deferred();
var timeout = 3600000;
var fun = function() {
if(timeout === 0) {
timerDefer.resolve({
status: 'fail',
statusDescription: 'Operation is timeout!',
errorCode: ''
});
return;
}
timeout = timeout - 1000;
$.when(
$.ajax({
type: "GET",
url: jobStatusUri
})
).then(
function(jobResponse) {
var responseDesc = jobResponse.responseDescriptor;
if(responseDesc.status === 'finished' || responseDesc.status === 'error') {
timerDefer.resolve(responseDesc);
}
}
);
};
var timerId = setInterval(fun, 1000);
$.when(timerDefer).then(
function(responseDesc) {
clearInterval(timerId);
if(responseDesc.status === 'finished') {
defer.resolve({status: 'success', instance: serviceInstance});
} else {
defer.resolve({
status: 'fail',
errorResult: {
status: responseDesc.status,
statusDescription: responseDesc.statusDescription,
errorCode: responseDesc.errorCode
}});
}
}
);
} else {
defer.resolve({status: 'fail', errorResult: response.result});
}
});
return defer;
}
function collectServiceParameters(parameters) {
var serviceParameters = {};
var i;
for (i = 0; i < parameters.length; i += 1) {
var value = $('#' + parameters[i].id).val();
serviceParameters[parameters[i].name] = value;
}
return serviceParameters;
}
function updateTable(serviceInstance) {
serviceInstance.createTime = formatDate(new Date());
$('#sai').bootstrapTable("append", serviceInstance);
}
function formatDate(date) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
}
function deleteNe(rowId, row) {
var deleteHandle = function(result) {
if(result) {
$.isLoading({ text: "Deleting service..." });
var instanceId = row.serviceId;
var serviceType = row.serviceType;
var gatewayService = '/openoapi/servicegateway/v1/services/' + instanceId + '/terminate';
var remove = function () {
$.isLoading( "hide" );
$('#sai').bootstrapTable('remove', {field: 'serviceId', values: [instanceId]});
};
var failFun = function(responseDesc) {
$.isLoading( "hide" );
showErrorMessage("Delete service failed", responseDesc);
}
if(serviceType === 'GSO') {
deleteGsoServiceInstance(gatewayService, instanceId, remove, failFun);
} else if (serviceType === 'NFVO') {
var nfvoLcmUri = '/openoapi/nslcm/v1';
deleteNonGsoServiceInstance(gatewayService, nfvoLcmUri, instanceId, remove, failFun);
} else if (serviceType === 'SDNO') {
var sdnoLcmUri = '/openoapi/sdnonslcm/v1';
deleteNonGsoServiceInstance(gatewayService, sdnoLcmUri, instanceId, remove, failFun);
}
}
};
bootbox.confirm("Do you confirm to delete service?", deleteHandle);
}
function deleteGsoServiceInstance(gatewayService, instanceId, remove, failFun) {
var gsoLcmUri = '/openoapi/gso/v1/services';
$.when(
deleteNetworkServiceInstance(gatewayService, gsoLcmUri, instanceId)
).then(
function(response) {
var gsoServiceUri = '/openoapi/gso/v1/services/toposequence/' + instanceId;
var timerDefer = $.Deferred();
var timeout = 3600000;
var fun = function() {
if(timeout === 0) {
timerDefer.resolve({
status: 'fail',
statusDescription: 'Operation is timeout!',
errorCode: ''
});
return;
}
timeout = timeout - 1000;
$.when(
$.ajax({
type: "GET",
url: gsoServiceUri
})
).then(
function(response) {
if(response.length == 0) {
timerDefer.resolve({status:'success', statusDescription: 'success to delete the service', errorCode: ''});
}
}
);
};
var timerId = setInterval(fun, 1000);
$.when(timerDefer).then(
function(responseDesc) {
clearInterval(timerId);
remove();
if(responseDesc.status != 'success'){
failFun({status: "fail", statusDescription: "delete service failed.", errorCode: "500"});
}
}
);
}
);
}
function deleteNonGsoServiceInstance(gatewayService, lcmUri, instanceId, remove, failFun) {
var nsUri = lcmUri + '/ns';
$.when(
terminateNetworkServiceInstance(gatewayService, nsUri, instanceId)
).then(
function(response) {
var jobId = response.jobId;
var jobStatusUri = lcmUri + '/jobs/' + jobId;
var timerDefer = $.Deferred();
var timeout = 3600000;
var fun = function() {
if(timeout === 0) {
timerDefer.resolve({
status: 'fail',
statusDescription: 'Operation is timeout!',
errorCode: ''
});
return;
}
timeout = timeout - 1000;
$.when(
$.ajax({
type: "GET",
url: jobStatusUri
})
).then(
function(jobResponse) {
var responseDesc = jobResponse.responseDescriptor;
if(responseDesc.status === 'finished' || responseDesc.status === 'error') {
timerDefer.resolve(responseDesc);
}
}
);
};
var timerId = setInterval(fun, 1000);
$.when(timerDefer).then(
function(responseDesc) {
clearInterval(timerId);
if(responseDesc.status === 'finished') {
$.when(
deleteNetworkServiceInstance(gatewayService, nsUri, instanceId)
).then(
function(nsResponse) {
if(nsResponse.status === 'success') {
remove();
} else {
failFun(nsResponse);
}
}
).fail(function() {
failFun({status: "fail", statusDescription: "delete service failed.", errorCode: "500"});
});
} else {
failFun(responseDesc);
}
}
);
}
).fail(function() {
failFun({status: "fail", statusDescription: "delete service failed.", errorCode: "500"});
});
}
function deleteNetworkServiceInstance(gatewayService, nsUri, instanceId) {
var instanceUri = nsUri + '/' + instanceId;
var parameter = {
'operation': "DELETE",
'gatewayUri': instanceUri
};
return $.ajax({
type: "POST",
url: gatewayService,
contentType: "application/json",
dataType: "json",
data: JSON.stringify(parameter)
});
}
function terminateNetworkServiceInstance(gatewayService, nsUri, instanceId) {
var instanceUri = nsUri + '/' + instanceId;
var nsTerminateUri = instanceUri + '/terminate';
var terminateParameter = {
'nsInstanceId': instanceId,
'terminationType': "graceful",
'gracefulTerminationTimeout': "60",
'operation': "POST",
'gatewayUri': nsTerminateUri
};
return $.ajax({
type: "POST",
url: gatewayService,
contentType: "application/json",
dataType: "json",
data: JSON.stringify(terminateParameter)
});
}