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;
25 import org.json.JSONObject;
26 import org.json.JSONArray;
27 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
28 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils
29 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
30 import org.openecomp.mso.bpmn.common.scripts.VidUtils
31 import org.openecomp.mso.bpmn.core.RollbackData
32 import org.openecomp.mso.bpmn.core.WorkflowException
33 import org.openecomp.mso.bpmn.core.domain.ModelInfo
34 import org.openecomp.mso.bpmn.core.domain.ModuleResource
35 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
36 import org.openecomp.mso.bpmn.core.domain.VnfResource
37 import org.openecomp.mso.bpmn.core.json.DecomposeJsonUtil
38 import org.openecomp.mso.bpmn.core.json.JsonUtils
39 import org.camunda.bpm.engine.delegate.BpmnError
40 import org.camunda.bpm.engine.delegate.DelegateExecution;
42 import static org.apache.commons.lang3.StringUtils.*;
47 * This class supports the macro VID Flow
48 * with the creation of a generic vnf and related VF modules.
50 class DoCreateVnfAndModules extends AbstractServiceTaskProcessor {
52 String Prefix="DCVAM_"
53 ExceptionUtil exceptionUtil = new ExceptionUtil()
54 JsonUtils jsonUtil = new JsonUtils()
55 VidUtils vidUtils = new VidUtils(this)
56 CatalogDbUtils cutils = new CatalogDbUtils()
59 * This method gets and validates the incoming
64 public void preProcessRequest(DelegateExecution execution) {
65 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
66 execution.setVariable("prefix",Prefix)
67 utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules PreProcessRequest Process*** ", isDebugLogEnabled)
69 setBasicDBAuthHeader(execution, isDebugLogEnabled)
73 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
75 String vnfModelInfo = execution.getVariable("vnfModelInfo")
77 String requestId = execution.getVariable("msoRequestId")
78 execution.setVariable("requestId", requestId)
79 execution.setVariable("mso-request-id", requestId)
80 utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugLogEnabled)
82 String serviceInstanceId = execution.getVariable("serviceInstanceId")
83 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugLogEnabled)
85 String vnfName = execution.getVariable("vnfName")
86 execution.setVariable("CREVI_vnfName", vnfName)
87 utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugLogEnabled)
89 String productFamilyId = execution.getVariable("productFamilyId")
90 utils.log("DEBUG", "Incoming Product Family Id is: " + productFamilyId, isDebugLogEnabled)
93 execution.setVariable("source", source)
94 utils.log("DEBUG", "Incoming Source is: " + source, isDebugLogEnabled)
96 String lcpCloudRegionId = execution.getVariable("lcpCloudRegionId")
97 utils.log("DEBUG", "Incoming LCP Cloud Region Id is: " + lcpCloudRegionId)
99 String tenantId = execution.getVariable("tenantId")
100 utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId)
102 String disableRollback = execution.getVariable("disableRollback")
103 utils.log("DEBUG", "Incoming Disable Rollback is: " + disableRollback, isDebugLogEnabled)
105 String asdcServiceModelVersion = execution.getVariable("asdcServiceModelVersion")
106 utils.log("DEBUG", "Incoming asdcServiceModelVersion: " + asdcServiceModelVersion, isDebugLogEnabled)
108 String vnfId = execution.getVariable("testVnfId") // for junits
110 vnfId = execution.getVariable("vnfId")
111 if (isBlank(vnfId)) {
112 vnfId = UUID.randomUUID().toString()
113 utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugLogEnabled)
116 execution.setVariable("vnfId", vnfId)
118 // Set aLaCarte to false
119 execution.setVariable("aLaCarte", false)
121 def rollbackData = execution.getVariable("rollbackData")
122 if (rollbackData == null) {
123 rollbackData = new RollbackData()
126 def isTest = execution.getVariable("isTest")
128 if (isTest == null || isTest == false) {
129 execution.setVariable("isBaseVfModule", "true")
131 execution.setVariable("numOfCreatedAddOnModules", 0)
133 rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "0")
134 execution.setVariable("rollbackData", rollbackData)
140 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugLogEnabled)
143 utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugLogEnabled)
144 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
147 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules PreProcessRequest Process ***", isDebugLogEnabled)
151 public void queryCatalogDB (DelegateExecution execution) {
152 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
153 execution.setVariable("prefix",Prefix)
155 utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules QueryCatalogDB Process *** ", isDebugLogEnabled)
157 VnfResource vnf = null
158 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
159 // if serviceDecomposition is specified, get info from serviceDecomposition
160 if (serviceDecomposition != null) {
161 utils.log("DEBUG", "Getting Catalog DB data from ServiceDecomposition object: " + serviceDecomposition.toJsonString(), isDebugLogEnabled)
162 List<VnfResource> vnfs = serviceDecomposition.getServiceVnfs()
163 utils.log("DEBUG", "Read vnfs", isDebugLogEnabled)
165 utils.log("DEBUG", "Error - vnfs are empty in serviceDecomposition object", isDebugLogEnabled)
166 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnfs are empty")
169 String serviceModelName = serviceDecomposition.getModelInfo().getModelName()
170 vnf.constructVnfType(serviceModelName)
171 String vnfType = vnf.getVnfType()
172 utils.log("DEBUG", "Incoming Vnf Type is: " + vnfType, isDebugLogEnabled)
173 execution.setVariable("vnfType", vnfType)
177 String vnfModelInfo = execution.getVariable("vnfModelInfo")
178 utils.log("DEBUG", "vnfModelInfo: " + vnfModelInfo, isDebugLogEnabled)
179 String vnfModelCustomizationUuid = jsonUtil.getJsonValueForKey(vnfModelInfo, "modelCustomizationUuid")
180 if (vnfModelCustomizationUuid == null) {
181 vnfModelCustomizationUuid = ""
183 utils.log("DEBUG", "querying Catalog DB by vnfModelCustomizationUuid: " + vnfModelCustomizationUuid, isDebugLogEnabled)
185 JSONArray vnfs = cutils.getAllVnfsByVnfModelCustomizationUuid(execution,
186 vnfModelCustomizationUuid)
187 utils.log("DEBUG", "obtained VNF list")
188 // Only one match here
189 JSONObject vnfObject = vnfs[0]
190 vnf = DecomposeJsonUtil.jsonToVnfResource(vnfObject.toString())
192 utils.log("DEBUG", "Read vnfResource", isDebugLogEnabled)
194 utils.log("DEBUG", "Error - vnf is empty in serviceDecomposition object", isDebugLogEnabled)
195 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnf is null")
197 execution.setVariable("vnfResourceDecomposition", vnf)
199 List<ModuleResource> vfModules = vnf.getAllVfModuleObjects()
200 utils.log("DEBUG", "Read vfModules", isDebugLogEnabled)
201 if (vfModules == null) {
202 utils.log("DEBUG", "Error - vfModules are empty in serviceDecomposition object", isDebugLogEnabled)
203 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vf modules are empty")
206 ModuleResource baseVfModule = null
208 for (int i = 0; i < vfModules.size; i++) {
209 utils.log("DEBUG", "handling VF Module ", isDebugLogEnabled)
210 ModuleResource vfModule = vfModules[i]
211 boolean isBase = vfModule.getIsBase()
213 ModelInfo baseVfModuleModelInfoObject = vfModule.getModelInfo()
214 String baseVfModuleModelInfoWithRoot = baseVfModuleModelInfoObject.toString()
215 String baseVfModuleModelInfo = jsonUtil.getJsonValue(baseVfModuleModelInfoWithRoot, "modelInfo")
216 execution.setVariable("baseVfModuleModelInfo", baseVfModuleModelInfo)
217 String baseVfModuleLabel = vfModule.getVfModuleLabel()
218 execution.setVariable("baseVfModuleLabel", baseVfModuleLabel)
219 String basePersonaModelId = baseVfModuleModelInfoObject.getModelInvariantUuid()
220 execution.setVariable("basePersonaModelId", basePersonaModelId)
221 baseVfModule = vfModule
227 List<ModuleResource>addOnModules = vfModules - baseVfModule
229 int addOnModulesToDeploy = 0
230 if (addOnModules != null) {
231 addOnModulesToDeploy = addOnModules.size
234 utils.log("DEBUG", "AddOnModulesToDeploy: " + addOnModulesToDeploy)
236 execution.setVariable("addOnModules", addOnModules)
237 execution.setVariable("addOnModulesToDeploy", addOnModulesToDeploy)
238 execution.setVariable("addOnModulesDeployed", 0)
240 }catch(Exception ex) {
241 utils.log("DEBUG", "Error Occured in DoCreateVnfAndModules QueryCatalogDB Process " + ex.getMessage(), isDebugLogEnabled)
242 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModules QueryCatalogDB Process")
245 // Generate vfModuleId for base VF Module
246 def baseVfModuleId = UUID.randomUUID().toString()
247 execution.setVariable("baseVfModuleId", baseVfModuleId)
249 String requestId = execution.getVariable("requestId")
250 if (requestId.equals("testRequestId123")) {
251 execution.setVariable("vnfId", "skask")
254 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules QueryCatalogDB Process ***", isDebugLogEnabled)
257 public void preProcessAddOnModule(DelegateExecution execution){
258 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
259 execution.setVariable("prefix", Prefix)
260 logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)
263 List<ModuleResource>addOnModules = execution.getVariable("addOnModules")
264 int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")
266 ModuleResource addOnModule = addOnModules[addOnIndex]
268 utils.log("DEBUG", "Got addon module", isDebugLogEnabled)
270 def newVfModuleId = UUID.randomUUID().toString()
271 execution.setVariable("addOnVfModuleId", newVfModuleId)
272 execution.setVariable("isBaseVfModule", "false")
274 execution.setVariable("instancesOfThisModuleDeployed", 0)
276 ModelInfo addOnVfModuleModelInfoObject = addOnModule.getModelInfo()
277 String addOnVfModuleModelInfoWithRoot = addOnVfModuleModelInfoObject.toString()
278 String addOnVfModuleModelInfo = jsonUtil.getJsonValue(addOnVfModuleModelInfoWithRoot, "modelInfo")
279 execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)
280 String addOnVfModuleLabel = addOnModule.getVfModuleLabel()
281 execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)
282 String addOnPersonaModelId = addOnVfModuleModelInfoObject.getModelInvariantUuid()
283 execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)
284 int addOnInitialCount = addOnModule.getInitialCount()
285 execution.setVariable("initialCount", addOnInitialCount)
289 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
290 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
292 logDebug("======== COMPLETED preProcessAddOnModule ======== ", isDebugLogEnabled)
295 public void postProcessAddOnModule(DelegateExecution execution){
296 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
297 execution.setVariable("prefix", Prefix)
298 logDebug(" ======== STARTED postProcessAddOnModule ======== ", isDebugLogEnabled)
301 int addOnModulesDeployed = execution.getVariable("addOnModulesDeployed")
302 execution.setVariable("addOnModulesDeployed", addOnModulesDeployed + 1)
305 utils.log("ERROR", "Exception Occured Processing postProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
306 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during postProcessAddOnModule Method:\n" + e.getMessage())
308 logDebug("======== COMPLETED postProcessAddOnModule ======== ", isDebugLogEnabled)
311 public void validateBaseModule(DelegateExecution execution){
312 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
313 execution.setVariable("prefix", Prefix)
314 logDebug(" ======== STARTED validateBaseModule ======== ", isDebugLogEnabled)
317 def baseRollbackData = execution.getVariable("DCVAM_baseRollbackData")
318 def rollbackData = execution.getVariable("rollbackData")
320 def baseModuleMap = baseRollbackData.get("VFMODULE")
321 baseModuleMap.each{ k, v -> rollbackData.put("VFMODULE_BASE", "${k}","${v}") }
322 execution.setVariable("rollbackData", rollbackData)
323 logDebug("addOnModulesDeployed: " + execution.getVariable("addOnModulesDeployed"), isDebugLogEnabled)
324 logDebug("addOnModulesToDeploy: " + execution.getVariable("addOnModulesToDeploy"), isDebugLogEnabled)
325 if (execution.getVariable("addOnModulesDeployed") < execution.getVariable("addOnModulesToDeploy")) {
326 logDebug("More add on modules to deploy", isDebugLogEnabled)
329 logDebug("No more add on modules to deploy", isDebugLogEnabled)
333 utils.log("ERROR", "Exception Occured Processing validateBaseModule. Exception is:\n" + e, isDebugLogEnabled)
334 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during validateBaseModule Method:\n" + e.getMessage())
336 logDebug("======== COMPLETED validateBaseModule ======== ", isDebugLogEnabled)
339 public void validateAddOnModule(DelegateExecution execution){
340 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
341 execution.setVariable("prefix", Prefix)
342 logDebug(" ======== STARTED validateAddOnModule ======== ", isDebugLogEnabled)
345 int instancesOfThisModuleDeployed = execution.getVariable("instancesOfThisModuleDeployed")
346 int numOfCreatedAddOnModules = execution.getVariable("numOfCreatedAddOnModules")
347 def addOnRollbackData = execution.getVariable("DCVAM_addOnRollbackData")
348 def rollbackData = execution.getVariable("rollbackData")
350 def addOnModuleMap = addOnRollbackData.get("VFMODULE")
351 numOfCreatedAddOnModules = numOfCreatedAddOnModules + 1
352 addOnModuleMap.each{ k, v -> rollbackData.put("VFMODULE_ADDON_" + numOfCreatedAddOnModules, "${k}","${v}") }
354 execution.setVariable("DCVAM_addOnRollbackData", null)
356 execution.setVariable("instancesOfThisModuleDeployed", instancesOfThisModuleDeployed + 1)
358 execution.setVariable("numOfCreatedAddOnModules", numOfCreatedAddOnModules)
359 rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "${numOfCreatedAddOnModules}")
360 execution.setVariable("rollbackData", rollbackData)
362 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
363 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
365 logDebug("======== COMPLETED validateAddOnModule ======== ", isDebugLogEnabled)
368 public void preProcessRollback (DelegateExecution execution) {
369 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
370 utils.log("DEBUG"," ***** preProcessRollback ***** ", isDebugLogEnabled)
373 Object workflowException = execution.getVariable("WorkflowException");
375 if (workflowException instanceof WorkflowException) {
376 utils.log("DEBUG", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugLogEnabled)
377 execution.setVariable("prevWorkflowException", workflowException);
378 //execution.setVariable("WorkflowException", null);
380 } catch (BpmnError e) {
381 utils.log("DEBUG", "BPMN Error during preProcessRollback", isDebugLogEnabled)
382 } catch(Exception ex) {
383 String msg = "Exception in preProcessRollback. " + ex.getMessage()
384 utils.log("DEBUG", msg, isDebugLogEnabled)
386 utils.log("DEBUG"," *** Exit preProcessRollback *** ", isDebugLogEnabled)
389 public void postProcessRollback (DelegateExecution execution) {
390 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
391 utils.log("DEBUG"," ***** postProcessRollback ***** ", isDebugLogEnabled)
394 Object workflowException = execution.getVariable("prevWorkflowException");
395 if (workflowException instanceof WorkflowException) {
396 utils.log("DEBUG", "Setting prevException to WorkflowException: ", isDebugLogEnabled)
397 execution.setVariable("WorkflowException", workflowException);
399 execution.setVariable("rollbackData", null)
400 } catch (BpmnError b) {
401 utils.log("DEBUG", "BPMN Error during postProcessRollback", isDebugLogEnabled)
403 } catch(Exception ex) {
404 msg = "Exception in postProcessRollback. " + ex.getMessage()
405 utils.log("DEBUG", msg, isDebugLogEnabled)
407 utils.log("DEBUG"," *** Exit postProcessRollback *** ", isDebugLogEnabled)