2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.mso.bpmn.common.scripts
\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
30 public class UpdateAAIGenericVnf extends AbstractServiceTaskProcessor {
\r
32 private XmlParser xmlParser = new XmlParser()
\r
33 ExceptionUtil exceptionUtil = new ExceptionUtil()
\r
36 * Initialize the flow's variables.
\r
38 * @param execution The flow's execution instance.
\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
55 * Check for missing elements in the received request.
\r
57 * @param execution The flow's execution instance.
\r
59 public void preProcessRequest(DelegateExecution execution) {
\r
60 def method = getClass().getSimpleName() + '.preProcessRequest(' +
\r
61 'execution=' + execution.getId() +
\r
63 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
64 logDebug('Entered ' + method, isDebugLogEnabled)
\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
72 def vnfId = getRequiredNodeText(execution, xml,'vnf-id')
\r
73 execution.setVariable('UAAIGenVnf_vnfId', vnfId)
\r
75 def personaModelId = getNodeTextForce(xml,'persona-model-id')
\r
76 if (personaModelId != null && !personaModelId.isEmpty()) {
\r
77 execution.setVariable('UAAIGenVnf_personaModelId', personaModelId)
\r
80 def personaModelVersion = getNodeTextForce(xml,'persona-model-version')
\r
81 if (personaModelVersion != null && !personaModelVersion.isEmpty()) {
\r
82 execution.setVariable('UAAIGenVnf_personaModelVersion', personaModelVersion)
\r
85 def ipv4OamAddress = getNodeTextForce(xml, 'ipv4-oam-address')
\r
86 if (ipv4OamAddress != null && !ipv4OamAddress.isEmpty()) {
\r
87 execution.setVariable('UAAIGenVnf_ipv4OamAddress', ipv4OamAddress)
\r
90 def managementV6Address = getNodeTextForce(xml, 'management-v6-address')
\r
91 if (managementV6Address != null && !managementV6Address.isEmpty()) {
\r
92 execution.setVariable('UAAIGenVnf_managementV6Address', managementV6Address)
\r
95 def orchestrationStatus = getNodeTextForce(xml, 'orchestration-status')
\r
96 if (orchestrationStatus != null && !orchestrationStatus.isEmpty()) {
\r
97 execution.setVariable('UAAIGenVnf_orchestrationStatus', orchestrationStatus)
\r
100 logDebug('Exited ' + method, isDebugLogEnabled)
\r
101 } catch (BpmnError 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
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
113 * @param execution The flow's execution instance.
\r
115 public void getGenericVnf(DelegateExecution execution) {
\r
116 def method = getClass().getSimpleName() + '.getGenericVnf(' +
\r
117 'execution=' + execution.getId() +
\r
119 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
120 logDebug('Entered ' + method, isDebugLogEnabled)
\r
123 def vnfId = execution.getVariable('UAAIGenVnf_vnfId')
\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
132 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)
\r
133 utils.logAudit("Sending GET to AAI endpoint: " + endPoint)
\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
148 logDebug('Exited ' + method, isDebugLogEnabled)
\r
149 } catch (BpmnError 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
158 * Construct and send a PUT request to AAI to update the Generic VNF.
\r
160 * @param execution The flow's execution instance.
\r
162 public void updateGenericVnf(DelegateExecution execution) {
\r
163 def method = getClass().getSimpleName() + '.updateGenericVnf(' +
\r
164 'execution=' + execution.getId() +
\r
166 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
167 logDebug('Entered ' + method, isDebugLogEnabled)
\r
170 def vnfId = execution.getVariable('UAAIGenVnf_vnfId')
\r
171 def genericVnf = execution.getVariable('UAAIGenVnf_getGenericVnfResponse')
\r
172 def origRequest = execution.getVariable('UpdateAAIGenericVnfRequest')
\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
180 throw new Exception(msg)
\r
183 // Handle persona-model-id/persona-model-version
\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
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
196 def String currPersonaModelId = ''
\r
197 if (currPersonaModelIdNode != null) {
\r
198 currPersonaModelId = currPersonaModelIdNode.text()
\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
203 throw new Exception(msg)
\r
206 // Construct payload
\r
207 personaModelVersionEntry = updateGenericVnfNode(origRequest, genericVnfNode, 'model-version-id')
\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
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
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
235 { ${personaModelVersionEntry}
\r
236 ${ipv4OamAddressEntry}
\r
237 ${managementV6AddressEntry}
\r
238 ${orchestrationStatusEntry}
\r
239 "vnf-id": "${vnfId}"
\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
250 logDebug('sending PATCH to AAI endpoint \'' + endPoint + '\'' + 'with payload \n' + payload, isDebugLogEnabled)
\r
251 utils.logAudit("Sending PATCH to AAI endpoint: " + endPoint)
\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
266 logDebug('Exited ' + method, isDebugLogEnabled)
\r
267 } catch (BpmnError 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
276 * Sets up json attributes for PATCH request for Update
\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
282 public String updateGenericVnfNode(String origRequest, Node genericVnfNode, String elementName) {
\r
284 if (!utils.nodeExists(origRequest, elementName)) {
\r
287 def elementValue = utils.getNodeText(origRequest, elementName)
\r
289 if (elementValue.equals('DELETE')) {
\r
290 // Set the element being deleted to null
\r
291 return """"${elementName}": null,"""
\r
294 return """"${elementName}": "${elementValue}","""
\r
300 * Generates a WorkflowException if the AAI query returns a response code other than 200.
\r
302 * @param execution The flow's execution instance.
\r
304 public void handleAAIQueryFailure(DelegateExecution execution) {
\r
305 def method = getClass().getSimpleName() + '.handleAAIQueryFailure(' +
\r
306 'execution=' + execution.getId() +
\r
308 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
309 logDebug('Entered ' + method, isDebugLogEnabled)
\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
319 utils.logAudit("Workflow Exception occurred when handling Quering AAI: " + exception.getErrorMessage())
\r
320 logDebug('Exited ' + method, isDebugLogEnabled)
\r
324 * Generates a WorkflowException if updating a VF Module in AAI returns a response code other than 200.
\r
326 * @param execution The flow's execution instance.
\r
328 public void handleUpdateGenericVnfFailure(DelegateExecution execution) {
\r
329 def method = getClass().getSimpleName() + '.handleUpdateGenericVnfFailure(' +
\r
330 'execution=' + execution.getId() +
\r
332 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
333 logDebug('Entered ' + method, isDebugLogEnabled)
\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
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
344 utils.logAudit("Workflow Exception occurred when Updating GenericVnf: " + exception.getErrorMessage())
\r
345 logDebug('Exited ' + method, isDebugLogEnabled)
\r