2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.mso.bpmn.infrastructure.scripts
22 import java.util.UUID;
24 import org.json.JSONObject;
25 import org.json.JSONArray;
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.runtime.Execution;
30 import static org.apache.commons.lang3.StringUtils.*;
32 import org.openecomp.mso.bpmn.core.json.JsonUtils
33 import org.openecomp.mso.rest.APIResponse
34 import org.openecomp.mso.rest.RESTClient
35 import org.openecomp.mso.rest.RESTConfig
36 import org.openecomp.mso.bpmn.common.scripts.AaiUtil
37 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
38 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils
39 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
40 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
41 import org.openecomp.mso.bpmn.common.scripts.VidUtils
42 import org.openecomp.mso.bpmn.core.RollbackData
43 import org.openecomp.mso.bpmn.core.WorkflowException
46 * This class supports the macro VID Flow
47 * with the deletion of a generic vnf and related VF modules.
49 class DoDeleteVnfAndModules extends AbstractServiceTaskProcessor {
51 String Prefix="DDVAM_"
52 ExceptionUtil exceptionUtil = new ExceptionUtil()
53 JsonUtils jsonUtil = new JsonUtils()
54 VidUtils vidUtils = new VidUtils(this)
55 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
58 * This method gets and validates the incoming
64 public void preProcessRequest(Execution execution) {
65 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
66 execution.setVariable("prefix",Prefix)
67 utils.log("DEBUG", " *** STARTED DoDeleteVnfAndModules PreProcessRequest Process*** ", isDebugEnabled)
72 String cloudConfiguration = execution.getVariable("cloudConfiguration")
74 String requestId = execution.getVariable("msoRequestId")
75 execution.setVariable("requestId", requestId)
76 execution.setVariable("mso-request-id", requestId)
77 utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugEnabled)
79 String serviceInstanceId = execution.getVariable("serviceInstanceId")
80 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
82 String vnfId = execution.getVariable("vnfId")
83 utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)
86 execution.setVariable("DDVAM_source", source)
87 utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)
89 String sdncVersion = execution.getVariable("sdncVersion")
90 if (sdncVersion == null) {
93 execution.setVariable("DDVAM_sdncVersion", sdncVersion)
94 utils.log("DEBUG", "Incoming Sdnc Version is: " + sdncVersion, isDebugEnabled)
96 String sdncCallbackUrl = (String) execution.getVariable('URN_mso_workflow_sdncadapter_callback')
97 if (sdncCallbackUrl == null || sdncCallbackUrl.trim().isEmpty()) {
98 def msg = 'Required variable \'URN_mso_workflow_sdncadapter_callback\' is missing'
100 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
102 execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
103 utils.logAudit("SDNC Callback URL: " + sdncCallbackUrl)
104 logDebug("SDNC Callback URL is: " + sdncCallbackUrl, isDebugEnabled)
106 if (!sdncVersion.equals("1702")) {
107 //String vnfModelInfo = execution.getVariable("vnfModelInfo")
108 //String serviceModelInfo = execution.getVariable("serviceModelInfo")
110 String serviceId = execution.getVariable("productFamilyId")
111 execution.setVariable("DDVAM_serviceId", serviceId)
112 utils.log("DEBUG", "Incoming Service Id is: " + serviceId, isDebugEnabled)
115 //String modelInvariantId = jsonUtil.getJsonValue(vnfModelInfo, "modelInvariantId")
116 //execution.setVariable("DDVAM_modelInvariantId", modelInvariantId)
117 //utils.log("DEBUG", "Incoming Invariant Id is: " + modelInvariantId, isDebugEnabled)
119 //String modelVersionId = jsonUtil.getJsonValue(vnfModelInfo, "modelVersionId")
120 //if (modelVersionId == null) {
121 // modelVersionId = ""
123 //execution.setVariable("DDVAM_modelVersionId", modelVersionId)
124 //utils.log("DEBUG", "Incoming Version Id is: " + modelVersionId, isDebugEnabled)
126 //String modelVersion = jsonUtil.getJsonValue(vnfModelInfo, "modelVersion")
127 //execution.setVariable("DDVAM_modelVersion", modelVersion)
128 //utils.log("DEBUG", "Incoming Model Version is: " + modelVersion, isDebugEnabled)
130 //String modelName = jsonUtil.getJsonValue(vnfModelInfo, "modelName")
131 //execution.setVariable("DDVAM_modelName", modelName)
132 //utils.log("DEBUG", "Incoming Model Name is: " + modelName, isDebugEnabled)
134 //String modelCustomizationId = jsonUtil.getJsonValue(vnfModelInfo, "modelCustomizationId")
135 //if (modelCustomizationId == null) {
136 // modelCustomizationId = ""
138 //execution.setVariable("DDVAM_modelCustomizationId", modelCustomizationId)
139 //utils.log("DEBUG", "Incoming Model Customization Id is: " + modelCustomizationId, isDebugEnabled)
141 String cloudSiteId = execution.getVariable("lcpCloudRegionId")
142 execution.setVariable("DDVAM_cloudSiteId", cloudSiteId)
143 utils.log("DEBUG", "Incoming Cloud Site Id is: " + cloudSiteId, isDebugEnabled)
145 String tenantId = execution.getVariable("tenantId")
146 execution.setVariable("DDVAM_tenantId", tenantId)
147 utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId, isDebugEnabled)
149 String globalSubscriberId = execution.getVariable("globalSubscriberId")
150 if (globalSubscriberId == null) {
151 globalSubscriberId = ""
153 execution.setVariable("DDVAM_globalSubscriberId", globalSubscriberId)
154 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
156 execution.setVariable("DDVAM_moduleCount", 0)
157 execution.setVariable("DDVAM_nextModule", 0)
161 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
164 utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugEnabled)
165 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
168 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules PreProcessRequest Process ***", isDebugEnabled)
173 public void preProcessAddOnModule(Execution execution){
174 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
175 execution.setVariable("prefix", Prefix)
176 logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)
179 JSONArray addOnModules = (JSONArray) execution.getVariable("addOnModules")
180 int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")
182 JSONObject addOnModule = addOnModules[addOnIndex]
184 def newVfModuleId = UUID.randomUUID().toString()
185 execution.setVariable("addOnVfModuleId", newVfModuleId)
187 execution.setVariable("instancesOfThisModelDeployed", 0)
189 JSONObject addOnVfModuleModelInfoObject = jsonUtil.getJsonValueForKey(addOnModule, "modelInfo")
190 String addOnVfModuleModelInfo = addOnVfModuleModelInfoObject.toString()
191 execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)
192 String addOnVfModuleLabel = jsonUtil.getJsonValueForKey(addOnModule, "vfModuleLabel")
193 execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)
194 String addOnPersonaModelId = jsonUtil.getJsonValueForKey(addOnVfModuleModelInfoObject, "modelInvariantId")
195 execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)
196 String addOnInitialCount = jsonUtil.getJsonValueForKey(addOnModule, "initialCount")
197 execution.setVariable("initialCount", addOnInitialCount)
201 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
202 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
204 logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
208 * Using the received vnfId and vfModuleId, query AAI to get the corresponding VNF info.
209 * A 200 response is expected with the VNF info in the response body. Will find out the base module info.
211 * @param execution The flow's execution instance.
213 public void queryAAIVfModule(Execution execution) {
214 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
215 def method = getClass().getSimpleName() + '.queryAAIVfModule(' +
216 'execution=' + execution.getId() +
218 logDebug('Entered ' + method, isDebugLogEnabled)
221 def vnfId = execution.getVariable('vnfId')
223 AaiUtil aaiUriUtil = new AaiUtil(this)
224 String aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
225 logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
227 String endPoint = execution.getVariable("URN_aai_endpoint") + "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"
228 utils.logAudit("AAI endPoint: " + endPoint)
231 RESTConfig config = new RESTConfig(endPoint);
232 def responseData = ''
233 def aaiRequestId = UUID.randomUUID().toString()
234 RESTClient client = new RESTClient(config).
235 addHeader('X-TransactionId', aaiRequestId).
236 addHeader('X-FromAppId', 'MSO').
237 addHeader('Content-Type', 'application/xml').
238 addHeader('Accept','application/xml');
239 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)
240 APIResponse response = client.httpGet()
241 utils.logAudit("createVfModule - invoking httpGet() to AAI")
243 responseData = response.getResponseBodyAsString()
244 if (responseData != null) {
245 logDebug("Received generic VNF data: " + responseData, isDebugLogEnabled)
249 utils.logAudit("createVfModule - queryAAIVfModule Response: " + responseData)
250 utils.logAudit("createVfModule - queryAAIVfModule ResponseCode: " + response.getStatusCode())
252 execution.setVariable('DCVFM_queryAAIVfModuleResponseCode', response.getStatusCode())
253 execution.setVariable('DCVFM_queryAAIVfModuleResponse', responseData)
254 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
255 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
256 //Map<String, String>[] vfModules = new HashMap<String,String>[]
257 def vfModulesList = new ArrayList<Map<String,String>>()
259 def vfModuleBaseEntry = null
260 if (response.getStatusCode() == 200) {
261 // Parse the VNF record from A&AI to find base module info
262 logDebug('Parsing the VNF data to find base module info', isDebugLogEnabled)
263 if (responseData != null) {
264 def vfModulesText = utils.getNodeXml(responseData, "vf-modules")
265 logDebug("vModulesText: " + vfModulesText, isDebugLogEnabled)
266 if (vfModulesText != null && !vfModulesText.trim().isEmpty()) {
267 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)
268 vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}
269 execution.setVariable("DDVAM_moduleCount", vfModules.size())
270 int vfModulesSize = 0
271 for (i in 0..vfModules.size()-1) {
272 def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])
274 Map<String, String> vfModuleEntry = new HashMap<String, String>()
275 def vfModuleId = utils.getNodeText1(vfModuleXml, "vf-module-id")
276 vfModuleEntry.put("vfModuleId", vfModuleId)
277 def vfModuleName = utils.getNodeText1(vfModuleXml, "vf-module-name")
278 vfModuleEntry.put("vfModuleName", vfModuleName)
280 def isBaseVfModule = utils.getNodeText(vfModuleXml, "is-base-vf-module")
281 // Save base vf module for last
282 if (isBaseVfModule == "true") {
283 vfModuleBaseEntry = vfModuleEntry
286 vfModulesList.add(vfModuleEntry)
289 if (vfModuleBaseEntry != null) {
290 vfModulesList.add(vfModuleBaseEntry)
296 execution.setVariable("DDVAM_vfModules", vfModulesList)
297 } catch (Exception ex) {
299 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)
300 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
302 logDebug('Exited ' + method, isDebugLogEnabled)
303 } catch (BpmnError e) {
305 } catch (Exception e) {
306 logError('Caught exception in ' + method, e)
307 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAIVfModule(): ' + e.getMessage())
311 public void prepareNextModuleToDelete(Execution execution){
312 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
313 execution.setVariable("prefix", Prefix)
314 logDebug(" ======== STARTED prepareNextModuleToDelete ======== ", isDebugLogEnabled)
317 int i = execution.getVariable("DDVAM_nextModule")
318 def vfModules = execution.getVariable("DDVAM_vfModules")
319 def vfModule = vfModules[i]
321 def vfModuleId = vfModule.get("vfModuleId")
322 execution.setVariable("DDVAM_vfModuleId", vfModuleId)
324 def vfModuleName = vfModule.get("vfModuleName")
325 execution.setVariable("DDVAM_vfModuleName", vfModuleName)
329 def vfModuleModelInfo = ""
330 execution.setVariable("DDVAM_vfModuleModelInfo", vfModuleModelInfo)
333 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
334 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during prepareNextModuleToDelete Method:\n" + e.getMessage())
336 logDebug("======== COMPLETED prepareNextModuleToDelete ======== ", isDebugLogEnabled)
339 public void preProcessSDNCDeactivateRequest(Execution execution){
340 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
341 execution.setVariable("prefix", Prefix)
342 logDebug(" ======== STARTED preProcessSDNCDeactivateRequest ======== ", isDebugLogEnabled)
343 def vnfId = execution.getVariable("vnfId")
344 def serviceInstanceId = execution.getVariable("serviceInstanceId")
349 String deactivateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "deactivate")
351 deactivateSDNCRequest = utils.formatXml(deactivateSDNCRequest)
352 execution.setVariable("DDVAM_deactivateSDNCRequest", deactivateSDNCRequest)
353 logDebug("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest, isDebugLogEnabled)
354 utils.logAudit("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest)
357 utils.log("ERROR", "Exception Occured Processing preProcessSDNCDeactivateRequest. Exception is:\n" + e, isDebugLogEnabled)
358 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessSDNCDeactivateRequest Method:\n" + e.getMessage())
360 logDebug("======== COMPLETED preProcessSDNCDeactivateRequest ======== ", isDebugLogEnabled)
363 public void preProcessSDNCUnassignRequest(Execution execution) {
364 def method = getClass().getSimpleName() + '.preProcessSDNCUnassignRequest(' +
365 'execution=' + execution.getId() +
367 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
368 logDebug('Entered ' + method, isDebugLogEnabled)
369 execution.setVariable("prefix", Prefix)
370 logDebug(" ======== STARTED preProcessSDNCUnassignRequest Process ======== ", isDebugLogEnabled)
372 String vnfId = execution.getVariable("vnfId")
373 String serviceInstanceId = execution.getVariable("serviceInstanceId")
375 String unassignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "unassign")
377 execution.setVariable("DDVAM_unassignSDNCRequest", unassignSDNCRequest)
378 logDebug("Outgoing UnassignSDNCRequest is: \n" + unassignSDNCRequest, isDebugLogEnabled)
379 utils.logAudit("Outgoing UnassignSDNCRequest is: \n" + unassignSDNCRequest)
382 log.debug("Exception Occured Processing preProcessSDNCUnassignRequest. Exception is:\n" + e, isDebugLogEnabled)
383 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during preProcessSDNCUnassignRequest Method:\n" + e.getMessage())
385 logDebug("======== COMPLETED preProcessSDNCUnassignRequest Process ======== ", isDebugLogEnabled)
388 public String buildSDNCRequest(Execution execution, String svcInstId, String action){
390 String uuid = execution.getVariable('testReqId') // for junits
392 uuid = execution.getVariable("mso-request-id") + "-" + System.currentTimeMillis()
394 def callbackURL = execution.getVariable("sdncCallbackUrl")
395 def requestId = execution.getVariable("msoRequestId")
396 def serviceId = execution.getVariable("DDVAM_serviceId")
397 def tenantId = execution.getVariable("DDVAM_tenantId")
398 def source = execution.getVariable("DDVAM_source")
399 def vnfId = execution.getVariable("vnfId")
400 def serviceInstanceId = execution.getVariable("serviceInstanceId")
401 def cloudSiteId = execution.getVariable("DDVAM_cloudSiteId")
402 def modelCustomizationId = execution.getVariable("DDVAM_modelCustomizationId")
403 //def serviceModelInfo = execution.getVariable("serviceModelInfo")
404 //def vnfModelInfo = execution.getVariable("vnfModelInfo")
405 //String serviceEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(serviceModelInfo)
406 //String vnfEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(vnfModelInfo)
407 def globalSubscriberId = execution.getVariable("DDVAM_globalSubscriberId")
408 def sdncVersion = execution.getVariable("DDVAM_sdncVersion")
411 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
412 xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
413 xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
414 <sdncadapter:RequestHeader>
415 <sdncadapter:RequestId>${requestId}</sdncadapter:RequestId>
416 <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>
417 <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
418 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
419 <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>
420 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
421 </sdncadapter:RequestHeader>
422 <sdncadapterworkflow:SDNCRequestData>
423 <request-information>
424 <request-id>${requestId}</request-id>
425 <request-action>DeleteVnfInstance</request-action>
426 <source>${source}</source>
430 </request-information>
431 <service-information>
433 <subscription-service-type/>
434 <service-instance-id>${serviceInstanceId}</service-instance-id>
435 <global-customer-id/>
436 </service-information>
438 <vnf-id>${vnfId}</vnf-id>
443 <tenant>${tenantId}</tenant>
444 <aic-cloud-region>${cloudSiteId}</aic-cloud-region>
446 </sdncadapterworkflow:SDNCRequestData>
447 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
449 utils.logAudit("sdncRequest: " + sdncRequest)
453 public void validateSDNCResponse(Execution execution, String response, String method){
454 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
455 execution.setVariable("prefix",Prefix)
456 logDebug(" *** STARTED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
458 WorkflowException workflowException = execution.getVariable("WorkflowException")
459 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
461 utils.logAudit("workflowException: " + workflowException)
463 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
464 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
466 utils.logAudit("SDNCResponse: " + response)
468 String sdncResponse = response
469 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
470 logDebug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call. Response is: \n" + sdncResponse, isDebugLogEnabled)
472 logDebug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.", isDebugLogEnabled)
473 throw new BpmnError("MSOWorkflowException")
475 logDebug(" *** COMPLETED ValidateSDNCResponse Process*** ", isDebugLogEnabled)