daae3d7dac80e6faf2461a3e9a0a33ee5b0a0946
[so.git] /
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 import org.openecomp.mso.bpmn.core.decomposition.ModelInfo
40 import org.openecomp.mso.bpmn.core.decomposition.ModuleResource
41 import org.openecomp.mso.bpmn.core.decomposition.ServiceDecomposition
42 import org.openecomp.mso.bpmn.core.decomposition.VnfResource
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 isDebugEnabled = execution.getVariable("isDebugLogEnabled")
66            execution.setVariable("prefix",Prefix)
67            utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules PreProcessRequest Process*** ", isDebugEnabled)
68
69            try{
70                    // Get Variables
71
72                    ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
73
74                    String vnfModelInfo = execution.getVariable("vnfModelInfo")
75
76                    String requestId = execution.getVariable("msoRequestId")
77                    execution.setVariable("requestId", requestId)
78                    execution.setVariable("mso-request-id", requestId)
79                    utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugEnabled)
80
81                    String serviceInstanceId = execution.getVariable("serviceInstanceId")
82                    utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
83
84                    String vnfType = execution.getVariable("vnfType")
85                    utils.log("DEBUG", "Incoming Vnf Type is: " + vnfType, isDebugEnabled)
86
87                    String vnfName = execution.getVariable("vnfName")
88                    execution.setVariable("CREVI_vnfName", vnfName)
89                    utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)
90
91                    String productFamilyId = execution.getVariable("productFamilyId")
92                    utils.log("DEBUG", "Incoming Product Family Id is: " + productFamilyId, isDebugEnabled)
93
94                    String source = "VID"
95                    execution.setVariable("source", source)
96                    utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)
97
98                    String lcpCloudRegionId = execution.getVariable("lcpCloudRegionId")
99                    utils.log("DEBUG", "Incoming LCP Cloud Region Id is: " + lcpCloudRegionId)
100
101                    String tenantId = execution.getVariable("tenantId")
102                    utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId)
103
104                    String disableRollback = execution.getVariable("disableRollback")
105                    utils.log("DEBUG", "Incoming Disable Rollback is: " + disableRollback, isDebugEnabled)
106
107                    String asdcServiceModelVersion = execution.getVariable("asdcServiceModelVersion")
108                    utils.log("DEBUG", "Incoming asdcServiceModelVersion: " + asdcServiceModelVersion, isDebugEnabled)
109
110                    String vnfId = execution.getVariable("testVnfId") // for junits
111                    if(isBlank(vnfId)){
112                            vnfId = execution.getVariable("vnfId")
113                            if (isBlank(vnfId)) {
114                                    vnfId = UUID.randomUUID().toString()
115                                    utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)
116                            }
117                    }
118                    execution.setVariable("vnfId", vnfId)
119
120                    def rollbackData = execution.getVariable("RollbackData")
121                    if (rollbackData == null) {
122                            rollbackData = new RollbackData()
123                    }
124
125                    execution.setVariable("numOfCreatedAddOnModules", 0)
126
127                    rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "0")
128                    execution.setVariable("RollbackData", rollbackData)
129
130                    sleep (20000)
131
132
133            }catch(BpmnError b){
134                    utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
135                    throw b
136            }catch(Exception e){
137                    utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugEnabled)
138                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")
139
140            }
141            utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules PreProcessRequest Process ***", isDebugEnabled)
142    }
143
144
145    public void queryCatalogDB (Execution execution) {
146            def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
147            execution.setVariable("prefix",Prefix)
148
149            utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules QueryCatalogDB Process *** ", isDebugEnabled)
150            try {
151                    ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
152                    // if serviceDecomposition is specified, get info from serviceDecomposition
153                    if (serviceDecomposition != null) {
154                            utils.log("DEBUG", "Getting Catalog DB data from ServiceDecomposition object: " + serviceDecomposition.toJsonString(), isDebugEnabled)
155                            List<VnfResource> vnfs = serviceDecomposition.getServiceVnfs()
156                            utils.log("DEBUG", "Read vnfs", isDebugEnabled)
157                            if (vnfs == null) {
158                                    utils.log("DEBUG", "Error - vnfs are empty in serviceDecomposition object", isDebugEnabled)
159                                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnfs are empty")
160                            }
161                            VnfResource vnf = vnfs[0]
162                            utils.log("DEBUG", "Read vnfResource", isDebugEnabled)
163                            if (vnf == null) {
164                                    utils.log("DEBUG", "Error - vnf is empty in serviceDecomposition object", isDebugEnabled)
165                                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vnf is null")
166                            }
167
168                            List<ModuleResource> vfModules = vnf.getAllVfModuleObjects()
169                            utils.log("DEBUG", "Read vfModules", isDebugEnabled)
170                            if (vfModules == null) {
171                                    utils.log("DEBUG", "Error - vfModules are empty in serviceDecomposition object", isDebugEnabled)
172                                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf queryCatalogDB, vf modules are empty")
173                            }
174                            JSONArray addOnModules = new JSONArray()
175
176                            for (int i = 0; i < vfModules.size; i++) {
177                                    utils.log("DEBUG", "handling VF Module ", isDebugEnabled)
178                                    ModuleResource vfModule = vfModules[i]
179                                    boolean isBase = vfModule.getIsBase()
180                                    if (isBase) {
181                                            ModelInfo baseVfModuleModelInfoObject = vfModule.getModelInfo()
182                                            String baseVfModuleModelInfoWithRoot = baseVfModuleModelInfoObject.toString()
183                                            String baseVfModuleModelInfo = jsonUtil.getJsonValue(baseVfModuleModelInfoWithRoot, "modelInfo")
184                                            execution.setVariable("baseVfModuleModelInfo", baseVfModuleModelInfo)
185                                            String baseVfModuleLabel = vfModule.getVfModuleLabel()
186                                            execution.setVariable("baseVfModuleLabel", baseVfModuleLabel)
187                                            String basePersonaModelId = baseVfModuleModelInfoObject.getModelInvariantId()
188                                            execution.setVariable("basePersonaModelId", basePersonaModelId)
189                                    }
190                                    else {
191                                            addOnModules.put(vfModules[i])
192                                    }
193                            }
194
195                            execution.setVariable("addOnModules", addOnModules)
196                            execution.setVariable("addOnModulesToDeploy", addOnModules.length())
197                            execution.setVariable("addOnModulesDeployed", 0)
198
199                    }
200                    else {
201                            //Get Vnf Info
202                            String vnfModelInfo = execution.getVariable("vnfModelInfo")
203                            utils.log("DEBUG", "vnfModelInfo: " + vnfModelInfo, isDebugEnabled)
204                            String vnfModelCustomizationUuid = jsonUtil.getJsonValueForKey(vnfModelInfo, "modelCustomizationId")
205                            if (vnfModelCustomizationUuid == null) {
206                                    vnfModelCustomizationUuid = jsonUtil.getJsonValueForKey(vnfModelInfo, "modelCustomizationUuid")
207                            }
208                            utils.log("DEBUG", "querying Catalog DB by vnfModelCustomizationUuid: " + vnfModelCustomizationUuid, isDebugEnabled)
209                            String catalogDbEndpoint = execution.getVariable("URN_mso_catalog_db_endpoint")
210
211                            JSONArray vnfs = cutils.getAllVnfsByVnfModelCustomizationUuid(catalogDbEndpoint,
212                                                            vnfModelCustomizationUuid)
213                            utils.log("DEBUG", "obtained VNF list")
214                            // Only one match here
215                            JSONObject vnf = vnfs[0]
216                            JSONArray vfModules = vnf.getJSONArray("vfModules")
217                            JSONArray addOnModules = new JSONArray()
218
219                            // Set up base Vf Module info
220                            for (int i = 0; i < vfModules.length(); i++) {
221                                    utils.log("DEBUG", "handling VF Module ")
222                                    JSONObject vfModule = vfModules[i]
223                                    String isBase = jsonUtil.getJsonValueForKey(vfModule, "isBase")
224                                    if (isBase.equals("true")) {
225                                            JSONObject baseVfModuleModelInfoObject = vfModule.getJSONObject("modelInfo")
226                                            String baseVfModuleModelInfo = baseVfModuleModelInfoObject.toString()                                          
227                                            execution.setVariable("baseVfModuleModelInfo", baseVfModuleModelInfo)
228                                            String baseVfModuleLabel = jsonUtil.getJsonValueForKey(vfModule, "vfModuleLabel")
229                                            execution.setVariable("baseVfModuleLabel", baseVfModuleLabel)
230                                            String basePersonaModelId = jsonUtil.getJsonValueForKey(baseVfModuleModelInfoObject, "modelInvariantId")
231                                            execution.setVariable("basePersonaModelId", basePersonaModelId)
232                                    }
233                                    else {
234                                            addOnModules.put(vfModules[i])
235                                    }
236                            }
237
238                            execution.setVariable("addOnModules", addOnModules)
239                            execution.setVariable("addOnModulesToDeploy", addOnModules.length())
240                            execution.setVariable("addOnModulesDeployed", 0)
241                    }
242
243            }catch(Exception ex) {
244                    utils.log("DEBUG", "Error Occured in DoCreateVnfAndModules QueryCatalogDB Process " + ex.getMessage(), isDebugEnabled)
245                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModules QueryCatalogDB Process")
246            }
247
248            // Generate vfModuleId for base VF Module
249            def baseVfModuleId = UUID.randomUUID().toString()
250            execution.setVariable("baseVfModuleId", baseVfModuleId)
251            // For JUnits
252            String requestId = execution.getVariable("requestId")
253            if (requestId.equals("testRequestId123")) {
254                    execution.setVariable("vnfId", "skask")
255            }
256
257            utils.log("DEBUG", "*** COMPLETED CreateVnfInfra PrepareCreateGenericVnf Process ***", isDebugEnabled)
258    }
259
260    public void preProcessAddOnModule(Execution execution){
261            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
262            execution.setVariable("prefix", Prefix)
263            logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)
264
265            try {
266                    JSONArray addOnModules = (JSONArray) execution.getVariable("addOnModules")
267                    int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")
268
269                    JSONObject addOnModule = addOnModules[addOnIndex]
270
271                    def newVfModuleId = UUID.randomUUID().toString()
272                    execution.setVariable("addOnVfModuleId", newVfModuleId)
273
274                    execution.setVariable("instancesOfThisModelDeployed", 0)
275
276                    JSONObject addOnVfModuleModelInfoObject = jsonUtil.getJsonValueForKey(addOnModule, "modelInfo")
277                    String addOnVfModuleModelInfoWithRoot = addOnVfModuleModelInfoObject.toString()                 
278                    String addOnVfModuleModelInfo = jsonUtil.getJsonValue(addOnVfModuleModelInfoWithRoot, "modelInfo")
279                    execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)
280                    String addOnVfModuleLabel = jsonUtil.getJsonValueForKey(addOnModule, "vfModuleLabel")
281                    execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)
282                    String addOnPersonaModelId = jsonUtil.getJsonValueForKey(addOnVfModuleModelInfoObject, "modelInvariantId")
283                    execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)
284                    String addOnInitialCount = jsonUtil.getJsonValueForKey(addOnModule, "initialCount")
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 preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
293    }
294
295    public void validateBaseModule(Execution execution){
296            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
297            execution.setVariable("prefix", Prefix)
298            logDebug(" ======== STARTED validateBaseModule ======== ", isDebugLogEnabled)
299
300            try {
301                    def baseRollbackData = execution.getVariable("DCVAM_baseRollbackData")
302                    def rollbackData = execution.getVariable("RollbackData")
303
304                    def baseModuleMap = baseRollbackData.get("VFMODULE")
305                    baseModuleMap.each{ k, v -> rollbackData.put("VFMODULE_BASE", "${k}","${v}") }
306                    execution.setVariable("RollbackData", rollbackData)
307                    logDebug("addOnModulesDeployed: " + execution.getVariable("addOnModulesDeployed"), isDebugLogEnabled)
308                    logDebug("addOnModulesToDeploy: " + execution.getVariable("addOnModulesToDeploy"), isDebugLogEnabled)
309                    if (execution.getVariable("addOnModulesDeployed") <  execution.getVariable("addOnModulesToDeploy")) {
310                            logDebug("More add on modules to deploy", isDebugLogEnabled)
311                    }
312                    else {
313                            logDebug("No more add on modules to deploy", isDebugLogEnabled)
314                    }
315
316            }catch(Exception e){
317                    utils.log("ERROR", "Exception Occured Processing validateBaseModule. Exception is:\n" + e, isDebugLogEnabled)
318                    exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during validateBaseModule Method:\n" + e.getMessage())
319            }
320            logDebug("======== COMPLETED validateBaseModule ======== ", isDebugLogEnabled)
321    }
322
323    public void validateAddOnModule(Execution execution){
324            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
325            execution.setVariable("prefix", Prefix)
326            logDebug(" ======== STARTED validateAddOnModule ======== ", isDebugLogEnabled)
327
328            try {
329                    int instancesOfThisModuleDeployed = execution.getVariable("instancesOfThisModuleDeployed")
330                    int numOfCreatedAddOnModules = execution.getVariable("numOfCreatedAddOnModules")
331                    def addOnRollbackData = execution.getVariable("DCVAM_addOnRollbackData")
332                    def rollbackData = execution.getVariable("RollbackData")
333
334                    def addOnModuleMap = addOnRollbackData.get("VFMODULE")
335                    numOfCreatedAddOnModules = numOfCreatedAddOnModules + 1
336                    addOnModuleMap.each{ k, v -> rollbackData.put("VFMODULE_ADDON_" + numOfCreatedAddOnModules, "${k}","${v}") }
337
338                    execution.setVariable("DCVAM_addOnRollbackData", null)
339
340                    execution.setVariable("instancesOfThisModuleDeployed", instancesOfThisModuleDeployed + 1)
341
342                    execution.setVariable("numOfCreatedAddOnModules", numOfCreatedAddOnModules)
343                    rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", "${numOfCreatedAddOnModules}")
344                    execution.setVariable("RollbackData", rollbackData)
345            }catch(Exception e){
346                    utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
347                    exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
348            }
349            logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
350    }
351
352    public void finishProcessingInitialCountDeployment(Execution execution){
353            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")
354            execution.setVariable("prefix", Prefix)
355            logDebug(" ======== STARTED finishProcessingInitialCountDeployment ======== ", isDebugLogEnabled)
356
357            try {
358                    int addOnModulesDeployed = execution.getVariable("addOnModulesDeployed")
359                    execution.setVariable("addOnModulesDeployed", addOnModulesDeployed + 1)
360            }catch(Exception e){
361                    utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)
362                    exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())
363            }
364            logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)
365    }
366
367
368
369 }