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