AT&T 1712 and 1802 release code
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / SDNCAdapterRestV2.groovy
1 package org.openecomp.mso.bpmn.common.scripts
2
3 import java.text.SimpleDateFormat
4 import java.net.URLEncoder
5
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
10
11 import groovy.json.*
12
13 import org.json.JSONObject
14
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
20
21 /**
22  * This version of SDNCAdapterRest allows for interim notifications to be sent for
23  * any non-final response received from SDNC.
24  */
25 class SDNCAdapterRestV2 extends SDNCAdapterRestV1 {
26
27         ExceptionUtil exceptionUtil = new ExceptionUtil()
28         JsonUtils jsonUtil = new JsonUtils()
29
30         /**
31          * Processes the incoming request.
32          */
33         public void preProcessRequest (DelegateExecution execution) {
34                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
35                         'execution=' + execution.getId() +
36                         ')'
37                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
38                 logDebug('Entered ' + method, isDebugLogEnabled)
39
40                 def prefix="SDNCREST_"
41                 execution.setVariable("prefix", prefix)
42                 setSuccessIndicator(execution, false)
43
44                 try {
45                         // Determine the request type and log the request
46
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)
52
53                         // Determine the SDNCAdapter endpoint
54
55                         String sdncAdapterEndpoint = execution.getVariable("URN_mso_adapters_sdnc_rest_endpoint")
56
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)
60                                 logError(msg)
61                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
62                         }
63
64                         while (sdncAdapterEndpoint.endsWith('/')) {
65                                 sdncAdapterEndpoint = sdncAdapterEndpoint.substring(0, sdncAdapterEndpoint.length()-1)
66                         }
67
68                         String sdncAdapterMethod = null
69                         String sdncAdapterUrl = null
70                         String sdncAdapterRequest = request
71
72                         if ('SDNCServiceRequest'.equals(requestType)) {
73                                 // Get the sdncRequestId from the request
74
75                                 String sdncRequestId = jsonUtil.getJsonValue(request, requestType + ".sdncRequestId")
76
77                                 if (sdncRequestId == null || sdncRequestId.isEmpty()) {
78                                         String msg = getProcessKey(execution) + ': no sdncRequestId in ' + requestType
79                                         logDebug(msg, isDebugLogEnabled)
80                                         logError(msg)
81                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
82                                 }
83
84                                 execution.setVariable('SDNCAResponse_CORRELATOR', sdncRequestId)
85                                 logDebug(getProcessKey(execution) + ': SDNCAResponse_CORRELATOR = ' + sdncRequestId, isDebugLogEnabled)
86
87                                 // Get the bpNotificationUrl from the request (just to make sure it's there)
88
89                                 String bpNotificationUrl = jsonUtil.getJsonValue(request, requestType + ".bpNotificationUrl")
90
91                                 if (bpNotificationUrl == null || bpNotificationUrl.isEmpty()) {
92                                         String msg = getProcessKey(execution) + ': no bpNotificationUrl in ' + requestType
93                                         logDebug(msg, isDebugLogEnabled)
94                                         logError(msg)
95                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
96                                 }
97
98                                 sdncAdapterMethod = 'POST'
99                                 sdncAdapterUrl = sdncAdapterEndpoint + '/services'
100
101                         } else {
102                                 String msg = getProcessKey(execution) + ': Unsupported request type: ' + requestType
103                                 logDebug(msg, isDebugLogEnabled)
104                                 logError(msg)
105                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
106                         }
107
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)
114
115                         // Get the Basic Auth credentials for the SDNCAdapter (yes... we ARE using the PO adapters credentials)
116
117                         String basicAuthValue = execution.getVariable("URN_mso_adapters_po_auth")
118
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")
122                         } else {
123                                 logDebug(getProcessKey(execution) + ": Obtained BasicAuth credentials for SDNCAdapter:" +
124                                         basicAuthValue, isDebugLogEnabled)
125                                 try {
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")
131                                 }
132                         }
133
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.
136
137                         String timeout = jsonUtil.getJsonValue(request, requestType + ".bpTimeout")
138
139                         if (timeout == null || timeout.isEmpty()) {
140                                 timeout = execution.getVariable("URN_mso_sdnc_timeout")
141                         }
142
143                         execution.setVariable(prefix + 'timeout', timeout)
144                         logDebug(getProcessKey(execution) + ': ' + prefix + 'timeout = ' + timeout, isDebugLogEnabled)
145                 } catch (BpmnError e) {
146                         throw e
147                 } catch (Exception e) {
148                         String msg = 'Caught exception in ' + method + ": " + e
149                         logDebug(msg, isDebugLogEnabled)
150                         logError(msg)
151                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
152                 }
153         }
154
155         /**
156          * Processes a callback. Check for possible interim notification.
157          */
158         public void processCallback(DelegateExecution execution){
159                 def method = getClass().getSimpleName() + '.processCallback(' +
160                         'execution=' + execution.getId() +
161                         ')'
162                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
163                 logDebug('Entered ' + method, isDebugLogEnabled)
164
165                 String prefix = execution.getVariable('prefix')
166                 String callback = execution.getVariable('SDNCAResponse_MESSAGE')
167                 utils.logAudit("Incoming SDNC Rest Callback is: " + callback)
168
169                 try {
170                         logDebug(getProcessKey(execution) + ": received callback:\n" + callback, isDebugLogEnabled)
171
172                         int callbackNumber = 1
173                         while (execution.getVariable(prefix + 'callback' + callbackNumber) != null) {
174                                 ++callbackNumber
175                         }
176
177                         execution.setVariable(prefix + 'callback' + callbackNumber, callback)
178                         execution.removeVariable('SDNCAResponse_MESSAGE')
179
180                         String responseType = jsonUtil.getJsonRootProperty(callback)
181
182                         // Get the ackFinalIndicator and make sure it's either Y or N.  Default to Y.
183                         String ackFinalIndicator = jsonUtil.getJsonValue(callback, responseType + ".ackFinalIndicator")
184
185                         if (!'N'.equals(ackFinalIndicator)) {
186                                 ackFinalIndicator = 'Y'
187                         }
188
189                         execution.setVariable(prefix + "ackFinalIndicator", ackFinalIndicator)
190
191                         if (responseType.endsWith('Error')) {
192                                 sdncAdapterBuildWorkflowException(execution, callback)
193                         }
194                         
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)
203                                 }
204                         }
205                         
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)
211                 }
212         }
213         
214         /**
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.
218          */
219         public void prepareInterimNotification(DelegateExecution execution) {
220                 def method = getClass().getSimpleName() + '.prepareInterimNotification(' +
221                         'execution=' + execution.getId() +
222                         ')'
223                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
224                 logDebug('Entered ' + method, isDebugLogEnabled)
225                 
226                 String prefix = execution.getVariable('prefix')
227                 utils.logAudit("Preparing Interim Notification")
228
229                 try {
230                         def interimNotification = execution.getVariable(prefix + "interimNotification")
231                         logDebug("Preparing Interim Notification:\n" + JsonUtils.prettyJson(interimNotification), isDebugLogEnabled)
232                         
233                         for (int i = 0; ; i++) {
234                                 def variable = JsonUtils.getJsonParamValue(interimNotification, 'variableList', 'variable', i)
235                                 
236                                 if (variable == null) {
237                                         break
238                                 }
239                                 
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)
245                                 }
246                         }
247                         
248                 } catch (Exception e) {
249                         String msg = "Error preparing interim notification"
250                         logDebug(getProcessKey(execution) + ': ' + msg, isDebugLogEnabled)
251                         exceptionUtil.buildWorkflowException(execution, 5300, msg)
252                 }
253         }
254 }