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.apache.commons.lang3.*
\r
24 import org.camunda.bpm.engine.delegate.BpmnError
\r
25 import org.camunda.bpm.engine.runtime.Execution
\r
26 import org.openecomp.mso.rest.APIResponse
\r
27 import org.openecomp.mso.rest.RESTClient
\r
28 import org.openecomp.mso.rest.RESTConfig
\r
30 class VnfAdapterRestV1 extends AbstractServiceTaskProcessor {
\r
32 ExceptionUtil exceptionUtil = new ExceptionUtil()
\r
34 // VNF Response Processing
\r
35 public void preProcessRequest (Execution execution) {
\r
36 def method = getClass().getSimpleName() + '.preProcessRequest(' +
\r
37 'execution=' + execution.getId() +
\r
39 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
40 logDebug('Entered ' + method, isDebugLogEnabled)
\r
42 def prefix="VNFREST_"
\r
43 execution.setVariable("prefix", prefix)
\r
44 setSuccessIndicator(execution, false)
\r
47 String request = validateRequest(execution, "mso-request-id")
\r
49 // Get the request type (the name of the root element) from the request
\r
51 Node root = new XmlParser().parseText(request)
\r
52 String requestType = root.name()
\r
53 execution.setVariable(prefix + 'requestType', requestType)
\r
54 logDebug(getProcessKey(execution) + ': ' + prefix + 'requestType = ' + requestType, isDebugLogEnabled)
\r
56 utils.logAudit('VnfAdapterRestV1, request: ' + request)
\r
57 // Get the messageId from the request
\r
59 String messageId = getChildText(root, 'messageId')
\r
61 if (messageId == null || messageId.isEmpty()) {
\r
62 String msg = getProcessKey(execution) + ': no messageId in ' + requestType
\r
64 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
67 execution.setVariable('VNFAResponse_CORRELATOR', messageId)
\r
68 logDebug(getProcessKey(execution) + ': VNFAResponse_CORRELATOR = ' + messageId, isDebugLogEnabled)
\r
70 // Get the notificationUrl from the request
\r
72 String notificationUrl = getChildText(root, 'notificationUrl')
\r
74 if (notificationUrl == null || notificationUrl.isEmpty()) {
\r
75 String msg = getProcessKey(execution) + ': no notificationUrl in ' + requestType
\r
77 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
80 execution.setVariable(prefix + 'notificationUrl', notificationUrl)
\r
81 logDebug(getProcessKey(execution) + ': ' + prefix + 'notificationUrl = ' + notificationUrl, isDebugLogEnabled)
\r
83 // Determine the VnfAdapter endpoint
\r
85 String vnfAdapterEndpoint = execution.getVariable("URN_mso_adapters_vnf_rest_endpoint")
\r
87 if (vnfAdapterEndpoint == null || vnfAdapterEndpoint.isEmpty()) {
\r
88 String msg = getProcessKey(execution) + ': mso:adapters:vnf:rest:endpoint URN mapping is not defined'
\r
90 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
93 while (vnfAdapterEndpoint.endsWith('/')) {
\r
94 vnfAdapterEndpoint = vnfAdapterEndpoint.substring(0, vnfAdapterEndpoint.length()-1)
\r
97 String vnfAdapterMethod = null
\r
98 String vnfAdapterUrl = null
\r
99 String vnfAdapterRequest = request
\r
101 if ('createVfModuleRequest'.equals(requestType)) {
\r
102 String vnfId = getChildText(root, 'vnfId')
\r
104 if (vnfId == null || vnfId.isEmpty()) {
\r
105 String msg = getProcessKey(execution) + ': no vnfId in ' + requestType
\r
107 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
110 vnfAdapterMethod = 'POST'
\r
111 vnfAdapterUrl = vnfAdapterEndpoint + '/' + URLEncoder.encode(vnfId, 'UTF-8') + '/vf-modules'
\r
113 } else if ('updateVfModuleRequest'.equals(requestType)) {
\r
114 String vnfId = getChildText(root, 'vnfId')
\r
116 if (vnfId == null || vnfId.isEmpty()) {
\r
117 String msg = getProcessKey(execution) + ': no vnfId in ' + requestType
\r
119 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
122 String vfModuleId = getChildText(root, 'vfModuleId')
\r
124 if (vfModuleId == null || vfModuleId.isEmpty()) {
\r
125 String msg = getProcessKey(execution) + ': no vfModuleId in ' + requestType
\r
127 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
130 vnfAdapterMethod = 'PUT'
\r
131 vnfAdapterUrl = vnfAdapterEndpoint + '/' + URLEncoder.encode(vnfId, 'UTF-8') +
\r
132 '/vf-modules/' + URLEncoder.encode(vfModuleId, 'UTF-8')
\r
134 } else if ('deleteVfModuleRequest'.equals(requestType)) {
\r
135 String vnfId = getChildText(root, 'vnfId')
\r
137 if (vnfId == null || vnfId.isEmpty()) {
\r
138 String msg = getProcessKey(execution) + ': no vnfId in ' + requestType
\r
140 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
143 String vfModuleId = getChildText(root, 'vfModuleId')
\r
145 if (vfModuleId == null || vfModuleId.isEmpty()) {
\r
146 String msg = getProcessKey(execution) + ': no vfModuleId in ' + requestType
\r
148 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
151 vnfAdapterMethod = 'DELETE'
\r
152 vnfAdapterUrl = vnfAdapterEndpoint + '/' + URLEncoder.encode(vnfId, 'UTF-8') +
\r
153 '/vf-modules/' + URLEncoder.encode(vfModuleId, 'UTF-8')
\r
155 } else if ('rollbackVfModuleRequest'.equals(requestType)) {
\r
156 Node vfModuleRollbackNode = getChild(root, 'vfModuleRollback')
\r
158 if (vfModuleRollbackNode == null) {
\r
159 String msg = getProcessKey(execution) + ': no vfModuleRollback in ' + requestType
\r
161 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
164 String vnfId = getChildText(vfModuleRollbackNode, 'vnfId')
\r
166 if (vnfId == null || vnfId.isEmpty()) {
\r
167 String msg = getProcessKey(execution) + ': no vnfId in ' + requestType
\r
169 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
172 String vfModuleId = getChildText(vfModuleRollbackNode, 'vfModuleId')
\r
174 if (vfModuleId == null || vfModuleId.isEmpty()) {
\r
175 String msg = getProcessKey(execution) + ': no vfModuleId in ' + requestType
\r
177 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
180 vnfAdapterMethod = 'DELETE'
\r
181 vnfAdapterUrl = vnfAdapterEndpoint + '/' + URLEncoder.encode(vnfId, 'UTF-8') +
\r
182 '/vf-modules/' + URLEncoder.encode(vfModuleId, 'UTF-8') + '/rollback'
\r
184 } else if ('createVolumeGroupRequest'.equals(requestType)) {
\r
185 vnfAdapterMethod = 'POST'
\r
186 if (vnfAdapterEndpoint.endsWith('v1/vnfs')) {
\r
187 vnfAdapterEndpoint = vnfAdapterEndpoint.substring(0, (vnfAdapterEndpoint.length()-'/vnfs'.length()))
\r
189 vnfAdapterUrl = vnfAdapterEndpoint + '/volume-groups'
\r
191 } else if ('updateVolumeGroupRequest'.equals(requestType)) {
\r
192 String volumeGroupId = getChildText(root, 'volumeGroupId')
\r
194 if (volumeGroupId == null || volumeGroupId.isEmpty()) {
\r
195 String msg = getProcessKey(execution) + ': no volumeGroupId in ' + requestType
\r
197 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
200 vnfAdapterMethod = 'PUT'
\r
201 if (vnfAdapterEndpoint.endsWith('v1/vnfs')) {
\r
202 vnfAdapterEndpoint = vnfAdapterEndpoint.substring(0, (vnfAdapterEndpoint.length()-'/vnfs'.length()))
\r
204 vnfAdapterUrl = vnfAdapterEndpoint + '/volume-groups/' + URLEncoder.encode(volumeGroupId, 'UTF-8')
\r
206 } else if ('deleteVolumeGroupRequest'.equals(requestType)) {
\r
207 String volumeGroupId = getChildText(root, 'volumeGroupId')
\r
209 if (volumeGroupId == null || volumeGroupId.isEmpty()) {
\r
210 String msg = getProcessKey(execution) + ': no volumeGroupId in ' + requestType
\r
212 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
215 vnfAdapterMethod = 'DELETE'
\r
216 if (vnfAdapterEndpoint.endsWith('v1/vnfs')) {
\r
217 vnfAdapterEndpoint = vnfAdapterEndpoint.substring(0, (vnfAdapterEndpoint.length()-'/vnfs'.length()))
\r
219 vnfAdapterUrl = vnfAdapterEndpoint + '/volume-groups/' + URLEncoder.encode(volumeGroupId, 'UTF-8')
\r
221 } else if ('rollbackVolumeGroupRequest'.equals(requestType)) {
\r
222 String volumeGroupId = getChildText(root, 'volumeGroupId')
\r
224 if (volumeGroupId == null || volumeGroupId.isEmpty()) {
\r
225 String msg = getProcessKey(execution) + ': no volumeGroupId in ' + requestType
\r
227 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
230 vnfAdapterMethod = 'DELETE'
\r
231 if (vnfAdapterEndpoint.endsWith('v1/vnfs')) {
\r
232 vnfAdapterEndpoint = vnfAdapterEndpoint.substring(0, (vnfAdapterEndpoint.length()-'/vnfs'.length()))
\r
234 vnfAdapterUrl = vnfAdapterEndpoint + '/volume-groups/' + URLEncoder.encode(volumeGroupId, 'UTF-8') + '/rollback'
\r
237 String msg = getProcessKey(execution) + ': Unsupported request type: ' + requestType
\r
239 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
242 execution.setVariable(prefix + 'vnfAdapterMethod', vnfAdapterMethod)
\r
243 logDebug(getProcessKey(execution) + ': ' + prefix + 'vnfAdapterMethod = ' + vnfAdapterMethod, isDebugLogEnabled)
\r
244 execution.setVariable(prefix + 'vnfAdapterUrl', vnfAdapterUrl)
\r
245 logDebug(getProcessKey(execution) + ': ' + prefix + 'vnfAdapterUrl = ' + vnfAdapterUrl, isDebugLogEnabled)
\r
246 execution.setVariable(prefix + 'vnfAdapterRequest', vnfAdapterRequest)
\r
247 logDebug(getProcessKey(execution) + ': ' + prefix + 'vnfAdapterRequest = \n' + vnfAdapterRequest, isDebugLogEnabled)
\r
249 // Get the Basic Auth credentials for the VnfAdapter
\r
251 String basicAuthValue = execution.getVariable("URN_mso_adapters_po_auth")
\r
253 if (basicAuthValue == null || basicAuthValue.isEmpty()) {
\r
254 logError(getProcessKey(execution) + ": mso:adapters:po:auth URN mapping is not defined")
\r
256 logDebug(getProcessKey(execution) + ": Obtained BasicAuth credentials for VnfAdapter:" +
\r
257 basicAuthValue, isDebugLogEnabled)
\r
259 def encodedString = utils.getBasicAuth(basicAuthValue, execution.getVariable("URN_mso_msoKey"))
\r
260 execution.setVariable(prefix + 'basicAuthHeaderValue', encodedString)
\r
261 } catch (IOException ex) {
\r
262 logError(getProcessKey(execution) + ": Unable to encode BasicAuth credentials for VnfAdapter")
\r
266 } catch (BpmnError e) {
\r
267 logDebug(" Rethrowing MSOWorkflowException", isDebugLogEnabled)
\r
269 } catch (Exception e) {
\r
270 String msg = 'Caught exception in ' + method + ": " + e
\r
273 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
278 * This method is used instead of an HTTP Connector task because the
\r
279 * connector does not allow DELETE with a body.
\r
281 public void sendRequestToVnfAdapter(Execution execution) {
\r
282 def method = getClass().getSimpleName() + '.sendRequestToVnfAdapter(' +
\r
283 'execution=' + execution.getId() +
\r
285 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
286 logDebug('Entered ' + method, isDebugLogEnabled)
\r
288 String prefix = execution.getVariable('prefix')
\r
291 String vnfAdapterMethod = execution.getVariable(prefix + 'vnfAdapterMethod')
\r
292 String vnfAdapterUrl = execution.getVariable(prefix + 'vnfAdapterUrl')
\r
293 String vnfAdapterRequest = execution.getVariable(prefix + 'vnfAdapterRequest')
\r
295 RESTConfig config = new RESTConfig(vnfAdapterUrl)
\r
296 RESTClient client = new RESTClient(config).
\r
297 addHeader("Content-Type", "application/xml").
\r
298 addAuthorizationHeader(execution.getVariable(prefix + "basicAuthHeaderValue"));
\r
300 APIResponse response;
\r
302 if ("GET".equals(vnfAdapterMethod)) {
\r
303 response = client.httpGet()
\r
304 } else if ("PUT".equals(vnfAdapterMethod)) {
\r
305 response = client.httpPut(vnfAdapterRequest)
\r
306 } else if ("POST".equals(vnfAdapterMethod)) {
\r
307 response = client.httpPost(vnfAdapterRequest)
\r
308 } else if ("DELETE".equals(vnfAdapterMethod)) {
\r
309 response = client.httpDelete(vnfAdapterRequest)
\r
311 String msg = 'Unsupported HTTP method "' + vnfAdapterMethod + '" in ' + method + ": " + e
\r
313 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
316 execution.setVariable(prefix + "vnfAdapterStatusCode", response.getStatusCode())
\r
317 execution.setVariable(prefix + "vnfAdapterResponse", response.getResponseBodyAsString())
\r
318 } catch (BpmnError e) {
\r
320 } catch (Exception e) {
\r
321 String msg = 'Caught exception in ' + method + ": " + e
\r
323 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
\r
327 public void processCallback(Execution execution){
\r
328 def method = getClass().getSimpleName() + '.processCallback(' +
\r
329 'execution=' + execution.getId() +
\r
331 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
332 logDebug('Entered ' + method, isDebugLogEnabled)
\r
334 String callback = execution.getVariable('VNFAResponse_MESSAGE')
\r
337 logDebug(getProcessKey(execution) + ": received callback:\n" + callback, isDebugLogEnabled)
\r
339 // The XML callback is available to the calling flow in any case,
\r
340 // even if a WorkflowException is generated.
\r
341 execution.setVariable(getProcessKey(execution) + 'Response', callback)
\r
342 // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead.
\r
343 execution.setVariable("WorkflowResponse", callback)
\r
345 Node root = new XmlParser().parseText(callback)
\r
346 if (root.name().endsWith('Exception')) {
\r
347 vnfAdapterWorkflowException(execution, callback)
\r
349 } catch (Exception e) {
\r
350 e.printStackTrace()
\r
351 callback = callback == null || String.valueOf(callback).isEmpty() ? "NONE" : callback
\r
352 String msg = "Received error from VnfAdapter: " + callback
\r
353 logDebug(getProcessKey(execution) + ': ' + msg, isDebugLogEnabled)
\r
354 exceptionUtil.buildWorkflowException(execution, 7020, msg)
\r
359 * Tries to parse the response as XML to extract the information to create
\r
360 * a WorkflowException. If the response cannot be parsed, a more generic
\r
361 * WorkflowException is created.
\r
363 public void vnfAdapterWorkflowException(Execution execution, Object response) {
\r
365 Node root = new XmlParser().parseText(response)
\r
366 String category = getChildText(root, "category")
\r
367 category = category == null || category.isEmpty() ? "" : " category='" + category + "'"
\r
368 String message = getChildText(root, "message")
\r
369 message = message == null || message.isEmpty() ? "" : " message='" + message + "'"
\r
370 String rolledBack = getChildText(root, "rolledBack")
\r
371 rolledBack = rolledBack == null || rolledBack.isEmpty() ? "" : " rolledBack='" + rolledBack + "'"
\r
372 exceptionUtil.buildWorkflowException(execution, 7020, "Received " + root.name() +
\r
373 " from VnfAdapter:" + category + message + rolledBack);
\r
374 } catch (Exception e) {
\r
375 response = response == null || String.valueOf(response).isEmpty() ? "NONE" : response
\r
376 exceptionUtil.buildWorkflowException(execution, 7020, "Received error from VnfAdapter: " + response)
\r
381 * Gets the named child of the specified node.
\r
382 * @param node the node
\r
383 * @param name the child name
\r
384 * @return the child node, or null if no such child exists
\r
386 private Node getChild(Node node, String name) {
\r
387 for (Node child : node.children()) {
\r
388 if (child.name() == name) {
\r
396 * Gets the text of the named child of the specified node.
\r
397 * @param node the node
\r
398 * @param name the child name
\r
399 * @return the child node text, or null if no such child exists
\r
401 private String getChildText(Node node, String name) {
\r
402 Node child = getChild(node, name)
\r
403 return child == null ? null : child.text()
\r