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=========================================================
21 package org.onap.so.bpmn.infrastructure.scripts
23 import org.json.JSONObject;
24 import org.json.JSONArray;
25 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
26 import org.onap.so.bpmn.common.scripts.CatalogDbUtils
27 import org.onap.so.bpmn.common.scripts.CatalogDbUtilsFactory
28 import org.onap.so.bpmn.common.scripts.ExceptionUtil
29 import org.onap.so.bpmn.common.scripts.VidUtils
30 import org.onap.so.bpmn.core.RollbackData
31 import org.onap.so.bpmn.core.WorkflowException
32 import org.onap.so.bpmn.core.domain.ModelInfo
33 import org.onap.so.bpmn.core.domain.ModuleResource
34 import org.onap.so.bpmn.core.domain.ServiceDecomposition
35 import org.onap.so.bpmn.core.domain.VnfResource
36 import org.onap.so.bpmn.core.json.DecomposeJsonUtil
37 import org.onap.so.bpmn.core.json.JsonUtils
38 import org.camunda.bpm.engine.delegate.BpmnError
39 import org.camunda.bpm.engine.delegate.DelegateExecution;
40 import org.onap.so.bpmn.infrastructure.aai.groovyflows.AAICreateResources;
41 import org.onap.so.logger.MsoLogger
42 import org.onap.so.logger.MessageEnum
44 import static org.apache.commons.lang3.StringUtils.*;
49 * This class supports the macro VID Flow
50 * with the creation of a generic vnf and related VF modules.
52 class DoCreateVnfAndModules extends AbstractServiceTaskProcessor {
54 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateVnfAndModules.class);
55 String Prefix="DCVAM_"
56 ExceptionUtil exceptionUtil = new ExceptionUtil()
57 JsonUtils jsonUtil = new JsonUtils()
58 VidUtils vidUtils = new VidUtils(this)
59 CatalogDbUtils catalogDbUtils = new CatalogDbUtilsFactory().create()
62 * This method gets and validates the incoming
67 public void preProcessRequest(DelegateExecution execution) {
69 execution.setVariable("prefix",Prefix)
70 msoLogger.trace("STARTED DoCreateVnfAndModules PreProcessRequest Process")
71 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
73 setBasicDBAuthHeader(execution, isDebugLogEnabled)
77 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
79 String vnfModelInfo = execution.getVariable("vnfModelInfo")
81 String requestId = execution.getVariable("msoRequestId")
82 execution.setVariable("requestId", requestId)
83 execution.setVariable("mso-request-id", requestId)
84 msoLogger.debug("Incoming Request Id is: " + requestId)
86 String serviceInstanceId = execution.getVariable("serviceInstanceId")
87 msoLogger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
89 String vnfName = execution.getVariable("vnfName")
90 execution.setVariable("CREVI_vnfName", vnfName)
91 msoLogger.debug("Incoming Vnf Name is: " + vnfName)
93 String productFamilyId = execution.getVariable("productFamilyId")
94 msoLogger.debug("Incoming Product Family Id is: " + productFamilyId)
97 execution.setVariable("source", source)
98 msoLogger.debug("Incoming Source is: " + source)
100 String lcpCloudRegionId = execution.getVariable("lcpCloudRegionId")
101 msoLogger.debug("Incoming LCP Cloud Region Id is: " + lcpCloudRegionId)
103 String tenantId = execution.getVariable("tenantId")
104 msoLogger.debug("Incoming Tenant Id is: " + tenantId)
106 String disableRollback = execution.getVariable("disableRollback")
107 msoLogger.debug("Incoming Disable Rollback is: " + disableRollback)
109 String asdcServiceModelVersion = execution.getVariable("asdcServiceModelVersion")
110 msoLogger.debug("Incoming asdcServiceModelVersion: " + asdcServiceModelVersion)
112 String vnfId = execution.getVariable("testVnfId") // for junits
114 vnfId = execution.getVariable("vnfId")
115 if (isBlank(vnfId)) {
116 vnfId = UUID.randomUUID().toString()
117 msoLogger.debug("Generated Vnf Id is: " + vnfId)
120 execution.setVariable("vnfId", vnfId)
122 // Set aLaCarte to false
123 execution.setVariable("aLaCarte", false)
125 def rollbackData = execution.getVariable("rollbackData")
126 if (rollbackData == null) {
127 rollbackData = new RollbackData()
130 def isTest = execution.getVariable("isTest")
132 if (isTest == null || isTest == false) {
133 execution.setVariable("isBaseVfModule", "true")
135 execution.setVariable("numOfCreatedAddOnModules", 0)
137 rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "0")
138 execution.setVariable("rollbackData", rollbackData)
144 msoLogger.debug("Rethrowing MSOWorkflowException")
147 msoLogger.debug(" Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage())
148 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
151 msoLogger.trace("COMPLETED DoCreateVnfAndModules PreProcessRequest Process")
155 public void queryCatalogDB (DelegateExecution execution) {
157 execution.setVariable("prefix",Prefix)
159 msoLogger.trace("STARTED DoCreateVnfAndModules QueryCatalogDB Process")
161 VnfResource vnf = null
162 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
163 // if serviceDecomposition is specified, get info from serviceDecomposition
164 if (serviceDecomposition != null) {
165 msoLogger.debug("Getting Catalog DB data from ServiceDecomposition object: " + serviceDecomposition.toJsonString())
166 List<VnfResource> vnfs = serviceDecomposition.getVnfResources()
167 msoLogger.debug("Read vnfs")
169 msoLogger.debug("Error - vnfs are empty in serviceDecomposition object")
170 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnfs are empty")
173 String serviceModelName = serviceDecomposition.getModelInfo().getModelName()
174 vnf.constructVnfType(serviceModelName)
175 String vnfType = vnf.getVnfType()
176 msoLogger.debug("Incoming Vnf Type is: " + vnfType)
177 execution.setVariable("vnfType", vnfType)
181 String vnfModelInfo = execution.getVariable("vnfModelInfo")
182 msoLogger.debug("vnfModelInfo: " + vnfModelInfo)
183 String vnfModelCustomizationUuid = jsonUtil.getJsonValueForKey(vnfModelInfo, "modelCustomizationUuid")
184 if (vnfModelCustomizationUuid == null) {
185 vnfModelCustomizationUuid = ""
187 msoLogger.debug("querying Catalog DB by vnfModelCustomizationUuid: " + vnfModelCustomizationUuid)
189 JSONArray vnfs = catalogDbUtils.getAllVnfsByVnfModelCustomizationUuid(execution,
190 vnfModelCustomizationUuid, "v1")
191 msoLogger.debug("obtained VNF list")
192 // Only one match here
193 JSONObject vnfObject = vnfs[0]
194 vnf = DecomposeJsonUtil.jsonToVnfResource(vnfObject.toString())
196 msoLogger.debug("Read vnfResource")
198 msoLogger.debug("Error - vnf is empty in serviceDecomposition object")
199 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnf is null")
201 execution.setVariable("vnfResourceDecomposition", vnf)
203 List<ModuleResource> vfModules = vnf.getAllVfModuleObjects()
204 msoLogger.debug("Read vfModules")
205 if (vfModules == null) {
206 msoLogger.debug("Error - vfModules are empty in serviceDecomposition object")
207 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vf modules are empty")
210 ModuleResource baseVfModule = null
212 for (int i = 0; i < vfModules.size; i++) {
213 msoLogger.debug("handling VF Module ")
214 ModuleResource vfModule = vfModules[i]
215 boolean isBase = vfModule.getIsBase()
217 ModelInfo baseVfModuleModelInfoObject = vfModule.getModelInfo()
218 String baseVfModuleModelInfoWithRoot = baseVfModuleModelInfoObject.toString()
219 String baseVfModuleModelInfo = jsonUtil.getJsonValue(baseVfModuleModelInfoWithRoot, "modelInfo")
220 execution.setVariable("baseVfModuleModelInfo", baseVfModuleModelInfo)
221 String baseVfModuleLabel = vfModule.getVfModuleLabel()
222 execution.setVariable("baseVfModuleLabel", baseVfModuleLabel)
223 String basePersonaModelId = baseVfModuleModelInfoObject.getModelInvariantUuid()
224 execution.setVariable("basePersonaModelId", basePersonaModelId)
225 baseVfModule = vfModule
231 List<ModuleResource>addOnModules = vfModules - baseVfModule
233 int addOnModulesToDeploy = 0
234 if (addOnModules != null) {
235 addOnModulesToDeploy = addOnModules.size
239 execution.setVariable("addOnModules", addOnModules)
240 execution.setVariable("addOnModulesToDeploy", addOnModulesToDeploy)
241 execution.setVariable("addOnModulesDeployed", 0)
243 }catch(Exception ex) {
244 msoLogger.debug("Error Occured in DoCreateVnfAndModules QueryCatalogDB Process " + ex.getMessage())
245 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModules QueryCatalogDB Process")
248 // Generate vfModuleId for base VF Module
249 def baseVfModuleId = UUID.randomUUID().toString()
250 execution.setVariable("baseVfModuleId", baseVfModuleId)
252 String requestId = execution.getVariable("requestId")
253 if (requestId.equals("testRequestId123")) {
254 execution.setVariable("vnfId", "skask")
257 msoLogger.trace("COMPLETED DoCreateVnfAndModules QueryCatalogDB Process")
260 public void preProcessAddOnModule(DelegateExecution execution){
262 execution.setVariable("prefix", Prefix)
263 msoLogger.debug("STARTED preProcessAddOnModule")
266 List<ModuleResource>addOnModules = execution.getVariable("addOnModules")
267 int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")
269 ModuleResource addOnModule = addOnModules[addOnIndex]
271 msoLogger.debug("Got addon module")
273 def newVfModuleId = UUID.randomUUID().toString()
274 execution.setVariable("addOnVfModuleId", newVfModuleId)
275 execution.setVariable("isBaseVfModule", "false")
277 execution.setVariable("instancesOfThisModuleDeployed", 0)
279 ModelInfo addOnVfModuleModelInfoObject = addOnModule.getModelInfo()
280 String addOnVfModuleModelInfoWithRoot = addOnVfModuleModelInfoObject.toString()
281 String addOnVfModuleModelInfo = jsonUtil.getJsonValue(addOnVfModuleModelInfoWithRoot, "modelInfo")
282 execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)
283 String addOnVfModuleLabel = addOnModule.getVfModuleLabel()
284 execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)
285 String addOnPersonaModelId = addOnVfModuleModelInfoObject.getModelInvariantUuid()
286 execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)
287 int addOnInitialCount = addOnModule.getInitialCount()
288 execution.setVariable("initialCount", addOnInitialCount)
292 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing preProcessAddOnModule ", "BPMN", MsoLogger.getServiceName(),MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
293 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
295 msoLogger.trace("COMPLETED preProcessAddOnModule")
298 public void postProcessAddOnModule(DelegateExecution execution){
300 execution.setVariable("prefix", Prefix)
301 msoLogger.trace("STARTED postProcessAddOnModule")
304 int addOnModulesDeployed = execution.getVariable("addOnModulesDeployed")
305 execution.setVariable("addOnModulesDeployed", addOnModulesDeployed + 1)
308 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing postProcessAddOnModule ", "BPMN", MsoLogger.getServiceName(),MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
309 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during postProcessAddOnModule Method:\n" + e.getMessage())
311 msoLogger.trace("COMPLETED postProcessAddOnModule")
314 public void validateBaseModule(DelegateExecution execution){
316 execution.setVariable("prefix", Prefix)
317 msoLogger.trace("STARTED validateBaseModule")
320 def baseRollbackData = execution.getVariable("DCVAM_baseRollbackData")
321 def rollbackData = execution.getVariable("rollbackData")
323 def baseModuleMap = baseRollbackData.get("VFMODULE")
324 baseModuleMap.each{ k, v -> rollbackData.put("VFMODULE_BASE", "${k}","${v}") }
325 execution.setVariable("rollbackData", rollbackData)
326 msoLogger.debug("addOnModulesDeployed: " + execution.getVariable("addOnModulesDeployed"))
327 msoLogger.debug("addOnModulesToDeploy: " + execution.getVariable("addOnModulesToDeploy"))
328 if (execution.getVariable("addOnModulesDeployed") < execution.getVariable("addOnModulesToDeploy")) {
329 msoLogger.debug("More add on modules to deploy")
332 msoLogger.debug("No more add on modules to deploy")
336 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing validateBaseModule ", "BPMN", MsoLogger.getServiceName(),MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
337 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during validateBaseModule Method:\n" + e.getMessage())
339 msoLogger.trace("COMPLETED validateBaseModule")
342 public void validateAddOnModule(DelegateExecution execution){
344 execution.setVariable("prefix", Prefix)
345 msoLogger.trace("STARTED validateAddOnModule")
348 int instancesOfThisModuleDeployed = execution.getVariable("instancesOfThisModuleDeployed")
349 int numOfCreatedAddOnModules = execution.getVariable("numOfCreatedAddOnModules")
350 def addOnRollbackData = execution.getVariable("DCVAM_addOnRollbackData")
351 def rollbackData = execution.getVariable("rollbackData")
353 def addOnModuleMap = addOnRollbackData.get("VFMODULE")
354 numOfCreatedAddOnModules = numOfCreatedAddOnModules + 1
355 addOnModuleMap.each{ k, v -> rollbackData.put("VFMODULE_ADDON_" + numOfCreatedAddOnModules, "${k}","${v}") }
357 execution.setVariable("DCVAM_addOnRollbackData", null)
359 execution.setVariable("instancesOfThisModuleDeployed", instancesOfThisModuleDeployed + 1)
361 execution.setVariable("numOfCreatedAddOnModules", numOfCreatedAddOnModules)
362 rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "${numOfCreatedAddOnModules}")
363 execution.setVariable("rollbackData", rollbackData)
365 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing preProcessAddOnModule ", "BPMN", MsoLogger.getServiceName(),MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
366 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
368 msoLogger.trace("COMPLETED validateAddOnModule")
371 public void preProcessRollback (DelegateExecution execution) {
373 msoLogger.trace("preProcessRollback")
376 Object workflowException = execution.getVariable("WorkflowException");
378 if (workflowException instanceof WorkflowException) {
379 msoLogger.debug("Prev workflowException: " + workflowException.getErrorMessage())
380 execution.setVariable("prevWorkflowException", workflowException);
381 //execution.setVariable("WorkflowException", null);
383 } catch (BpmnError e) {
384 msoLogger.debug("BPMN Error during preProcessRollback")
385 } catch(Exception ex) {
386 String msg = "Exception in preProcessRollback. " + ex.getMessage()
389 msoLogger.trace("Exit preProcessRollback")
392 public void postProcessRollback (DelegateExecution execution) {
394 msoLogger.trace("postProcessRollback")
397 Object workflowException = execution.getVariable("prevWorkflowException");
398 if (workflowException instanceof WorkflowException) {
399 msoLogger.debug("Setting prevException to WorkflowException: ")
400 execution.setVariable("WorkflowException", workflowException);
402 execution.setVariable("rollbackData", null)
403 } catch (BpmnError b) {
404 msoLogger.debug("BPMN Error during postProcessRollback")
406 } catch(Exception ex) {
407 msg = "Exception in postProcessRollback. " + ex.getMessage()
410 msoLogger.trace("Exit postProcessRollback")
413 public void createPlatform (DelegateExecution execution) {
415 msoLogger.trace("START createPlatform")
417 String platformName = execution.getVariable("platformName")
418 String vnfId = execution.getVariable("vnfId")
420 msoLogger.debug("Platform NAME: " + platformName)
421 msoLogger.debug("VnfID: " + vnfId)
423 if(isBlank(platformName)){
424 msoLogger.debug("platformName was not found. Continuing on with flow...")
426 msoLogger.debug("platformName was found.")
428 AAICreateResources aaiCR = new AAICreateResources()
429 aaiCR.createAAIPlatform(platformName, vnfId)
430 }catch(Exception ex){
431 String msg = "Exception in createPlatform. " + ex.getMessage();
435 msoLogger.trace("Exit createPlatform")
438 public void createLineOfBusiness (DelegateExecution execution) {
440 msoLogger.trace("START createLineOfBusiness")
442 String lineOfBusiness = execution.getVariable("lineOfBusiness")
443 String vnfId = execution.getVariable("vnfId")
445 msoLogger.debug("LineOfBusiness NAME: " + lineOfBusiness)
446 msoLogger.debug("VnfID: " + vnfId)
448 if(isBlank(lineOfBusiness)){
449 msoLogger.debug("LineOfBusiness was not found. Continuing on with flow...")
451 msoLogger.debug("LineOfBusiness was found.")
453 AAICreateResources aaiCR = new AAICreateResources()
454 aaiCR.createAAILineOfBusiness(lineOfBusiness, vnfId)
455 }catch(Exception ex){
456 String msg = "Exception in LineOfBusiness. " + ex.getMessage();
460 msoLogger.trace("Exit createLineOfBusiness")