2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.infrastructure.scripts
25 import static org.apache.commons.lang3.StringUtils.*;
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil
31 import org.onap.so.bpmn.common.scripts.MsoUtils
32 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
33 import org.onap.so.bpmn.core.RollbackData
34 import org.onap.so.bpmn.core.WorkflowException
35 import org.onap.so.bpmn.core.json.JsonUtils
36 import org.onap.so.logger.MessageEnum
37 import org.onap.so.logger.MsoLogger
38 import org.slf4j.Logger
39 import org.slf4j.LoggerFactory
42 * This class supports the macro VID Flow
43 * with the rollback of a creation of a generic vnf and related VF modules.
45 class DoCreateVnfAndModulesRollback extends AbstractServiceTaskProcessor {
47 private static final Logger logger = LoggerFactory.getLogger( DoCreateVnfAndModulesRollback.class);
48 String Prefix="DCVAMR_"
49 ExceptionUtil exceptionUtil = new ExceptionUtil()
50 JsonUtils jsonUtil = new JsonUtils()
54 * This method gets and validates the incoming
60 public void preProcessRequest(DelegateExecution execution) {
62 execution.setVariable("prefix",Prefix)
63 logger.debug("STARTED DoCreateVnfAndModulesRollback PreProcessRequest Process")
66 // Get Rollback Variables
68 def rollbackData = execution.getVariable("rollbackData")
69 logger.debug("Incoming RollbackData is: " + rollbackData.toString())
70 execution.setVariable("rolledBack", null)
71 execution.setVariable("rollbackError", null)
73 if (execution.getVariable("disableRollback").equals("true" ))
75 execution.setVariable("skipRollback", true)
78 String vnfId = rollbackData.get("VNF", "vnfId")
79 logger.debug("Rollback vnfId is: " + vnfId)
80 execution.setVariable("DCVAMR_vnfId", vnfId)
82 execution.setVariable("mso-request-id", execution.getVariable("msoRequestId"))
84 execution.setVariable("DCVAMR_rollbackSDNCAssign", rollbackData.get("VNF", "rollbackSDNCAssign"))
85 execution.setVariable("DCVAMR_rollbackSDNCActivate", rollbackData.get("VNF", "rollbackSDNCActivate"))
86 execution.setVariable("DCVAMR_rollbackVnfCreate", rollbackData.get("VNF", "rollbackVnfCreate"))
88 String sdncCallbackUrl = rollbackData.get("VNF", "sdncCallbackUrl")
89 logger.debug("Rollback sdncCallbackUrl is: " + sdncCallbackUrl)
90 execution.setVariable("DCVAMR_sdncCallbackUrl", sdncCallbackUrl)
92 String tenantId= rollbackData.get("VNF", "tenantId")
93 logger.debug("Rollback tenantId is: " + tenantId)
94 execution.setVariable("DCVAMR_tenantId", tenantId)
96 String source= rollbackData.get("VNF", "source")
97 logger.debug("Rollback source is: " + source)
98 execution.setVariable("DCVAMR_source", source)
100 String serviceInstanceId = rollbackData.get("VNF", "serviceInstanceId")
101 logger.debug("Rollback serviceInstanceId is: " + serviceInstanceId)
102 execution.setVariable("DCVAMR_serviceInstanceId", serviceInstanceId)
104 String cloudSiteId = rollbackData.get("VNF", "cloudSiteId")
105 logger.debug("Rollback cloudSiteId is: " + cloudSiteId)
106 execution.setVariable("DCVAMR_cloudSiteId", cloudSiteId)
108 def numOfAddOnModulesString = rollbackData.get("VNFANDMODULES", "numOfCreatedAddOnModules")
109 int numOfAddOnModules = 0
110 if (numOfAddOnModulesString != null) {
111 numOfAddOnModules = Integer.parseInt(numOfAddOnModulesString)
113 execution.setVariable("DCVAMR_numOfAddOnModules", numOfAddOnModules)
115 def baseVfModuleRollbackMap = rollbackData.get("VFMODULE_BASE")
116 if (baseVfModuleRollbackMap == null) {
117 // there are no VF Modules to delete
118 execution.setVariable("DCVAMR_numOfModulesToDelete", 0)
121 execution.setVariable("DCVAMR_numOfModulesToDelete", numOfAddOnModules + 1)
124 // Set aLaCarte to false
125 execution.setVariable("DCVAMR_aLaCarte", false)
128 logger.debug("Rethrowing MSOWorkflowException")
131 logger.debug(" Error Occured in DoCreateVnfAndModulesRollback PreProcessRequest method!" + e.getMessage())
132 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModulesRollback PreProcessRequest")
135 logger.trace("COMPLETED DoCreateVnfAndModulesRollback PreProcessRequest Process")
140 public void preProcessCreateVfModuleRollback(DelegateExecution execution){
142 execution.setVariable("prefix", Prefix)
143 logger.trace("STARTED preProcessCreateVfModuleRollback")
147 def rollbackData = execution.getVariable("rollbackData")
149 def vfModuleRollbackData = new RollbackData()
151 def numOfModulesToDelete = execution.getVariable("DCVAMR_numOfModulesToDelete")
152 logger.debug("numOfModulesToDelete: " + numOfModulesToDelete)
155 if (numOfModulesToDelete > 1) {
156 int addOnModuleIndex = numOfModulesToDelete - 1
157 moduleMap = rollbackData.get("VFMODULE_ADDON_" + addOnModuleIndex)
158 logger.debug("Removing ADDON VF module # " + addOnModuleIndex)
161 moduleMap = rollbackData.get("VFMODULE_BASE")
162 logger.debug("Removing BASE VF module")
164 moduleMap.each{ k, v -> vfModuleRollbackData.put("VFMODULE", "${k}","${v}") }
165 execution.setVariable("DCVAMR_RollbackData", vfModuleRollbackData)
168 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
169 "Exception Occured Processing preProcessCreateVfModuleRollback ", "BPMN",
170 MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
171 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessCreateVfModuleRollback Method:\n" + e.getMessage())
173 logger.trace("COMPLETED preProcessCreateVfModuleRollback")
177 public void postProcessCreateVfModuleRollback(DelegateExecution execution){
179 execution.setVariable("prefix", Prefix)
180 logger.trace("STARTED postProcessCreateVfModuleRollback")
181 def rolledBack = false
184 rolledBack = execution.getVariable("DCVM_rolledBack")
185 def numOfModulesToDelete = execution.getVariable("DCVAMR_numOfModulesToDelete")
186 execution.setVariable("DCVAMR_numOfModulesToDelete", numOfModulesToDelete - 1)
188 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
189 "Exception Occured Processing postProcessCreateVfModuleRollback ", "BPMN",
190 MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
191 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during postProcessCreateVfModuleRollback Method:\n" + e.getMessage())
193 if (rolledBack == false) {
194 logger.debug("Failure on DoCreateVfModuleRollback")
195 logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
196 "Unsuccessful rollback of DoCreateVfModule ", "BPMN",
197 MsoLogger.ErrorCode.UnknownError.getValue());
198 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during rollback of DoCreateVfModule")
200 logger.trace("COMPLETED postProcessCreateVfModuleRollback")
204 public void preProcessSDNCDeactivateRequest(DelegateExecution execution){
206 execution.setVariable("prefix", Prefix)
207 logger.trace("STARTED preProcessSDNCDeactivateRequest")
208 def vnfId = execution.getVariable("vnfId")
209 def serviceInstanceId = execution.getVariable("serviceInstanceId")
214 String deactivateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "deactivate")
216 deactivateSDNCRequest = utils.formatXml(deactivateSDNCRequest)
217 execution.setVariable(Prefix + "deactivateSDNCRequest", deactivateSDNCRequest)
218 logger.debug("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest)
221 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
222 "Exception Occured Processing preProcessSDNCDeactivateRequest ", "BPMN",
223 MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
224 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessSDNCDeactivateRequest Method:\n" + e.getMessage())
226 logger.trace("COMPLETED preProcessSDNCDeactivateRequest")
229 public void preProcessSDNCUnassignRequest(DelegateExecution execution) {
230 def method = getClass().getSimpleName() + '.preProcessSDNCUnassignRequest(' +
231 'execution=' + execution.getId() +
233 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
234 logger.trace('Entered ' + method)
235 execution.setVariable("prefix", Prefix)
236 logger.trace("STARTED preProcessSDNCUnassignRequest Process")
238 String vnfId = execution.getVariable("vnfId")
239 String serviceInstanceId = execution.getVariable("serviceInstanceId")
241 String unassignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "unassign")
243 execution.setVariable(Prefix + "unassignSDNCRequest", unassignSDNCRequest)
244 logger.debug("Outgoing UnassignSDNCRequest is: \n" + unassignSDNCRequest)
247 logger.debug("Exception Occured Processing preProcessSDNCUnassignRequest. Exception is:\n" + e)
248 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during preProcessSDNCUnassignRequest Method:\n" + e.getMessage())
250 logger.trace("COMPLETED preProcessSDNCUnassignRequest Process")
253 public String buildSDNCRequest(DelegateExecution execution, String svcInstId, String action){
255 String uuid = execution.getVariable('testReqId') // for junits
257 uuid = execution.getVariable("msoRequestId") + "-" + System.currentTimeMillis()
259 def callbackURL = execution.getVariable(Prefix + "sdncCallbackUrl")
260 def requestId = execution.getVariable("msoRequestId")
261 def tenantId = execution.getVariable(Prefix + "tenantId")
262 def source = execution.getVariable(Prefix + "source")
263 def vnfId = execution.getVariable(Prefix + "vnfId")
264 def serviceInstanceId = execution.getVariable(Prefix + "serviceInstanceId")
265 def cloudSiteId = execution.getVariable(Prefix + "cloudSiteId")
268 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
269 xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
270 xmlns:sdncadapter="http://org.onap.so/workflow/sdnc/adapter/schema/v1">
271 <sdncadapter:RequestHeader>
272 <sdncadapter:RequestId>${MsoUtils.xmlEscape(uuid)}</sdncadapter:RequestId>
273 <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(svcInstId)}</sdncadapter:SvcInstanceId>
274 <sdncadapter:SvcAction>${MsoUtils.xmlEscape(action)}</sdncadapter:SvcAction>
275 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
276 <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackURL)}</sdncadapter:CallbackUrl>
277 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
278 </sdncadapter:RequestHeader>
279 <sdncadapterworkflow:SDNCRequestData>
280 <request-information>
281 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
282 <request-action>DeleteVnfInstance</request-action>
283 <source>${MsoUtils.xmlEscape(source)}</source>
287 </request-information>
288 <service-information>
290 <subscription-service-type/>
291 <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
292 <global-customer-id/>
293 </service-information>
295 <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
300 <tenant>${MsoUtils.xmlEscape(tenantId)}</tenant>
301 <aic-cloud-region>${MsoUtils.xmlEscape(cloudSiteId)}</aic-cloud-region>
303 </sdncadapterworkflow:SDNCRequestData>
304 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
306 logger.debug("sdncRequest: " + sdncRequest)
310 public void validateSDNCResponse(DelegateExecution execution, String response, String method){
312 execution.setVariable("prefix",Prefix)
313 logger.debug("STARTED ValidateSDNCResponse Process")
315 WorkflowException workflowException = execution.getVariable("WorkflowException")
316 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
318 logger.debug("workflowException: " + workflowException)
320 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
321 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
323 String sdncResponse = response
324 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
325 logger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call. Response is: \n" + sdncResponse)
327 logger.debug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.")
328 throw new BpmnError("MSOWorkflowException")
330 logger.debug("COMPLETED ValidateSDNCResponse Process")
333 public void setSuccessfulRollbackStatus (DelegateExecution execution){
335 execution.setVariable("prefix", Prefix)
336 logger.trace("STARTED setSuccessfulRollbackStatus")
339 // Set rolledBack to true, rollbackError to null
340 execution.setVariable("rolledBack", true)
341 execution.setVariable("rollbackError", null)
344 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), "Exception Occured " +
345 "Processing setSuccessfulRollbackStatus ", "BPMN", MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
346 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during setSuccessfulRollbackStatus Method:\n" + e.getMessage())
348 logger.trace("COMPLETED setSuccessfulRollbackStatus")
351 public void setFailedRollbackStatus (DelegateExecution execution){
353 execution.setVariable("prefix", Prefix)
354 logger.trace("STARTED setFailedRollbackStatus")
357 // Set rolledBack to false, rollbackError to actual value, rollbackData to null
358 execution.setVariable("rolledBack", false)
359 def rollbackError = execution.getVariable("rollbackError")
360 if (rollbackError == null) {
361 execution.setVariable("rollbackError", 'Caught exception in DoCreateVnfAndModulesRollback')
363 execution.setVariable("rollbackData", null)
366 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), "Exception Occured " +
367 "Processing setFailedRollbackStatus. ", "BPMN",MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
368 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during setFailedRollbackStatus Method:\n" + e.getMessage())
370 logger.trace("COMPLETED setFailedRollbackStatus")