861da523975d64a6f78e09e99a4989c17057230b
[so.git] /
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - MSO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package org.openecomp.mso.bpmn.infrastructure.scripts\r
21 \r
22 import java.util.UUID;\r
23 \r
24 import org.json.JSONObject;\r
25 import org.json.JSONArray;\r
26 \r
27 import org.camunda.bpm.engine.delegate.BpmnError\r
28 import org.camunda.bpm.engine.runtime.Execution;\r
29 \r
30 import static org.apache.commons.lang3.StringUtils.*;\r
31 \r
32 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
33 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
34 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils\r
35 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
36 import org.openecomp.mso.bpmn.common.scripts.VidUtils\r
37 import org.openecomp.mso.bpmn.core.RollbackData\r
38 import org.openecomp.mso.bpmn.core.WorkflowException\r
39 \r
40 \r
41 \r
42 /**\r
43 * This class supports the macro VID Flow\r
44 * with the creation of a generic vnf and related VF modules.\r
45 */\r
46 class DoCreateVnfAndModules extends AbstractServiceTaskProcessor {\r
47 \r
48    String Prefix="DCVAM_"\r
49    ExceptionUtil exceptionUtil = new ExceptionUtil()\r
50    JsonUtils jsonUtil = new JsonUtils()\r
51    VidUtils vidUtils = new VidUtils(this)\r
52    CatalogDbUtils cutils = new CatalogDbUtils()\r
53 \r
54    /**\r
55         * This method gets and validates the incoming\r
56         * request.\r
57         *\r
58         * @param - execution\r
59         */\r
60    public void preProcessRequest(Execution execution) {\r
61            def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
62            execution.setVariable("prefix",Prefix)\r
63            utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules PreProcessRequest Process*** ", isDebugEnabled)\r
64 \r
65            try{\r
66                    // Get Variables\r
67                    \r
68                    \r
69                    String cloudConfiguration = execution.getVariable("cloudConfiguration")\r
70                    String vnfModelInfo = execution.getVariable("vnfModelInfo")\r
71                    \r
72                    String requestId = execution.getVariable("requestId")\r
73                         execution.setVariable("mso-request-id", requestId)\r
74                    utils.log("DEBUG", "Incoming Request Id is: " + requestId, isDebugEnabled)\r
75 \r
76                    String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
77                    utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)\r
78 \r
79                    String vnfType = execution.getVariable("vnfType")\r
80                    utils.log("DEBUG", "Incoming Vnf Type is: " + vnfType, isDebugEnabled)\r
81 \r
82                    String vnfName = execution.getVariable("vnfName")\r
83                    execution.setVariable("CREVI_vnfName", vnfName)\r
84                    utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)\r
85 \r
86                    String productFamilyId = execution.getVariable("productFamilyId")\r
87                    utils.log("DEBUG", "Incoming Product Family Id is: " + productFamilyId, isDebugEnabled)\r
88 \r
89                    String source = "VID"\r
90                    execution.setVariable("source", source)\r
91                    utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)\r
92 \r
93                    String disableRollback = execution.getVariable("disableRollback")\r
94                    utils.log("DEBUG", "Incoming Disable Rollback is: " + disableRollback, isDebugEnabled)\r
95                    \r
96                    String asdcServiceModelVersion = execution.getVariable("asdcServiceModelVersion")\r
97                    utils.log("DEBUG", "Incoming asdcServiceModelVersion: " + asdcServiceModelVersion, isDebugEnabled)\r
98 \r
99                    String vnfId = execution.getVariable("testVnfId") // for junits\r
100                    if(isBlank(vnfId)){\r
101                            vnfId = execution.getVariable("vnfId")\r
102                            if (isBlank(vnfId)) {\r
103                                    vnfId = UUID.randomUUID().toString()\r
104                                    utils.log("DEBUG", "Generated Vnf Id is: " + vnfId, isDebugEnabled)\r
105                            }\r
106                    }\r
107                    execution.setVariable("vnfId", vnfId)\r
108                    \r
109                    def rollbackData = execution.getVariable("RollbackData")\r
110                    if (rollbackData == null) {\r
111                            rollbackData = new RollbackData()\r
112                    }\r
113                    \r
114                    execution.setVariable("numOfCreatedAddOnModules", 0)\r
115                    \r
116                    rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", 0)\r
117                    execution.setVariable("RollbackData", rollbackData)\r
118                    \r
119            }catch(BpmnError b){\r
120                    utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
121                    throw b\r
122            }catch(Exception e){\r
123                    utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugEnabled)\r
124                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")\r
125 \r
126            }\r
127            utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules PreProcessRequest Process ***", isDebugEnabled)\r
128    }\r
129 \r
130    \r
131    public void queryCatalogDB (Execution execution) {\r
132            def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
133            execution.setVariable("prefix",Prefix)\r
134 \r
135            utils.log("DEBUG", " *** STARTED DoCreateVnfAndModules QueryCatalogDB Process *** ", isDebugEnabled)\r
136            try {\r
137                    //Get Vnf Info\r
138                    String vnfModelInfo = execution.getVariable("vnfModelInfo")\r
139                    String vnfModelCustomizationUuid = jsonUtil.getJsonValueForKey(vnfModelInfo, "modelCustomizationId")\r
140                    utils.log("DEBUG", "querying Catalog DB by vnfModelCustomizationUuid: " + vnfModelCustomizationUuid)\r
141                    String catalogDbEndpoint = execution.getVariable("URN_mso_catalog_db_endpoint")\r
142                    \r
143                    JSONArray vnfs = cutils.getAllVnfsByVnfModelCustomizationUuid(catalogDbEndpoint,\r
144                                                    vnfModelCustomizationUuid)\r
145                    utils.log("DEBUG", "obtained VNF list")\r
146                    // Only one match here\r
147                    JSONObject vnf = vnfs[0]\r
148                    JSONArray vfModules = vnf.getJSONArray("vfModules")\r
149                    JSONArray addOnModules = new JSONArray()\r
150                    \r
151                    // Set up base Vf Module info\r
152                    for (int i = 0; i < vfModules.length(); i++) {\r
153                            utils.log("DEBUG", "handling VF Module ")\r
154                            JSONObject vfModule = vfModules[i]\r
155                            String isBase = jsonUtil.getJsonValueForKey(vfModule, "isBase")\r
156                            if (isBase.equals("true")) {\r
157                                    JSONObject baseVfModuleModelInfoObject = vfModule.getJSONObject("modelInfo")\r
158                                    String baseVfModuleModelInfo = baseVfModuleModelInfoObject.toString()\r
159                                    execution.setVariable("baseVfModuleModelInfo", baseVfModuleModelInfo)\r
160                                    String baseVfModuleLabel = jsonUtil.getJsonValueForKey(vfModule, "vfModuleLabel")\r
161                                    execution.setVariable("baseVfModuleLabel", baseVfModuleLabel)\r
162                                    String basePersonaModelId = jsonUtil.getJsonValueForKey(baseVfModuleModelInfoObject, "modelInvariantId")\r
163                                    execution.setVariable("basePersonaModelId", basePersonaModelId)\r
164                            }\r
165                            else {\r
166                                    addOnModules.add(vfModules[i])\r
167                            }\r
168                    }\r
169                    \r
170                    execution.setVariable("addOnModules", addOnModules)\r
171                    execution.setVariable("addOnModulesToDeploy", addOnModules.length())\r
172                    execution.setVariable("addOnModulesDeployed", 0)\r
173                    \r
174            }catch(Exception ex) {\r
175                    utils.log("DEBUG", "Error Occured in DoCreateVnfAndModules QueryCatalogDB Process " + ex.getMessage(), isDebugEnabled)\r
176                    exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModules QueryCatalogDB Process")\r
177            }\r
178            \r
179            // Generate vfModuleId for base VF Module\r
180            def baseVfModuleId = UUID.randomUUID().toString()\r
181            execution.setVariable("baseVfModuleId", baseVfModuleId)\r
182            utils.log("DEBUG", "*** COMPLETED CreateVnfInfra PrepareCreateGenericVnf Process ***", isDebugEnabled)\r
183    }\r
184    \r
185    public void preProcessAddOnModule(Execution execution){\r
186            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
187            execution.setVariable("prefix", Prefix)\r
188            logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)\r
189            \r
190            try {\r
191                    JSONArray addOnModules = (JSONArray) execution.getVariable("addOnModules")\r
192                    int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")\r
193                    \r
194                    JSONObject addOnModule = addOnModules[addOnIndex]\r
195                    \r
196                    def newVfModuleId = UUID.randomUUID().toString()\r
197                    execution.setVariable("addOnVfModuleId", newVfModuleId)\r
198                    \r
199                    execution.setVariable("instancesOfThisModelDeployed", 0)\r
200                    \r
201                    JSONObject addOnVfModuleModelInfoObject = jsonUtil.getJsonValueForKey(addOnModule, "modelInfo")\r
202                    String addOnVfModuleModelInfo = addOnVfModuleModelInfoObject.toString()\r
203                    execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)\r
204                    String addOnVfModuleLabel = jsonUtil.getJsonValueForKey(addOnModule, "vfModuleLabel")\r
205                    execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)\r
206                    String addOnPersonaModelId = jsonUtil.getJsonValueForKey(addOnVfModuleModelInfoObject, "modelInvariantId")\r
207                    execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)\r
208                    String addOnInitialCount = jsonUtil.getJsonValueForKey(addOnModule, "initialCount")\r
209                    execution.setVariable("initialCount", addOnInitialCount)\r
210                                    \r
211            \r
212            }catch(Exception e){\r
213                    utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)\r
214                    exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())\r
215            }\r
216            logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)\r
217    }\r
218    \r
219    public void validateBaseModule(Execution execution){\r
220            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
221            execution.setVariable("prefix", Prefix)\r
222            logDebug(" ======== STARTED validateBaseModule ======== ", isDebugLogEnabled)\r
223            \r
224            try {\r
225                    def baseRollbackData = execution.getVariable("DCVAM_baseRollbackData")\r
226                    def rollbackData = execution.getVariable("RollbackData")\r
227                    \r
228                    def baseModuleMap = baseRollbackData.get("VFMODULE")\r
229                    baseModuleMap.each{ k, v -> rollbackData.put("VFMODULE_BASE", "${k}","${v}") }\r
230                    execution.setVariable("RollbackData", rollbackData)\r
231                    \r
232            }catch(Exception e){\r
233                    utils.log("ERROR", "Exception Occured Processing validateBaseModule. Exception is:\n" + e, isDebugLogEnabled)\r
234                    exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during validateBaseModule Method:\n" + e.getMessage())\r
235            }\r
236            logDebug("======== COMPLETED validateBaseModule ======== ", isDebugLogEnabled)\r
237    }\r
238    \r
239    public void validateAddOnModule(Execution execution){\r
240            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
241            execution.setVariable("prefix", Prefix)\r
242            logDebug(" ======== STARTED validateAddOnModule ======== ", isDebugLogEnabled)\r
243            \r
244            try {\r
245                    int instancesOfThisModuleDeployed = execution.getVariable("instancesOfThisModuleDeployed")\r
246                    int numOfCreatedAddOnModules = execution.getVariable("numOfCreatedAddOnModules")\r
247                    def addOnRollbackData = execution.getVariable("DCVAM_addOnRollbackData")\r
248                    def rollbackData = execution.getVariable("RollbackData")\r
249                    \r
250                    def addOnModuleMap = addOnRollbackData.get("VFMODULE")\r
251                    numOfCreatedAddOnModules = numOfCreatedAddOnModules + 1\r
252                    addOnModuleMap.each{ k, v -> rollbackData.put("VFMODULE_ADDON_" + numOfCreatedAddOnModules, "${k}","${v}") }\r
253                    \r
254                    execution.setVariable("DCVAM_addOnRollbackData", null)\r
255                    \r
256                    execution.setVariable("instancesOfThisModuleDeployed", instancesOfThisModuleDeployed + 1)\r
257                    \r
258                    execution.setVariable("numOfCreatedAddOnModules", numOfCreatedAddOnModules)\r
259                    rollbackData.put("VNFANDMODULES", "numOfCreatedAddOnModules", numOfCreatedAddOnModules)\r
260                    execution.setVariable("RollbackData", rollbackData)\r
261            }catch(Exception e){\r
262                    utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)\r
263                    exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())\r
264            }\r
265            logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)\r
266    }\r
267    \r
268    public void finishProcessingInitialCountDeployment(Execution execution){\r
269            def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
270            execution.setVariable("prefix", Prefix)\r
271            logDebug(" ======== STARTED finishProcessingInitialCountDeployment ======== ", isDebugLogEnabled)\r
272            \r
273            try {\r
274                    int addOnModulesDeployed = execution.getVariable("addOnModulesDeployed")\r
275                    execution.setVariable("addOnModulesDeployed", addOnModulesDeployed + 1)\r
276            }catch(Exception e){\r
277                    utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)\r
278                    exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())\r
279            }\r
280            logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)\r
281    }\r
282    \r
283    \r
284    \r
285 }\r