[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoCreateVnfAndModules.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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
24 import org.json.JSONObject;
25 import org.json.JSONArray;
26
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.runtime.Execution;
29
30 import static org.apache.commons.lang3.StringUtils.*;
31
32 import org.openecomp.mso.bpmn.core.json.JsonUtils
33 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
34 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils
35 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
36 import org.openecomp.mso.bpmn.common.scripts.VidUtils
37 import org.openecomp.mso.bpmn.core.RollbackData
38 import org.openecomp.mso.bpmn.core.WorkflowException
39
40
41
42 /**
43  * This class supports the macro VID Flow
44  * with the creation of a generic vnf and related VF modules.
45  */
46 class DoCreateVnfAndModules extends AbstractServiceTaskProcessor {
47
48         String Prefix="DCVAM_"
49         ExceptionUtil exceptionUtil = new ExceptionUtil()
50         JsonUtils jsonUtil = new JsonUtils()
51         VidUtils vidUtils = new VidUtils(this)
52         CatalogDbUtils cutils = new CatalogDbUtils()
53
54         /**
55          * This method gets and validates the incoming
56          * request.
57          *
58          * @param - execution
59          */
60         public void preProcessRequest(Execution execution) {
61                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
62                 execution.setVariable("prefix",Prefix)
63                 utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules PreProcessRequest Process*** ", isDebugEnabled)
64
65                 try{
66                         // Get Variables        
67                         
68                         
69                         String cloudConfiguration = execution.getVariable("cloudConfiguration")
70                         String vnfModelInfo = execution.getVariable("vnfModelInfo")
71                         
72                         String requestId = execution.getVariable("requestId")
73                         execution.setVariable("mso-request-id", requestId)
74                         utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugEnabled)
75
76                         String serviceInstanceId = execution.getVariable("serviceInstanceId")                   
77                         utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
78
79                         String vnfType = execution.getVariable("vnfType")                       
80                         utils.log("DEBUG", "Incoming Vnf Type is: " + vnfType, isDebugEnabled)
81
82                         String vnfName = execution.getVariable("vnfName")
83                         execution.setVariable("CREVI_vnfName", vnfName)
84                         utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)
85
86                         String productFamilyId = execution.getVariable("productFamilyId")                       
87                         utils.log("DEBUG", "Incoming Product Family Id is: " + productFamilyId, isDebugEnabled)
88
89                         String source = "VID"
90                         execution.setVariable("source", source)
91                         utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)
92
93                         String disableRollback = execution.getVariable("disableRollback")                       
94                         utils.log("DEBUG", "Incoming Disable Rollback is: " + disableRollback, isDebugEnabled)
95                         
96                         String asdcServiceModelVersion = execution.getVariable("asdcServiceModelVersion")
97                         utils.log("DEBUG", "Incoming asdcServiceModelVersion: " + asdcServiceModelVersion, isDebugEnabled)
98
99                         String vnfId = execution.getVariable("testVnfId") // for junits
100                         if(isBlank(vnfId)){
101                                 vnfId = execution.getVariable("vnfId")
102                                 if (isBlank(vnfId)) {
103                                         vnfId = UUID.randomUUID().toString()
104                                         utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)
105                                 }
106                         }
107                         execution.setVariable("vnfId", vnfId)                   
108                         
109                 }catch(BpmnError b){
110                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
111                         throw b
112                 }catch(Exception e){
113                         utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugEnabled)
114                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
115
116                 }
117                 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules PreProcessRequest Process ***", isDebugEnabled)
118         }       
119
120         
121         public void queryCatalogDB (Execution execution) {
122                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
123                 execution.setVariable("prefix",Prefix)
124
125                 utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules QueryCatalogDB Process *** ", isDebugEnabled)
126                 try {
127                         //Get Vnf Info
128                         String vnfModelInfo = execution.getVariable("vnfModelInfo")
129                         String vnfModelCustomizationUuid = jsonUtil.getJsonValueForKey(vnfModelInfo, "modelCustomizationId")
130                         utils.log("DEBUG", "querying Catalog DB by vnfModelCustomizationUuid: " + vnfModelCustomizationUuid)
131                         String catalogDbEndpoint = execution.getVariable("URN_mso_catalog_db_endpoint")
132                         
133                         JSONArray vnfs = cutils.getAllVnfsByVnfModelCustomizationUuid(catalogDbEndpoint, 
134                                                         vnfModelCustomizationUuid)
135                         utils.log("DEBUG", "obtained VNF list")
136                         // Only one match here
137                         JSONObject vnf = vnfs[0]
138                         JSONArray vfModules = vnf.getJSONArray("vfModules")
139                         JSONArray addOnModules = new JSONArray()
140                         
141                         // Set up base Vf Module info
142                         for (int i = 0; i < vfModules.length(); i++) {
143                                 utils.log("DEBUG", "handling VF Module ")
144                                 JSONObject vfModule = vfModules[i]
145                                 String isBase = jsonUtil.getJsonValueForKey(vfModule, "isBase")
146                                 if (isBase.equals("true")) {
147                                         JSONObject baseVfModuleModelInfoObject = vfModule.getJSONObject("modelInfo")
148                                         String baseVfModuleModelInfo = baseVfModuleModelInfoObject.toString()
149                                         execution.setVariable("baseVfModuleModelInfo", baseVfModuleModelInfo)
150                                         String baseVfModuleLabel = jsonUtil.getJsonValueForKey(vfModule, "vfModuleLabel")
151                                         execution.setVariable("baseVfModuleLabel", baseVfModuleLabel)
152                                         String basePersonaModelId = jsonUtil.getJsonValueForKey(baseVfModuleModelInfoObject, "modelInvariantId")
153                                         execution.setVariable("basePersonaModelId", basePersonaModelId)                                 
154                                 }
155                                 else {
156                                         addOnModules.add(vfModules[i])
157                                 }                       
158                         }
159                         
160                         execution.setVariable("addOnModules", addOnModules)
161                         execution.setVariable("addOnModulesToDeploy", addOnModules.length())
162                         execution.setVariable("addOnModulesDeployed", 0)                        
163                         
164                 }catch(Exception ex) {
165                         utils.log("DEBUG", "Error Occured in DoCreateVnfAndModules QueryCatalogDB Process " + ex.getMessage(), isDebugEnabled)
166                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModules QueryCatalogDB Process")
167                 }
168                 
169                 // Generate vfModuleId for base VF Module
170                 def baseVfModuleId = UUID.randomUUID().toString()
171                 execution.setVariable("baseVfModuleId", baseVfModuleId)
172                 utils.log("DEBUG", "*** COMPLETED CreateVnfInfra PrepareCreateGenericVnf Process ***", isDebugEnabled)
173         }
174         
175         public void preProcessAddOnModule(Execution execution){
176                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
177                 execution.setVariable("prefix", Prefix)
178                 logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)
179                 
180                 try {                   
181                         JSONArray addOnModules = (JSONArray) execution.getVariable("addOnModules")
182                         int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")
183                         
184                         JSONObject addOnModule = addOnModules[addOnIndex]
185                         
186                         def newVfModuleId = UUID.randomUUID().toString()
187                         execution.setVariable("addOnVfModuleId", newVfModuleId)
188                         
189                         execution.setVariable("instancesOfThisModelDeployed", 0)
190                         
191                         JSONObject addOnVfModuleModelInfoObject = jsonUtil.getJsonValueForKey(addOnModule, "modelInfo")
192                         String addOnVfModuleModelInfo = addOnVfModuleModelInfoObject.toString()
193                         execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)
194                         String addOnVfModuleLabel = jsonUtil.getJsonValueForKey(addOnModule, "vfModuleLabel")
195                         execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)
196                         String addOnPersonaModelId = jsonUtil.getJsonValueForKey(addOnVfModuleModelInfoObject, "modelInvariantId")
197                         execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)
198                         String addOnInitialCount = jsonUtil.getJsonValueForKey(addOnModule, "initialCount")
199                         execution.setVariable("initialCount", addOnInitialCount)
200                                         
201                 
202                 }catch(Exception e){
203                         utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
204                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
205                 }
206                 logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
207         }
208         
209         public void validateAddOnModule(Execution execution){
210                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
211                 execution.setVariable("prefix", Prefix)
212                 logDebug(" ======== STARTED validateAddOnModule ======== ", isDebugLogEnabled)
213                 
214                 try {
215                         int instancesOfThisModuleDeployed = execution.getVariable("instancesOfThisModuleDeployed")
216                         execution.setVariable("instancesOfThisModuleDeployed", instancesOfThisModuleDeployed + 1)
217                 }catch(Exception e){
218                         utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
219                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
220                 }
221                 logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
222         }
223         
224         public void finishProcessingInitialCountDeployment(Execution execution){
225                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
226                 execution.setVariable("prefix", Prefix)
227                 logDebug(" ======== STARTED finishProcessingInitialCountDeployment ======== ", isDebugLogEnabled)
228                 
229                 try {
230                         int addOnModulesDeployed = execution.getVariable("addOnModulesDeployed")
231                         execution.setVariable("addOnModulesDeployed", addOnModulesDeployed + 1)                 
232                 }catch(Exception e){
233                         utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
234                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
235                 }
236                 logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
237         }
238         
239         
240         
241 }