AT&T 1712 and 1802 release code
[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.delegate.DelegateExecution;
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
58    /**
59         * This method gets and validates the incoming
60         * request.
61         *
62         * @param - execution
63         */
64    public void preProcessRequest(DelegateExecution execution) {
65            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
66            execution.setVariable("prefix",Prefix)
67            utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules PreProcessRequest Process*** ", isDebugLogEnabled)
68            
69            setBasicDBAuthHeader(execution, isDebugLogEnabled)
70            try{
71                    // Get Variables
72
73                    ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
74
75                    String vnfModelInfo = execution.getVariable("vnfModelInfo")
76
77                    String requestId = execution.getVariable("msoRequestId")
78                    execution.setVariable("requestId", requestId)
79                    execution.setVariable("mso-request-id", requestId)
80                    utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugLogEnabled)
81
82                    String serviceInstanceId = execution.getVariable("serviceInstanceId")
83                    utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugLogEnabled)
84
85                    String vnfName = execution.getVariable("vnfName")
86                    execution.setVariable("CREVI_vnfName", vnfName)
87                    utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugLogEnabled)
88
89                    String productFamilyId = execution.getVariable("productFamilyId")
90                    utils.log("DEBUG", "Incoming Product Family Id is: " + productFamilyId, isDebugLogEnabled)
91
92                    String source = "VID"
93                    execution.setVariable("source", source)
94                    utils.log("DEBUG", "Incoming Source is: " + source, isDebugLogEnabled)
95
96                    String lcpCloudRegionId = execution.getVariable("lcpCloudRegionId")
97                    utils.log("DEBUG", "Incoming LCP Cloud Region Id is: " + lcpCloudRegionId)
98
99                    String tenantId = execution.getVariable("tenantId")
100                    utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId)
101
102                    String disableRollback = execution.getVariable("disableRollback")
103                    utils.log("DEBUG", "Incoming Disable Rollback is: " + disableRollback, isDebugLogEnabled)
104
105                    String asdcServiceModelVersion = execution.getVariable("asdcServiceModelVersion")
106                    utils.log("DEBUG", "Incoming asdcServiceModelVersion: " + asdcServiceModelVersion, isDebugLogEnabled)
107
108                    String vnfId = execution.getVariable("testVnfId") // for junits
109                    if(isBlank(vnfId)){
110                            vnfId = execution.getVariable("vnfId")
111                            if (isBlank(vnfId)) {
112                                    vnfId = UUID.randomUUID().toString()
113                                    utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugLogEnabled)
114                            }
115                    }
116                    execution.setVariable("vnfId", vnfId)
117                    
118                    // Set aLaCarte to false
119                    execution.setVariable("aLaCarte", false)
120
121                    def rollbackData = execution.getVariable("rollbackData")
122                    if (rollbackData == null) {
123                            rollbackData = new RollbackData()
124                    }
125                    
126                    def isTest = execution.getVariable("isTest")
127                    
128                         if (isTest == null || isTest == false) {
129                                 execution.setVariable("isBaseVfModule", "true")
130                         }
131                    execution.setVariable("numOfCreatedAddOnModules", 0)
132
133                    rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "0")
134                    execution.setVariable("rollbackData", rollbackData)
135
136                    sleep (20000)
137
138
139            }catch(BpmnError b){
140                    utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugLogEnabled)
141                    throw b
142            }catch(Exception e){
143                    utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugLogEnabled)
144                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
145
146            }
147            utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules PreProcessRequest Process ***", isDebugLogEnabled)
148    }
149
150
151    public void queryCatalogDB (DelegateExecution execution) {
152            def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
153            execution.setVariable("prefix",Prefix)
154
155            utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules QueryCatalogDB Process *** ", isDebugLogEnabled)
156            try {
157                    VnfResource vnf = null
158                    ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
159                    // if serviceDecomposition is specified, get info from serviceDecomposition
160                    if (serviceDecomposition != null) {
161                            utils.log("DEBUG", "Getting Catalog DB data from ServiceDecomposition object: " + serviceDecomposition.toJsonString(), isDebugLogEnabled)
162                            List<VnfResource> vnfs = serviceDecomposition.getServiceVnfs()
163                            utils.log("DEBUG", "Read vnfs", isDebugLogEnabled)
164                            if (vnfs == null) {
165                                    utils.log("DEBUG", "Error - vnfs are empty in serviceDecomposition object", isDebugLogEnabled)
166                                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnfs are empty")
167                            }
168                            vnf = vnfs[0]
169                            String serviceModelName = serviceDecomposition.getModelInfo().getModelName()
170                            vnf.constructVnfType(serviceModelName)
171                            String vnfType = vnf.getVnfType()
172                            utils.log("DEBUG", "Incoming Vnf Type is: " + vnfType, isDebugLogEnabled)
173                            execution.setVariable("vnfType", vnfType)
174                    }
175                    else {
176                            //Get Vnf Info
177                            String vnfModelInfo = execution.getVariable("vnfModelInfo")
178                            utils.log("DEBUG", "vnfModelInfo: " + vnfModelInfo, isDebugLogEnabled)
179                            String vnfModelCustomizationUuid = jsonUtil.getJsonValueForKey(vnfModelInfo, "modelCustomizationUuid")
180                            if (vnfModelCustomizationUuid == null) {
181                                            vnfModelCustomizationUuid = ""
182                            }
183                            utils.log("DEBUG", "querying Catalog DB by vnfModelCustomizationUuid: " + vnfModelCustomizationUuid, isDebugLogEnabled)
184                           
185                            JSONArray vnfs = cutils.getAllVnfsByVnfModelCustomizationUuid(execution,
186                                                            vnfModelCustomizationUuid)
187                            utils.log("DEBUG", "obtained VNF list")
188                            // Only one match here
189                            JSONObject vnfObject = vnfs[0]
190                            vnf = DecomposeJsonUtil.jsonToVnfResource(vnfObject.toString())
191                    }
192                    utils.log("DEBUG", "Read vnfResource", isDebugLogEnabled)
193                    if (vnf == null) {
194                            utils.log("DEBUG", "Error - vnf is empty in serviceDecomposition object", isDebugLogEnabled)
195                            exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnf is null")
196                    }
197                    execution.setVariable("vnfResourceDecomposition", vnf)
198
199                    List<ModuleResource> vfModules = vnf.getAllVfModuleObjects()
200                    utils.log("DEBUG", "Read vfModules", isDebugLogEnabled)
201                    if (vfModules == null) {
202                            utils.log("DEBUG", "Error - vfModules are empty in serviceDecomposition object", isDebugLogEnabled)
203                            exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vf modules are empty")
204                    }
205                                                   
206                    ModuleResource baseVfModule = null
207
208                    for (int i = 0; i < vfModules.size; i++) {
209                            utils.log("DEBUG", "handling VF Module ", isDebugLogEnabled)
210                            ModuleResource vfModule = vfModules[i]
211                            boolean isBase = vfModule.getIsBase()
212                            if (isBase) {
213                                            ModelInfo baseVfModuleModelInfoObject = vfModule.getModelInfo()
214                                            String baseVfModuleModelInfoWithRoot = baseVfModuleModelInfoObject.toString()
215                                            String baseVfModuleModelInfo = jsonUtil.getJsonValue(baseVfModuleModelInfoWithRoot, "modelInfo")
216                                            execution.setVariable("baseVfModuleModelInfo", baseVfModuleModelInfo)
217                                            String baseVfModuleLabel = vfModule.getVfModuleLabel()
218                                            execution.setVariable("baseVfModuleLabel", baseVfModuleLabel)
219                                            String basePersonaModelId = baseVfModuleModelInfoObject.getModelInvariantUuid()
220                                            execution.setVariable("basePersonaModelId", basePersonaModelId)
221                                            baseVfModule = vfModule
222                                            break
223                            }               
224                                 
225                         }
226                            
227                         List<ModuleResource>addOnModules = vfModules - baseVfModule
228                            
229                         int addOnModulesToDeploy = 0
230                         if (addOnModules != null) {                             
231                                    addOnModulesToDeploy = addOnModules.size
232                         }
233                            
234                         utils.log("DEBUG", "AddOnModulesToDeploy: " + addOnModulesToDeploy)
235
236                         execution.setVariable("addOnModules", addOnModules)
237                         execution.setVariable("addOnModulesToDeploy", addOnModulesToDeploy)
238                         execution.setVariable("addOnModulesDeployed", 0)          
239
240            }catch(Exception ex) {
241                    utils.log("DEBUG", "Error Occured in DoCreateVnfAndModules QueryCatalogDB Process " + ex.getMessage(), isDebugLogEnabled)
242                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModules QueryCatalogDB Process")
243            }
244
245            // Generate vfModuleId for base VF Module
246            def baseVfModuleId = UUID.randomUUID().toString()
247            execution.setVariable("baseVfModuleId", baseVfModuleId)
248            // For JUnits
249            String requestId = execution.getVariable("requestId")
250            if (requestId.equals("testRequestId123")) {
251                    execution.setVariable("vnfId", "skask")
252            }
253
254            utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules QueryCatalogDB Process ***", isDebugLogEnabled)
255    }
256
257    public void preProcessAddOnModule(DelegateExecution execution){
258            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
259            execution.setVariable("prefix", Prefix)
260            logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)
261
262            try {
263                    List<ModuleResource>addOnModules = execution.getVariable("addOnModules")
264                    int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")
265
266                    ModuleResource addOnModule = addOnModules[addOnIndex]
267                    
268                    utils.log("DEBUG", "Got addon module", isDebugLogEnabled)
269
270                    def newVfModuleId = UUID.randomUUID().toString()
271                    execution.setVariable("addOnVfModuleId", newVfModuleId)
272                    execution.setVariable("isBaseVfModule", "false")
273                    
274                    execution.setVariable("instancesOfThisModuleDeployed", 0)
275
276                    ModelInfo addOnVfModuleModelInfoObject = addOnModule.getModelInfo()            
277                    String addOnVfModuleModelInfoWithRoot = addOnVfModuleModelInfoObject.toString()
278                    String addOnVfModuleModelInfo = jsonUtil.getJsonValue(addOnVfModuleModelInfoWithRoot, "modelInfo")
279                    execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)
280                    String addOnVfModuleLabel = addOnModule.getVfModuleLabel()
281                    execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)
282                    String addOnPersonaModelId = addOnVfModuleModelInfoObject.getModelInvariantUuid()
283                    execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)
284                    int addOnInitialCount = addOnModule.getInitialCount()
285                    execution.setVariable("initialCount", addOnInitialCount)
286
287
288            }catch(Exception e){
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())
291            }
292            logDebug("======== COMPLETED preProcessAddOnModule ======== ", isDebugLogEnabled)
293    }
294
295    public void postProcessAddOnModule(DelegateExecution execution){
296            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
297            execution.setVariable("prefix", Prefix)
298            logDebug(" ======== STARTED postProcessAddOnModule ======== ", isDebugLogEnabled)
299
300            try {
301                    int addOnModulesDeployed = execution.getVariable("addOnModulesDeployed")
302                    execution.setVariable("addOnModulesDeployed", addOnModulesDeployed + 1)
303
304            }catch(Exception e){
305                    utils.log("ERROR", "Exception Occured Processing postProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
306                    exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during postProcessAddOnModule Method:\n" + e.getMessage())
307            }
308            logDebug("======== COMPLETED postProcessAddOnModule ======== ", isDebugLogEnabled)
309    }
310    
311    public void validateBaseModule(DelegateExecution execution){
312            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
313            execution.setVariable("prefix", Prefix)
314            logDebug(" ======== STARTED validateBaseModule ======== ", isDebugLogEnabled)
315
316            try {
317                    def baseRollbackData = execution.getVariable("DCVAM_baseRollbackData")
318                    def rollbackData = execution.getVariable("rollbackData")
319
320                    def baseModuleMap = baseRollbackData.get("VFMODULE")
321                    baseModuleMap.each{ k, v -> rollbackData.put("VFMODULE_BASE", "${k}","${v}") }
322                    execution.setVariable("rollbackData", rollbackData)
323                    logDebug("addOnModulesDeployed: " + execution.getVariable("addOnModulesDeployed"), isDebugLogEnabled)
324                    logDebug("addOnModulesToDeploy: " + execution.getVariable("addOnModulesToDeploy"), isDebugLogEnabled)
325                    if (execution.getVariable("addOnModulesDeployed") <  execution.getVariable("addOnModulesToDeploy")) {
326                            logDebug("More add on modules to deploy", isDebugLogEnabled)
327                    }
328                    else {
329                            logDebug("No more add on modules to deploy", isDebugLogEnabled)
330                    }
331
332            }catch(Exception e){
333                    utils.log("ERROR", "Exception Occured Processing validateBaseModule. Exception is:\n" + e, isDebugLogEnabled)
334                    exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during validateBaseModule Method:\n" + e.getMessage())
335            }
336            logDebug("======== COMPLETED validateBaseModule ======== ", isDebugLogEnabled)
337    }
338
339    public void validateAddOnModule(DelegateExecution execution){
340            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
341            execution.setVariable("prefix", Prefix)
342            logDebug(" ======== STARTED validateAddOnModule ======== ", isDebugLogEnabled)
343
344            try {
345                    int instancesOfThisModuleDeployed = execution.getVariable("instancesOfThisModuleDeployed")
346                    int numOfCreatedAddOnModules = execution.getVariable("numOfCreatedAddOnModules")
347                    def addOnRollbackData = execution.getVariable("DCVAM_addOnRollbackData")
348                    def rollbackData = execution.getVariable("rollbackData")
349
350                    def addOnModuleMap = addOnRollbackData.get("VFMODULE")
351                    numOfCreatedAddOnModules = numOfCreatedAddOnModules + 1
352                    addOnModuleMap.each{ k, v -> rollbackData.put("VFMODULE_ADDON_" + numOfCreatedAddOnModules, "${k}","${v}") }
353
354                    execution.setVariable("DCVAM_addOnRollbackData", null)
355
356                    execution.setVariable("instancesOfThisModuleDeployed", instancesOfThisModuleDeployed + 1)
357
358                    execution.setVariable("numOfCreatedAddOnModules", numOfCreatedAddOnModules)
359                    rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "${numOfCreatedAddOnModules}")
360                    execution.setVariable("rollbackData", rollbackData)
361            }catch(Exception e){
362                    utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
363                    exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
364            }
365            logDebug("======== COMPLETED validateAddOnModule ======== ", isDebugLogEnabled)
366    }   
367    
368    public void preProcessRollback (DelegateExecution execution) {
369            def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
370            utils.log("DEBUG"," ***** preProcessRollback ***** ", isDebugLogEnabled)
371            try {
372                    
373                    Object workflowException = execution.getVariable("WorkflowException");
374
375                    if (workflowException instanceof WorkflowException) {
376                            utils.log("DEBUG", "Prev workflowException: " + workflowException.getErrorMessage(), isDebugLogEnabled)
377                            execution.setVariable("prevWorkflowException", workflowException);
378                            //execution.setVariable("WorkflowException", null);
379                    }
380            } catch (BpmnError e) {
381                    utils.log("DEBUG", "BPMN Error during preProcessRollback", isDebugLogEnabled)
382            } catch(Exception ex) {
383                    String msg = "Exception in preProcessRollback. " + ex.getMessage()
384                    utils.log("DEBUG", msg, isDebugLogEnabled)
385            }
386            utils.log("DEBUG"," *** Exit preProcessRollback *** ", isDebugLogEnabled)
387    }
388
389    public void postProcessRollback (DelegateExecution execution) {
390            def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
391            utils.log("DEBUG"," ***** postProcessRollback ***** ", isDebugLogEnabled)
392            String msg = ""
393            try {
394                    Object workflowException = execution.getVariable("prevWorkflowException");
395                    if (workflowException instanceof WorkflowException) {
396                            utils.log("DEBUG", "Setting prevException to WorkflowException: ", isDebugLogEnabled)
397                            execution.setVariable("WorkflowException", workflowException);
398                    }
399                    execution.setVariable("rollbackData", null)
400            } catch (BpmnError b) {
401                    utils.log("DEBUG", "BPMN Error during postProcessRollback", isDebugLogEnabled)
402                    throw b;
403            } catch(Exception ex) {
404                    msg = "Exception in postProcessRollback. " + ex.getMessage()
405                    utils.log("DEBUG", msg, isDebugLogEnabled)
406            }
407            utils.log("DEBUG"," *** Exit postProcessRollback *** ", isDebugLogEnabled)
408    }
409
410
411 }