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;
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.runtime.Execution;
29 import static org.apache.commons.lang3.StringUtils.*;
31 import org.openecomp.mso.bpmn.core.json.JsonUtils
32 import org.openecomp.mso.rest.APIResponse
33 import org.openecomp.mso.rest.RESTClient
34 import org.openecomp.mso.rest.RESTConfig
35 import org.openecomp.mso.bpmn.common.scripts.AaiUtil
36 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
37 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
38 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
39 import org.openecomp.mso.bpmn.common.scripts.VidUtils
40 import org.openecomp.mso.bpmn.core.RollbackData
41 import org.openecomp.mso.bpmn.core.WorkflowException
42 import org.springframework.web.util.UriUtils;
45 * This class supports the macro VID Flow
46 * with the deletion of a generic vnf and related VF modules.
48 class DoDeleteVnfAndModules extends AbstractServiceTaskProcessor {
50 String Prefix="DDVAM_"
51 ExceptionUtil exceptionUtil = new ExceptionUtil()
52 JsonUtils jsonUtil = new JsonUtils()
53 VidUtils vidUtils = new VidUtils(this)
54 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
57 * This method gets and validates the incoming
63 public void preProcessRequest(Execution execution) {
64 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
65 execution.setVariable("prefix",Prefix)
66 utils.log("DEBUG", " *** STARTED DoDeleteVnfAndModules PreProcessRequest Process*** ", isDebugEnabled)
71 String cloudConfiguration = execution.getVariable("cloudConfiguration")
72 utils.log("DEBUG", "Cloud Configuration: " + cloudConfiguration, isDebugEnabled)
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 execution.setVariable("DDVAM_isVidRequest", "true")
91 String sdncVersion = execution.getVariable("sdncVersion")
92 if (sdncVersion == null) {
95 execution.setVariable("DDVAM_sdncVersion", sdncVersion)
96 utils.log("DEBUG", "Incoming Sdnc Version is: " + sdncVersion, isDebugEnabled)
98 String sdncCallbackUrl = (String) execution.getVariable('URN_mso_workflow_sdncadapter_callback')
99 if (sdncCallbackUrl == null || sdncCallbackUrl.trim().isEmpty()) {
100 def msg = 'Required variable \'URN_mso_workflow_sdncadapter_callback\' is missing'
102 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
104 execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
105 utils.logAudit("SDNC Callback URL: " + sdncCallbackUrl)
106 logDebug("SDNC Callback URL is: " + sdncCallbackUrl, isDebugEnabled)
108 if (!sdncVersion.equals("1702")) {
109 //String vnfModelInfo = execution.getVariable("vnfModelInfo")
110 //String serviceModelInfo = execution.getVariable("serviceModelInfo")
112 String serviceId = execution.getVariable("productFamilyId")
113 execution.setVariable("DDVAM_serviceId", serviceId)
114 utils.log("DEBUG", "Incoming Service Id is: " + serviceId, isDebugEnabled)
117 //String modelInvariantId = jsonUtil.getJsonValue(vnfModelInfo, "modelInvariantId")
118 //execution.setVariable("DDVAM_modelInvariantId", modelInvariantId)
119 //utils.log("DEBUG", "Incoming Invariant Id is: " + modelInvariantId, isDebugEnabled)
121 //String modelVersionId = jsonUtil.getJsonValue(vnfModelInfo, "modelVersionId")
122 //if (modelVersionId == null) {
123 // modelVersionId = ""
125 //execution.setVariable("DDVAM_modelVersionId", modelVersionId)
126 //utils.log("DEBUG", "Incoming Version Id is: " + modelVersionId, isDebugEnabled)
128 //String modelVersion = jsonUtil.getJsonValue(vnfModelInfo, "modelVersion")
129 //execution.setVariable("DDVAM_modelVersion", modelVersion)
130 //utils.log("DEBUG", "Incoming Model Version is: " + modelVersion, isDebugEnabled)
132 //String modelName = jsonUtil.getJsonValue(vnfModelInfo, "modelName")
133 //execution.setVariable("DDVAM_modelName", modelName)
134 //utils.log("DEBUG", "Incoming Model Name is: " + modelName, isDebugEnabled)
136 //String modelCustomizationId = jsonUtil.getJsonValue(vnfModelInfo, "modelCustomizationId")
137 //if (modelCustomizationId == null) {
138 // modelCustomizationId = ""
140 //execution.setVariable("DDVAM_modelCustomizationId", modelCustomizationId)
141 //utils.log("DEBUG", "Incoming Model Customization Id is: " + modelCustomizationId, isDebugEnabled)
143 String cloudSiteId = execution.getVariable("lcpCloudRegionId")
144 execution.setVariable("DDVAM_cloudSiteId", cloudSiteId)
145 utils.log("DEBUG", "Incoming Cloud Site Id is: " + cloudSiteId, isDebugEnabled)
147 String tenantId = execution.getVariable("tenantId")
148 execution.setVariable("DDVAM_tenantId", tenantId)
149 utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId, isDebugEnabled)
151 String globalSubscriberId = execution.getVariable("globalSubscriberId")
152 if (globalSubscriberId == null) {
153 globalSubscriberId = ""
155 execution.setVariable("DDVAM_globalSubscriberId", globalSubscriberId)
156 utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)
158 execution.setVariable("DDVAM_moduleCount", 0)
159 execution.setVariable("DDVAM_nextModule", 0)
163 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
166 utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugEnabled)
167 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
170 utils.log("DEBUG", "*** COMPLETED DoDeleteVnfAndModules PreProcessRequest Process ***", isDebugEnabled)
175 public void preProcessAddOnModule(Execution execution){
176 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
177 execution.setVariable("prefix", Prefix)
178 logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)
181 JSONArray addOnModules = (JSONArray) execution.getVariable("addOnModules")
182 int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")
184 JSONObject addOnModule = addOnModules[addOnIndex]
186 def newVfModuleId = UUID.randomUUID().toString()
187 execution.setVariable("addOnVfModuleId", newVfModuleId)
189 execution.setVariable("instancesOfThisModelDeployed", 0)
191 JSONObject addOnVfModuleModelInfoObject = jsonUtil.getJsonValueForKey(addOnModule, "modelInfo")
192 String addOnVfModuleModelInfo = addOnVfModuleModelInfoObject.toString()
193 execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)
194 String addOnVfModuleLabel = jsonUtil.getJsonValueForKey(addOnModule, "vfModuleLabel")
195 execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)
196 String addOnPersonaModelId = jsonUtil.getJsonValueForKey(addOnVfModuleModelInfoObject, "modelInvariantUuid")
197 execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)
198 String addOnInitialCount = jsonUtil.getJsonValueForKey(addOnModule, "initialCount")
199 execution.setVariable("initialCount", addOnInitialCount)
203 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
204 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
206 logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
210 * Using the received vnfId and vfModuleId, query AAI to get the corresponding VNF info.
211 * A 200 response is expected with the VNF info in the response body. Will find out the base module info.
213 * @param execution The flow's execution instance.
215 public void queryAAIVfModule(Execution execution) {
216 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
217 def method = getClass().getSimpleName() + '.queryAAIVfModule(' +
218 'execution=' + execution.getId() +
220 logDebug('Entered ' + method, isDebugLogEnabled)
223 def vnfId = execution.getVariable('vnfId')
225 AaiUtil aaiUriUtil = new AaiUtil(this)
226 String aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
227 logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)
229 String endPoint = execution.getVariable("URN_aai_endpoint") + "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"
230 utils.logAudit("AAI endPoint: " + endPoint)
233 RESTConfig config = new RESTConfig(endPoint);
234 def responseData = ''
235 def aaiRequestId = UUID.randomUUID().toString()
236 RESTClient client = new RESTClient(config).
237 addHeader('X-TransactionId', aaiRequestId).
238 addHeader('X-FromAppId', 'MSO').
239 addHeader('Content-Type', 'application/xml').
240 addHeader('Accept','application/xml');
241 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)
242 APIResponse response = client.httpGet()
243 utils.logAudit("createVfModule - invoking httpGet() to AAI")
245 responseData = response.getResponseBodyAsString()
246 if (responseData != null) {
247 logDebug("Received generic VNF data: " + responseData, isDebugLogEnabled)
251 utils.logAudit("createVfModule - queryAAIVfModule Response: " + responseData)
252 utils.logAudit("createVfModule - queryAAIVfModule ResponseCode: " + response.getStatusCode())
254 execution.setVariable('DCVFM_queryAAIVfModuleResponseCode', response.getStatusCode())
255 execution.setVariable('DCVFM_queryAAIVfModuleResponse', responseData)
256 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
257 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
258 //Map<String, String>[] vfModules = new HashMap<String,String>[]
259 def vfModulesList = new ArrayList<Map<String,String>>()
261 def vfModuleBaseEntry = null
262 if (response.getStatusCode() == 200) {
263 // Parse the VNF record from A&AI to find base module info
264 logDebug('Parsing the VNF data to find base module info', isDebugLogEnabled)
265 if (responseData != null) {
266 def vfModulesText = utils.getNodeXml(responseData, "vf-modules")
267 logDebug("vModulesText: " + vfModulesText, isDebugLogEnabled)
268 if (vfModulesText != null && !vfModulesText.trim().isEmpty()) {
269 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)
270 vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}
271 execution.setVariable("DDVAM_moduleCount", vfModules.size())
272 int vfModulesSize = 0
273 for (i in 0..vfModules.size()-1) {
274 def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])
276 Map<String, String> vfModuleEntry = new HashMap<String, String>()
277 def vfModuleId = utils.getNodeText1(vfModuleXml, "vf-module-id")
278 vfModuleEntry.put("vfModuleId", vfModuleId)
279 def vfModuleName = utils.getNodeText1(vfModuleXml, "vf-module-name")
280 vfModuleEntry.put("vfModuleName", vfModuleName)
282 def isBaseVfModule = utils.getNodeText(vfModuleXml, "is-base-vf-module")
283 // Save base vf module for last
284 if (isBaseVfModule == "true") {
285 vfModuleBaseEntry = vfModuleEntry
288 vfModulesList.add(vfModuleEntry)
291 if (vfModuleBaseEntry != null) {
292 vfModulesList.add(vfModuleBaseEntry)
298 execution.setVariable("DDVAM_vfModules", vfModulesList)
299 } catch (Exception ex) {
301 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)
302 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
304 logDebug('Exited ' + method, isDebugLogEnabled)
305 } catch (BpmnError e) {
307 } catch (Exception e) {
308 logError('Caught exception in ' + method, e)
309 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAIVfModule(): ' + e.getMessage())
313 public void prepareNextModuleToDelete(Execution execution){
314 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
315 execution.setVariable("prefix", Prefix)
316 logDebug(" ======== STARTED prepareNextModuleToDelete ======== ", isDebugLogEnabled)
319 int i = execution.getVariable("DDVAM_nextModule")
320 def vfModules = execution.getVariable("DDVAM_vfModules")
321 def vfModule = vfModules[i]
323 def vfModuleId = vfModule.get("vfModuleId")
324 execution.setVariable("DDVAM_vfModuleId", vfModuleId)
326 def vfModuleName = vfModule.get("vfModuleName")
327 execution.setVariable("DDVAM_vfModuleName", vfModuleName)
331 def vfModuleModelInfo = ""
332 execution.setVariable("DDVAM_vfModuleModelInfo", vfModuleModelInfo)
335 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
336 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during prepareNextModuleToDelete Method:\n" + e.getMessage())
338 logDebug("======== COMPLETED prepareNextModuleToDelete ======== ", isDebugLogEnabled)
341 public void preProcessSDNCDeactivateRequest(Execution execution){
342 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
343 execution.setVariable("prefix", Prefix)
344 logDebug(" ======== STARTED preProcessSDNCDeactivateRequest ======== ", isDebugLogEnabled)
345 def vnfId = execution.getVariable("vnfId")
346 def serviceInstanceId = execution.getVariable("serviceInstanceId")
351 String deactivateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "deactivate")
353 deactivateSDNCRequest = utils.formatXml(deactivateSDNCRequest)
354 execution.setVariable("DDVAM_deactivateSDNCRequest", deactivateSDNCRequest)
355 logDebug("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest, isDebugLogEnabled)
356 utils.logAudit("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest)
359 utils.log("ERROR", "Exception Occured Processing preProcessSDNCDeactivateRequest. Exception is:\n" + e, isDebugLogEnabled)
360 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessSDNCDeactivateRequest Method:\n" + e.getMessage())
362 logDebug("======== COMPLETED preProcessSDNCDeactivateRequest ======== ", isDebugLogEnabled)
365 public void preProcessSDNCUnassignRequest(Execution execution) {
366 def method = getClass().getSimpleName() + '.preProcessSDNCUnassignRequest(' +
367 'execution=' + execution.getId() +
369 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
370 logDebug('Entered ' + method, isDebugLogEnabled)
371 execution.setVariable("prefix", Prefix)
372 logDebug(" ======== STARTED preProcessSDNCUnassignRequest Process ======== ", isDebugLogEnabled)
374 String vnfId = execution.getVariable("vnfId")
375 String serviceInstanceId = execution.getVariable("serviceInstanceId")
377 String unassignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "unassign")
379 execution.setVariable("DDVAM_unassignSDNCRequest", unassignSDNCRequest)
380 logDebug("Outgoing UnassignSDNCRequest is: \n" + unassignSDNCRequest, isDebugLogEnabled)
381 utils.logAudit("Outgoing UnassignSDNCRequest is: \n" + unassignSDNCRequest)
384 log.debug("Exception Occured Processing preProcessSDNCUnassignRequest. Exception is:\n" + e, isDebugLogEnabled)
385 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during preProcessSDNCUnassignRequest Method:\n" + e.getMessage())
387 logDebug("======== COMPLETED preProcessSDNCUnassignRequest Process ======== ", isDebugLogEnabled)
390 public String buildSDNCRequest(Execution execution, String svcInstId, String action){
392 String uuid = execution.getVariable('testReqId') // for junits
394 uuid = execution.getVariable("msoRequestId") + "-" + System.currentTimeMillis()
396 def callbackURL = execution.getVariable("sdncCallbackUrl")
397 def requestId = execution.getVariable("msoRequestId")
398 def serviceId = execution.getVariable("DDVAM_serviceId")
399 def tenantId = execution.getVariable("DDVAM_tenantId")
400 def source = execution.getVariable("DDVAM_source")
401 def vnfId = execution.getVariable("vnfId")
402 def serviceInstanceId = execution.getVariable("serviceInstanceId")
403 def cloudSiteId = execution.getVariable("DDVAM_cloudSiteId")
404 def modelCustomizationId = execution.getVariable("DDVAM_modelCustomizationId")
405 //def serviceModelInfo = execution.getVariable("serviceModelInfo")
406 //def vnfModelInfo = execution.getVariable("vnfModelInfo")
407 //String serviceEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(serviceModelInfo)
408 //String vnfEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(vnfModelInfo)
409 def globalSubscriberId = execution.getVariable("DDVAM_globalSubscriberId")
410 def sdncVersion = execution.getVariable("DDVAM_sdncVersion")
413 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
414 xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
415 xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
416 <sdncadapter:RequestHeader>
417 <sdncadapter:RequestId>${uuid}</sdncadapter:RequestId>
418 <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>
419 <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
420 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
421 <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>
422 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
423 </sdncadapter:RequestHeader>
424 <sdncadapterworkflow:SDNCRequestData>
425 <request-information>
426 <request-id>${requestId}</request-id>
427 <request-action>DeleteVnfInstance</request-action>
428 <source>${source}</source>
432 </request-information>
433 <service-information>
435 <subscription-service-type/>
436 <service-instance-id>${serviceInstanceId}</service-instance-id>
437 <global-customer-id/>
438 </service-information>
440 <vnf-id>${vnfId}</vnf-id>
445 <tenant>${tenantId}</tenant>
446 <aic-cloud-region>${cloudSiteId}</aic-cloud-region>
448 </sdncadapterworkflow:SDNCRequestData>
449 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
451 utils.logAudit("sdncRequest: " + sdncRequest)
455 public void validateSDNCResponse(Execution execution, String response, String method){
456 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
457 execution.setVariable("prefix",Prefix)
458 logDebug(" *** STARTED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
460 WorkflowException workflowException = execution.getVariable("WorkflowException")
461 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
463 utils.logAudit("workflowException: " + workflowException)
465 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
466 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
468 utils.logAudit("SDNCResponse: " + response)
470 String sdncResponse = response
471 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
472 logDebug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call. Response is: \n" + sdncResponse, isDebugLogEnabled)
474 logDebug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.", isDebugLogEnabled)
475 throw new BpmnError("MSOWorkflowException")
477 logDebug(" *** COMPLETED ValidateSDNCResponse Process*** ", isDebugLogEnabled)