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()
57 DecomposeJsonUtil decomposeJsonUtil = new DecomposeJsonUtil()
60 * This method gets and validates the incoming
65 public void preProcessRequest(Execution execution) {
66 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
67 execution.setVariable("prefix",Prefix)
68 utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules PreProcessRequest Process*** ", isDebugEnabled)
70 setBasicDBAuthHeader(execution, isDebugLogEnabled)
74 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
76 String vnfModelInfo = execution.getVariable("vnfModelInfo")
78 String requestId = execution.getVariable("msoRequestId")
79 execution.setVariable("requestId", requestId)
80 execution.setVariable("mso-request-id", requestId)
81 utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugEnabled)
83 String serviceInstanceId = execution.getVariable("serviceInstanceId")
84 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
86 String vnfName = execution.getVariable("vnfName")
87 execution.setVariable("CREVI_vnfName", vnfName)
88 utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)
90 String productFamilyId = execution.getVariable("productFamilyId")
91 utils.log("DEBUG", "Incoming Product Family Id is: " + productFamilyId, isDebugEnabled)
94 execution.setVariable("source", source)
95 utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)
97 String lcpCloudRegionId = execution.getVariable("lcpCloudRegionId")
98 utils.log("DEBUG", "Incoming LCP Cloud Region Id is: " + lcpCloudRegionId)
100 String tenantId = execution.getVariable("tenantId")
101 utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId)
103 String disableRollback = execution.getVariable("disableRollback")
104 utils.log("DEBUG", "Incoming Disable Rollback is: " + disableRollback, isDebugEnabled)
106 String asdcServiceModelVersion = execution.getVariable("asdcServiceModelVersion")
107 utils.log("DEBUG", "Incoming asdcServiceModelVersion: " + asdcServiceModelVersion, isDebugEnabled)
109 String vnfId = execution.getVariable("testVnfId") // for junits
111 vnfId = execution.getVariable("vnfId")
112 if (isBlank(vnfId)) {
113 vnfId = UUID.randomUUID().toString()
114 utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)
117 execution.setVariable("vnfId", vnfId)
119 def rollbackData = execution.getVariable("rollbackData")
120 if (rollbackData == null) {
121 rollbackData = new RollbackData()
124 def isTest = execution.getVariable("isTest")
126 if (isTest == null || isTest == false) {
127 execution.setVariable("isBaseVfModule", "true")
129 execution.setVariable("numOfCreatedAddOnModules", 0)
131 rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "0")
132 execution.setVariable("rollbackData", rollbackData)
138 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
141 utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugEnabled)
142 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
145 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules PreProcessRequest Process ***", isDebugEnabled)
149 public void queryCatalogDB (Execution execution) {
150 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
151 execution.setVariable("prefix",Prefix)
153 utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules QueryCatalogDB Process *** ", isDebugEnabled)
155 VnfResource vnf = null
156 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
157 // if serviceDecomposition is specified, get info from serviceDecomposition
158 if (serviceDecomposition != null) {
159 utils.log("DEBUG", "Getting Catalog DB data from ServiceDecomposition object: " + serviceDecomposition.toJsonString(), isDebugEnabled)
160 List<VnfResource> vnfs = serviceDecomposition.getServiceVnfs()
161 utils.log("DEBUG", "Read vnfs", isDebugEnabled)
163 utils.log("DEBUG", "Error - vnfs are empty in serviceDecomposition object", isDebugEnabled)
164 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnfs are empty")
167 String serviceModelName = serviceDecomposition.getModelInfo().getModelName()
168 vnf.constructVnfType(serviceModelName)
169 String vnfType = vnf.getVnfType()
170 utils.log("DEBUG", "Incoming Vnf Type is: " + vnfType, isDebugEnabled)
171 execution.setVariable("vnfType", vnfType)
175 String vnfModelInfo = execution.getVariable("vnfModelInfo")
176 utils.log("DEBUG", "vnfModelInfo: " + vnfModelInfo, isDebugEnabled)
177 String vnfModelCustomizationUuid = jsonUtil.getJsonValueForKey(vnfModelInfo, "modelCustomizationUuid")
178 if (vnfModelCustomizationUuid == null) {
179 vnfModelCustomizationUuid = ""
181 utils.log("DEBUG", "querying Catalog DB by vnfModelCustomizationUuid: " + vnfModelCustomizationUuid, isDebugEnabled)
183 JSONArray vnfs = cutils.getAllVnfsByVnfModelCustomizationUuid(execution,
184 vnfModelCustomizationUuid)
185 utils.log("DEBUG", "obtained VNF list")
186 // Only one match here
187 JSONObject vnfObject = vnfs[0]
188 vnf = decomposeJsonUtil.JsonToVnfResource(vnfObject.toString())
190 utils.log("DEBUG", "Read vnfResource", isDebugEnabled)
192 utils.log("DEBUG", "Error - vnf is empty in serviceDecomposition object", isDebugEnabled)
193 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnf is null")
195 execution.setVariable("vnfResourceDecomposition", vnf)
197 List<ModuleResource> vfModules = vnf.getAllVfModuleObjects()
198 utils.log("DEBUG", "Read vfModules", isDebugEnabled)
199 if (vfModules == null) {
200 utils.log("DEBUG", "Error - vfModules are empty in serviceDecomposition object", isDebugEnabled)
201 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vf modules are empty")
204 ModuleResource baseVfModule = null
206 for (int i = 0; i < vfModules.size; i++) {
207 utils.log("DEBUG", "handling VF Module ", isDebugEnabled)
208 ModuleResource vfModule = vfModules[i]
209 boolean isBase = vfModule.getIsBase()
211 ModelInfo baseVfModuleModelInfoObject = vfModule.getModelInfo()
212 String baseVfModuleModelInfoWithRoot = baseVfModuleModelInfoObject.toString()
213 String baseVfModuleModelInfo = jsonUtil.getJsonValue(baseVfModuleModelInfoWithRoot, "modelInfo")
214 execution.setVariable("baseVfModuleModelInfo", baseVfModuleModelInfo)
215 String baseVfModuleLabel = vfModule.getVfModuleLabel()
216 execution.setVariable("baseVfModuleLabel", baseVfModuleLabel)
217 String basePersonaModelId = baseVfModuleModelInfoObject.getModelInvariantUuid()
218 execution.setVariable("basePersonaModelId", basePersonaModelId)
219 baseVfModule = vfModule
225 List<ModuleResource>addOnModules = vfModules - baseVfModule
227 int addOnModulesToDeploy = 0
228 if (addOnModules != null) {
229 addOnModulesToDeploy = addOnModules.size
232 utils.log("DEBUG", "AddOnModulesToDeploy: " + addOnModulesToDeploy)
234 execution.setVariable("addOnModules", addOnModules)
235 execution.setVariable("addOnModulesToDeploy", addOnModulesToDeploy)
236 execution.setVariable("addOnModulesDeployed", 0)
238 }catch(Exception ex) {
239 utils.log("DEBUG", "Error Occured in DoCreateVnfAndModules QueryCatalogDB Process " + ex.getMessage(), isDebugEnabled)
240 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModules QueryCatalogDB Process")
243 // Generate vfModuleId for base VF Module
244 def baseVfModuleId = UUID.randomUUID().toString()
245 execution.setVariable("baseVfModuleId", baseVfModuleId)
247 String requestId = execution.getVariable("requestId")
248 if (requestId.equals("testRequestId123")) {
249 execution.setVariable("vnfId", "skask")
252 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules QueryCatalogDB Process ***", isDebugEnabled)
255 public void preProcessAddOnModule(Execution execution){
256 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
257 execution.setVariable("prefix", Prefix)
258 logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)
261 List<ModuleResource>addOnModules = execution.getVariable("addOnModules")
262 int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")
264 ModuleResource addOnModule = addOnModules[addOnIndex]
266 utils.log("DEBUG", "Got addon module", isDebugLogEnabled)
268 def newVfModuleId = UUID.randomUUID().toString()
269 execution.setVariable("addOnVfModuleId", newVfModuleId)
270 execution.setVariable("isBaseVfModule", "false")
272 execution.setVariable("instancesOfThisModuleDeployed", 0)
274 ModelInfo addOnVfModuleModelInfoObject = addOnModule.getModelInfo()
275 String addOnVfModuleModelInfoWithRoot = addOnVfModuleModelInfoObject.toString()
276 String addOnVfModuleModelInfo = jsonUtil.getJsonValue(addOnVfModuleModelInfoWithRoot, "modelInfo")
277 execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)
278 String addOnVfModuleLabel = addOnModule.getVfModuleLabel()
279 execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)
280 String addOnPersonaModelId = addOnVfModuleModelInfoObject.getModelInvariantUuid()
281 execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)
282 int addOnInitialCount = addOnModule.getInitialCount()
283 execution.setVariable("initialCount", addOnInitialCount)
287 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
288 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
290 logDebug("======== COMPLETED preProcessAddOnModule ======== ", isDebugLogEnabled)
293 public void postProcessAddOnModule(Execution execution){
294 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
295 execution.setVariable("prefix", Prefix)
296 logDebug(" ======== STARTED postProcessAddOnModule ======== ", isDebugLogEnabled)
299 int addOnModulesDeployed = execution.getVariable("addOnModulesDeployed")
300 execution.setVariable("addOnModulesDeployed", addOnModulesDeployed + 1)
303 utils.log("ERROR", "Exception Occured Processing postProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
304 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during postProcessAddOnModule Method:\n" + e.getMessage())
306 logDebug("======== COMPLETED postProcessAddOnModule ======== ", isDebugLogEnabled)
309 public void validateBaseModule(Execution execution){
310 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
311 execution.setVariable("prefix", Prefix)
312 logDebug(" ======== STARTED validateBaseModule ======== ", isDebugLogEnabled)
315 def baseRollbackData = execution.getVariable("DCVAM_baseRollbackData")
316 def rollbackData = execution.getVariable("rollbackData")
318 def baseModuleMap = baseRollbackData.get("VFMODULE")
319 baseModuleMap.each{ k, v -> rollbackData.put("VFMODULE_BASE", "${k}","${v}") }
320 execution.setVariable("rollbackData", rollbackData)
321 logDebug("addOnModulesDeployed: " + execution.getVariable("addOnModulesDeployed"), isDebugLogEnabled)
322 logDebug("addOnModulesToDeploy: " + execution.getVariable("addOnModulesToDeploy"), isDebugLogEnabled)
323 if (execution.getVariable("addOnModulesDeployed") < execution.getVariable("addOnModulesToDeploy")) {
324 logDebug("More add on modules to deploy", isDebugLogEnabled)
327 logDebug("No more add on modules to deploy", isDebugLogEnabled)
331 utils.log("ERROR", "Exception Occured Processing validateBaseModule. Exception is:\n" + e, isDebugLogEnabled)
332 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during validateBaseModule Method:\n" + e.getMessage())
334 logDebug("======== COMPLETED validateBaseModule ======== ", isDebugLogEnabled)
337 public void validateAddOnModule(Execution execution){
338 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
339 execution.setVariable("prefix", Prefix)
340 logDebug(" ======== STARTED validateAddOnModule ======== ", isDebugLogEnabled)
343 int instancesOfThisModuleDeployed = execution.getVariable("instancesOfThisModuleDeployed")
344 int numOfCreatedAddOnModules = execution.getVariable("numOfCreatedAddOnModules")
345 def addOnRollbackData = execution.getVariable("DCVAM_addOnRollbackData")
346 def rollbackData = execution.getVariable("rollbackData")
348 def addOnModuleMap = addOnRollbackData.get("VFMODULE")
349 numOfCreatedAddOnModules = numOfCreatedAddOnModules + 1
350 addOnModuleMap.each{ k, v -> rollbackData.put("VFMODULE_ADDON_" + numOfCreatedAddOnModules, "${k}","${v}") }
352 execution.setVariable("DCVAM_addOnRollbackData", null)
354 execution.setVariable("instancesOfThisModuleDeployed", instancesOfThisModuleDeployed + 1)
356 execution.setVariable("numOfCreatedAddOnModules", numOfCreatedAddOnModules)
357 rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "${numOfCreatedAddOnModules}")
358 execution.setVariable("rollbackData", rollbackData)
360 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
361 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
363 logDebug("======== COMPLETED validateAddOnModule ======== ", isDebugLogEnabled)
366 public void preProcessRollback (Execution execution) {
367 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
368 utils.log("DEBUG"," ***** preProcessRollback ***** ", isDebugEnabled)
371 Object workflowException = execution.getVariable("WorkflowException");
373 if (workflowException instanceof WorkflowException) {
374 utils.log("DEBUG", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugEnabled)
375 execution.setVariable("prevWorkflowException", workflowException);
376 //execution.setVariable("WorkflowException", null);
378 } catch (BpmnError e) {
379 utils.log("DEBUG", "BPMN Error during preProcessRollback", isDebugEnabled)
380 } catch(Exception ex) {
381 String msg = "Exception in preProcessRollback. " + ex.getMessage()
382 utils.log("DEBUG", msg, isDebugEnabled)
384 utils.log("DEBUG"," *** Exit preProcessRollback *** ", isDebugEnabled)
387 public void postProcessRollback (Execution execution) {
388 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
389 utils.log("DEBUG"," ***** postProcessRollback ***** ", isDebugEnabled)
392 Object workflowException = execution.getVariable("prevWorkflowException");
393 if (workflowException instanceof WorkflowException) {
394 utils.log("DEBUG", "Setting prevException to WorkflowException: ", isDebugEnabled)
395 execution.setVariable("WorkflowException", workflowException);
397 execution.setVariable("rollbackData", null)
398 } catch (BpmnError b) {
399 utils.log("DEBUG", "BPMN Error during postProcessRollback", isDebugEnabled)
401 } catch(Exception ex) {
402 msg = "Exception in postProcessRollback. " + ex.getMessage()
403 utils.log("DEBUG", msg, isDebugEnabled)
405 utils.log("DEBUG"," *** Exit postProcessRollback *** ", isDebugEnabled)