7d7505f307e336f4417686a7f51b7e4c216d1743
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoUpdateVnfAndModules.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.io.ObjectInputStream.BlockDataInputStream\r
23 import java.util.UUID;\r
24 \r
25 import org.json.JSONObject;\r
26 import org.json.JSONArray;\r
27 import org.camunda.bpm.engine.delegate.BpmnError\r
28 import org.camunda.bpm.engine.runtime.Execution;\r
29 import org.hibernate.jpa.criteria.predicate.IsEmptyPredicate\r
30 \r
31 import static org.apache.commons.lang3.StringUtils.*;\r
32 \r
33 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
34 import org.openecomp.mso.rest.APIResponse\r
35 import org.openecomp.mso.rest.RESTClient\r
36 import org.openecomp.mso.rest.RESTConfig\r
37 import org.openecomp.mso.bpmn.common.scripts.AaiUtil\r
38 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
39 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
40 import org.openecomp.mso.bpmn.core.domain.ModelInfo\r
41 import org.openecomp.mso.bpmn.core.domain.ModuleResource\r
42 import org.openecomp.mso.bpmn.core.domain.VnfResource\r
43 import org.openecomp.mso.bpmn.core.WorkflowException\r
44 import org.springframework.web.util.UriUtils;\r
45 \r
46 /**\r
47  * This class supports the VID Flow\r
48  * with the update of a generic vnf and related VF modules.\r
49  */\r
50 class DoUpdateVnfAndModules extends AbstractServiceTaskProcessor {\r
51 \r
52         String Prefix="DUVAM_"\r
53         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
54         JsonUtils jsonUtil = new JsonUtils()    \r
55 \r
56         /**\r
57          * This method gets and validates the incoming\r
58          * request.\r
59          *\r
60          * @param - execution\r
61          *      \r
62          */\r
63         public void preProcessRequest(Execution execution) {\r
64                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
65                 execution.setVariable("prefix",Prefix)\r
66                 utils.log("DEBUG", " *** STARTED DoUpdateVnfAndModules PreProcessRequest Process*** ", isDebugEnabled)\r
67 \r
68                 try{\r
69                         // Get Variables                                \r
70                         \r
71                         String requestId = execution.getVariable("msoRequestId")\r
72                         execution.setVariable("requestId", 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("DUVAM_source", source)\r
84                         utils.log("DEBUG", "Incoming Source is: " + source, isDebugEnabled)\r
85                         \r
86                         String sdncVersion = execution.getVariable("sdncVersion")\r
87                         if (sdncVersion == null) {\r
88                                 sdncVersion = "1702"\r
89                         }\r
90                         execution.setVariable("DUVAM_sdncVersion", sdncVersion)\r
91                         utils.log("DEBUG", "Incoming Sdnc Version is: " + sdncVersion, isDebugEnabled)\r
92                         \r
93                         VnfResource vnfResource = (VnfResource) execution.getVariable("vnfResourceDecomposition")                       \r
94                         \r
95                         String vnfModelInfo = execution.getVariable("vnfModelInfo")\r
96                         String serviceModelInfo = execution.getVariable("serviceModelInfo")\r
97                                 \r
98                         String serviceId = execution.getVariable("productFamilyId")\r
99                         execution.setVariable("DUVAM_serviceId", serviceId)\r
100                         utils.log("DEBUG", "Incoming Service Id is: " + serviceId, isDebugEnabled)                              \r
101                         \r
102                         String modelUuid = jsonUtil.getJsonValue(vnfModelInfo, "modelUuid")                     \r
103                         execution.setVariable("DUVAM_modelUuid", modelUuid)\r
104                         utils.log("DEBUG", "Incoming modelUuid is: " + modelUuid, isDebugEnabled)                               \r
105                                 \r
106                         String modelCustomizationUuid = jsonUtil.getJsonValue(vnfModelInfo, "modelCustomizationUuid")                   \r
107                         execution.setVariable("DUVAM_modelCustomizationUuid", modelCustomizationUuid)\r
108                         utils.log("DEBUG", "Incoming Model Customization Uuid is: " + modelCustomizationUuid, isDebugEnabled)\r
109                                         \r
110                         String cloudSiteId = execution.getVariable("lcpCloudRegionId")\r
111                         execution.setVariable("DUVAM_cloudSiteId", cloudSiteId)\r
112                         utils.log("DEBUG", "Incoming Cloud Site Id is: " + cloudSiteId, isDebugEnabled)\r
113                                         \r
114                         String tenantId = execution.getVariable("tenantId")\r
115                         execution.setVariable("DUVAM_tenantId", tenantId)\r
116                         utils.log("DEBUG", "Incoming Tenant Id is: " + tenantId, isDebugEnabled)\r
117                                 \r
118                         String globalSubscriberId = execution.getVariable("globalSubscriberId")\r
119                         if (globalSubscriberId == null) {\r
120                                 globalSubscriberId = ""\r
121                         }\r
122                         execution.setVariable("DUVAM_globalSubscriberId", globalSubscriberId)\r
123                         utils.log("DEBUG", "Incoming Global Subscriber Id is: " + globalSubscriberId, isDebugEnabled)\r
124                         \r
125                         execution.setVariable("DUVAM_moduleCount", 0)\r
126                         execution.setVariable("DUVAM_nextModule", 0)\r
127                         \r
128                         \r
129                 }catch(BpmnError b){\r
130                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
131                         throw b\r
132                 }catch(Exception e){\r
133                         utils.log("DEBUG", " Error Occured in DoUpdateVnfAndModules PreProcessRequest method!" + e.getMessage(), isDebugEnabled)\r
134                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoUpdateVnfAndModules PreProcessRequest")\r
135 \r
136                 }\r
137                 utils.log("DEBUG", "*** COMPLETED DoUpdateVnfAndModules PreProcessRequest Process ***", isDebugEnabled)\r
138         }       \r
139         \r
140         /**\r
141          * Using the received vnfId and vfModuleId, query AAI to get the corresponding VNF info.\r
142          * A 200 response is expected with the VNF info in the response body. Will find out the base module info.\r
143          *\r
144          * @param execution The flow's execution instance.\r
145          */\r
146         public void queryAAIVfModule(Execution execution) {\r
147                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")\r
148                 def method = getClass().getSimpleName() + '.queryAAIVfModule(' +\r
149                         'execution=' + execution.getId() +\r
150                         ')'\r
151                 logDebug('Entered ' + method, isDebugLogEnabled)\r
152 \r
153                 try {\r
154                         def vnfId = execution.getVariable('vnfId')\r
155                         \r
156                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
157                         String  aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)\r
158                         logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)\r
159 \r
160                         String endPoint = execution.getVariable("URN_aai_endpoint") + "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"\r
161                         utils.logAudit("AAI endPoint: " + endPoint)\r
162 \r
163                         try {\r
164                                 RESTConfig config = new RESTConfig(endPoint);\r
165                                 def responseData = ''\r
166                                 def aaiRequestId = UUID.randomUUID().toString()\r
167                                 RESTClient client = new RESTClient(config).\r
168                                         addHeader('X-TransactionId', aaiRequestId).\r
169                                         addHeader('X-FromAppId', 'MSO').\r
170                                         addHeader('Content-Type', 'application/xml').\r
171                                         addHeader('Accept','application/xml');\r
172                                 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)\r
173                                 APIResponse response = client.httpGet()\r
174                                 utils.logAudit("createVfModule - invoking httpGet() to AAI")\r
175 \r
176                                 responseData = response.getResponseBodyAsString()\r
177                                 if (responseData != null) {\r
178                                         logDebug("Received generic VNF data: " + responseData, isDebugLogEnabled)\r
179 \r
180                                 }\r
181 \r
182                                 utils.logAudit("createVfModule - queryAAIVfModule Response: " + responseData)\r
183                                 utils.logAudit("createVfModule - queryAAIVfModule ResponseCode: " + response.getStatusCode())\r
184 \r
185                                 execution.setVariable('DUVAM_queryAAIVfModuleResponseCode', response.getStatusCode())\r
186                                 execution.setVariable('DUVAM_queryAAIVfModuleResponse', responseData)\r
187                                 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)\r
188                                 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)\r
189                                 //Map<String, String>[] vfModules = new HashMap<String,String>[]\r
190                                 def vfModulesList = new ArrayList<Map<String,String>>()\r
191                                 def vfModules = null\r
192                                 def vfModuleBaseEntry = null\r
193                                 if (response.getStatusCode() == 200) {\r
194                                         // Parse the VNF record from A&AI to find base module info\r
195                                         logDebug('Parsing the VNF data to find base module info', isDebugLogEnabled)\r
196                                         if (responseData != null) {\r
197                                                 def vfModulesText = utils.getNodeXml(responseData, "vf-modules")\r
198                                                 logDebug("vModulesText: " + vfModulesText, isDebugLogEnabled)\r
199                                                 if (vfModulesText != null && !vfModulesText.trim().isEmpty()) {\r
200                                                         def xmlVfModules= new XmlSlurper().parseText(vfModulesText)\r
201                                                         vfModules = xmlVfModules.'**'.findAll {it.name() == "vf-module"}\r
202                                                         execution.setVariable("DUVAM_moduleCount", vfModules.size())\r
203                                                         int vfModulesSize = 0\r
204                                                         for (i in 0..vfModules.size()-1) {\r
205                                                                 def vfModuleXml = groovy.xml.XmlUtil.serialize(vfModules[i])\r
206                                                         \r
207                                                                 Map<String, String> vfModuleEntry = new HashMap<String, String>()\r
208                                                                 def vfModuleId = utils.getNodeText1(vfModuleXml, "vf-module-id")\r
209                                                                 vfModuleEntry.put("vfModuleId", vfModuleId)\r
210                                                                 def vfModuleName = utils.getNodeText1(vfModuleXml, "vf-module-name")\r
211                                                                 vfModuleEntry.put("vfModuleName", vfModuleName)\r
212                                                                 def modelInvariantUuid = utils.getNodeText1(vfModuleXml, "model-invariant-id")\r
213                                                                 vfModuleEntry.put("modelInvariantUuid", modelInvariantUuid)\r
214                                                                 def modelUuid = utils.getNodeText1(vfModuleXml, "model-version-id")\r
215                                                                 vfModuleEntry.put("modelUuid", modelUuid)\r
216                                                                 def modelCustomizationUuid = utils.getNodeText1(vfModuleXml, "model-customization-id")\r
217                                                                 vfModuleEntry.put("modelCustomizationUuid", modelCustomizationUuid)\r
218                                                                                                                 \r
219                                                                 def isBaseVfModule = utils.getNodeText(vfModuleXml, "is-base-vf-module")\r
220                                                                 vfModuleEntry.put("isBaseVfModule", isBaseVfModule)\r
221                                                                 // Save base vf module to add it to the start of the list later\r
222                                                                 if (isBaseVfModule == "true") {                                                                 \r
223                                                                         vfModuleBaseEntry = vfModuleEntry\r
224                                                                 }\r
225                                                                 else {                                          \r
226                                                                         vfModulesList.add(vfModuleEntry)\r
227                                                                 }\r
228                                                         }\r
229                                                         // Start the list with the base module if any\r
230                                                         if (vfModuleBaseEntry != null) {\r
231                                                                 vfModulesList.add(0, vfModuleBaseEntry)\r
232                                                         }                                       \r
233                                                 }\r
234                                                 \r
235                                         }                                       \r
236                                 }\r
237                                 else {\r
238                                         logDebug('Response code from AAI GET is: ' + response.getStatusCode(), isDebugLogEnabled)\r
239                                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Response code from AAI GET is: ' + response.getStatusCode())\r
240                                 }\r
241                                 execution.setVariable("DUVAM_vfModules", vfModulesList)\r
242                         } catch (Exception ex) {\r
243                                 ex.printStackTrace()\r
244                                 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)\r
245                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())\r
246                         }\r
247                         logDebug('Exited ' + method, isDebugLogEnabled)\r
248                 } catch (BpmnError e) {\r
249                         throw e;\r
250                 } catch (Exception e) {\r
251                         logError('Caught exception in ' + method, e)\r
252                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAIVfModule(): ' + e.getMessage())\r
253                 }\r
254         }\r
255         \r
256         public void prepareNextModuleToUpdate(Execution execution){\r
257                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
258                 execution.setVariable("prefix", Prefix)\r
259                 logDebug(" ======== STARTED prepareNextModuleToUpdate ======== ", isDebugLogEnabled)\r
260                 \r
261                 try {\r
262                         int i = execution.getVariable("DUVAM_nextModule")\r
263                         def vfModules = execution.getVariable("DUVAM_vfModules")\r
264                         def vfModule = vfModules[i]\r
265                         \r
266                         def vfModuleId = vfModule.get("vfModuleId")\r
267                         execution.setVariable("DUVAM_vfModuleId", vfModuleId)\r
268                         \r
269                         def vfModuleName = vfModule.get("vfModuleName")\r
270                         execution.setVariable("DUVAM_vfModuleName", vfModuleName)\r
271                         \r
272                         def isBaseVfModule = vfModule.get("isBaseVfModule")\r
273                         execution.setVariable("DUVAM_isBaseVfModule", isBaseVfModule)\r
274                         \r
275                         String modelInvariantUuid = vfModule.get("modelInvariantUuid")\r
276                         logDebug("ModelInvariantUuid: " + modelInvariantUuid, isDebugLogEnabled)                        \r
277                         \r
278                         execution.setVariable("DUVAM_volumeGroupId", "")\r
279                         execution.setVariable("DUVAM_volumeGroupName", "")\r
280                         \r
281                         VnfResource vnfResource = (VnfResource) execution.getVariable("vnfResourceDecomposition")\r
282                         List<ModuleResource> moduleResources = vnfResource.getVfModules()\r
283                         \r
284                         for (j in 0..moduleResources.size()-1) {                                \r
285                                 ModelInfo modelInfo = moduleResources[j].getModelInfo()\r
286                                 String modelInvariantUuidFromDecomposition = modelInfo.getModelInvariantUuid()\r
287                                 logDebug("modelInvariantUuidFromDecomposition: " + modelInvariantUuidFromDecomposition, isDebugLogEnabled)\r
288                                 \r
289                                 if (modelInvariantUuid.equals(modelInvariantUuidFromDecomposition)) {\r
290                                         String vfModuleModelInfo = modelInfo.toJsonString()\r
291                                         String vfModuleModelInfoValue = jsonUtil.getJsonValue(vfModuleModelInfo, "modelInfo")\r
292                                         execution.setVariable("DUVAM_vfModuleModelInfo", vfModuleModelInfoValue)\r
293                                         logDebug("vfModuleModelInfo: " + vfModuleModelInfoValue, isDebugLogEnabled)\r
294                                         break\r
295                                 }\r
296                                 \r
297                         }                       \r
298                         \r
299                 }catch(Exception e){\r
300                         utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)\r
301                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during prepareNextModuleToUpdate Method:\n" + e.getMessage())\r
302                 }\r
303                 logDebug("======== COMPLETED prepareNextModuleToUpdate ======== ", isDebugLogEnabled)\r
304         }\r
305         \r
306         \r
307         /**\r
308          * Prepare a Request for invoking the UpdateAAIGenericVnf subflow.\r
309          *\r
310          * @param execution The flow's execution instance.\r
311          */\r
312         public void prepUpdateAAIGenericVnf(Execution execution) {\r
313                 def method = getClass().getSimpleName() + '.prepUpdateAAIGenericVnf(' +\r
314                         'execution=' + execution.getId() +\r
315                         ')'\r
316                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
317                 logDebug('Entered ' + method, isDebugLogEnabled)\r
318  \r
319                 try {                   \r
320                         def vnfId = execution.getVariable('vnfId')\r
321                         VnfResource vnfResource = (VnfResource) execution.getVariable("vnfResourceDecomposition")\r
322                         ModelInfo vnfDecompModelInfo = vnfResource.getModelInfo()\r
323                         String vnfModelInfo = execution.getVariable("vnfModelInfo")\r
324                         String modelUuid = execution.getVariable("DUVAM_modelUuid")\r
325                         if (modelUuid == null || modelUuid.isEmpty()) {\r
326                                 modelUuid = vnfDecompModelInfo.getModelUuid()                   \r
327                         }\r
328                         String modelCustomizationUuid = execution.getVariable("DUVAM_modelCustomizationUuid")\r
329                         if (modelCustomizationUuid == null || modelCustomizationUuid.isEmpty()) {\r
330                                 modelCustomizationUuid = vnfDecompModelInfo.getModelCustomizationUuid()\r
331                         }                               \r
332                         String nfType = vnfResource.getNfType()\r
333                         String nfTypeString = ''\r
334                         if (nfType != null && !nfType.isEmpty()) {\r
335                                 nfTypeString = "<nf-type>" + nfType + "</nf-type>"\r
336                         }                       \r
337                         String nfRole = vnfResource.getNfRole()\r
338                         String nfRoleString = ''\r
339                         if (nfRole != null && !nfRole.isEmpty()) {\r
340                                 nfRoleString = "<nf-role>" + nfRole + "</nf-role>"\r
341                         }\r
342                         String nfFunction = vnfResource.getNfFunction()\r
343                         String nfFunctionString = ''\r
344                         if (nfFunction != null && !nfFunction.isEmpty()) {\r
345                                 nfFunctionString = "<nf-function>" + nfFunction + "</nf-function>"\r
346                         }\r
347                         String nfNamingCode = vnfResource.getNfNamingCode()\r
348                         String nfNamingCodeString = ''\r
349                         if (nfNamingCode != null && !nfNamingCode.isEmpty()) {\r
350                                 nfNamingCodeString = "<nf-naming-code>" + nfNamingCode + "</nf-naming-code>"\r
351                         }                       \r
352  \r
353                         String updateAAIGenericVnfRequest = """\r
354                                         <UpdateAAIGenericVnfRequest>\r
355                                                 <vnf-id>${vnfId}</vnf-id>\r
356                                                 <model-version-id>${modelUuid}</model-version-id>\r
357                                                 <model-customization-id>${modelCustomizationUuid}</model-customization-id>\r
358                                                 ${nfTypeString}\r
359                                                 ${nfRoleString}\r
360                                                 ${nfFunctionString}\r
361                                                 ${nfNamingCodeString}\r
362                                         </UpdateAAIGenericVnfRequest>\r
363                                 """\r
364                                 updateAAIGenericVnfRequest = utils.formatXml(updateAAIGenericVnfRequest)\r
365                                 execution.setVariable('DUVAM_updateAAIGenericVnfRequest', updateAAIGenericVnfRequest)\r
366                                 utils.logAudit("updateAAIGenericVnfRequest : " + updateAAIGenericVnfRequest)\r
367                                 logDebug('Request for UpdateAAIGenericVnf:\n' + updateAAIGenericVnfRequest, isDebugLogEnabled)\r
368  \r
369  \r
370                         logDebug('Exited ' + method, isDebugLogEnabled)\r
371                 } catch (BpmnError e) {\r
372                         throw e;\r
373                 } catch (Exception e) {\r
374                         logError('Caught exception in ' + method, e)\r
375                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepUpdateAAIGenericVnf(): ' + e.getMessage())\r
376                 }\r
377         }\r
378         \r
379         /**\r
380          * APP-C Call - placeholder.\r
381          *\r
382          * @param execution The flow's execution instance.\r
383          */\r
384         public void callAppCf(Execution execution) {\r
385                 def method = getClass().getSimpleName() + '.callAppC(' +\r
386                         'execution=' + execution.getId() +\r
387                         ')'\r
388                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
389                 logDebug('Entered ' + method, isDebugLogEnabled)        \r
390         }\r
391 }\r