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.runtime.Execution;
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(Execution 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 def rollbackData = execution.getVariable("rollbackData")
119 if (rollbackData == null) {
120 rollbackData = new RollbackData()
123 def isTest = execution.getVariable("isTest")
125 if (isTest == null || isTest == false) {
126 execution.setVariable("isBaseVfModule", "true")
128 execution.setVariable("numOfCreatedAddOnModules", 0)
130 rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "0")
131 execution.setVariable("rollbackData", rollbackData)
137 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugLogEnabled)
140 utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugLogEnabled)
141 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
144 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules PreProcessRequest Process ***", isDebugLogEnabled)
148 public void queryCatalogDB (Execution execution) {
149 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
150 execution.setVariable("prefix",Prefix)
152 utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules QueryCatalogDB Process *** ", isDebugLogEnabled)
154 VnfResource vnf = null
155 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
156 // if serviceDecomposition is specified, get info from serviceDecomposition
157 if (serviceDecomposition != null) {
158 utils.log("DEBUG", "Getting Catalog DB data from ServiceDecomposition object: " + serviceDecomposition.toJsonString(), isDebugLogEnabled)
159 List<VnfResource> vnfs = serviceDecomposition.getServiceVnfs()
160 utils.log("DEBUG", "Read vnfs", isDebugLogEnabled)
162 utils.log("DEBUG", "Error - vnfs are empty in serviceDecomposition object", isDebugLogEnabled)
163 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnfs are empty")
166 String serviceModelName = serviceDecomposition.getModelInfo().getModelName()
167 vnf.constructVnfType(serviceModelName)
168 String vnfType = vnf.getVnfType()
169 utils.log("DEBUG", "Incoming Vnf Type is: " + vnfType, isDebugLogEnabled)
170 execution.setVariable("vnfType", vnfType)
174 String vnfModelInfo = execution.getVariable("vnfModelInfo")
175 utils.log("DEBUG", "vnfModelInfo: " + vnfModelInfo, isDebugLogEnabled)
176 String vnfModelCustomizationUuid = jsonUtil.getJsonValueForKey(vnfModelInfo, "modelCustomizationUuid")
177 if (vnfModelCustomizationUuid == null) {
178 vnfModelCustomizationUuid = ""
180 utils.log("DEBUG", "querying Catalog DB by vnfModelCustomizationUuid: " + vnfModelCustomizationUuid, isDebugLogEnabled)
182 JSONArray vnfs = cutils.getAllVnfsByVnfModelCustomizationUuid(execution,
183 vnfModelCustomizationUuid)
184 utils.log("DEBUG", "obtained VNF list")
185 // Only one match here
186 JSONObject vnfObject = vnfs[0]
187 vnf = DecomposeJsonUtil.jsonToVnfResource(vnfObject.toString())
189 utils.log("DEBUG", "Read vnfResource", isDebugLogEnabled)
191 utils.log("DEBUG", "Error - vnf is empty in serviceDecomposition object", isDebugLogEnabled)
192 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnf is null")
194 execution.setVariable("vnfResourceDecomposition", vnf)
196 List<ModuleResource> vfModules = vnf.getAllVfModuleObjects()
197 utils.log("DEBUG", "Read vfModules", isDebugLogEnabled)
198 if (vfModules == null) {
199 utils.log("DEBUG", "Error - vfModules are empty in serviceDecomposition object", isDebugLogEnabled)
200 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vf modules are empty")
203 ModuleResource baseVfModule = null
205 for (int i = 0; i < vfModules.size; i++) {
206 utils.log("DEBUG", "handling VF Module ", isDebugLogEnabled)
207 ModuleResource vfModule = vfModules[i]
208 boolean isBase = vfModule.getIsBase()
210 ModelInfo baseVfModuleModelInfoObject = vfModule.getModelInfo()
211 String baseVfModuleModelInfoWithRoot = baseVfModuleModelInfoObject.toString()
212 String baseVfModuleModelInfo = jsonUtil.getJsonValue(baseVfModuleModelInfoWithRoot, "modelInfo")
213 execution.setVariable("baseVfModuleModelInfo", baseVfModuleModelInfo)
214 String baseVfModuleLabel = vfModule.getVfModuleLabel()
215 execution.setVariable("baseVfModuleLabel", baseVfModuleLabel)
216 String basePersonaModelId = baseVfModuleModelInfoObject.getModelInvariantUuid()
217 execution.setVariable("basePersonaModelId", basePersonaModelId)
218 baseVfModule = vfModule
224 List<ModuleResource>addOnModules = vfModules - baseVfModule
226 int addOnModulesToDeploy = 0
227 if (addOnModules != null) {
228 addOnModulesToDeploy = addOnModules.size
231 utils.log("DEBUG", "AddOnModulesToDeploy: " + addOnModulesToDeploy)
233 execution.setVariable("addOnModules", addOnModules)
234 execution.setVariable("addOnModulesToDeploy", addOnModulesToDeploy)
235 execution.setVariable("addOnModulesDeployed", 0)
237 }catch(Exception ex) {
238 utils.log("DEBUG", "Error Occured in DoCreateVnfAndModules QueryCatalogDB Process " + ex.getMessage(), isDebugLogEnabled)
239 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModules QueryCatalogDB Process")
242 // Generate vfModuleId for base VF Module
243 def baseVfModuleId = UUID.randomUUID().toString()
244 execution.setVariable("baseVfModuleId", baseVfModuleId)
246 String requestId = execution.getVariable("requestId")
247 if (requestId.equals("testRequestId123")) {
248 execution.setVariable("vnfId", "skask")
251 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules QueryCatalogDB Process ***", isDebugLogEnabled)
254 public void preProcessAddOnModule(Execution execution){
255 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
256 execution.setVariable("prefix", Prefix)
257 logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)
260 List<ModuleResource>addOnModules = execution.getVariable("addOnModules")
261 int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")
263 ModuleResource addOnModule = addOnModules[addOnIndex]
265 utils.log("DEBUG", "Got addon module", isDebugLogEnabled)
267 def newVfModuleId = UUID.randomUUID().toString()
268 execution.setVariable("addOnVfModuleId", newVfModuleId)
269 execution.setVariable("isBaseVfModule", "false")
271 execution.setVariable("instancesOfThisModuleDeployed", 0)
273 ModelInfo addOnVfModuleModelInfoObject = addOnModule.getModelInfo()
274 String addOnVfModuleModelInfoWithRoot = addOnVfModuleModelInfoObject.toString()
275 String addOnVfModuleModelInfo = jsonUtil.getJsonValue(addOnVfModuleModelInfoWithRoot, "modelInfo")
276 execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)
277 String addOnVfModuleLabel = addOnModule.getVfModuleLabel()
278 execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)
279 String addOnPersonaModelId = addOnVfModuleModelInfoObject.getModelInvariantUuid()
280 execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)
281 int addOnInitialCount = addOnModule.getInitialCount()
282 execution.setVariable("initialCount", addOnInitialCount)
286 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
287 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
289 logDebug("======== COMPLETED preProcessAddOnModule ======== ", isDebugLogEnabled)
292 public void postProcessAddOnModule(Execution execution){
293 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
294 execution.setVariable("prefix", Prefix)
295 logDebug(" ======== STARTED postProcessAddOnModule ======== ", isDebugLogEnabled)
298 int addOnModulesDeployed = execution.getVariable("addOnModulesDeployed")
299 execution.setVariable("addOnModulesDeployed", addOnModulesDeployed + 1)
302 utils.log("ERROR", "Exception Occured Processing postProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
303 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during postProcessAddOnModule Method:\n" + e.getMessage())
305 logDebug("======== COMPLETED postProcessAddOnModule ======== ", isDebugLogEnabled)
308 public void validateBaseModule(Execution execution){
309 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
310 execution.setVariable("prefix", Prefix)
311 logDebug(" ======== STARTED validateBaseModule ======== ", isDebugLogEnabled)
314 def baseRollbackData = execution.getVariable("DCVAM_baseRollbackData")
315 def rollbackData = execution.getVariable("rollbackData")
317 def baseModuleMap = baseRollbackData.get("VFMODULE")
318 baseModuleMap.each{ k, v -> rollbackData.put("VFMODULE_BASE", "${k}","${v}") }
319 execution.setVariable("rollbackData", rollbackData)
320 logDebug("addOnModulesDeployed: " + execution.getVariable("addOnModulesDeployed"), isDebugLogEnabled)
321 logDebug("addOnModulesToDeploy: " + execution.getVariable("addOnModulesToDeploy"), isDebugLogEnabled)
322 if (execution.getVariable("addOnModulesDeployed") < execution.getVariable("addOnModulesToDeploy")) {
323 logDebug("More add on modules to deploy", isDebugLogEnabled)
326 logDebug("No more add on modules to deploy", isDebugLogEnabled)
330 utils.log("ERROR", "Exception Occured Processing validateBaseModule. Exception is:\n" + e, isDebugLogEnabled)
331 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during validateBaseModule Method:\n" + e.getMessage())
333 logDebug("======== COMPLETED validateBaseModule ======== ", isDebugLogEnabled)
336 public void validateAddOnModule(Execution execution){
337 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
338 execution.setVariable("prefix", Prefix)
339 logDebug(" ======== STARTED validateAddOnModule ======== ", isDebugLogEnabled)
342 int instancesOfThisModuleDeployed = execution.getVariable("instancesOfThisModuleDeployed")
343 int numOfCreatedAddOnModules = execution.getVariable("numOfCreatedAddOnModules")
344 def addOnRollbackData = execution.getVariable("DCVAM_addOnRollbackData")
345 def rollbackData = execution.getVariable("rollbackData")
347 def addOnModuleMap = addOnRollbackData.get("VFMODULE")
348 numOfCreatedAddOnModules = numOfCreatedAddOnModules + 1
349 addOnModuleMap.each{ k, v -> rollbackData.put("VFMODULE_ADDON_" + numOfCreatedAddOnModules, "${k}","${v}") }
351 execution.setVariable("DCVAM_addOnRollbackData", null)
353 execution.setVariable("instancesOfThisModuleDeployed", instancesOfThisModuleDeployed + 1)
355 execution.setVariable("numOfCreatedAddOnModules", numOfCreatedAddOnModules)
356 rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "${numOfCreatedAddOnModules}")
357 execution.setVariable("rollbackData", rollbackData)
359 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
360 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
362 logDebug("======== COMPLETED validateAddOnModule ======== ", isDebugLogEnabled)
365 public void preProcessRollback (Execution execution) {
366 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
367 utils.log("DEBUG"," ***** preProcessRollback ***** ", isDebugLogEnabled)
370 Object workflowException = execution.getVariable("WorkflowException");
372 if (workflowException instanceof WorkflowException) {
373 utils.log("DEBUG", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugLogEnabled)
374 execution.setVariable("prevWorkflowException", workflowException);
375 //execution.setVariable("WorkflowException", null);
377 } catch (BpmnError e) {
378 utils.log("DEBUG", "BPMN Error during preProcessRollback", isDebugLogEnabled)
379 } catch(Exception ex) {
380 String msg = "Exception in preProcessRollback. " + ex.getMessage()
381 utils.log("DEBUG", msg, isDebugLogEnabled)
383 utils.log("DEBUG"," *** Exit preProcessRollback *** ", isDebugLogEnabled)
386 public void postProcessRollback (Execution execution) {
387 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
388 utils.log("DEBUG"," ***** postProcessRollback ***** ", isDebugLogEnabled)
391 Object workflowException = execution.getVariable("prevWorkflowException");
392 if (workflowException instanceof WorkflowException) {
393 utils.log("DEBUG", "Setting prevException to WorkflowException: ", isDebugLogEnabled)
394 execution.setVariable("WorkflowException", workflowException);
396 execution.setVariable("rollbackData", null)
397 } catch (BpmnError b) {
398 utils.log("DEBUG", "BPMN Error during postProcessRollback", isDebugLogEnabled)
400 } catch(Exception ex) {
401 msg = "Exception in postProcessRollback. " + ex.getMessage()
402 utils.log("DEBUG", msg, isDebugLogEnabled)
404 utils.log("DEBUG"," *** Exit postProcessRollback *** ", isDebugLogEnabled)