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