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 utils.logAudit("createVfModule - invoking httpGet() to AAI")
234 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, endPoint)
236 def responseData = response.getResponseBodyAsString()
237 if (responseData != null) {
238 logDebug("Received generic VNF data: " + responseData, isDebugLogEnabled)
242 utils.logAudit("createVfModule - queryAAIVfModule Response: " + responseData)
243 utils.logAudit("createVfModule - queryAAIVfModule ResponseCode: " + response.getStatusCode())
245 execution.setVariable('DCVFM_queryAAIVfModuleResponseCode', response.getStatusCode())
246 execution.setVariable('DCVFM_queryAAIVfModuleResponse', responseData)
247 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)
248 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)
249 //Map<String, String>[] vfModules = new HashMap<String,String>[]
250 def vfModulesList = new ArrayList<Map<String,String>>()
252 def vfModuleBaseEntry = null
253 if (response.getStatusCode() == 200) {
254 // Parse the VNF record from A&AI to find base module info
255 logDebug('Parsing the VNF data to find base module info', isDebugLogEnabled)
256 if (responseData != null) {
257 def vfModulesText = utils.getNodeXml(responseData, "vf-modules")
258 logDebug("vModulesText: " + vfModulesText, isDebugLogEnabled)
259 if (vfModulesText != null && !vfModulesText.trim().isEmpty()) {
260 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)
261 vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}
262 execution.setVariable("DDVAM_moduleCount", vfModules.size())
263 int vfModulesSize = 0
264 for (i in 0..vfModules.size()-1) {
265 def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])
267 Map<String, String> vfModuleEntry = new HashMap<String, String>()
268 def vfModuleId = utils.getNodeText1(vfModuleXml, "vf-module-id")
269 vfModuleEntry.put("vfModuleId", vfModuleId)
270 def vfModuleName = utils.getNodeText1(vfModuleXml, "vf-module-name")
271 vfModuleEntry.put("vfModuleName", vfModuleName)
273 def isBaseVfModule = utils.getNodeText(vfModuleXml, "is-base-vf-module")
274 // Save base vf module for last
275 if (isBaseVfModule == "true") {
276 vfModuleBaseEntry = vfModuleEntry
279 vfModulesList.add(vfModuleEntry)
282 if (vfModuleBaseEntry != null) {
283 vfModulesList.add(vfModuleBaseEntry)
289 execution.setVariable("DDVAM_vfModules", vfModulesList)
290 } catch (Exception ex) {
292 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)
293 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())
295 logDebug('Exited ' + method, isDebugLogEnabled)
296 } catch (BpmnError e) {
298 } catch (Exception e) {
299 logError('Caught exception in ' + method, e)
300 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAIVfModule(): ' + e.getMessage())
304 public void prepareNextModuleToDelete(Execution execution){
305 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
306 execution.setVariable("prefix", Prefix)
307 logDebug(" ======== STARTED prepareNextModuleToDelete ======== ", isDebugLogEnabled)
310 int i = execution.getVariable("DDVAM_nextModule")
311 def vfModules = execution.getVariable("DDVAM_vfModules")
312 def vfModule = vfModules[i]
314 def vfModuleId = vfModule.get("vfModuleId")
315 execution.setVariable("DDVAM_vfModuleId", vfModuleId)
317 def vfModuleName = vfModule.get("vfModuleName")
318 execution.setVariable("DDVAM_vfModuleName", vfModuleName)
322 def vfModuleModelInfo = ""
323 execution.setVariable("DDVAM_vfModuleModelInfo", vfModuleModelInfo)
326 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
327 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during prepareNextModuleToDelete Method:\n" + e.getMessage())
329 logDebug("======== COMPLETED prepareNextModuleToDelete ======== ", isDebugLogEnabled)
332 public void preProcessSDNCDeactivateRequest(Execution execution){
333 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
334 execution.setVariable("prefix", Prefix)
335 logDebug(" ======== STARTED preProcessSDNCDeactivateRequest ======== ", isDebugLogEnabled)
336 def vnfId = execution.getVariable("vnfId")
337 def serviceInstanceId = execution.getVariable("serviceInstanceId")
342 String deactivateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "deactivate")
344 deactivateSDNCRequest = utils.formatXml(deactivateSDNCRequest)
345 execution.setVariable("DDVAM_deactivateSDNCRequest", deactivateSDNCRequest)
346 logDebug("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest, isDebugLogEnabled)
347 utils.logAudit("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest)
350 utils.log("ERROR", "Exception Occured Processing preProcessSDNCDeactivateRequest. Exception is:\n" + e, isDebugLogEnabled)
351 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessSDNCDeactivateRequest Method:\n" + e.getMessage())
353 logDebug("======== COMPLETED preProcessSDNCDeactivateRequest ======== ", isDebugLogEnabled)
356 public void preProcessSDNCUnassignRequest(Execution execution) {
357 def method = getClass().getSimpleName() + '.preProcessSDNCUnassignRequest(' +
358 'execution=' + execution.getId() +
360 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
361 logDebug('Entered ' + method, isDebugLogEnabled)
362 execution.setVariable("prefix", Prefix)
363 logDebug(" ======== STARTED preProcessSDNCUnassignRequest Process ======== ", isDebugLogEnabled)
365 String vnfId = execution.getVariable("vnfId")
366 String serviceInstanceId = execution.getVariable("serviceInstanceId")
368 String unassignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "unassign")
370 execution.setVariable("DDVAM_unassignSDNCRequest", unassignSDNCRequest)
371 logDebug("Outgoing UnassignSDNCRequest is: \n" + unassignSDNCRequest, isDebugLogEnabled)
372 utils.logAudit("Outgoing UnassignSDNCRequest is: \n" + unassignSDNCRequest)
375 log.debug("Exception Occured Processing preProcessSDNCUnassignRequest. Exception is:\n" + e, isDebugLogEnabled)
376 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during preProcessSDNCUnassignRequest Method:\n" + e.getMessage())
378 logDebug("======== COMPLETED preProcessSDNCUnassignRequest Process ======== ", isDebugLogEnabled)
381 public String buildSDNCRequest(Execution execution, String svcInstId, String action){
383 String uuid = execution.getVariable('testReqId') // for junits
385 uuid = execution.getVariable("msoRequestId") + "-" + System.currentTimeMillis()
387 def callbackURL = execution.getVariable("sdncCallbackUrl")
388 def requestId = execution.getVariable("msoRequestId")
389 def serviceId = execution.getVariable("DDVAM_serviceId")
390 def tenantId = execution.getVariable("DDVAM_tenantId")
391 def source = execution.getVariable("DDVAM_source")
392 def vnfId = execution.getVariable("vnfId")
393 def serviceInstanceId = execution.getVariable("serviceInstanceId")
394 def cloudSiteId = execution.getVariable("DDVAM_cloudSiteId")
395 def modelCustomizationId = execution.getVariable("DDVAM_modelCustomizationId")
396 //def serviceModelInfo = execution.getVariable("serviceModelInfo")
397 //def vnfModelInfo = execution.getVariable("vnfModelInfo")
398 //String serviceEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(serviceModelInfo)
399 //String vnfEcompModelInformation = sdncAdapterUtils.modelInfoToEcompModelInformation(vnfModelInfo)
400 def globalSubscriberId = execution.getVariable("DDVAM_globalSubscriberId")
401 def sdncVersion = execution.getVariable("DDVAM_sdncVersion")
404 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
405 xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
406 xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
407 <sdncadapter:RequestHeader>
408 <sdncadapter:RequestId>${uuid}</sdncadapter:RequestId>
409 <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>
410 <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>
411 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
412 <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>
413 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
414 </sdncadapter:RequestHeader>
415 <sdncadapterworkflow:SDNCRequestData>
416 <request-information>
417 <request-id>${requestId}</request-id>
418 <request-action>DeleteVnfInstance</request-action>
419 <source>${source}</source>
423 </request-information>
424 <service-information>
426 <subscription-service-type/>
427 <service-instance-id>${serviceInstanceId}</service-instance-id>
428 <global-customer-id/>
429 </service-information>
431 <vnf-id>${vnfId}</vnf-id>
436 <tenant>${tenantId}</tenant>
437 <aic-cloud-region>${cloudSiteId}</aic-cloud-region>
439 </sdncadapterworkflow:SDNCRequestData>
440 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
442 utils.logAudit("sdncRequest: " + sdncRequest)
446 public void validateSDNCResponse(Execution execution, String response, String method){
447 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
448 execution.setVariable("prefix",Prefix)
449 logDebug(" *** STARTED ValidateSDNCResponse Process*** ", isDebugLogEnabled)
451 WorkflowException workflowException = execution.getVariable("WorkflowException")
452 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
454 utils.logAudit("workflowException: " + workflowException)
456 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
457 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
459 utils.logAudit("SDNCResponse: " + response)
461 String sdncResponse = response
462 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
463 logDebug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call. Response is: \n" + sdncResponse, isDebugLogEnabled)
465 logDebug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.", isDebugLogEnabled)
466 throw new BpmnError("MSOWorkflowException")
468 logDebug(" *** COMPLETED ValidateSDNCResponse Process*** ", isDebugLogEnabled)