Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoUpdateVnfAndModules.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\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.delegate.DelegateExecution;\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(DelegateExecution 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(DelegateExecution 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                                                                 \r
222                                                                 String volumeGroupId = ''\r
223                                                                 \r
224                                                                 logDebug("Next module!", isDebugLogEnabled)\r
225                                                                 def vfModuleRelationships = vfModules[i].'**'.findAll {it.name() == 'relationship-data'}\r
226                                                                 if (vfModuleRelationships.size() > 0) {\r
227                                                                         for (j in 0..vfModuleRelationships.size()-1) {                                                                          \r
228                                                                                 if (vfModuleRelationships[j] != null) {\r
229                                                                         \r
230                                                                                         def relationshipKey = vfModuleRelationships[j].'**'.findAll {it.name() == 'relationship-key'}                                                                                   \r
231                                                                                 \r
232                                                                                         if (relationshipKey[0] == 'volume-group.volume-group-id') {\r
233                                                                                                 def relationshipValue = vfModuleRelationships[j].'**'.findAll {it.name() == 'relationship-value'}\r
234                                                                                                 volumeGroupId = relationshipValue[0]\r
235                                                                                                 break\r
236                                                                                         }\r
237                                                                                 }\r
238                                                                         }\r
239                                                                 }\r
240                                                                 \r
241                                                                 vfModuleEntry.put("volumeGroupId", volumeGroupId)\r
242                                                                 logDebug("volumeGroupId is: " + volumeGroupId, isDebugLogEnabled)\r
243 \r
244                                                                 // Save base vf module to add it to the start of the list later\r
245                                                                 if (isBaseVfModule == "true") {                                                                 \r
246                                                                         vfModuleBaseEntry = vfModuleEntry\r
247                                                                 }\r
248                                                                 else {                                          \r
249                                                                         vfModulesList.add(vfModuleEntry)\r
250                                                                 }\r
251                                                         }\r
252                                                         // Start the list with the base module if any\r
253                                                         if (vfModuleBaseEntry != null) {\r
254                                                                 vfModulesList.add(0, vfModuleBaseEntry)\r
255                                                         }                                       \r
256                                                 }\r
257                                                 \r
258                                         }                                       \r
259                                 }\r
260                                 else {\r
261                                         logDebug('Response code from AAI GET is: ' + response.getStatusCode(), isDebugLogEnabled)\r
262                                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Response code from AAI GET is: ' + response.getStatusCode())\r
263                                 }\r
264                                 execution.setVariable("DUVAM_vfModules", vfModulesList)\r
265                         } catch (Exception ex) {\r
266                                 ex.printStackTrace()\r
267                                 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)\r
268                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'AAI GET Failed:' + ex.getMessage())\r
269                         }\r
270                         logDebug('Exited ' + method, isDebugLogEnabled)\r
271                 } catch (BpmnError e) {\r
272                         throw e;\r
273                 } catch (Exception e) {\r
274                         logError('Caught exception in ' + method, e)\r
275                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAIVfModule(): ' + e.getMessage())\r
276                 }\r
277         }\r
278         \r
279         public void prepareNextModuleToUpdate(DelegateExecution execution){\r
280                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
281                 execution.setVariable("prefix", Prefix)\r
282                 logDebug(" ======== STARTED prepareNextModuleToUpdate ======== ", isDebugLogEnabled)\r
283                 \r
284                 try {\r
285                         int i = execution.getVariable("DUVAM_nextModule")\r
286                         def vfModules = execution.getVariable("DUVAM_vfModules")\r
287                         def vfModule = vfModules[i]\r
288                         \r
289                         def vfModuleId = vfModule.get("vfModuleId")\r
290                         execution.setVariable("DUVAM_vfModuleId", vfModuleId)\r
291                         \r
292                         def vfModuleName = vfModule.get("vfModuleName")\r
293                         execution.setVariable("DUVAM_vfModuleName", vfModuleName)\r
294                         \r
295                         def isBaseVfModule = vfModule.get("isBaseVfModule")\r
296                         execution.setVariable("DUVAM_isBaseVfModule", isBaseVfModule)\r
297                         \r
298                         String modelInvariantUuid = vfModule.get("modelInvariantUuid")\r
299                         logDebug("ModelInvariantUuid: " + modelInvariantUuid, isDebugLogEnabled)                        \r
300                         \r
301                         def volumeGroupId = vfModule.get("volumeGroupId")\r
302                         execution.setVariable("DUVAM_volumeGroupId", volumeGroupId)\r
303 \r
304                         execution.setVariable("DUVAM_volumeGroupName", "")\r
305                         \r
306                         VnfResource vnfResource = (VnfResource) execution.getVariable("vnfResourceDecomposition")\r
307                         List<ModuleResource> moduleResources = vnfResource.getVfModules()\r
308                         \r
309                         for (j in 0..moduleResources.size()-1) {                                \r
310                                 ModelInfo modelInfo = moduleResources[j].getModelInfo()\r
311                                 String modelInvariantUuidFromDecomposition = modelInfo.getModelInvariantUuid()\r
312                                 logDebug("modelInvariantUuidFromDecomposition: " + modelInvariantUuidFromDecomposition, isDebugLogEnabled)\r
313                                 \r
314                                 if (modelInvariantUuid.equals(modelInvariantUuidFromDecomposition)) {\r
315                                         String vfModuleModelInfo = modelInfo.toJsonString()\r
316                                         String vfModuleModelInfoValue = jsonUtil.getJsonValue(vfModuleModelInfo, "modelInfo")\r
317                                         execution.setVariable("DUVAM_vfModuleModelInfo", vfModuleModelInfoValue)\r
318                                         logDebug("vfModuleModelInfo: " + vfModuleModelInfoValue, isDebugLogEnabled)\r
319                                         break\r
320                                 }\r
321                                 \r
322                         }                       \r
323                         \r
324                 }catch(Exception e){\r
325                         utils.log("ERROR", "Exception Occured Processing preProcessAddOnModule. Exception is:\n" + e, isDebugLogEnabled)\r
326                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during prepareNextModuleToUpdate Method:\n" + e.getMessage())\r
327                 }\r
328                 logDebug("======== COMPLETED prepareNextModuleToUpdate ======== ", isDebugLogEnabled)\r
329         }\r
330         \r
331         \r
332         /**\r
333          * Prepare a Request for invoking the UpdateAAIGenericVnf subflow.\r
334          *\r
335          * @param execution The flow's execution instance.\r
336          */\r
337         public void prepUpdateAAIGenericVnf(DelegateExecution execution) {\r
338                 def method = getClass().getSimpleName() + '.prepUpdateAAIGenericVnf(' +\r
339                         'execution=' + execution.getId() +\r
340                         ')'\r
341                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
342                 logDebug('Entered ' + method, isDebugLogEnabled)\r
343  \r
344                 try {                   \r
345                         def vnfId = execution.getVariable('vnfId')\r
346                         VnfResource vnfResource = (VnfResource) execution.getVariable("vnfResourceDecomposition")\r
347                         ModelInfo vnfDecompModelInfo = vnfResource.getModelInfo()\r
348                         String vnfModelInfo = execution.getVariable("vnfModelInfo")\r
349                         String modelUuid = execution.getVariable("DUVAM_modelUuid")\r
350                         if (modelUuid == null || modelUuid.isEmpty()) {\r
351                                 modelUuid = vnfDecompModelInfo.getModelUuid()                   \r
352                         }\r
353                         String modelCustomizationUuid = execution.getVariable("DUVAM_modelCustomizationUuid")\r
354                         if (modelCustomizationUuid == null || modelCustomizationUuid.isEmpty()) {\r
355                                 modelCustomizationUuid = vnfDecompModelInfo.getModelCustomizationUuid()\r
356                         }                               \r
357                         String nfType = vnfResource.getNfType()\r
358                         String nfTypeString = ''\r
359                         if (nfType != null && !nfType.isEmpty()) {\r
360                                 nfTypeString = "<nf-type>" + nfType + "</nf-type>"\r
361                         }                       \r
362                         String nfRole = vnfResource.getNfRole()\r
363                         String nfRoleString = ''\r
364                         if (nfRole != null && !nfRole.isEmpty()) {\r
365                                 nfRoleString = "<nf-role>" + nfRole + "</nf-role>"\r
366                         }\r
367                         String nfFunction = vnfResource.getNfFunction()\r
368                         String nfFunctionString = ''\r
369                         if (nfFunction != null && !nfFunction.isEmpty()) {\r
370                                 nfFunctionString = "<nf-function>" + nfFunction + "</nf-function>"\r
371                         }\r
372                         String nfNamingCode = vnfResource.getNfNamingCode()\r
373                         String nfNamingCodeString = ''\r
374                         if (nfNamingCode != null && !nfNamingCode.isEmpty()) {\r
375                                 nfNamingCodeString = "<nf-naming-code>" + nfNamingCode + "</nf-naming-code>"\r
376                         }                       \r
377  \r
378                         String updateAAIGenericVnfRequest = """\r
379                                         <UpdateAAIGenericVnfRequest>\r
380                                                 <vnf-id>${vnfId}</vnf-id>\r
381                                                 <model-version-id>${modelUuid}</model-version-id>\r
382                                                 <model-customization-id>${modelCustomizationUuid}</model-customization-id>\r
383                                                 ${nfTypeString}\r
384                                                 ${nfRoleString}\r
385                                                 ${nfFunctionString}\r
386                                                 ${nfNamingCodeString}\r
387                                         </UpdateAAIGenericVnfRequest>\r
388                                 """\r
389                                 updateAAIGenericVnfRequest = utils.formatXml(updateAAIGenericVnfRequest)\r
390                                 execution.setVariable('DUVAM_updateAAIGenericVnfRequest', updateAAIGenericVnfRequest)\r
391                                 utils.logAudit("updateAAIGenericVnfRequest : " + updateAAIGenericVnfRequest)\r
392                                 logDebug('Request for UpdateAAIGenericVnf:\n' + updateAAIGenericVnfRequest, isDebugLogEnabled)\r
393  \r
394  \r
395                         logDebug('Exited ' + method, isDebugLogEnabled)\r
396                 } catch (BpmnError e) {\r
397                         throw e;\r
398                 } catch (Exception e) {\r
399                         logError('Caught exception in ' + method, e)\r
400                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepUpdateAAIGenericVnf(): ' + e.getMessage())\r
401                 }\r
402         }\r
403         \r
404         /**\r
405          * APP-C Call - placeholder.\r
406          *\r
407          * @param execution The flow's execution instance.\r
408          */\r
409         public void callAppCf(DelegateExecution execution) {\r
410                 def method = getClass().getSimpleName() + '.callAppC(' +\r
411                         'execution=' + execution.getId() +\r
412                         ')'\r
413                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
414                 logDebug('Entered ' + method, isDebugLogEnabled)        \r
415         }\r
416 }\r