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 * This version of SDNCAdapterRest allows for interim notifications to be sent for
23 * any non-final response received from SDNC.
25 class SDNCAdapterRestV2 extends SDNCAdapterRestV1 {
27 ExceptionUtil exceptionUtil = new ExceptionUtil()
28 JsonUtils jsonUtil = new JsonUtils()
31 * Processes the incoming request.
33 public void preProcessRequest (DelegateExecution execution) {
34 def method = getClass().getSimpleName() + '.preProcessRequest(' +
35 'execution=' + execution.getId() +
37 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
38 logDebug('Entered ' + method, isDebugLogEnabled)
40 def prefix="SDNCREST_"
41 execution.setVariable("prefix", prefix)
42 setSuccessIndicator(execution, false)
45 // Determine the request type and log the request
47 String request = validateRequest(execution, "mso-request-id")
48 String requestType = jsonUtil.getJsonRootProperty(request)
49 execution.setVariable(prefix + 'requestType', requestType)
50 logDebug(getProcessKey(execution) + ': ' + prefix + 'requestType = ' + requestType, isDebugLogEnabled)
51 utils.logAudit('SDNCAdapterRestV2, request: ' + request)
53 // Determine the SDNCAdapter endpoint
55 String sdncAdapterEndpoint = execution.getVariable("URN_mso_adapters_sdnc_rest_endpoint")
57 if (sdncAdapterEndpoint == null || sdncAdapterEndpoint.isEmpty()) {
58 String msg = getProcessKey(execution) + ': mso:adapters:sdnc:rest:endpoint URN mapping is not defined'
59 logDebug(msg, isDebugLogEnabled)
61 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
64 while (sdncAdapterEndpoint.endsWith('/')) {
65 sdncAdapterEndpoint = sdncAdapterEndpoint.substring(0, sdncAdapterEndpoint.length()-1)
68 String sdncAdapterMethod = null
69 String sdncAdapterUrl = null
70 String sdncAdapterRequest = request
72 if ('SDNCServiceRequest'.equals(requestType)) {
73 // Get the sdncRequestId from the request
75 String sdncRequestId = jsonUtil.getJsonValue(request, requestType + ".sdncRequestId")
77 if (sdncRequestId == null || sdncRequestId.isEmpty()) {
78 String msg = getProcessKey(execution) + ': no sdncRequestId in ' + requestType
79 logDebug(msg, isDebugLogEnabled)
81 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
84 execution.setVariable('SDNCAResponse_CORRELATOR', sdncRequestId)
85 logDebug(getProcessKey(execution) + ': SDNCAResponse_CORRELATOR = ' + sdncRequestId, isDebugLogEnabled)
87 // Get the bpNotificationUrl from the request (just to make sure it's there)
89 String bpNotificationUrl = jsonUtil.getJsonValue(request, requestType + ".bpNotificationUrl")
91 if (bpNotificationUrl == null || bpNotificationUrl.isEmpty()) {
92 String msg = getProcessKey(execution) + ': no bpNotificationUrl in ' + requestType
93 logDebug(msg, isDebugLogEnabled)
95 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
98 sdncAdapterMethod = 'POST'
99 sdncAdapterUrl = sdncAdapterEndpoint + '/services'
102 String msg = getProcessKey(execution) + ': Unsupported request type: ' + requestType
103 logDebug(msg, isDebugLogEnabled)
105 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
108 execution.setVariable(prefix + 'sdncAdapterMethod', sdncAdapterMethod)
109 logDebug(getProcessKey(execution) + ': ' + prefix + 'sdncAdapterMethod = ' + sdncAdapterMethod, isDebugLogEnabled)
110 execution.setVariable(prefix + 'sdncAdapterUrl', sdncAdapterUrl)
111 logDebug(getProcessKey(execution) + ': ' + prefix + 'sdncAdapterUrl = ' + sdncAdapterUrl, isDebugLogEnabled)
112 execution.setVariable(prefix + 'sdncAdapterRequest', sdncAdapterRequest)
113 logDebug(getProcessKey(execution) + ': ' + prefix + 'sdncAdapterRequest = \n' + sdncAdapterRequest, isDebugLogEnabled)
115 // Get the Basic Auth credentials for the SDNCAdapter (yes... we ARE using the PO adapters credentials)
117 String basicAuthValue = execution.getVariable("URN_mso_adapters_po_auth")
119 if (basicAuthValue == null || basicAuthValue.isEmpty()) {
120 logDebug(getProcessKey(execution) + ": mso:adapters:po:auth URN mapping is not defined", isDebugLogEnabled)
121 logError(getProcessKey(execution) + ": mso:adapters:po:auth URN mapping is not defined")
123 logDebug(getProcessKey(execution) + ": Obtained BasicAuth credentials for SDNCAdapter:" +
124 basicAuthValue, isDebugLogEnabled)
126 def encodedString = utils.getBasicAuth(basicAuthValue, execution.getVariable("URN_mso_msoKey"))
127 execution.setVariable(prefix + 'basicAuthHeaderValue', encodedString)
128 } catch (IOException ex) {
129 logDebug(getProcessKey(execution) + ": Unable to encode BasicAuth credentials for SDNCAdapter", isDebugLogEnabled)
130 logError(getProcessKey(execution) + ": Unable to encode BasicAuth credentials for SDNCAdapter")
134 // Set the timeout value, e.g. PT5M. It may be specified in the request as the
135 // bpTimeout value. If it's not in the request, use the URN mapping value.
137 String timeout = jsonUtil.getJsonValue(request, requestType + ".bpTimeout")
139 if (timeout == null || timeout.isEmpty()) {
140 timeout = execution.getVariable("URN_mso_sdnc_timeout")
143 execution.setVariable(prefix + 'timeout', timeout)
144 logDebug(getProcessKey(execution) + ': ' + prefix + 'timeout = ' + timeout, isDebugLogEnabled)
145 } catch (BpmnError e) {
147 } catch (Exception e) {
148 String msg = 'Caught exception in ' + method + ": " + e
149 logDebug(msg, isDebugLogEnabled)
151 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
156 * Processes a callback. Check for possible interim notification.
158 public void processCallback(DelegateExecution execution){
159 def method = getClass().getSimpleName() + '.processCallback(' +
160 'execution=' + execution.getId() +
162 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
163 logDebug('Entered ' + method, isDebugLogEnabled)
165 String prefix = execution.getVariable('prefix')
166 String callback = execution.getVariable('SDNCAResponse_MESSAGE')
167 utils.logAudit("Incoming SDNC Rest Callback is: " + callback)
170 logDebug(getProcessKey(execution) + ": received callback:\n" + callback, isDebugLogEnabled)
172 int callbackNumber = 1
173 while (execution.getVariable(prefix + 'callback' + callbackNumber) != null) {
177 execution.setVariable(prefix + 'callback' + callbackNumber, callback)
178 execution.removeVariable('SDNCAResponse_MESSAGE')
180 String responseType = jsonUtil.getJsonRootProperty(callback)
182 // Get the ackFinalIndicator and make sure it's either Y or N. Default to Y.
183 String ackFinalIndicator = jsonUtil.getJsonValue(callback, responseType + ".ackFinalIndicator")
185 if (!'N'.equals(ackFinalIndicator)) {
186 ackFinalIndicator = 'Y'
189 execution.setVariable(prefix + "ackFinalIndicator", ackFinalIndicator)
191 if (responseType.endsWith('Error')) {
192 sdncAdapterBuildWorkflowException(execution, callback)
195 // Check for possible interim notification
196 execution.setVariable(prefix + "interimNotification", null)
197 execution.setVariable(prefix + "doInterimNotification", false)
198 if ('N'.equals(ackFinalIndicator)) {
199 def interimNotification = execution.getVariable(prefix + "InterimNotification" + callbackNumber)
200 if (interimNotification != null) {
201 execution.setVariable(prefix + "interimNotification", interimNotification)
202 execution.setVariable(prefix + "doInterimNotification", true)
206 } catch (Exception e) {
207 callback = callback == null || String.valueOf(callback).isEmpty() ? "NONE" : callback
208 String msg = "Received error from SDNCAdapter: " + callback
209 logDebug(getProcessKey(execution) + ': ' + msg, isDebugLogEnabled)
210 exceptionUtil.buildWorkflowException(execution, 5300, msg)
215 * Prepare to send an interim notification by extracting the variable/value definitions
216 * in the interimNotification JSON object and placing them in the execution. These
217 * variable/value definitions will be passed to the notification service.
219 public void prepareInterimNotification(DelegateExecution execution) {
220 def method = getClass().getSimpleName() + '.prepareInterimNotification(' +
221 'execution=' + execution.getId() +
223 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
224 logDebug('Entered ' + method, isDebugLogEnabled)
226 String prefix = execution.getVariable('prefix')
227 utils.logAudit("Preparing Interim Notification")
230 def interimNotification = execution.getVariable(prefix + "interimNotification")
231 logDebug("Preparing Interim Notification:\n" + JsonUtils.prettyJson(interimNotification), isDebugLogEnabled)
233 for (int i = 0; ; i++) {
234 def variable = JsonUtils.getJsonParamValue(interimNotification, 'variableList', 'variable', i)
236 if (variable == null) {
240 def String variableName = JsonUtils.getJsonValue(variable, "name")
241 if ((variableName != null) && !variableName.isEmpty()) {
242 def variableValue = JsonUtils.getJsonValue(variable, "value")
243 execution.setVariable(variableName, variableValue)
244 logDebug("Setting "+ variableName + "=" + variableValue, isDebugLogEnabled)
248 } catch (Exception e) {
249 String msg = "Error preparing interim notification"
250 logDebug(getProcessKey(execution) + ': ' + msg, isDebugLogEnabled)
251 exceptionUtil.buildWorkflowException(execution, 5300, msg)