1 package org.openecomp.mso.bpmn.common.scripts
3 import java.text.SimpleDateFormat
4 import java.net.URLEncoder
6 import org.apache.commons.codec.binary.Base64
7 import org.apache.commons.lang3.*
8 import org.camunda.bpm.engine.delegate.BpmnError
9 import org.camunda.bpm.engine.delegate.DelegateExecution
13 import org.json.JSONObject
15 import org.openecomp.mso.bpmn.core.WorkflowException
16 import org.openecomp.mso.bpmn.core.json.JsonUtils
17 import org.openecomp.mso.rest.APIResponse
18 import org.openecomp.mso.rest.RESTClient
19 import org.openecomp.mso.rest.RESTConfig
22 class SDNCAdapterRestV1 extends AbstractServiceTaskProcessor {
24 ExceptionUtil exceptionUtil = new ExceptionUtil()
25 JsonUtils jsonUtil = new JsonUtils()
28 * Processes the incoming request.
30 public void preProcessRequest (DelegateExecution execution) {
31 def method = getClass().getSimpleName() + '.preProcessRequest(' +
32 'execution=' + execution.getId() +
34 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
35 logDebug('Entered ' + method, isDebugLogEnabled)
37 def prefix="SDNCREST_"
38 execution.setVariable("prefix", prefix)
39 setSuccessIndicator(execution, false)
42 // Determine the request type and log the request
44 String request = validateRequest(execution, "mso-request-id")
45 String requestType = jsonUtil.getJsonRootProperty(request)
46 execution.setVariable(prefix + 'requestType', requestType)
47 logDebug(getProcessKey(execution) + ': ' + prefix + 'requestType = ' + requestType, isDebugLogEnabled)
48 utils.logAudit('SDNCAdapterRestV1, request: ' + request)
50 // Determine the SDNCAdapter endpoint
52 String sdncAdapterEndpoint = execution.getVariable("URN_mso_adapters_sdnc_rest_endpoint")
54 if (sdncAdapterEndpoint == null || sdncAdapterEndpoint.isEmpty()) {
55 String msg = getProcessKey(execution) + ': mso:adapters:sdnc:rest:endpoint URN mapping is not defined'
56 logDebug(msg, isDebugLogEnabled)
58 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
61 while (sdncAdapterEndpoint.endsWith('/')) {
62 sdncAdapterEndpoint = sdncAdapterEndpoint.substring(0, sdncAdapterEndpoint.length()-1)
65 String sdncAdapterMethod = null
66 String sdncAdapterUrl = null
67 String sdncAdapterRequest = request
69 if ('SDNCServiceRequest'.equals(requestType)) {
70 // Get the sdncRequestId from the request
72 String sdncRequestId = jsonUtil.getJsonValue(request, requestType + ".sdncRequestId")
74 if (sdncRequestId == null || sdncRequestId.isEmpty()) {
75 String msg = getProcessKey(execution) + ': no sdncRequestId in ' + requestType
76 logDebug(msg, isDebugLogEnabled)
78 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
81 execution.setVariable('SDNCAResponse_CORRELATOR', sdncRequestId)
82 logDebug(getProcessKey(execution) + ': SDNCAResponse_CORRELATOR = ' + sdncRequestId, isDebugLogEnabled)
84 // Get the bpNotificationUrl from the request (just to make sure it's there)
86 String bpNotificationUrl = jsonUtil.getJsonValue(request, requestType + ".bpNotificationUrl")
88 if (bpNotificationUrl == null || bpNotificationUrl.isEmpty()) {
89 String msg = getProcessKey(execution) + ': no bpNotificationUrl in ' + requestType
90 logDebug(msg, isDebugLogEnabled)
92 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
95 sdncAdapterMethod = 'POST'
96 sdncAdapterUrl = sdncAdapterEndpoint + '/services'
99 String msg = getProcessKey(execution) + ': Unsupported request type: ' + requestType
100 logDebug(msg, isDebugLogEnabled)
102 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
105 execution.setVariable(prefix + 'sdncAdapterMethod', sdncAdapterMethod)
106 logDebug(getProcessKey(execution) + ': ' + prefix + 'sdncAdapterMethod = ' + sdncAdapterMethod, isDebugLogEnabled)
107 execution.setVariable(prefix + 'sdncAdapterUrl', sdncAdapterUrl)
108 logDebug(getProcessKey(execution) + ': ' + prefix + 'sdncAdapterUrl = ' + sdncAdapterUrl, isDebugLogEnabled)
109 execution.setVariable(prefix + 'sdncAdapterRequest', sdncAdapterRequest)
110 logDebug(getProcessKey(execution) + ': ' + prefix + 'sdncAdapterRequest = \n' + sdncAdapterRequest, isDebugLogEnabled)
112 // Get the Basic Auth credentials for the SDNCAdapter (yes... we ARE using the PO adapters credentials)
114 String basicAuthValue = execution.getVariable("URN_mso_adapters_po_auth")
116 if (basicAuthValue == null || basicAuthValue.isEmpty()) {
117 logDebug(getProcessKey(execution) + ": mso:adapters:po:auth URN mapping is not defined", isDebugLogEnabled)
118 logError(getProcessKey(execution) + ": mso:adapters:po:auth URN mapping is not defined")
120 logDebug(getProcessKey(execution) + ": Obtained BasicAuth credentials for SDNCAdapter:" +
121 basicAuthValue, isDebugLogEnabled)
123 def encodedString = utils.getBasicAuth(basicAuthValue, execution.getVariable("URN_mso_msoKey"))
124 execution.setVariable(prefix + 'basicAuthHeaderValue', encodedString)
125 } catch (IOException ex) {
126 logDebug(getProcessKey(execution) + ": Unable to encode BasicAuth credentials for SDNCAdapter", isDebugLogEnabled)
127 logError(getProcessKey(execution) + ": Unable to encode BasicAuth credentials for SDNCAdapter")
131 // Set the timeout value, e.g. PT5M. It may be specified in the request as the
132 // bpTimeout value. If it's not in the request, use the URN mapping value.
134 String timeout = jsonUtil.getJsonValue(request, requestType + ".bpTimeout")
136 if (timeout == null || timeout.isEmpty()) {
137 timeout = execution.getVariable("URN_mso_sdnc_timeout")
140 execution.setVariable(prefix + 'timeout', timeout)
141 logDebug(getProcessKey(execution) + ': ' + prefix + 'timeout = ' + timeout, isDebugLogEnabled)
142 } catch (BpmnError e) {
144 } catch (Exception e) {
145 String msg = 'Caught exception in ' + method + ": " + e
146 logDebug(msg, isDebugLogEnabled)
148 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
153 * Sends the request to the SDNC adapter.
155 public void sendRequestToSDNCAdapter(DelegateExecution execution) {
156 def method = getClass().getSimpleName() + '.sendRequestToSDNCAdapter(' +
157 'execution=' + execution.getId() +
159 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
160 logDebug('Entered ' + method, isDebugLogEnabled)
162 String prefix = execution.getVariable('prefix')
165 String sdncAdapterMethod = execution.getVariable(prefix + 'sdncAdapterMethod')
166 String sdncAdapterUrl = execution.getVariable(prefix + 'sdncAdapterUrl')
167 String sdncAdapterRequest = execution.getVariable(prefix + 'sdncAdapterRequest')
168 utils.logAudit("Outgoing SDNC Rest Request is: " + sdncAdapterRequest)
170 RESTConfig config = new RESTConfig(sdncAdapterUrl)
171 RESTClient client = new RESTClient(config).
172 addHeader("Content-Type", "application/json").
173 addAuthorizationHeader(execution.getVariable(prefix + "basicAuthHeaderValue"))
177 if ("GET".equals(sdncAdapterMethod)) {
178 response = client.httpGet()
179 } else if ("PUT".equals(sdncAdapterMethod)) {
180 response = client.httpPut(sdncAdapterRequest)
181 } else if ("POST".equals(sdncAdapterMethod)) {
182 response = client.httpPost(sdncAdapterRequest)
183 } else if ("DELETE".equals(sdncAdapterMethod)) {
184 response = client.httpDelete(sdncAdapterRequest)
186 String msg = 'Unsupported HTTP method "' + sdncAdapterMethod + '" in ' + method + ": " + e
187 logDebug(msg, isDebugLogEnabled)
189 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
192 execution.setVariable(prefix + "sdncAdapterStatusCode", response.getStatusCode())
193 execution.setVariable(prefix + "sdncAdapterResponse", response.getResponseBodyAsString())
194 } catch (BpmnError e) {
196 } catch (Exception e) {
197 String msg = 'Caught exception in ' + method + ": " + e
198 logDebug(msg, isDebugLogEnabled)
200 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
205 * Processes a callback.
207 public void processCallback(DelegateExecution execution){
208 def method = getClass().getSimpleName() + '.processCallback(' +
209 'execution=' + execution.getId() +
211 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
212 logDebug('Entered ' + method, isDebugLogEnabled)
214 String prefix = execution.getVariable('prefix')
215 String callback = execution.getVariable('SDNCAResponse_MESSAGE')
216 utils.logAudit("Incoming SDNC Rest Callback is: " + callback)
219 logDebug(getProcessKey(execution) + ": received callback:\n" + callback, isDebugLogEnabled)
221 int callbackNumber = 1
222 while (execution.getVariable(prefix + 'callback' + callbackNumber) != null) {
226 execution.setVariable(prefix + 'callback' + callbackNumber, callback)
227 execution.removeVariable('SDNCAResponse_MESSAGE')
229 String responseType = jsonUtil.getJsonRootProperty(callback)
231 // Get the ackFinalIndicator and make sure it's either Y or N. Default to Y.
232 String ackFinalIndicator = jsonUtil.getJsonValue(callback, responseType + ".ackFinalIndicator")
234 if (!'N'.equals(ackFinalIndicator)) {
235 ackFinalIndicator = 'Y'
238 execution.setVariable(prefix + "ackFinalIndicator", ackFinalIndicator)
240 if (responseType.endsWith('Error')) {
241 sdncAdapterBuildWorkflowException(execution, callback)
243 } catch (Exception e) {
244 callback = callback == null || String.valueOf(callback).isEmpty() ? "NONE" : callback
245 String msg = "Received error from SDNCAdapter: " + callback
246 logDebug(getProcessKey(execution) + ': ' + msg, isDebugLogEnabled)
247 exceptionUtil.buildWorkflowException(execution, 5300, msg)
252 * Tries to parse the response as XML to extract the information to create
253 * a WorkflowException. If the response cannot be parsed, a more generic
254 * WorkflowException is created.
256 public void sdncAdapterBuildWorkflowException(DelegateExecution execution, String response) {
258 String responseType = jsonUtil.getJsonRootProperty(response)
259 String responseCode = jsonUtil.getJsonValue(response, responseType + ".responseCode")
260 String responseMessage = jsonUtil.getJsonValue(response, responseType + ".responseMessage")
264 if (responseCode != null && !responseCode.isEmpty()) {
265 info += " responseCode='" + responseCode + "'"
268 if (responseMessage != null && !responseMessage.isEmpty()) {
269 info += " responseMessage='" + responseMessage + "'"
272 // Note: the mapping function handles a null or empty responseCode
273 int mappedResponseCode = Integer.parseInt(exceptionUtil.MapSDNCResponseCodeToErrorCode(responseCode));
274 exceptionUtil.buildWorkflowException(execution, mappedResponseCode, "Received " + responseType +
275 " from SDNCAdapter:" + info)
276 } catch (Exception e) {
277 response = response == null || String.valueOf(response).isEmpty() ? "NONE" : response
278 exceptionUtil.buildWorkflowException(execution, 5300, "Received error from SDNCAdapter: " + response)
283 * Gets the last callback request from the execution, or null if there was no callback.
285 public String getLastCallback(DelegateExecution execution) {
286 def method = getClass().getSimpleName() + '.getLastCallback(' +
287 'execution=' + execution.getId() +
289 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
290 logDebug('Entered ' + method, isDebugLogEnabled)
292 String prefix = execution.getVariable('prefix')
295 int callbackNumber = 1
296 String callback = null
299 String thisCallback = (String) execution.getVariable(prefix + 'callback' + callbackNumber)
301 if (thisCallback == null) {
305 callback = thisCallback
310 } catch (Exception e) {
311 String msg = 'Caught exception in ' + method + ": " + e
312 logDebug(msg, isDebugLogEnabled)
314 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
319 * Sets the timeout value to wait for the next notification.
321 public void setTimeoutValue(DelegateExecution execution) {
322 def method = getClass().getSimpleName() + '.setTimeoutValue(' +
323 'execution=' + execution.getId() +
325 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
326 logDebug('Entered ' + method, isDebugLogEnabled)
328 String prefix = execution.getVariable('prefix')
331 def timeoutValue = execution.getVariable("URN_mso_sdnc_timeout")
333 if (execution.getVariable(prefix + 'callback1') != null) {
334 // Waiting for subsequent notifications
336 } catch (Exception e) {
337 String msg = 'Caught exception in ' + method + ": " + e
338 logDebug(msg, isDebugLogEnabled)
340 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)