Merge "Config Assign"
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / AbstractServiceTaskProcessor.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.common.scripts;
22
23 import org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.camunda.bpm.engine.variable.VariableMap
26 import org.camunda.bpm.engine.variable.Variables
27 import org.camunda.bpm.engine.variable.Variables.SerializationDataFormats
28 import org.camunda.bpm.engine.variable.impl.value.ObjectValueImpl
29 import org.onap.so.bpmn.common.workflow.context.WorkflowCallbackResponse
30 import org.onap.so.bpmn.common.workflow.context.WorkflowContextHolder
31 import org.onap.so.bpmn.core.UrnPropertiesReader
32 import org.onap.so.bpmn.core.WorkflowException
33 import org.onap.so.client.aai.AAIResourcesClient
34 import org.springframework.web.util.UriUtils
35
36 import groovy.json.JsonSlurper
37
38 public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcessor {
39         public MsoUtils utils = new MsoUtils()
40
41
42         /**
43          * Logs a message at the ERROR level.
44          * @param message the message
45          */
46         public void logError(String message) {
47                 log('ERROR', message, null, "true")
48         }
49
50         /**
51          * Logs a message at the ERROR level.
52          * @param message the message
53          * @param cause the cause (stracktrace will be included in the output)
54          */
55         public void logError(String message, Throwable cause) {
56                 log('ERROR', message, cause, "true")
57         }
58
59         /**
60          * Logs a message at the WARN level.
61          * @param message the message
62          */
63         public void logWarn(String message) {
64                 log('WARN', message, null, "true")
65         }
66
67         /**
68          * Logs a message at the WARN level.
69          * @param message the message
70          * @param cause the cause (stracktrace will be included in the output)
71          */
72         public void logWarn(String message, Throwable cause) {
73                 log('WARN', message, cause, "true")
74         }
75
76         /**
77          * Logs a message at the DEBUG level.
78          * @param message the message
79          * @param isDebugLogEnabled a flag indicating if DEBUG level is enabled
80          */
81         public void logDebug(String message, String isDebugLogEnabled) {
82                 log('DEBUG', message, null, isDebugLogEnabled)
83         }
84
85         /**
86          * Logs a message at the DEBUG level.
87          * @param message the message
88          * @param cause the cause (stracktrace will be included in the output)
89          * @param isDebugLogEnabled a flag indicating if DEBUG level is enabled
90          */
91         public void logDebug(String message, Throwable cause, String isDebugLogEnabled) {
92                 log('DEBUG', message, cause, isDebugLogEnabled)
93         }
94
95         /**
96          * Logs a message at the specified level.
97          * @param level the level (DEBUG, INFO, WARN, ERROR)
98          * @param message the message
99          * @param isLevelEnabled a flag indicating if the level is enabled
100          *        (used only at the DEBUG level)
101          */
102         public void log(String level, String message, String isLevelEnabled) {
103                 log(level, message,  null, isLevelEnabled)
104         }
105
106         /**
107          * Logs a message at the specified level.
108          * @param level the level (DEBUG, INFO, WARN, ERROR)
109          * @param message the message
110          * @param cause the cause (stracktrace will be included in the output)
111          * @param isLevelEnabled a flag indicating if the level is enabled
112          *        (used only at the DEBUG level)
113          */
114         public void log(String level, String message, Throwable cause, String isLevelEnabled) {
115                 if (cause == null) {
116                         utils.log(level, message, isLevelEnabled);
117                 } else {
118                         StringWriter stringWriter = new StringWriter();
119                         PrintWriter printWriter = new PrintWriter(stringWriter);
120                         printWriter.println(message);
121                         cause.printStackTrace(printWriter);
122                         utils.log(level, stringWriter.toString(), isLevelEnabled);
123                         printWriter.close();
124                 }
125         }
126
127         /**
128          * Logs a WorkflowException at the ERROR level with the specified message.
129          * @param execution the execution
130          */
131         public void logWorkflowException(DelegateExecution execution, String message) {
132                 def workflowException = execution.getVariable("WorkflowException")
133
134                 if (workflowException == null) {
135                         logError(message);
136                 } else {
137                         logError(message + ": " + workflowException)
138                 }
139         }
140
141         /**
142          * Saves the WorkflowException in the execution to the specified variable,
143          * clearing the WorkflowException variable so the workflow can continue
144          * processing (perhaps catching another WorkflowException).
145          * @param execution the execution
146          * @return the name of the destination variable
147          */
148         public saveWorkflowException(DelegateExecution execution, String variable) {
149                 if (variable == null) {
150                         throw new NullPointerException();
151                 }
152
153                 execution.setVariable(variable, execution.getVariable("WorkflowException"))
154                 execution.setVariable("WorkflowException", null)
155         }
156
157
158         /**
159          * Validates that the request exists and that the mso-request-id variable is set.
160          * Additional required variables may be checked by specifying their names.
161          * NOTE: services requiring mso-service-instance-id must specify it explicitly!
162          * If a problem is found, buildAndThrowWorkflowException builds a WorkflowException
163          * and throws an MSOWorkflowException.  This method also sets up the log context for
164          * the workflow.
165          *
166          * @param execution the execution
167          * @return the validated request
168          */
169         public String validateRequest(DelegateExecution execution, String... requiredVariables) {
170                 ExceptionUtil exceptionUtil = new ExceptionUtil()
171                 def method = getClass().getSimpleName() + '.validateRequest(' +
172                         'execution=' + execution.getId() +
173                         ', requredVariables=' + requiredVariables +
174                         ')'
175                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
176                 logDebug('Entered ' + method, isDebugLogEnabled)
177
178                 String processKey = getProcessKey(execution)
179                 def prefix = execution.getVariable("prefix")
180
181                 if (prefix == null) {
182                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " prefix is null")
183                 }
184
185                 try {
186                         def request = execution.getVariable(prefix + 'Request')
187
188                         if (request == null) {
189                                 request = execution.getVariable(processKey + 'Request')
190
191                                 if (request == null) {
192                                         request = execution.getVariable('bpmnRequest')
193                                 }
194
195                                 setVariable(execution, processKey + 'Request', null)
196                                 setVariable(execution, 'bpmnRequest', null)
197                                 setVariable(execution, prefix + 'Request', request)
198                         }
199
200                         if (request == null) {
201                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request is null")
202                         }
203
204                         // All requests must have a request ID.
205                         // Some requests (e.g. SDN-MOBILITY) do not have a service instance ID.
206
207                         String requestId = null
208                         String serviceInstanceId = null
209
210                         List<String> allRequiredVariables = new ArrayList<String>()
211                         allRequiredVariables.add("mso-request-id")
212
213                         if (requiredVariables != null) {
214                                 for (String variable : requiredVariables) {
215                                         if (!allRequiredVariables.contains(variable)) {
216                                                 allRequiredVariables.add(variable)
217                                         }
218                                 }
219                         }
220
221                         for (String variable : allRequiredVariables) {
222                                 def value = execution.getVariable(variable)
223                                 if (value == null || ((value instanceof CharSequence) && value.length() == 0)) {
224                                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey +
225                                                 " request was received with no '" + variable + "' variable")
226                                 }
227
228                                 if ("mso-request-id".equals(variable)) {
229                                         requestId = (String) value
230                                 } else if ("mso-service-instance-id".equals(variable)) {
231                                         serviceInstanceId = (String) value
232                                 }
233                         }
234
235                         if (serviceInstanceId == null) {
236                                 serviceInstanceId = (String) execution.getVariable("mso-service-instance-id")
237                         }
238
239                         logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
240                         logDebug('Exited ' + method, isDebugLogEnabled)
241                         return request
242                 } catch (BpmnError e) {
243                         throw e
244                 } catch (Exception e) {
245                         logError('Caught exception in ' + method, e)
246                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Invalid Message")
247                 }
248         }
249
250         /**
251          * gets vars stored in a JSON object in prefix+Request and returns as a LazyMap
252          * setting log context here too
253          * @param execution the execution
254          * @return the inputVars
255          */
256         public Map validateJSONReq(DelegateExecution execution) {
257                 def method = getClass().getSimpleName() + '.validateJSONReq(' +
258                                 'execution=' + execution.getId() +
259                                 ')'
260                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
261                 logDebug('Entered ' + method, isDebugLogEnabled)
262
263                 String processKey = getProcessKey(execution);
264                 def prefix = execution.getVariable("prefix")
265
266                 def request = getVariable(execution, prefix + 'Request')
267
268                 if (request == null) {
269                         request = getVariable(execution, processKey + 'Request')
270
271                         if (request == null) {
272                                 request = getVariable(execution, 'bpmnRequest')
273                         }
274                         execution.setVariable(prefix + 'Request', request)
275                 }
276
277                 def jsonSlurper = new JsonSlurper()
278                 def parsed = jsonSlurper.parseText(request)
279
280
281                 logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
282                 logDebug('Exited ' + method, isDebugLogEnabled)
283                 return parsed
284
285         }
286
287         /**
288          * Sends a response to the workflow service that invoked the process.  This method
289          * may only be used by top-level processes that were directly invoked by the
290          * asynchronous workflow service.
291          * @param execution the execution
292          * @param responseCode the response code
293          * @param content the message content
294          * @throws IllegalArgumentException if the response code is invalid
295          *         by HTTP standards
296          * @throws UnsupportedOperationException if not invoked by an asynchronous,
297          *         top-level process
298          * @throws IllegalStateException if a response has already been sent
299          */
300         protected void sendWorkflowResponse(DelegateExecution execution, Object responseCode, String response) {
301                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
302                 try {
303                         String processKey = getProcessKey(execution);
304
305                         // isAsyncProcess is injected by the workflow service that started the flow
306                         if (!String.valueOf(execution.getVariable("isAsyncProcess")).equals("true")) {
307                                 throw new UnsupportedOperationException(processKey + ": " +
308                                         "sendWorkflowResponse is valid only in asynchronous workflows");
309                         }
310
311                         if (String.valueOf(execution.getVariable(processKey + "WorkflowResponseSent")).equals("true")) {
312                                         logDebug("Sync response has already been sent for " + processKey, isDebugLogEnabled)
313                         }else{
314
315                                 logDebug("Building " + processKey + " response ", isDebugLogEnabled)
316
317                                 int intResponseCode;
318
319                                 try {
320                                         intResponseCode = Integer.parseInt(String.valueOf(responseCode));
321
322                                         if (intResponseCode < 100 || intResponseCode > 599) {
323                                                 throw new NumberFormatException(String.valueOf(responseCode));
324                                         }
325                                 } catch (NumberFormatException e) {
326                                         throw new IllegalArgumentException("Process " + processKey
327                                                 + " provided an invalid HTTP response code: " + responseCode);
328                                 }
329
330                                 // Only 2XX responses are considered "Success"
331                                 String status = (intResponseCode >= 200 && intResponseCode <= 299) ?
332                                         "Success" : "Fail";
333
334                                 // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead
335                                 execution.setVariable(processKey + "ResponseCode", String.valueOf(intResponseCode))
336                                 execution.setVariable(processKey + "Response", response);
337                                 execution.setVariable(processKey + "Status", status);
338                                 execution.setVariable("WorkflowResponse", response)
339
340                                 logDebug("Sending response for " + processKey
341                                         + " ResponseCode=" + intResponseCode
342                                         + " Status=" + status
343                                         + " Response=\n" + response,
344                                         isDebugLogEnabled)
345
346                                 // TODO: ensure that this flow was invoked asynchronously?
347
348                                 WorkflowCallbackResponse callbackResponse = new WorkflowCallbackResponse()
349                                 callbackResponse.setStatusCode(intResponseCode)
350                                 callbackResponse.setMessage(status)
351                                 callbackResponse.setResponse(response)
352
353                                 // TODO: send this data with HTTP POST
354
355                                 WorkflowContextHolder.getInstance().processCallback(
356                                         processKey,
357                                         execution.getProcessInstanceId(),
358                                         execution.getVariable("mso-request-id"),
359                                         callbackResponse)
360
361                                 execution.setVariable(processKey + "WorkflowResponseSent", "true");
362                         }
363
364                 } catch (Exception ex) {
365                         logError("Unable to send workflow response to client ....", ex)
366                 }
367         }
368
369         /**
370          * Returns true if a workflow response has already been sent.
371          * @param execution the execution
372          */
373         protected boolean isWorkflowResponseSent(DelegateExecution execution) {
374                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
375                 String processKey = getProcessKey(execution);
376                 return String.valueOf(execution.getVariable(processKey + "WorkflowResponseSent")).equals("true");
377         }
378
379         /**
380          * Returns the process definition key (i.e. the process name) of the
381          * current process.
382          *
383          * @param execution the execution
384          */
385         public String getProcessKey(DelegateExecution execution) {
386                 def testKey = execution.getVariable("testProcessKey")
387                 if(testKey!=null){
388                         return testKey
389                 }
390                 return execution.getProcessEngineServices().getRepositoryService()
391                         .getProcessDefinition(execution.getProcessDefinitionId()).getKey()
392         }
393
394         /**
395          * Returns the process definition key (i.e. the process name) of the
396          * top-level process.
397          * @param execution the execution
398          */
399         public String getMainProcessKey(DelegateExecution execution) {
400                 DelegateExecution exec = execution
401
402                 while (true) {
403                         DelegateExecution parent = exec.getSuperExecution()
404
405                         if (parent == null) {
406                                 parent = exec.getParent()
407
408                                 if (parent == null) {
409                                         break
410                                 }
411                         }
412
413                         exec = parent
414                 }
415
416                 return execution.getProcessEngineServices().getRepositoryService()
417                         .getProcessDefinition(exec.getProcessDefinitionId()).getKey()
418         }
419
420         /**
421          * Gets the node for the named element from the given xml. If the element
422          * does not exist in the xml or is empty, a WorkflowException is created
423          * (and as a result, a MSOWorkflowException event is thrown).
424          *
425          * @param execution The flow's execution.
426          * @param xml Xml to search.
427          * @param elementName Name of element to search for.
428          * @return The element node, if found in the xml.
429          */
430         protected String getRequiredNodeXml(DelegateExecution execution, String xml, String elementName) {
431                 ExceptionUtil exceptionUtil = new ExceptionUtil()
432                 def element = utils.getNodeXml(xml, elementName, false)
433                 if (element.trim().isEmpty()) {
434                         def msg = 'Required element \'' + elementName + '\' is missing or empty'
435                         logError(msg)
436                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
437                 } else {
438                         return element
439                 }
440         }
441
442         /**
443          * Gets the value of the named element from the given xml. If the element
444          * does not exist in the xml or is empty, a WorkflowException is created
445          * (and as a result, a MSOWorkflowException event is thrown).
446          *
447          * @param execution The flow's execution.
448          * @param xml Xml to search.
449          * @param elementName Name of element to whose value to get.
450          * @return The non-empty value of the element, if found in the xml.
451          */
452         protected String getRequiredNodeText(DelegateExecution execution, String xml, String elementName) {
453                 ExceptionUtil exceptionUtil = new ExceptionUtil()
454                 def elementText = utils.getNodeText(xml, elementName)
455                 if ((elementText == null) || (elementText.isEmpty())) {
456                         def msg = 'Required element \'' + elementName + '\' is missing or empty'
457                         logError(msg)
458                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
459                 } else {
460                         return elementText
461                 }
462         }
463
464         /**
465          * Get the text for the specified element from the specified xml.  If
466          * the element does not exist, return the specified default value.
467          *
468          * @param xml Xml from which to get the element's text
469          * @param elementName Name of element whose text to get
470          * @param defaultValue the default value
471          * @return the element's text or the default value if the element does not
472          * exist in the given xml
473          */
474         protected String getNodeText(String xml, String elementName, String defaultValue) {
475                 def nodeText = utils.getNodeText(xml, elementName)
476                 return (nodeText == null) ? defaultValue : nodeText
477         }
478
479         /**
480          * Get the text for the specified element from the specified xml.  If
481          * the element does not exist, return an empty string.
482          *
483          * @param xml Xml from which to get the element's text.
484          * @param elementName Name of element whose text to get.
485          * @return the element's text or an empty string if the element does not
486          * exist in the given xml.
487          */
488         protected String getNodeTextForce(String xml, String elementName) {
489                 return getNodeText(xml, elementName, '');
490         }
491
492         /**
493         *Store the variable as typed with java serialization type
494         *@param execution
495         *@param name
496         *@param value
497         */
498         public void setVariable(DelegateExecution execution, String name, Object value) {
499                 VariableMap variables = Variables.createVariables()
500                 variables.putValueTyped('payload', Variables.objectValue(value)
501                 .serializationDataFormat(SerializationDataFormats.JAVA) // tells the engine to use java serialization for persisting the value
502                 .create())
503                 execution.setVariable(name,variables)
504         }
505
506         //TODO not sure how this will look in Cockpit
507
508         /**
509          * Returns the variable map
510         *@param execution
511         *@param name
512         *@return
513         **/
514         public String getVariable(DelegateExecution execution, String name) {
515                 def myObj = execution.getVariable(name)
516                 if(myObj instanceof VariableMap){
517                         VariableMap serializedObjectMap = (VariableMap) myObj
518                         ObjectValueImpl payloadObj = serializedObjectMap.getValueTyped('payload')
519                         return payloadObj.getValue()
520                 }else{
521                         return myObj
522                 }
523         }
524
525
526         /**
527          * Returns true if a value equals one of the provided set. Equality is
528          * determined by using the equals method if the value object and the
529          * object in the provided set have the same class. Otherwise, the objects
530          * are converted to strings and then compared.  Nulls are permitted for
531          * the value as well as in the provided set
532          * Example:
533          * <pre>
534          *     def statusCode = getStatusCode()
535          *     isOneOf(statusCode, 200, 201, 204)
536          * </pre>
537          * @param value the value to test
538          * @param these a set of permissable values
539          * @return true if the value is in the provided set
540          */
541         public boolean isOneOf(Object value, Object... these) {
542                 for (Object thisOne : these) {
543                         if (thisOne == null) {
544                                 if (value == null) {
545                                         return true
546                                 }
547                         } else {
548                                 if (value != null) {
549                                         if (value.getClass() == thisOne.getClass()) {
550                                                 if (value.equals(thisOne)) {
551                                                         return true
552                                                 }
553                                         } else {
554                                                 if (String.valueOf(value).equals(String.valueOf(thisOne))) {
555                                                         return true
556                                                 }
557                                         }
558                                 }
559                         }
560                 }
561                 return false
562         }
563
564         /**
565          * Sets flows success indicator variable.
566          *
567          */
568         public void setSuccessIndicator(DelegateExecution execution, boolean isSuccess) {
569                 String prefix = execution.getVariable('prefix')
570                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
571
572                 logDebug('Entered SetSuccessIndicator Method', isDebugLogEnabled)
573                 execution.setVariable(prefix+'SuccessIndicator', isSuccess)
574                 logDebug('Outgoing SuccessIndicator is: ' + execution.getVariable(prefix+'SuccessIndicator') + '', isDebugLogEnabled)
575         }
576
577         /**
578          * Sends a Error Sync Response
579          *
580          */
581         public void sendSyncError(DelegateExecution execution) {
582                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
583                 String requestId = execution.getVariable("mso-request-id")
584                 logDebug('sendSyncError, requestId: ' + requestId, isDebugEnabled)
585                 WorkflowException workflowExceptionObj = execution.getVariable("WorkflowException")
586                 if (workflowExceptionObj != null) {
587                         String errorMessage = workflowExceptionObj.getErrorMessage()
588                         def errorCode = workflowExceptionObj.getErrorCode()
589                         logDebug('sendSyncError, requestId: '  + requestId + ' | errorMessage: ' + errorMessage + ' | errorCode: ' + errorCode, isDebugEnabled)
590                         sendWorkflowResponse(execution, errorCode, errorMessage)
591                 }
592         }
593
594         /**
595          * Executes a named groovy script method in the current object
596          */
597         public void executeMethod(String methodName, Object... args) {
598
599                 if (args != null && args.size() > 0) {
600
601                         // First argument of method to call is always the execution object
602                         DelegateExecution execution = (DelegateExecution) args[0]
603
604                         def classAndMethod = getClass().getSimpleName() + '.' + methodName + '(execution=' + execution.getId() + ')'
605                         def isDebugEnabled =  execution.getVariable('isDebugLogEnabled')
606
607                         logDebug('Entered ' + classAndMethod, isDebugEnabled)
608                         logDebug('Received parameters: ' + args, isDebugEnabled)
609
610                         try{
611                                 def methodToCall = this.metaClass.getMetaMethod(methodName, args)
612                                 logDebug('Method to call: ' + methodToCall, isDebugEnabled)
613                                 methodToCall?.invoke(this, args)
614                         }
615                         catch(BpmnError bpmnError) {
616                                 logDebug('Rethrowing BpmnError ' + bpmnError.getMessage(), isDebugEnabled)
617                                 throw bpmnError
618                         }
619                         catch(Exception e) {
620                                 e.printStackTrace()
621                                 logDebug('Unexpected error encountered - ' + e.getMessage(), isDebugEnabled)
622                                 (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 9999, e.getMessage())
623                         }
624                         finally {
625                                 logDebug('Exited ' + classAndMethod, isDebugEnabled)
626                         }
627                 }
628         }
629
630         /**
631          *This method determines and adds the appropriate ending to come
632          *after a number (-st, -nd, -rd, or -th)
633          *
634          *@param int n
635          *
636          *@return String ending - number with suffix
637          */
638         public static String labelMaker(Object n) {
639                 Integer num
640                 if(n instanceof String){
641                         num = Integer.parseInt(n)
642                 }else{
643                         num = n
644                 }
645
646                 String ending = ""; //the end to be added to the number
647                 if(num != null){
648                 if ((num % 10 == 1) && (num != 11)) {
649                         ending = num + "st";
650                         } else if ((num % 10 == 2) && (num != 12)) {
651                         ending = num + "nd";
652                         } else if ((num % 10 == 3) && (num != 13)) {
653                         ending = num + "rd";
654                         } else {
655                         ending = num + "th";
656                 }
657                 }
658                 return ending
659         }
660
661         /**
662          * Constructs a workflow message callback URL for the specified message type and correlator.
663          * This type of callback URL is used when a workflow wants an MSO adapter (like the SDNC
664          * adapter) to call it back.  In other words, this is for callbacks internal to the MSO
665          * complex.  Use <code>createWorkflowMessageAdapterCallbackURL</code> if the callback
666          * will come from outside the MSO complex.
667          * @param messageType the message type (e.g. SDNCAResponse or VNFAResponse)
668          * @param correlator the correlator value (e.g. a request ID)
669          */
670         public String createCallbackURL(DelegateExecution execution, String messageType, String correlator) {
671                 String endpoint = UrnPropertiesReader.getVariable("mso.workflow.message.endpoint", execution)
672
673                 if (endpoint == null || endpoint.isEmpty()) {
674                         ExceptionUtil exceptionUtil = new ExceptionUtil()
675                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000,
676                                 'mso:workflow:message:endpoint URN mapping is not set')
677                 }
678
679                 while (endpoint.endsWith('/')) {
680                         endpoint = endpoint.substring(0, endpoint.length()-1)
681                 }
682
683                 return endpoint +
684                         '/' + UriUtils.encodePathSegment(messageType, 'UTF-8') +
685                         '/' + UriUtils.encodePathSegment(correlator, 'UTF-8')
686         }
687
688         /**
689          *
690          * Constructs a workflow message callback URL for the specified message type and correlator.
691          * This type of callback URL is used when a workflow wants a system outside the MSO complex
692          * to call it back through the Workflow Message Adapter.
693          * @param messageType the message type (e.g. SNIROResponse)
694          * @param correlator the correlator value (e.g. a request ID)
695          */
696         public String createWorkflowMessageAdapterCallbackURL(DelegateExecution execution, String messageType, String correlator) {
697                 String endpoint = UrnPropertiesReader.getVariable("mso.adapters.workflow.message.endpoint", execution)
698
699                 if (endpoint == null || endpoint.isEmpty()) {
700                         ExceptionUtil exceptionUtil = new ExceptionUtil()
701                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000,
702                                 'mso:adapters:workflow:message:endpoint URN mapping is not set')
703                 }
704
705                 while (endpoint.endsWith('/')) {
706                         endpoint = endpoint.substring(0, endpoint.length()-1)
707                 }
708
709                 return endpoint +
710                         '/' + UriUtils.encodePathSegment(messageType, 'UTF-8') +
711                         '/' + UriUtils.encodePathSegment(correlator, 'UTF-8')
712         }
713
714         public void setRollbackEnabled(DelegateExecution execution, isDebugLogEnabled) {
715
716                 // Rollback settings
717                 def prefix = execution.getVariable('prefix')
718                 def disableRollback = execution.getVariable("disableRollback")
719                 def defaultRollback = UrnPropertiesReader.getVariable("mso.rollback", execution).toBoolean()
720
721                 logDebug('disableRollback: ' + disableRollback, isDebugLogEnabled)
722                 logDebug('defaultRollback: ' + defaultRollback, isDebugLogEnabled)
723
724                 def rollbackEnabled
725
726                 if(disableRollback == null || disableRollback == '' ) {
727                         // get from default urn settings for mso_rollback
728                         disableRollback = !defaultRollback
729                         rollbackEnabled = defaultRollback
730                         logDebug('disableRollback is null or empty!', isDebugLogEnabled)
731                 }
732                 else {
733                         if(disableRollback == true) {
734                                 rollbackEnabled = false
735                         }
736                         else if(disableRollback == false){
737                                 rollbackEnabled = true
738                         }
739                         else {
740                                 rollbackEnabled = defaultRollback
741                         }
742                 }
743                 
744                 execution.setVariable(prefix+"backoutOnFailure", rollbackEnabled)
745                 logDebug('rollbackEnabled (aka backoutOnFailure): ' + rollbackEnabled, isDebugLogEnabled)
746         }
747
748         public void setBasicDBAuthHeader(DelegateExecution execution, isDebugLogEnabled) {
749                 try {
750                         String basicAuthValueDB = UrnPropertiesReader.getVariable("mso.adapters.db.auth", execution)
751                         def encodedString = utils.getBasicAuth(basicAuthValueDB, UrnPropertiesReader.getVariable("mso.msoKey", execution))
752                         execution.setVariable("BasicAuthHeaderValueDB",encodedString)
753                 } catch (IOException ex) {
754                         String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()
755                         utils.log("DEBUG", dataErrorMessage, isDebugLogEnabled)
756                         (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
757                 }
758         }
759     public AAIResourcesClient getAAIClient(){
760         return  new AAIResourcesClient();
761     }
762 }