Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / UpdateAAIGenericVnf.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 \r
21 package org.openecomp.mso.bpmn.common.scripts\r
22 \r
23 import org.camunda.bpm.engine.delegate.BpmnError\r
24 import org.camunda.bpm.engine.delegate.DelegateExecution\r
25 import org.openecomp.mso.bpmn.core.WorkflowException\r
26 import org.openecomp.mso.rest.APIResponse\r
27 import org.springframework.web.util.UriUtils\r
28 \r
29 \r
30 public class UpdateAAIGenericVnf extends AbstractServiceTaskProcessor {\r
31 \r
32         private XmlParser xmlParser = new XmlParser()\r
33         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
34 \r
35         /**\r
36          * Initialize the flow's variables.\r
37          *\r
38          * @param execution The flow's execution instance.\r
39          */\r
40         public void initProcessVariables(DelegateExecution execution) {\r
41                 execution.setVariable('prefix', 'UAAIGenVnf_')\r
42                 execution.setVariable('UAAIGenVnf_vnfId', null)\r
43                 execution.setVariable('UAAIGenVnf_personaModelId', null)\r
44                 execution.setVariable('UAAIGenVnf_personaModelVersion', null)\r
45                 execution.setVariable("UAAIGenVnf_ipv4OamAddress", null)\r
46                 execution.setVariable('UAAIGenVnf_managementV6Address', null)\r
47                 execution.setVariable('UAAIGenVnf_orchestrationStatus', null)\r
48                 execution.setVariable('UAAIGenVnf_getGenericVnfResponseCode' ,null)\r
49                 execution.setVariable('UAAIGenVnf_getGenericVnfResponse', '')\r
50                 execution.setVariable('UAAIGenVnf_updateGenericVnfResponseCode', null)\r
51                 execution.setVariable('UAAIGenVnf_updateGenericVnfResponse', '')\r
52         }\r
53 \r
54         /**\r
55          * Check for missing elements in the received request.\r
56          *\r
57          * @param execution The flow's execution instance.\r
58          */\r
59         public void preProcessRequest(DelegateExecution execution) {\r
60                 def method = getClass().getSimpleName() + '.preProcessRequest(' +\r
61                         'execution=' + execution.getId() +\r
62                         ')'\r
63                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
64                 logDebug('Entered ' + method, isDebugLogEnabled)\r
65 \r
66                 try {\r
67                         def xml = execution.getVariable('UpdateAAIGenericVnfRequest')\r
68                         logDebug('Received request xml:\n' + xml, isDebugLogEnabled)\r
69                         utils.logAudit("UpdateAAIGenericVnf Request XML: " + xml)\r
70                         initProcessVariables(execution)\r
71 \r
72                         def vnfId = getRequiredNodeText(execution, xml,'vnf-id')\r
73                         execution.setVariable('UAAIGenVnf_vnfId', vnfId)\r
74 \r
75                         def personaModelId = getNodeTextForce(xml,'persona-model-id')\r
76                         if (personaModelId != null && !personaModelId.isEmpty()) {\r
77                                 execution.setVariable('UAAIGenVnf_personaModelId', personaModelId)\r
78                         }\r
79 \r
80                         def personaModelVersion = getNodeTextForce(xml,'persona-model-version')\r
81                         if (personaModelVersion != null && !personaModelVersion.isEmpty()) {\r
82                                 execution.setVariable('UAAIGenVnf_personaModelVersion', personaModelVersion)\r
83                         }\r
84 \r
85                         def ipv4OamAddress = getNodeTextForce(xml, 'ipv4-oam-address')\r
86                         if (ipv4OamAddress != null && !ipv4OamAddress.isEmpty()) {\r
87                                 execution.setVariable('UAAIGenVnf_ipv4OamAddress', ipv4OamAddress)\r
88                         }\r
89 \r
90                         def managementV6Address = getNodeTextForce(xml, 'management-v6-address')\r
91                         if (managementV6Address != null && !managementV6Address.isEmpty()) {\r
92                                 execution.setVariable('UAAIGenVnf_managementV6Address', managementV6Address)\r
93                         }\r
94                         \r
95                         def orchestrationStatus = getNodeTextForce(xml, 'orchestration-status')\r
96                         if (orchestrationStatus != null && !orchestrationStatus.isEmpty()) {\r
97                                 execution.setVariable('UAAIGenVnf_orchestrationStatus', orchestrationStatus)\r
98                         }\r
99 \r
100                         logDebug('Exited ' + method, isDebugLogEnabled)\r
101                 } catch (BpmnError e) {\r
102                         throw e;\r
103                 } catch (Exception e) {\r
104                         logError('Caught exception in ' + method, e)\r
105                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())\r
106                 }\r
107         }\r
108 \r
109         /**\r
110          * Using the received vnfId, query AAI to get the corresponding Generic VNF.\r
111          * A 200 response is expected with the VF Module in the response body.\r
112          *\r
113          * @param execution The flow's execution instance.\r
114          */\r
115         public void getGenericVnf(DelegateExecution execution) {\r
116                 def method = getClass().getSimpleName() + '.getGenericVnf(' +\r
117                         'execution=' + execution.getId() +\r
118                         ')'\r
119                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
120                 logDebug('Entered ' + method, isDebugLogEnabled)\r
121 \r
122                 try {\r
123                         def vnfId = execution.getVariable('UAAIGenVnf_vnfId')\r
124 \r
125                         // Construct endpoint\r
126                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
127                         def aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)\r
128                         logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)\r
129                         String endPoint = execution.getVariable('URN_aai_endpoint') + aai_uri + '/' + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"\r
130 \r
131                         try {\r
132                                 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)\r
133                                 utils.logAudit("Sending GET to AAI endpoint: " + endPoint)\r
134 \r
135                                 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, endPoint)\r
136                                 def responseData = response.getResponseBodyAsString()\r
137                                 execution.setVariable('UAAIGenVnf_getGenericVnfResponseCode', response.getStatusCode())\r
138                                 execution.setVariable('UAAIGenVnf_getGenericVnfResponse', responseData)\r
139                                 utils.logAudit("UpdateAAIGenericVnf Response data: " + responseData)\r
140                                 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)\r
141                                 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)\r
142                         } catch (Exception ex) {\r
143                                 ex.printStackTrace()\r
144                                 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(),isDebugLogEnabled)\r
145                                 execution.setVariable('UAAIGenVnf_getGenericVnfResponseCode', 500)\r
146                                 execution.setVariable('UAAIGenVnf_getGenericVnfResponse', 'AAI GET Failed:' + ex.getMessage())\r
147                         }\r
148                         logDebug('Exited ' + method, isDebugLogEnabled)\r
149                 } catch (BpmnError e) {\r
150                         throw e;\r
151                 } catch (Exception e) {\r
152                         logError('Caught exception in ' + method, e)\r
153                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in getGenericVnf(): ' + e.getMessage())\r
154                 }\r
155         }\r
156 \r
157         /**\r
158          * Construct and send a PUT request to AAI to update the Generic VNF.\r
159          *\r
160          * @param execution The flow's execution instance.\r
161          */\r
162         public void updateGenericVnf(DelegateExecution execution) {\r
163                 def method = getClass().getSimpleName() + '.updateGenericVnf(' +\r
164                         'execution=' + execution.getId() +\r
165                         ')'\r
166                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
167                 logDebug('Entered ' + method, isDebugLogEnabled)\r
168 \r
169                 try {\r
170                         def vnfId = execution.getVariable('UAAIGenVnf_vnfId')\r
171                         def genericVnf = execution.getVariable('UAAIGenVnf_getGenericVnfResponse')\r
172                         def origRequest = execution.getVariable('UpdateAAIGenericVnfRequest')\r
173 \r
174                         utils.logAudit("UpdateGenericVnf Request: " + origRequest)\r
175                         // Confirm resource-version is in retrieved Generic VNF\r
176                         def Node genericVnfNode = xmlParser.parseText(genericVnf)\r
177                         if (utils.getChildNode(genericVnfNode, 'resource-version') == null) {\r
178                                 def msg = 'Can\'t update Generic VNF ' + vnfId + ' since \'resource-version\' is missing'\r
179                                 logError(msg)\r
180                                 throw new Exception(msg)\r
181                         }\r
182 \r
183                         // Handle persona-model-id/persona-model-version\r
184 \r
185                         def String newPersonaModelId = execution.getVariable('UAAIGenVnf_personaModelId')\r
186                         def String newPersonaModelVersion = execution.getVariable('UAAIGenVnf_personaModelVersion')\r
187                         def String personaModelVersionEntry = ""\r
188                         if (newPersonaModelId != null || newPersonaModelVersion != null) {\r
189 \r
190                                 // Confirm "new" persona-model-id is same as "current" persona-model-id\r
191                                 def Node currPersonaModelIdNode = utils.getChildNode(genericVnfNode, 'model-invariant-id')\r
192                                 if (currPersonaModelIdNode == null) {\r
193                                         // check the old attribute name\r
194                                         currPersonaModelIdNode = utils.getChildNode(genericVnfNode, 'persona-model-id')\r
195                                 }\r
196                                 def String currPersonaModelId = ''\r
197                                 if (currPersonaModelIdNode != null) {\r
198                                         currPersonaModelId = currPersonaModelIdNode.text()\r
199                                 }\r
200                                 if (!newPersonaModelId.equals(currPersonaModelId)) {\r
201                                         def msg = 'Can\'t update Generic VNF ' + vnfId + ' since there is \'persona-model-id\' mismatch between the current and new values'\r
202                                         logError(msg)\r
203                                         throw new Exception(msg)\r
204                                 }\r
205 \r
206                                 // Construct payload\r
207                                 personaModelVersionEntry = updateGenericVnfNode(origRequest, genericVnfNode, 'model-version-id')\r
208                         }\r
209 \r
210                         // Handle ipv4-oam-address\r
211                         def String ipv4OamAddress = execution.getVariable('UAAIGenVnf_ipv4OamAddress')\r
212                         def String ipv4OamAddressEntry = ""\r
213                         if (ipv4OamAddress != null) {\r
214                                 // Construct payload\r
215                                 ipv4OamAddressEntry = updateGenericVnfNode(origRequest, genericVnfNode, 'ipv4-oam-address')\r
216                         }\r
217 \r
218                         // Handle management-v6-address\r
219                         def String managementV6Address = execution.getVariable('UAAIGenVnf_managementV6Address')\r
220                         def String managementV6AddressEntry = ""\r
221                         if (managementV6Address != null) {\r
222                                 // Construct payload\r
223                                 managementV6AddressEntry = updateGenericVnfNode(origRequest, genericVnfNode, 'management-v6-address')\r
224                         }\r
225                         \r
226                         // Handle orchestration-status\r
227                         def String orchestrationStatus = execution.getVariable('UAAIGenVnf_orchestrationStatus')\r
228                         def String orchestrationStatusEntry = ""\r
229                         if (orchestrationStatus != null) {\r
230                                 // Construct payload\r
231                                 orchestrationStatusEntry = updateGenericVnfNode(origRequest, genericVnfNode, 'orchestration-status')\r
232                         }\r
233 \r
234                         def payload = """\r
235                                         {       ${personaModelVersionEntry}\r
236                                                 ${ipv4OamAddressEntry}\r
237                                                 ${managementV6AddressEntry}\r
238                                                 ${orchestrationStatusEntry}\r
239                                                 "vnf-id": "${vnfId}"                                    \r
240                                         }\r
241                         """\r
242 \r
243                         // Construct endpoint\r
244                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
245                         def aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)\r
246                         logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)\r
247                         String endPoint = execution.getVariable('URN_aai_endpoint') + aai_uri + '/' + UriUtils.encode(vnfId, "UTF-8")\r
248 \r
249                         try {\r
250                                 logDebug('sending PATCH to AAI endpoint \'' + endPoint + '\'' + 'with payload \n' + payload, isDebugLogEnabled)\r
251                                 utils.logAudit("Sending PATCH to AAI endpoint: " + endPoint)\r
252 \r
253                                 APIResponse response = aaiUriUtil.executeAAIPatchCall(execution, endPoint, payload)\r
254                                 def responseData = response.getResponseBodyAsString()\r
255                                 execution.setVariable('UAAIGenVnf_updateGenericVnfResponseCode', response.getStatusCode())\r
256                                 execution.setVariable('UAAIGenVnf_updateGenericVnfResponse', responseData)\r
257                                 utils.logAudit("UpdateAAIGenericVnf Response Data: " + responseData)\r
258                                 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)\r
259                                 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)\r
260                         } catch (Exception ex) {\r
261                                 ex.printStackTrace()\r
262                                 logDebug('Exception occurred while executing AAI PATCH:' + ex.getMessage(),isDebugLogEnabled)\r
263                                 execution.setVariable('UAAIGenVnf_updateGenericVnfResponseCode', 500)\r
264                                 execution.setVariable('UAAIGenVnf_updateGenericVnfResponse', 'AAI PATCH Failed:' + ex.getMessage())\r
265                         }\r
266                         logDebug('Exited ' + method, isDebugLogEnabled)\r
267                 } catch (BpmnError e) {\r
268                         throw e;\r
269                 } catch (Exception e) {\r
270                         logError('Caught exception in ' + method, e)\r
271                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in updateGenericVnf(): ' + e.getMessage())\r
272                 }\r
273         }\r
274 \r
275         /**\r
276          * Sets up json attributes for PATCH request for Update\r
277          * \r
278          * @param origRequest Incoming update request with Generic VNF element(s) to be updated.\r
279          * @param genericVnf Current Generic VNF retrieved from AAI.\r
280          * @param element Name of element to be inserted.\r
281          */\r
282         public String updateGenericVnfNode(String origRequest, Node genericVnfNode, String elementName) {\r
283 \r
284                 if (!utils.nodeExists(origRequest, elementName)) {\r
285                         return ""\r
286                 }\r
287                 def elementValue = utils.getNodeText(origRequest, elementName)\r
288 \r
289                 if (elementValue.equals('DELETE')) {\r
290                         // Set the element being deleted to null\r
291                         return """"${elementName}": null,"""\r
292                 }\r
293                 else {\r
294                         return """"${elementName}": "${elementValue}","""               \r
295                 }\r
296                 \r
297         }\r
298 \r
299         /**\r
300          * Generates a WorkflowException if the AAI query returns a response code other than 200.\r
301          *\r
302          * @param execution The flow's execution instance.\r
303          */\r
304         public void handleAAIQueryFailure(DelegateExecution execution) {\r
305                 def method = getClass().getSimpleName() + '.handleAAIQueryFailure(' +\r
306                         'execution=' + execution.getId() +\r
307                         ')'\r
308                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
309                 logDebug('Entered ' + method, isDebugLogEnabled)\r
310 \r
311                 logError('Error occurred attempting to query AAI, Response Code ' +\r
312                         execution.getVariable('UAAIGenVnf_getGenericVnfResponseCode') + ', Error Response ' +\r
313                         execution.getVariable('UAAIGenVnf_getGenericVnfResponse'))\r
314                 String processKey = getProcessKey(execution);\r
315                 WorkflowException exception = new WorkflowException(processKey, 5000,\r
316                         execution.getVariable('UAAIGenVnf_getGenericVnfResponse'))\r
317                 execution.setVariable('WorkflowException', exception)\r
318 \r
319                 utils.logAudit("Workflow Exception occurred when handling Quering AAI: " + exception.getErrorMessage())\r
320                 logDebug('Exited ' + method, isDebugLogEnabled)\r
321         }\r
322 \r
323         /**\r
324          * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.\r
325          *\r
326          * @param execution The flow's execution instance.\r
327          */\r
328         public void handleUpdateGenericVnfFailure(DelegateExecution execution) {\r
329                 def method = getClass().getSimpleName() + '.handleUpdateGenericVnfFailure(' +\r
330                         'execution=' + execution.getId() +\r
331                         ')'\r
332                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
333                 logDebug('Entered ' + method, isDebugLogEnabled)\r
334 \r
335                 logError('Error occurred attempting to update Generic VNF in AAI, Response Code ' +\r
336                         execution.getVariable('UAAIGenVnf_updateGenericVnfResponseCode') + ', Error Response ' +\r
337                         execution.getVariable('UAAIGenVnf_updateGenericVnfResponse'))\r
338 \r
339                 String processKey = getProcessKey(execution);\r
340                 WorkflowException exception = new WorkflowException(processKey, 5000,\r
341                         execution.getVariable('UAAIGenVnf_updateGenericVnfResponse'))\r
342                 execution.setVariable('WorkflowException', exception)\r
343 \r
344                 utils.logAudit("Workflow Exception occurred when Updating GenericVnf: " + exception.getErrorMessage())\r
345                 logDebug('Exited ' + method, isDebugLogEnabled)\r
346         }\r
347 }