[MSO-8] Second step of the rebase for MSO
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoDeleteVnfAndModules.groovy
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.rest.APIResponse\r
34 import org.openecomp.mso.rest.RESTClient\r
35 import org.openecomp.mso.rest.RESTConfig\r
36 import org.openecomp.mso.bpmn.common.scripts.AaiUtil\r
37 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
38 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils\r
39 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
40 import org.openecomp.mso.bpmn.common.scripts.VidUtils\r
41 import org.openecomp.mso.bpmn.core.RollbackData\r
42 import org.openecomp.mso.bpmn.core.WorkflowException\r
43 \r
44 /**\r
45  * This class supports the macro VID Flow\r
46  * with the deletion of a generic vnf and related VF modules.\r
47  */\r
48 class DoDeleteVnfAndModules extends AbstractServiceTaskProcessor {\r
49 \r
50         String Prefix="DDVAM_"\r
51         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
52         JsonUtils jsonUtil = new JsonUtils()\r
53         VidUtils vidUtils = new VidUtils(this)  \r
54 \r
55         /**\r
56          * This method gets and validates the incoming\r
57          * request.\r
58          *\r
59          * @param - execution\r
60          *      \r
61          */\r
62         public void preProcessRequest(Execution execution) {\r
63                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
64                 execution.setVariable("prefix",Prefix)\r
65                 utils.log("DEBUG", " *** STARTED DoDeleteVnfAndModules PreProcessRequest Process*** ", isDebugEnabled)\r
66 \r
67                 try{\r
68                         // Get Variables                                \r
69                         \r
70                         String cloudConfiguration = execution.getVariable("cloudConfiguration")                 \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 vnfId = execution.getVariable("vnfId")                   \r
80                         utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)                      \r
81                         \r
82                         String source = "VID"\r
83                         execution.setVariable("source", source)\r
84                         utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)\r
85                         \r
86                         execution.setVariable("DDVAM_moduleCount", 0)\r
87                         execution.setVariable("DDVAM_nextModule", 0)\r
88                         \r
89                         \r
90                 }catch(BpmnError b){\r
91                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
92                         throw b\r
93                 }catch(Exception e){\r
94                         utils.log("DEBUG", " Error Occured in DoCreateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugEnabled)\r
95                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnf PreProcessRequest")\r
96 \r
97                 }\r
98                 utils.log("DEBUG", "*** COMPLETED DoCreateVnfAndModules PreProcessRequest Process ***", isDebugEnabled)\r
99         }       \r
100 \r
101         \r
102         \r
103         public void preProcessAddOnModule(Execution execution){\r
104                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
105                 execution.setVariable("prefix", Prefix)\r
106                 logDebug(" ======== STARTED preProcessAddOnModule ======== ", isDebugLogEnabled)\r
107                 \r
108                 try {                   \r
109                         JSONArray addOnModules = (JSONArray) execution.getVariable("addOnModules")\r
110                         int addOnIndex = (int) execution.getVariable("addOnModulesDeployed")\r
111                         \r
112                         JSONObject addOnModule = addOnModules[addOnIndex]\r
113                         \r
114                         def newVfModuleId = UUID.randomUUID().toString()\r
115                         execution.setVariable("addOnVfModuleId", newVfModuleId)\r
116                         \r
117                         execution.setVariable("instancesOfThisModelDeployed", 0)\r
118                         \r
119                         JSONObject addOnVfModuleModelInfoObject = jsonUtil.getJsonValueForKey(addOnModule, "modelInfo")\r
120                         String addOnVfModuleModelInfo = addOnVfModuleModelInfoObject.toString()\r
121                         execution.setVariable("addOnVfModuleModelInfo", addOnVfModuleModelInfo)\r
122                         String addOnVfModuleLabel = jsonUtil.getJsonValueForKey(addOnModule, "vfModuleLabel")\r
123                         execution.setVariable("addOnVfModuleLabel", addOnVfModuleLabel)\r
124                         String addOnPersonaModelId = jsonUtil.getJsonValueForKey(addOnVfModuleModelInfoObject, "modelInvariantId")\r
125                         execution.setVariable("addOnPersonaModelId", addOnPersonaModelId)\r
126                         String addOnInitialCount = jsonUtil.getJsonValueForKey(addOnModule, "initialCount")\r
127                         execution.setVariable("initialCount", addOnInitialCount)\r
128                                         \r
129                 \r
130                 }catch(Exception e){\r
131                         utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)\r
132                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())\r
133                 }\r
134                 logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)\r
135         }\r
136         \r
137         /**\r
138          * Using the received vnfId and vfModuleId, query AAI to get the corresponding VNF info.\r
139          * A 200 response is expected with the VNF info in the response body. Will find out the base module info.\r
140          *\r
141          * @param execution The flow's execution instance.\r
142          */\r
143         public void queryAAIVfModule(Execution execution) {\r
144                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")\r
145                 def method = getClass().getSimpleName() + '.queryAAIVfModule(' +\r
146                         'execution=' + execution.getId() +\r
147                         ')'\r
148                 logDebug('Entered ' + method, isDebugLogEnabled)\r
149 \r
150                 try {\r
151                         def vnfId = execution.getVariable('DvnfId')\r
152                         \r
153                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
154                         String  aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)\r
155                         logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)\r
156 \r
157                         String endPoint = execution.getVariable("URN_aai_endpoint") + "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"\r
158                         utils.logAudit("AAI endPoint: " + endPoint)\r
159 \r
160                         try {\r
161                                 RESTConfig config = new RESTConfig(endPoint);\r
162                                 def responseData = ''\r
163                                 def aaiRequestId = UUID.randomUUID().toString()\r
164                                 RESTClient client = new RESTClient(config).\r
165                                         addHeader('X-TransactionId', aaiRequestId).\r
166                                         addHeader('X-FromAppId', 'MSO').\r
167                                         addHeader('Content-Type', 'application/xml').\r
168                                         addHeader('Accept','application/xml');\r
169                                 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)\r
170                                 APIResponse response = client.httpGet()\r
171                                 utils.logAudit("createVfModule - invoking httpGet() to AAI")\r
172 \r
173                                 responseData = response.getResponseBodyAsString()\r
174                                 if (responseData != null) {\r
175                                         logDebug("Received generic VNF data: " + responseData, isDebugLogEnabled)\r
176 \r
177                                 }\r
178 \r
179                                 utils.logAudit("createVfModule - queryAAIVfModule Response: " + responseData)\r
180                                 utils.logAudit("createVfModule - queryAAIVfModule ResponseCode: " + response.getStatusCode())\r
181 \r
182                                 execution.setVariable('DCVFM_queryAAIVfModuleResponseCode', response.getStatusCode())\r
183                                 execution.setVariable('DCVFM_queryAAIVfModuleResponse', responseData)\r
184                                 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)\r
185                                 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)\r
186                                 //Map<String, String>[] vfModules = new HashMap<String,String>[]\r
187                                 List<Map<String,String>> vfModulesList = new ArrayList<Map<String,String>>();\r
188                                 if (response.getStatusCode() == 200) {\r
189                                         // Parse the VNF record from A&AI to find base module info\r
190                                         logDebug('Parsing the VNF data to find base module info', isDebugLogEnabled)\r
191                                         if (responseData != null) {\r
192                                                 def vfModulesText = utils.getNodeXml(responseData, "vf-modules")\r
193                                                 def xmlVfModules= new XmlSlurper().parseText(vfModulesText)\r
194                                                 def vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}\r
195                                                 execution.setVariable("DDVAM_moduleCount", vfModules.size())\r
196                                                 int vfModulesSize = 0\r
197                                                 for (i in 0..vfModules.size()-1) {\r
198                                                         def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])\r
199                                                         \r
200                                                         Map<String, String> vfModuleEntry = new HashMap<String, String>()\r
201                                                         def vfModuleId = utils.getNodeText1(vfModuleXml, "vf-module-id")\r
202                                                         vfModuleEntry.put("vfModuleId", vfModuleName)\r
203                                                         def vfModuleName = utils.getNodeText1(vfModuleXml, "vf-module-name")\r
204                                                         vfModuleEntry.put("vfModuleName", vfModuleName)         \r
205                                                         vfModulesList.add(vfModuleEntry)                                        \r
206                                                 }\r
207                                                 \r
208                                         }                                       \r
209                                 }\r
210                                 execution.setVariable("DDVAM_vfModules", vfModules)\r
211                         } catch (Exception ex) {\r
212                                 ex.printStackTrace()\r
213                                 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)\r
214                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())\r
215                         }\r
216                         logDebug('Exited ' + method, isDebugLogEnabled)\r
217                 } catch (BpmnError e) {\r
218                         throw e;\r
219                 } catch (Exception e) {\r
220                         logError('Caught exception in ' + method, e)\r
221                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAIVfModule(): ' + e.getMessage())\r
222                 }\r
223         }\r
224         \r
225         public void prepareNextModuleToDelete(Execution execution){\r
226                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
227                 execution.setVariable("prefix", Prefix)\r
228                 logDebug(" ======== STARTED prepareNextModuleToDelete ======== ", isDebugLogEnabled)\r
229                 \r
230                 try {\r
231                         int i = execution.getVariable("DDVAM_nextModule")\r
232                         def vfModules = execution.getVariable("DDVAM_vfModules")\r
233                         def vfModule = vfModules[i]\r
234                         \r
235                         def vfModuleId = vfModule.get("vfModuleId")\r
236                         execution.setVariable("DDVAM_vfModuleId", vfModuleId)\r
237                         \r
238                         def vfModuleName = vfModule.get("vfModuleName")\r
239                         execution.setVariable("DDVAM_vfModuleName", vfModuleName)\r
240                         \r
241                         \r
242                         // HARDCODED FOR NOW\r
243                         def vfModuleModelInfo = ""\r
244                         execution.setVariable("DDVAM_vfModuleModelInfo", vfModuleModelInfo)                     \r
245                         \r
246                 }catch(Exception e){\r
247                         utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)\r
248                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessAddOnModule Method:\n" + e.getMessage())\r
249                 }\r
250                 logDebug("======== COMPLETED preProcessSDNCAssignRequest ======== ", isDebugLogEnabled)\r
251         }\r
252         \r
253         \r
254         \r
255         \r
256 }\r