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;
24 import org.json.JSONObject;
25 import org.json.JSONArray;
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.runtime.Execution;
30 import static org.apache.commons.lang3.StringUtils.*;
32 import org.openecomp.mso.bpmn.core.json.JsonUtils
33 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
34 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils
35 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
36 import org.openecomp.mso.bpmn.common.scripts.VidUtils
37 import org.openecomp.mso.bpmn.core.RollbackData
38 import org.openecomp.mso.bpmn.core.WorkflowException
39 import org.openecomp.mso.bpmn.core.decomposition.ModelInfo
40 import org.openecomp.mso.bpmn.core.decomposition.ModuleResource
41 import org.openecomp.mso.bpmn.core.decomposition.ServiceDecomposition
42 import org.openecomp.mso.bpmn.core.decomposition.VnfResource
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 isDebugEnabled = execution.getVariable("isDebugLogEnabled")
66 execution.setVariable("prefix",Prefix)
67 utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules PreProcessRequest Process*** ", isDebugEnabled)
72 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
74 String vnfModelInfo = execution.getVariable("vnfModelInfo")
76 String requestId = execution.getVariable("msoRequestId")
77 execution.setVariable("requestId", requestId)
78 execution.setVariable("mso-request-id", requestId)
79 utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugEnabled)
81 String serviceInstanceId = execution.getVariable("serviceInstanceId")
82 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
84 String vnfType = execution.getVariable("vnfType")
85 utils.log("DEBUG", "Incoming Vnf Type is: " + vnfType, isDebugEnabled)
87 String vnfName = execution.getVariable("vnfName")
88 execution.setVariable("CREVI_vnfName", vnfName)
89 utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)
91 String productFamilyId = execution.getVariable("productFamilyId")
92 utils.log("DEBUG", "Incoming Product Family Id is: " + productFamilyId, isDebugEnabled)
95 execution.setVariable("source", source)
96 utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)
98 String lcpCloudRegionId = execution.getVariable("lcpCloudRegionId")
99 utils.log("DEBUG", "Incoming LCP Cloud Region Id is: " + lcpCloudRegionId)
101 String tenantId = execution.getVariable("tenantId")
102 utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId)
104 String disableRollback = execution.getVariable("disableRollback")
105 utils.log("DEBUG", "Incoming Disable Rollback is: " + disableRollback, isDebugEnabled)
107 String asdcServiceModelVersion = execution.getVariable("asdcServiceModelVersion")
108 utils.log("DEBUG", "Incoming asdcServiceModelVersion: " + asdcServiceModelVersion, isDebugEnabled)
110 String vnfId = execution.getVariable("testVnfId") // for junits
112 vnfId = execution.getVariable("vnfId")
113 if (isBlank(vnfId)) {
114 vnfId = UUID.randomUUID().toString()
115 utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)
118 execution.setVariable("vnfId", vnfId)
120 def rollbackData = execution.getVariable("RollbackData")
121 if (rollbackData == null) {
122 rollbackData = new RollbackData()
125 execution.setVariable("numOfCreatedAddOnModules", 0)
127 rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "0")
128 execution.setVariable("RollbackData", rollbackData)
134 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
137 utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugEnabled)
138 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
141 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules PreProcessRequest Process ***", isDebugEnabled)
145 public void queryCatalogDB (Execution execution) {
146 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
147 execution.setVariable("prefix",Prefix)
149 utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules QueryCatalogDB Process *** ", isDebugEnabled)
151 ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
152 // if serviceDecomposition is specified, get info from serviceDecomposition
153 if (serviceDecomposition != null) {
154 utils.log("DEBUG", "Getting Catalog DB data from ServiceDecomposition object: " + serviceDecomposition.toJsonString(), isDebugEnabled)
155 List<VnfResource> vnfs = serviceDecomposition.getServiceVnfs()
156 utils.log("DEBUG", "Read vnfs", isDebugEnabled)
158 utils.log("DEBUG", "Error - vnfs are empty in serviceDecomposition object", isDebugEnabled)
159 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnfs are empty")
161 VnfResource vnf = vnfs[0]
162 utils.log("DEBUG", "Read vnfResource", isDebugEnabled)
164 utils.log("DEBUG", "Error - vnf is empty in serviceDecomposition object", isDebugEnabled)
165 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnf is null")
168 List<ModuleResource> vfModules = vnf.getAllVfModuleObjects()
169 utils.log("DEBUG", "Read vfModules", isDebugEnabled)
170 if (vfModules == null) {
171 utils.log("DEBUG", "Error - vfModules are empty in serviceDecomposition object", isDebugEnabled)
172 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vf modules are empty")
174 JSONArray addOnModules = new JSONArray()
176 for (int i = 0; i < vfModules.size; i++) {
177 utils.log("DEBUG", "handling VF Module ", isDebugEnabled)
178 ModuleResource vfModule = vfModules[i]
179 boolean isBase = vfModule.getIsBase()
181 ModelInfo baseVfModuleModelInfoObject = vfModule.getModelInfo()
182 String baseVfModuleModelInfoWithRoot = baseVfModuleModelInfoObject.toString()
183 String baseVfModuleModelInfo = jsonUtil.getJsonValue(baseVfModuleModelInfoWithRoot, "modelInfo")
184 execution.setVariable("baseVfModuleModelInfo", baseVfModuleModelInfo)
185 String baseVfModuleLabel = vfModule.getVfModuleLabel()
186 execution.setVariable("baseVfModuleLabel", baseVfModuleLabel)
187 String basePersonaModelId = baseVfModuleModelInfoObject.getModelInvariantId()
188 execution.setVariable("basePersonaModelId", basePersonaModelId)
191 addOnModules.put(vfModules[i])
195 execution.setVariable("addOnModules", addOnModules)
196 execution.setVariable("addOnModulesToDeploy", addOnModules.length())
197 execution.setVariable("addOnModulesDeployed", 0)
202 String vnfModelInfo = execution.getVariable("vnfModelInfo")
203 utils.log("DEBUG", "vnfModelInfo: " + vnfModelInfo, isDebugEnabled)
204 String vnfModelCustomizationUuid = jsonUtil.getJsonValueForKey(vnfModelInfo, "modelCustomizationId")
205 if (vnfModelCustomizationUuid == null) {
206 vnfModelCustomizationUuid = jsonUtil.getJsonValueForKey(vnfModelInfo, "modelCustomizationUuid")
208 utils.log("DEBUG", "querying Catalog DB by vnfModelCustomizationUuid: " + vnfModelCustomizationUuid, isDebugEnabled)
209 String catalogDbEndpoint = execution.getVariable("URN_mso_catalog_db_endpoint")
211 JSONArray vnfs = cutils.getAllVnfsByVnfModelCustomizationUuid(catalogDbEndpoint,
212 vnfModelCustomizationUuid)
213 utils.log("DEBUG", "obtained VNF list")
214 // Only one match here
215 JSONObject vnf = vnfs[0]
216 JSONArray vfModules = vnf.getJSONArray("vfModules")
217 JSONArray addOnModules = new JSONArray()
219 // Set up base Vf Module info
220 for (int i = 0; i < vfModules.length(); i++) {
221 utils.log("DEBUG", "handling VF Module ")
222 JSONObject vfModule = vfModules[i]
223 String isBase = jsonUtil.getJsonValueForKey(vfModule, "isBase")
224 if (isBase.equals("true")) {
225 JSONObject baseVfModuleModelInfoObject = vfModule.getJSONObject("modelInfo")
226 String baseVfModuleModelInfo = baseVfModuleModelInfoObject.toString()
227 execution.setVariable("baseVfModuleModelInfo", baseVfModuleModelInfo)
228 String baseVfModuleLabel = jsonUtil.getJsonValueForKey(vfModule, "vfModuleLabel")
229 execution.setVariable("baseVfModuleLabel", baseVfModuleLabel)
230 String basePersonaModelId = jsonUtil.getJsonValueForKey(baseVfModuleModelInfoObject, "modelInvariantId")
231 execution.setVariable("basePersonaModelId", basePersonaModelId)
234 addOnModules.put(vfModules[i])
238 execution.setVariable("addOnModules", addOnModules)
239 execution.setVariable("addOnModulesToDeploy", addOnModules.length())
240 execution.setVariable("addOnModulesDeployed", 0)
243 }catch(Exception ex) {
244 utils.log("DEBUG", "Error Occured in DoCreateVnfAndModules QueryCatalogDB Process " + ex.getMessage(), isDebugEnabled)
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 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra PrepareCreateGenericVnf Process ***", isDebugEnabled)
260 public void preProcessAddOnModule(Execution execution){
261 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
262 execution.setVariable("prefix", Prefix)
263 logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)
266 JSONArray addOnModules = (JSONArray) execution.getVariable("addOnModules")
267 int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")
269 JSONObject addOnModule = addOnModules[addOnIndex]
271 def newVfModuleId = UUID.randomUUID().toString()
272 execution.setVariable("addOnVfModuleId", newVfModuleId)
274 execution.setVariable("instancesOfThisModelDeployed", 0)
276 JSONObject addOnVfModuleModelInfoObject = jsonUtil.getJsonValueForKey(addOnModule, "modelInfo")
277 String addOnVfModuleModelInfoWithRoot = addOnVfModuleModelInfoObject.toString()
278 String addOnVfModuleModelInfo = jsonUtil.getJsonValue(addOnVfModuleModelInfoWithRoot, "modelInfo")
279 execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)
280 String addOnVfModuleLabel = jsonUtil.getJsonValueForKey(addOnModule, "vfModuleLabel")
281 execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)
282 String addOnPersonaModelId = jsonUtil.getJsonValueForKey(addOnVfModuleModelInfoObject, "modelInvariantId")
283 execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)
284 String addOnInitialCount = jsonUtil.getJsonValueForKey(addOnModule, "initialCount")
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 preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
295 public void validateBaseModule(Execution execution){
296 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
297 execution.setVariable("prefix", Prefix)
298 logDebug(" ======== STARTED validateBaseModule ======== ", isDebugLogEnabled)
301 def baseRollbackData = execution.getVariable("DCVAM_baseRollbackData")
302 def rollbackData = execution.getVariable("RollbackData")
304 def baseModuleMap = baseRollbackData.get("VFMODULE")
305 baseModuleMap.each{ k, v -> rollbackData.put("VFMODULE_BASE", "${k}","${v}") }
306 execution.setVariable("RollbackData", rollbackData)
307 logDebug("addOnModulesDeployed: " + execution.getVariable("addOnModulesDeployed"), isDebugLogEnabled)
308 logDebug("addOnModulesToDeploy: " + execution.getVariable("addOnModulesToDeploy"), isDebugLogEnabled)
309 if (execution.getVariable("addOnModulesDeployed") < execution.getVariable("addOnModulesToDeploy")) {
310 logDebug("More add on modules to deploy", isDebugLogEnabled)
313 logDebug("No more add on modules to deploy", isDebugLogEnabled)
317 utils.log("ERROR", "Exception Occured Processing validateBaseModule. Exception is:\n" + e, isDebugLogEnabled)
318 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during validateBaseModule Method:\n" + e.getMessage())
320 logDebug("======== COMPLETED validateBaseModule ======== ", isDebugLogEnabled)
323 public void validateAddOnModule(Execution execution){
324 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
325 execution.setVariable("prefix", Prefix)
326 logDebug(" ======== STARTED validateAddOnModule ======== ", isDebugLogEnabled)
329 int instancesOfThisModuleDeployed = execution.getVariable("instancesOfThisModuleDeployed")
330 int numOfCreatedAddOnModules = execution.getVariable("numOfCreatedAddOnModules")
331 def addOnRollbackData = execution.getVariable("DCVAM_addOnRollbackData")
332 def rollbackData = execution.getVariable("RollbackData")
334 def addOnModuleMap = addOnRollbackData.get("VFMODULE")
335 numOfCreatedAddOnModules = numOfCreatedAddOnModules + 1
336 addOnModuleMap.each{ k, v -> rollbackData.put("VFMODULE_ADDON_" + numOfCreatedAddOnModules, "${k}","${v}") }
338 execution.setVariable("DCVAM_addOnRollbackData", null)
340 execution.setVariable("instancesOfThisModuleDeployed", instancesOfThisModuleDeployed + 1)
342 execution.setVariable("numOfCreatedAddOnModules", numOfCreatedAddOnModules)
343 rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "${numOfCreatedAddOnModules}")
344 execution.setVariable("RollbackData", rollbackData)
346 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
347 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
349 logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
352 public void finishProcessingInitialCountDeployment(Execution execution){
353 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
354 execution.setVariable("prefix", Prefix)
355 logDebug(" ======== STARTED finishProcessingInitialCountDeployment ======== ", isDebugLogEnabled)
358 int addOnModulesDeployed = execution.getVariable("addOnModulesDeployed")
359 execution.setVariable("addOnModulesDeployed", addOnModulesDeployed + 1)
361 utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
362 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
364 logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)