Groovy scripts header correction
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoCreateVnfAndModules.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.openecomp.mso.bpmn.infrastructure.scripts
21
22 import java.util.UUID;
23 import java.util.List
24
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;
41
42 import static org.apache.commons.lang3.StringUtils.*;
43
44
45
46 /**
47 * This class supports the macro VID Flow
48 * with the creation of a generic vnf and related VF modules.
49 */
50 class DoCreateVnfAndModules extends AbstractServiceTaskProcessor {
51
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()
58
59    /**
60         * This method gets and validates the incoming
61         * request.
62         *
63         * @param - execution
64         */
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)
69            
70            setBasicDBAuthHeader(execution, isDebugLogEnabled)
71            try{
72                    // Get Variables
73
74                    ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
75
76                    String vnfModelInfo = execution.getVariable("vnfModelInfo")
77
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)
82
83                    String serviceInstanceId = execution.getVariable("serviceInstanceId")
84                    utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
85
86                    String vnfName = execution.getVariable("vnfName")
87                    execution.setVariable("CREVI_vnfName", vnfName)
88                    utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)
89
90                    String productFamilyId = execution.getVariable("productFamilyId")
91                    utils.log("DEBUG", "Incoming Product Family Id is: " + productFamilyId, isDebugEnabled)
92
93                    String source = "VID"
94                    execution.setVariable("source", source)
95                    utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)
96
97                    String lcpCloudRegionId = execution.getVariable("lcpCloudRegionId")
98                    utils.log("DEBUG", "Incoming LCP Cloud Region Id is: " + lcpCloudRegionId)
99
100                    String tenantId = execution.getVariable("tenantId")
101                    utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId)
102
103                    String disableRollback = execution.getVariable("disableRollback")
104                    utils.log("DEBUG", "Incoming Disable Rollback is: " + disableRollback, isDebugEnabled)
105
106                    String asdcServiceModelVersion = execution.getVariable("asdcServiceModelVersion")
107                    utils.log("DEBUG", "Incoming asdcServiceModelVersion: " + asdcServiceModelVersion, isDebugEnabled)
108
109                    String vnfId = execution.getVariable("testVnfId") // for junits
110                    if(isBlank(vnfId)){
111                            vnfId = execution.getVariable("vnfId")
112                            if (isBlank(vnfId)) {
113                                    vnfId = UUID.randomUUID().toString()
114                                    utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)
115                            }
116                    }
117                    execution.setVariable("vnfId", vnfId)
118
119                    def rollbackData = execution.getVariable("rollbackData")
120                    if (rollbackData == null) {
121                            rollbackData = new RollbackData()
122                    }
123                    
124                    def isTest = execution.getVariable("isTest")
125                    
126                         if (isTest == null || isTest == false) {
127                                 execution.setVariable("isBaseVfModule", "true")
128                         }
129                    execution.setVariable("numOfCreatedAddOnModules", 0)
130
131                    rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "0")
132                    execution.setVariable("rollbackData", rollbackData)
133
134                    sleep (20000)
135
136
137            }catch(BpmnError b){
138                    utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
139                    throw b
140            }catch(Exception e){
141                    utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugEnabled)
142                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
143
144            }
145            utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules PreProcessRequest Process ***", isDebugEnabled)
146    }
147
148
149    public void queryCatalogDB (Execution execution) {
150            def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
151            execution.setVariable("prefix",Prefix)
152
153            utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules QueryCatalogDB Process *** ", isDebugEnabled)
154            try {
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)
162                            if (vnfs == null) {
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")
165                            }
166                            vnf = vnfs[0]
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)
172                    }
173                    else {
174                            //Get Vnf Info
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 = ""
180                            }
181                            utils.log("DEBUG", "querying Catalog DB by vnfModelCustomizationUuid: " + vnfModelCustomizationUuid, isDebugEnabled)
182                           
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())                         
189                    }
190                    utils.log("DEBUG", "Read vnfResource", isDebugEnabled)
191                    if (vnf == null) {
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")
194                    }
195                    execution.setVariable("vnfResourceDecomposition", vnf)
196
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")
202                    }
203                                                   
204                    ModuleResource baseVfModule = null
205
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()
210                            if (isBase) {
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
220                                            break
221                            }               
222                                 
223                         }
224                            
225                         List<ModuleResource>addOnModules = vfModules - baseVfModule
226                            
227                         int addOnModulesToDeploy = 0
228                         if (addOnModules != null) {                             
229                                    addOnModulesToDeploy = addOnModules.size
230                         }
231                            
232                         utils.log("DEBUG", "AddOnModulesToDeploy: " + addOnModulesToDeploy)
233
234                         execution.setVariable("addOnModules", addOnModules)
235                         execution.setVariable("addOnModulesToDeploy", addOnModulesToDeploy)
236                         execution.setVariable("addOnModulesDeployed", 0)          
237
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")
241            }
242
243            // Generate vfModuleId for base VF Module
244            def baseVfModuleId = UUID.randomUUID().toString()
245            execution.setVariable("baseVfModuleId", baseVfModuleId)
246            // For JUnits
247            String requestId = execution.getVariable("requestId")
248            if (requestId.equals("testRequestId123")) {
249                    execution.setVariable("vnfId", "skask")
250            }
251
252            utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules QueryCatalogDB Process ***", isDebugEnabled)
253    }
254
255    public void preProcessAddOnModule(Execution execution){
256            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
257            execution.setVariable("prefix", Prefix)
258            logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)
259
260            try {
261                    List<ModuleResource>addOnModules = execution.getVariable("addOnModules")
262                    int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")
263
264                    ModuleResource addOnModule = addOnModules[addOnIndex]
265                    
266                    utils.log("DEBUG", "Got addon module", isDebugLogEnabled)
267
268                    def newVfModuleId = UUID.randomUUID().toString()
269                    execution.setVariable("addOnVfModuleId", newVfModuleId)
270                    execution.setVariable("isBaseVfModule", "false")
271                    
272                    execution.setVariable("instancesOfThisModuleDeployed", 0)
273
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)
284
285
286            }catch(Exception e){
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())
289            }
290            logDebug("======== COMPLETED preProcessAddOnModule ======== ", isDebugLogEnabled)
291    }
292
293    public void postProcessAddOnModule(Execution execution){
294            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
295            execution.setVariable("prefix", Prefix)
296            logDebug(" ======== STARTED postProcessAddOnModule ======== ", isDebugLogEnabled)
297
298            try {
299                    int addOnModulesDeployed = execution.getVariable("addOnModulesDeployed")
300                    execution.setVariable("addOnModulesDeployed", addOnModulesDeployed + 1)
301
302            }catch(Exception e){
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())
305            }
306            logDebug("======== COMPLETED postProcessAddOnModule ======== ", isDebugLogEnabled)
307    }
308    
309    public void validateBaseModule(Execution execution){
310            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
311            execution.setVariable("prefix", Prefix)
312            logDebug(" ======== STARTED validateBaseModule ======== ", isDebugLogEnabled)
313
314            try {
315                    def baseRollbackData = execution.getVariable("DCVAM_baseRollbackData")
316                    def rollbackData = execution.getVariable("rollbackData")
317
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)
325                    }
326                    else {
327                            logDebug("No more add on modules to deploy", isDebugLogEnabled)
328                    }
329
330            }catch(Exception e){
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())
333            }
334            logDebug("======== COMPLETED validateBaseModule ======== ", isDebugLogEnabled)
335    }
336
337    public void validateAddOnModule(Execution execution){
338            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
339            execution.setVariable("prefix", Prefix)
340            logDebug(" ======== STARTED validateAddOnModule ======== ", isDebugLogEnabled)
341
342            try {
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")
347
348                    def addOnModuleMap = addOnRollbackData.get("VFMODULE")
349                    numOfCreatedAddOnModules = numOfCreatedAddOnModules + 1
350                    addOnModuleMap.each{ k, v -> rollbackData.put("VFMODULE_ADDON_" + numOfCreatedAddOnModules, "${k}","${v}") }
351
352                    execution.setVariable("DCVAM_addOnRollbackData", null)
353
354                    execution.setVariable("instancesOfThisModuleDeployed", instancesOfThisModuleDeployed + 1)
355
356                    execution.setVariable("numOfCreatedAddOnModules", numOfCreatedAddOnModules)
357                    rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "${numOfCreatedAddOnModules}")
358                    execution.setVariable("rollbackData", rollbackData)
359            }catch(Exception e){
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())
362            }
363            logDebug("======== COMPLETED validateAddOnModule ======== ", isDebugLogEnabled)
364    }   
365    
366    public void preProcessRollback (Execution execution) {
367            def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
368            utils.log("DEBUG"," ***** preProcessRollback ***** ", isDebugEnabled)
369            try {
370                    
371                    Object workflowException = execution.getVariable("WorkflowException");
372
373                    if (workflowException instanceof WorkflowException) {
374                            utils.log("DEBUG", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugEnabled)
375                            execution.setVariable("prevWorkflowException", workflowException);
376                            //execution.setVariable("WorkflowException", null);
377                    }
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)
383            }
384            utils.log("DEBUG"," *** Exit preProcessRollback *** ", isDebugEnabled)
385    }
386
387    public void postProcessRollback (Execution execution) {
388            def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
389            utils.log("DEBUG"," ***** postProcessRollback ***** ", isDebugEnabled)
390            String msg = ""
391            try {
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);
396                    }
397                    execution.setVariable("rollbackData", null)
398            } catch (BpmnError b) {
399                    utils.log("DEBUG", "BPMN Error during postProcessRollback", isDebugEnabled)
400                    throw b;
401            } catch(Exception ex) {
402                    msg = "Exception in postProcessRollback. " + ex.getMessage()
403                    utils.log("DEBUG", msg, isDebugEnabled)
404            }
405            utils.log("DEBUG"," *** Exit postProcessRollback *** ", isDebugEnabled)
406    }
407
408
409 }