aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOInfrastructureBPMN/src/main/groovy/org/openecomp/mso/bpmn/infrastructure/scripts/DoCreateVnfAndModules.groovy
blob: 862c46ad19485002370a1e3b05b9cc6d9976d717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.mso.bpmn.infrastructure.scripts

import java.util.UUID;
import java.util.List

import org.json.JSONObject;
import org.json.JSONArray;
import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils
import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
import org.openecomp.mso.bpmn.common.scripts.VidUtils
import org.openecomp.mso.bpmn.core.RollbackData
import org.openecomp.mso.bpmn.core.WorkflowException
import org.openecomp.mso.bpmn.core.domain.ModelInfo
import org.openecomp.mso.bpmn.core.domain.ModuleResource
import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
import org.openecomp.mso.bpmn.core.domain.VnfResource
import org.openecomp.mso.bpmn.core.json.DecomposeJsonUtil
import org.openecomp.mso.bpmn.core.json.JsonUtils
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.runtime.Execution;

import static org.apache.commons.lang3.StringUtils.*;



/**
* This class supports the macro VID Flow
* with the creation of a generic vnf and related VF modules.
*/
class DoCreateVnfAndModules extends AbstractServiceTaskProcessor {

   String Prefix="DCVAM_"
   ExceptionUtil exceptionUtil = new ExceptionUtil()
   JsonUtils jsonUtil = new JsonUtils()
   VidUtils vidUtils = new VidUtils(this)
   CatalogDbUtils cutils = new CatalogDbUtils()
   DecomposeJsonUtil decomposeJsonUtil = new DecomposeJsonUtil()

   /**
	* This method gets and validates the incoming
	* request.
	*
	* @param - execution
	*/
   public void preProcessRequest(Execution execution) {
	   def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
	   execution.setVariable("prefix",Prefix)
	   utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules PreProcessRequest Process*** ", isDebugLogEnabled)
	   
	   setBasicDBAuthHeader(execution, isDebugLogEnabled)
	   try{
		   // Get Variables

		   ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")

		   String vnfModelInfo = execution.getVariable("vnfModelInfo")

		   String requestId = execution.getVariable("msoRequestId")
		   execution.setVariable("requestId", requestId)
		   execution.setVariable("mso-request-id", requestId)
		   utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugLogEnabled)

		   String serviceInstanceId = execution.getVariable("serviceInstanceId")
		   utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugLogEnabled)

		   String vnfName = execution.getVariable("vnfName")
		   execution.setVariable("CREVI_vnfName", vnfName)
		   utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugLogEnabled)

		   String productFamilyId = execution.getVariable("productFamilyId")
		   utils.log("DEBUG", "Incoming Product Family Id is: " + productFamilyId, isDebugLogEnabled)

		   String source = "VID"
		   execution.setVariable("source", source)
		   utils.log("DEBUG", "Incoming Source is: " + source, isDebugLogEnabled)

		   String lcpCloudRegionId = execution.getVariable("lcpCloudRegionId")
		   utils.log("DEBUG", "Incoming LCP Cloud Region Id is: " + lcpCloudRegionId)

		   String tenantId = execution.getVariable("tenantId")
		   utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId)

		   String disableRollback = execution.getVariable("disableRollback")
		   utils.log("DEBUG", "Incoming Disable Rollback is: " + disableRollback, isDebugLogEnabled)

		   String asdcServiceModelVersion = execution.getVariable("asdcServiceModelVersion")
		   utils.log("DEBUG", "Incoming asdcServiceModelVersion: " + asdcServiceModelVersion, isDebugLogEnabled)

		   String vnfId = execution.getVariable("testVnfId") // for junits
		   if(isBlank(vnfId)){
			   vnfId = execution.getVariable("vnfId")
			   if (isBlank(vnfId)) {
				   vnfId = UUID.randomUUID().toString()
				   utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugLogEnabled)
			   }
		   }
		   execution.setVariable("vnfId", vnfId)

		   def rollbackData = execution.getVariable("rollbackData")
		   if (rollbackData == null) {
			   rollbackData = new RollbackData()
		   }
		   
		   def isTest = execution.getVariable("isTest")
		   
			if (isTest == null || isTest == false) {
				execution.setVariable("isBaseVfModule", "true")
			}
		   execution.setVariable("numOfCreatedAddOnModules", 0)

		   rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "0")
		   execution.setVariable("rollbackData", rollbackData)

		   sleep (20000)


	   }catch(BpmnError b){
		   utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugLogEnabled)
		   throw b
	   }catch(Exception e){
		   utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugLogEnabled)
		   exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")

	   }
	   utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules PreProcessRequest Process ***", isDebugLogEnabled)
   }


   public void queryCatalogDB (Execution execution) {
	   def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
	   execution.setVariable("prefix",Prefix)

	   utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules QueryCatalogDB Process *** ", isDebugLogEnabled)
	   try {
		   VnfResource vnf = null
		   ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
		   // if serviceDecomposition is specified, get info from serviceDecomposition
		   if (serviceDecomposition != null) {
			   utils.log("DEBUG", "Getting Catalog DB data from ServiceDecomposition object: " + serviceDecomposition.toJsonString(), isDebugLogEnabled)
			   List<VnfResource> vnfs = serviceDecomposition.getServiceVnfs()
			   utils.log("DEBUG", "Read vnfs", isDebugLogEnabled)
			   if (vnfs == null) {
				   utils.log("DEBUG", "Error - vnfs are empty in serviceDecomposition object", isDebugLogEnabled)
				   exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnfs are empty")
			   }
			   vnf = vnfs[0]
			   String serviceModelName = serviceDecomposition.getModelInfo().getModelName()
			   vnf.constructVnfType(serviceModelName)
			   String vnfType = vnf.getVnfType()
			   utils.log("DEBUG", "Incoming Vnf Type is: " + vnfType, isDebugLogEnabled)
			   execution.setVariable("vnfType", vnfType)
		   }
		   else {
			   //Get Vnf Info
			   String vnfModelInfo = execution.getVariable("vnfModelInfo")
			   utils.log("DEBUG", "vnfModelInfo: " + vnfModelInfo, isDebugLogEnabled)
			   String vnfModelCustomizationUuid = jsonUtil.getJsonValueForKey(vnfModelInfo, "modelCustomizationUuid")
			   if (vnfModelCustomizationUuid == null) {
					   vnfModelCustomizationUuid = ""
			   }
			   utils.log("DEBUG", "querying Catalog DB by vnfModelCustomizationUuid: " + vnfModelCustomizationUuid, isDebugLogEnabled)
			  
			   JSONArray vnfs = cutils.getAllVnfsByVnfModelCustomizationUuid(execution,
							   vnfModelCustomizationUuid)
			   utils.log("DEBUG", "obtained VNF list")
			   // Only one match here
			   JSONObject vnfObject = vnfs[0]
			   vnf = decomposeJsonUtil.JsonToVnfResource(vnfObject.toString())			   
		   }
		   utils.log("DEBUG", "Read vnfResource", isDebugLogEnabled)
		   if (vnf == null) {
			   utils.log("DEBUG", "Error - vnf is empty in serviceDecomposition object", isDebugLogEnabled)
			   exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnf is null")
		   }
		   execution.setVariable("vnfResourceDecomposition", vnf)

		   List<ModuleResource> vfModules = vnf.getAllVfModuleObjects()
		   utils.log("DEBUG", "Read vfModules", isDebugLogEnabled)
		   if (vfModules == null) {
			   utils.log("DEBUG", "Error - vfModules are empty in serviceDecomposition object", isDebugLogEnabled)
			   exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vf modules are empty")
		   }
			   			  
		   ModuleResource baseVfModule = null

		   for (int i = 0; i < vfModules.size; i++) {
			   utils.log("DEBUG", "handling VF Module ", isDebugLogEnabled)
			   ModuleResource vfModule = vfModules[i]
			   boolean isBase = vfModule.getIsBase()
			   if (isBase) {
					   ModelInfo baseVfModuleModelInfoObject = vfModule.getModelInfo()
					   String baseVfModuleModelInfoWithRoot = baseVfModuleModelInfoObject.toString()
					   String baseVfModuleModelInfo = jsonUtil.getJsonValue(baseVfModuleModelInfoWithRoot, "modelInfo")
					   execution.setVariable("baseVfModuleModelInfo", baseVfModuleModelInfo)
					   String baseVfModuleLabel = vfModule.getVfModuleLabel()
					   execution.setVariable("baseVfModuleLabel", baseVfModuleLabel)
					   String basePersonaModelId = baseVfModuleModelInfoObject.getModelInvariantUuid()
					   execution.setVariable("basePersonaModelId", basePersonaModelId)
					   baseVfModule = vfModule
					   break
			   }		   
				
			}
			   
			List<ModuleResource>addOnModules = vfModules - baseVfModule
			   
			int addOnModulesToDeploy = 0
			if (addOnModules != null) {				
				   addOnModulesToDeploy = addOnModules.size
			}
			   
			utils.log("DEBUG", "AddOnModulesToDeploy: " + addOnModulesToDeploy)

			execution.setVariable("addOnModules", addOnModules)
			execution.setVariable("addOnModulesToDeploy", addOnModulesToDeploy)
			execution.setVariable("addOnModulesDeployed", 0)	  

	   }catch(Exception ex) {
		   utils.log("DEBUG", "Error Occured in DoCreateVnfAndModules QueryCatalogDB Process " + ex.getMessage(), isDebugLogEnabled)
		   exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModules QueryCatalogDB Process")
	   }

	   // Generate vfModuleId for base VF Module
	   def baseVfModuleId = UUID.randomUUID().toString()
	   execution.setVariable("baseVfModuleId", baseVfModuleId)
	   // For JUnits
	   String requestId = execution.getVariable("requestId")
	   if (requestId.equals("testRequestId123")) {
		   execution.setVariable("vnfId", "skask")
	   }

	   utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules QueryCatalogDB Process ***", isDebugLogEnabled)
   }

   public void preProcessAddOnModule(Execution execution){
	   def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
	   execution.setVariable("prefix", Prefix)
	   logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)

	   try {
		   List<ModuleResource>addOnModules = execution.getVariable("addOnModules")
		   int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")

		   ModuleResource addOnModule = addOnModules[addOnIndex]
		   
		   utils.log("DEBUG", "Got addon module", isDebugLogEnabled)

		   def newVfModuleId = UUID.randomUUID().toString()
		   execution.setVariable("addOnVfModuleId", newVfModuleId)
		   execution.setVariable("isBaseVfModule", "false")
		   
		   execution.setVariable("instancesOfThisModuleDeployed", 0)

		   ModelInfo addOnVfModuleModelInfoObject = addOnModule.getModelInfo()		  
		   String addOnVfModuleModelInfoWithRoot = addOnVfModuleModelInfoObject.toString()
		   String addOnVfModuleModelInfo = jsonUtil.getJsonValue(addOnVfModuleModelInfoWithRoot, "modelInfo")
		   execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)
		   String addOnVfModuleLabel = addOnModule.getVfModuleLabel()
		   execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)
		   String addOnPersonaModelId = addOnVfModuleModelInfoObject.getModelInvariantUuid()
		   execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)
		   int addOnInitialCount = addOnModule.getInitialCount()
		   execution.setVariable("initialCount", addOnInitialCount)


	   }catch(Exception e){
		   utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
		   exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
	   }
	   logDebug("======== COMPLETED preProcessAddOnModule ======== ", isDebugLogEnabled)
   }

   public void postProcessAddOnModule(Execution execution){
	   def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
	   execution.setVariable("prefix", Prefix)
	   logDebug(" ======== STARTED postProcessAddOnModule ======== ", isDebugLogEnabled)

	   try {
		   int addOnModulesDeployed = execution.getVariable("addOnModulesDeployed")
		   execution.setVariable("addOnModulesDeployed", addOnModulesDeployed + 1)

	   }catch(Exception e){
		   utils.log("ERROR", "Exception Occured Processing postProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
		   exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during postProcessAddOnModule Method:\n" + e.getMessage())
	   }
	   logDebug("======== COMPLETED postProcessAddOnModule ======== ", isDebugLogEnabled)
   }
   
   public void validateBaseModule(Execution execution){
	   def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
	   execution.setVariable("prefix", Prefix)
	   logDebug(" ======== STARTED validateBaseModule ======== ", isDebugLogEnabled)

	   try {
		   def baseRollbackData = execution.getVariable("DCVAM_baseRollbackData")
		   def rollbackData = execution.getVariable("rollbackData")

		   def baseModuleMap = baseRollbackData.get("VFMODULE")
		   baseModuleMap.each{ k, v -> rollbackData.put("VFMODULE_BASE", "${k}","${v}") }
		   execution.setVariable("rollbackData", rollbackData)
		   logDebug("addOnModulesDeployed: " + execution.getVariable("addOnModulesDeployed"), isDebugLogEnabled)
		   logDebug("addOnModulesToDeploy: " + execution.getVariable("addOnModulesToDeploy"), isDebugLogEnabled)
		   if (execution.getVariable("addOnModulesDeployed") <  execution.getVariable("addOnModulesToDeploy")) {
			   logDebug("More add on modules to deploy", isDebugLogEnabled)
		   }
		   else {
			   logDebug("No more add on modules to deploy", isDebugLogEnabled)
		   }

	   }catch(Exception e){
		   utils.log("ERROR", "Exception Occured Processing validateBaseModule. Exception is:\n" + e, isDebugLogEnabled)
		   exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during validateBaseModule Method:\n" + e.getMessage())
	   }
	   logDebug("======== COMPLETED validateBaseModule ======== ", isDebugLogEnabled)
   }

   public void validateAddOnModule(Execution execution){
	   def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
	   execution.setVariable("prefix", Prefix)
	   logDebug(" ======== STARTED validateAddOnModule ======== ", isDebugLogEnabled)

	   try {
		   int instancesOfThisModuleDeployed = execution.getVariable("instancesOfThisModuleDeployed")
		   int numOfCreatedAddOnModules = execution.getVariable("numOfCreatedAddOnModules")
		   def addOnRollbackData = execution.getVariable("DCVAM_addOnRollbackData")
		   def rollbackData = execution.getVariable("rollbackData")

		   def addOnModuleMap = addOnRollbackData.get("VFMODULE")
		   numOfCreatedAddOnModules = numOfCreatedAddOnModules + 1
		   addOnModuleMap.each{ k, v -> rollbackData.put("VFMODULE_ADDON_" + numOfCreatedAddOnModules, "${k}","${v}") }

		   execution.setVariable("DCVAM_addOnRollbackData", null)

		   execution.setVariable("instancesOfThisModuleDeployed", instancesOfThisModuleDeployed + 1)

		   execution.setVariable("numOfCreatedAddOnModules", numOfCreatedAddOnModules)
		   rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "${numOfCreatedAddOnModules}")
		   execution.setVariable("rollbackData", rollbackData)
	   }catch(Exception e){
		   utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
		   exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
	   }
	   logDebug("======== COMPLETED validateAddOnModule ======== ", isDebugLogEnabled)
   }   
   
   public void preProcessRollback (Execution execution) {
	   def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
	   utils.log("DEBUG"," ***** preProcessRollback ***** ", isDebugLogEnabled)
	   try {
		   
		   Object workflowException = execution.getVariable("WorkflowException");

		   if (workflowException instanceof WorkflowException) {
			   utils.log("DEBUG", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugLogEnabled)
			   execution.setVariable("prevWorkflowException", workflowException);
			   //execution.setVariable("WorkflowException", null);
		   }
	   } catch (BpmnError e) {
		   utils.log("DEBUG", "BPMN Error during preProcessRollback", isDebugLogEnabled)
	   } catch(Exception ex) {
		   String msg = "Exception in preProcessRollback. " + ex.getMessage()
		   utils.log("DEBUG", msg, isDebugLogEnabled)
	   }
	   utils.log("DEBUG"," *** Exit preProcessRollback *** ", isDebugLogEnabled)
   }

   public void postProcessRollback (Execution execution) {
	   def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
	   utils.log("DEBUG"," ***** postProcessRollback ***** ", isDebugLogEnabled)
	   String msg = ""
	   try {
		   Object workflowException = execution.getVariable("prevWorkflowException");
		   if (workflowException instanceof WorkflowException) {
			   utils.log("DEBUG", "Setting prevException to WorkflowException: ", isDebugLogEnabled)
			   execution.setVariable("WorkflowException", workflowException);
		   }
		   execution.setVariable("rollbackData", null)
	   } catch (BpmnError b) {
		   utils.log("DEBUG", "BPMN Error during postProcessRollback", isDebugLogEnabled)
		   throw b;
	   } catch(Exception ex) {
		   msg = "Exception in postProcessRollback. " + ex.getMessage()
		   utils.log("DEBUG", msg, isDebugLogEnabled)
	   }
	   utils.log("DEBUG"," *** Exit postProcessRollback *** ", isDebugLogEnabled)
   }


}