Merge 'origin/casablanca' into master
[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                         utils.logContext(requestId, serviceInstanceId)
240                         logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
241                         logDebug('Exited ' + method, isDebugLogEnabled)
242                         return request
243                 } catch (BpmnError e) {
244                         throw e
245                 } catch (Exception e) {
246                         logError('Caught exception in ' + method, e)
247                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Invalid Message")
248                 }
249         }
250
251         /**
252          * gets vars stored in a JSON object in prefix+Request and returns as a LazyMap
253          * setting log context here too
254          * @param execution the execution
255          * @return the inputVars
256          */
257         public Map validateJSONReq(DelegateExecution execution) {
258                 def method = getClass().getSimpleName() + '.validateJSONReq(' +
259                                 'execution=' + execution.getId() +
260                                 ')'
261                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
262                 logDebug('Entered ' + method, isDebugLogEnabled)
263
264                 String processKey = getProcessKey(execution);
265                 def prefix = execution.getVariable("prefix")
266
267                 def requestId =getVariable(execution, "mso-request-id")
268                 def serviceInstanceId = getVariable(execution, "mso-service-instance-id")
269                 if(requestId!=null && serviceInstanceId!=null){
270                         utils.logContext(requestId, serviceInstanceId)
271                 }
272
273
274                 def request = getVariable(execution, prefix + 'Request')
275
276                 if (request == null) {
277                         request = getVariable(execution, processKey + 'Request')
278
279                         if (request == null) {
280                                 request = getVariable(execution, 'bpmnRequest')
281                         }
282                         execution.setVariable(prefix + 'Request', request)
283                 }
284
285                 def jsonSlurper = new JsonSlurper()
286                 def parsed = jsonSlurper.parseText(request)
287
288
289                 logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
290                 logDebug('Exited ' + method, isDebugLogEnabled)
291                 return parsed
292
293         }
294
295         /**
296          * Sends a response to the workflow service that invoked the process.  This method
297          * may only be used by top-level processes that were directly invoked by the
298          * asynchronous workflow service.
299          * @param execution the execution
300          * @param responseCode the response code
301          * @param content the message content
302          * @throws IllegalArgumentException if the response code is invalid
303          *         by HTTP standards
304          * @throws UnsupportedOperationException if not invoked by an asynchronous,
305          *         top-level process
306          * @throws IllegalStateException if a response has already been sent
307          */
308         protected void sendWorkflowResponse(DelegateExecution execution, Object responseCode, String response) {
309                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
310                 try {
311                         String processKey = getProcessKey(execution);
312
313                         // isAsyncProcess is injected by the workflow service that started the flow
314                         if (!String.valueOf(execution.getVariable("isAsyncProcess")).equals("true")) {
315                                 throw new UnsupportedOperationException(processKey + ": " +
316                                         "sendWorkflowResponse is valid only in asynchronous workflows");
317                         }
318
319                         if (String.valueOf(execution.getVariable(processKey + "WorkflowResponseSent")).equals("true")) {
320                                         logDebug("Sync response has already been sent for " + processKey, isDebugLogEnabled)
321                         }else{
322
323                                 logDebug("Building " + processKey + " response ", isDebugLogEnabled)
324
325                                 int intResponseCode;
326
327                                 try {
328                                         intResponseCode = Integer.parseInt(String.valueOf(responseCode));
329
330                                         if (intResponseCode < 100 || intResponseCode > 599) {
331                                                 throw new NumberFormatException(String.valueOf(responseCode));
332                                         }
333                                 } catch (NumberFormatException e) {
334                                         throw new IllegalArgumentException("Process " + processKey
335                                                 + " provided an invalid HTTP response code: " + responseCode);
336                                 }
337
338                                 // Only 2XX responses are considered "Success"
339                                 String status = (intResponseCode >= 200 && intResponseCode <= 299) ?
340                                         "Success" : "Fail";
341
342                                 // TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead
343                                 execution.setVariable(processKey + "ResponseCode", String.valueOf(intResponseCode))
344                                 execution.setVariable(processKey + "Response", response);
345                                 execution.setVariable(processKey + "Status", status);
346                                 execution.setVariable("WorkflowResponse", response)
347
348                                 logDebug("Sending response for " + processKey
349                                         + " ResponseCode=" + intResponseCode
350                                         + " Status=" + status
351                                         + " Response=\n" + response,
352                                         isDebugLogEnabled)
353
354                                 // TODO: ensure that this flow was invoked asynchronously?
355
356                                 WorkflowCallbackResponse callbackResponse = new WorkflowCallbackResponse()
357                                 callbackResponse.setStatusCode(intResponseCode)
358                                 callbackResponse.setMessage(status)
359                                 callbackResponse.setResponse(response)
360
361                                 // TODO: send this data with HTTP POST
362
363                                 WorkflowContextHolder.getInstance().processCallback(
364                                         processKey,
365                                         execution.getProcessInstanceId(),
366                                         execution.getVariable("mso-request-id"),
367                                         callbackResponse)
368
369                                 execution.setVariable(processKey + "WorkflowResponseSent", "true");
370                         }
371
372                 } catch (Exception ex) {
373                         logError("Unable to send workflow response to client ....", ex)
374                 }
375         }
376
377         /**
378          * Returns true if a workflow response has already been sent.
379          * @param execution the execution
380          */
381         protected boolean isWorkflowResponseSent(DelegateExecution execution) {
382                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
383                 String processKey = getProcessKey(execution);
384                 return String.valueOf(execution.getVariable(processKey + "WorkflowResponseSent")).equals("true");
385         }
386
387         /**
388          * Returns the process definition key (i.e. the process name) of the
389          * current process.
390          * 
391          * @param execution the execution
392          */
393         public String getProcessKey(DelegateExecution execution) {
394                 def testKey = execution.getVariable("testProcessKey")
395                 if(testKey!=null){
396                         return testKey
397                 }
398                 return execution.getProcessEngineServices().getRepositoryService()
399                         .getProcessDefinition(execution.getProcessDefinitionId()).getKey()
400         }
401
402         /**
403          * Returns the process definition key (i.e. the process name) of the
404          * top-level process.
405          * @param execution the execution
406          */
407         public String getMainProcessKey(DelegateExecution execution) {
408                 DelegateExecution exec = execution
409
410                 while (true) {
411                         DelegateExecution parent = exec.getSuperExecution()
412
413                         if (parent == null) {
414                                 parent = exec.getParent()
415
416                                 if (parent == null) {
417                                         break
418                                 }
419                         }
420
421                         exec = parent
422                 }
423
424                 return execution.getProcessEngineServices().getRepositoryService()
425                         .getProcessDefinition(exec.getProcessDefinitionId()).getKey()
426         }
427
428         /**
429          * Gets the node for the named element from the given xml. If the element
430          * does not exist in the xml or is empty, a WorkflowException is created
431          * (and as a result, a MSOWorkflowException event is thrown).
432          *
433          * @param execution The flow's execution.
434          * @param xml Xml to search.
435          * @param elementName Name of element to search for.
436          * @return The element node, if found in the xml.
437          */
438         protected String getRequiredNodeXml(DelegateExecution execution, String xml, String elementName) {
439                 ExceptionUtil exceptionUtil = new ExceptionUtil()
440                 def element = utils.getNodeXml(xml, elementName, false)
441                 if (element.trim().isEmpty()) {
442                         def msg = 'Required element \'' + elementName + '\' is missing or empty'
443                         logError(msg)
444                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
445                 } else {
446                         return element
447                 }
448         }
449
450         /**
451          * Gets the value of the named element from the given xml. If the element
452          * does not exist in the xml or is empty, a WorkflowException is created
453          * (and as a result, a MSOWorkflowException event is thrown).
454          *
455          * @param execution The flow's execution.
456          * @param xml Xml to search.
457          * @param elementName Name of element to whose value to get.
458          * @return The non-empty value of the element, if found in the xml.
459          */
460         protected String getRequiredNodeText(DelegateExecution execution, String xml, String elementName) {
461                 ExceptionUtil exceptionUtil = new ExceptionUtil()
462                 def elementText = utils.getNodeText(xml, elementName)
463                 if ((elementText == null) || (elementText.isEmpty())) {
464                         def msg = 'Required element \'' + elementName + '\' is missing or empty'
465                         logError(msg)
466                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)
467                 } else {
468                         return elementText
469                 }
470         }
471
472         /**
473          * Get the text for the specified element from the specified xml.  If
474          * the element does not exist, return the specified default value.
475          *
476          * @param xml Xml from which to get the element's text
477          * @param elementName Name of element whose text to get
478          * @param defaultValue the default value
479          * @return the element's text or the default value if the element does not
480          * exist in the given xml
481          */
482         protected String getNodeText(String xml, String elementName, String defaultValue) {
483                 def nodeText = utils.getNodeText(xml, elementName)
484                 return (nodeText == null) ? defaultValue : nodeText
485         }
486
487         /**
488          * Get the text for the specified element from the specified xml.  If
489          * the element does not exist, return an empty string.
490          *
491          * @param xml Xml from which to get the element's text.
492          * @param elementName Name of element whose text to get.
493          * @return the element's text or an empty string if the element does not
494          * exist in the given xml.
495          */
496         protected String getNodeTextForce(String xml, String elementName) {
497                 return getNodeText(xml, elementName, '');
498         }
499
500         /**
501         *Store the variable as typed with java serialization type
502         *@param execution
503         *@param name
504         *@param value
505         */
506         public void setVariable(DelegateExecution execution, String name, Object value) {
507                 VariableMap variables = Variables.createVariables()
508                 variables.putValueTyped('payload', Variables.objectValue(value)
509                 .serializationDataFormat(SerializationDataFormats.JAVA) // tells the engine to use java serialization for persisting the value
510                 .create())
511                 execution.setVariable(name,variables)
512         }
513
514         //TODO not sure how this will look in Cockpit
515
516         /**
517          * Returns the variable map
518         *@param execution
519         *@param name
520         *@return
521         **/
522         public String getVariable(DelegateExecution execution, String name) {
523                 def myObj = execution.getVariable(name)
524                 if(myObj instanceof VariableMap){
525                         VariableMap serializedObjectMap = (VariableMap) myObj
526                         ObjectValueImpl payloadObj = serializedObjectMap.getValueTyped('payload')
527                         return payloadObj.getValue()
528                 }else{
529                         return myObj
530                 }
531         }
532
533
534         /**
535          * Returns true if a value equals one of the provided set. Equality is
536          * determined by using the equals method if the value object and the
537          * object in the provided set have the same class. Otherwise, the objects
538          * are converted to strings and then compared.  Nulls are permitted for
539          * the value as well as in the provided set
540          * Example:
541          * <pre>
542          *     def statusCode = getStatusCode()
543          *     isOneOf(statusCode, 200, 201, 204)
544          * </pre>
545          * @param value the value to test
546          * @param these a set of permissable values
547          * @return true if the value is in the provided set
548          */
549         public boolean isOneOf(Object value, Object... these) {
550                 for (Object thisOne : these) {
551                         if (thisOne == null) {
552                                 if (value == null) {
553                                         return true
554                                 }
555                         } else {
556                                 if (value != null) {
557                                         if (value.getClass() == thisOne.getClass()) {
558                                                 if (value.equals(thisOne)) {
559                                                         return true
560                                                 }
561                                         } else {
562                                                 if (String.valueOf(value).equals(String.valueOf(thisOne))) {
563                                                         return true
564                                                 }
565                                         }
566                                 }
567                         }
568                 }
569                 return false
570         }
571
572         /**
573          * Sets flows success indicator variable.
574          *
575          */
576         public void setSuccessIndicator(DelegateExecution execution, boolean isSuccess) {
577                 String prefix = execution.getVariable('prefix')
578                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
579
580                 logDebug('Entered SetSuccessIndicator Method', isDebugLogEnabled)
581                 execution.setVariable(prefix+'SuccessIndicator', isSuccess)
582                 logDebug('Outgoing SuccessIndicator is: ' + execution.getVariable(prefix+'SuccessIndicator') + '', isDebugLogEnabled)
583         }
584
585         /**
586          * Sends a Error Sync Response
587          *
588          */
589         public void sendSyncError(DelegateExecution execution) {
590                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
591                 String requestId = execution.getVariable("mso-request-id")
592                 logDebug('sendSyncError, requestId: ' + requestId, isDebugEnabled)
593                 WorkflowException workflowExceptionObj = execution.getVariable("WorkflowException")
594                 if (workflowExceptionObj != null) {
595                         String errorMessage = workflowExceptionObj.getErrorMessage()
596                         def errorCode = workflowExceptionObj.getErrorCode()
597                         logDebug('sendSyncError, requestId: '  + requestId + ' | errorMessage: ' + errorMessage + ' | errorCode: ' + errorCode, isDebugEnabled)
598                         sendWorkflowResponse(execution, errorCode, errorMessage)
599                 }
600         }
601
602         /**
603          * Executes a named groovy script method in the current object
604          */
605         public void executeMethod(String methodName, Object... args) {
606
607                 if (args != null && args.size() > 0) {
608
609                         // First argument of method to call is always the execution object
610                         DelegateExecution execution = (DelegateExecution) args[0]
611
612                         def classAndMethod = getClass().getSimpleName() + '.' + methodName + '(execution=' + execution.getId() + ')'
613                         def isDebugEnabled =  execution.getVariable('isDebugLogEnabled')
614
615                         logDebug('Entered ' + classAndMethod, isDebugEnabled)
616                         logDebug('Received parameters: ' + args, isDebugEnabled)
617
618                         try{
619                                 def methodToCall = this.metaClass.getMetaMethod(methodName, args)
620                                 logDebug('Method to call: ' + methodToCall, isDebugEnabled)
621                                 methodToCall?.invoke(this, args)
622                         }
623                         catch(BpmnError bpmnError) {
624                                 logDebug('Rethrowing BpmnError ' + bpmnError.getMessage(), isDebugEnabled)
625                                 throw bpmnError
626                         }
627                         catch(Exception e) {
628                                 e.printStackTrace()
629                                 logDebug('Unexpected error encountered - ' + e.getMessage(), isDebugEnabled)
630                                 (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 9999, e.getMessage())
631                         }
632                         finally {
633                                 logDebug('Exited ' + classAndMethod, isDebugEnabled)
634                         }
635                 }
636         }
637
638         /**
639          *This method determines and adds the appropriate ending to come
640          *after a number (-st, -nd, -rd, or -th)
641          *
642          *@param int n
643          *
644          *@return String ending - number with suffix
645          */
646         public static String labelMaker(Object n) {
647                 Integer num
648                 if(n instanceof String){
649                         num = Integer.parseInt(n)
650                 }else{
651                         num = n
652                 }
653
654                 String ending = ""; //the end to be added to the number
655                 if(num != null){
656                 if ((num % 10 == 1) && (num != 11)) {
657                         ending = num + "st";
658                         } else if ((num % 10 == 2) && (num != 12)) {
659                         ending = num + "nd";
660                         } else if ((num % 10 == 3) && (num != 13)) {
661                         ending = num + "rd";
662                         } else {
663                         ending = num + "th";
664                 }
665                 }
666                 return ending
667         }
668
669         /**
670          * Constructs a workflow message callback URL for the specified message type and correlator.
671          * This type of callback URL is used when a workflow wants an MSO adapter (like the SDNC
672          * adapter) to call it back.  In other words, this is for callbacks internal to the MSO
673          * complex.  Use <code>createWorkflowMessageAdapterCallbackURL</code> if the callback
674          * will come from outside the MSO complex.
675          * @param messageType the message type (e.g. SDNCAResponse or VNFAResponse)
676          * @param correlator the correlator value (e.g. a request ID)
677          */
678         public String createCallbackURL(DelegateExecution execution, String messageType, String correlator) {
679                 String endpoint = UrnPropertiesReader.getVariable("mso.workflow.message.endpoint", execution)
680
681                 if (endpoint == null || endpoint.isEmpty()) {
682                         ExceptionUtil exceptionUtil = new ExceptionUtil()
683                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000,
684                                 'mso:workflow:message:endpoint URN mapping is not set')
685                 }
686
687                 while (endpoint.endsWith('/')) {
688                         endpoint = endpoint.substring(0, endpoint.length()-1)
689                 }
690
691                 return endpoint +
692                         '/' + UriUtils.encodePathSegment(messageType, 'UTF-8') +
693                         '/' + UriUtils.encodePathSegment(correlator, 'UTF-8')
694         }
695
696         /**
697          *
698          * Constructs a workflow message callback URL for the specified message type and correlator.
699          * This type of callback URL is used when a workflow wants a system outside the MSO complex
700          * to call it back through the Workflow Message Adapter.
701          * @param messageType the message type (e.g. SNIROResponse)
702          * @param correlator the correlator value (e.g. a request ID)
703          */
704         public String createWorkflowMessageAdapterCallbackURL(DelegateExecution execution, String messageType, String correlator) {
705                 String endpoint = UrnPropertiesReader.getVariable("mso.adapters.workflow.message.endpoint", execution)
706
707                 if (endpoint == null || endpoint.isEmpty()) {
708                         ExceptionUtil exceptionUtil = new ExceptionUtil()
709                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000,
710                                 'mso:adapters:workflow:message:endpoint URN mapping is not set')
711                 }
712
713                 while (endpoint.endsWith('/')) {
714                         endpoint = endpoint.substring(0, endpoint.length()-1)
715                 }
716
717                 return endpoint +
718                         '/' + UriUtils.encodePathSegment(messageType, 'UTF-8') +
719                         '/' + UriUtils.encodePathSegment(correlator, 'UTF-8')
720         }
721         
722         public void setRollbackEnabled(DelegateExecution execution, isDebugLogEnabled) {
723                 
724                 // Rollback settings
725                 def prefix = execution.getVariable('prefix')
726                 def disableRollback = execution.getVariable("disableRollback")
727                 def defaultRollback = UrnPropertiesReader.getVariable("mso.rollback", execution).toBoolean()
728                 
729                 logDebug('disableRollback: ' + disableRollback, isDebugLogEnabled)
730                 logDebug('defaultRollback: ' + defaultRollback, isDebugLogEnabled)
731                 
732                 def rollbackEnabled
733                 
734                 if(disableRollback == null || disableRollback == '' ) {
735                         // get from default urn settings for mso_rollback
736                         disableRollback = !defaultRollback
737                         rollbackEnabled = defaultRollback
738                         logDebug('disableRollback is null or empty!', isDebugLogEnabled)
739                 }
740                 else {
741                         if(disableRollback == true) {
742                                 rollbackEnabled = false
743                         }
744                         else if(disableRollback == false){
745                                 rollbackEnabled = true
746                         }
747                         else {
748                                 rollbackEnabled = defaultRollback
749                         }
750                 }
751                 
752                 execution.setVariable(prefix+"backoutOnFailure", rollbackEnabled)
753                 logDebug('rollbackEnabled (aka backoutOnFailure): ' + rollbackEnabled, isDebugLogEnabled)
754         }
755         
756         public void setBasicDBAuthHeader(DelegateExecution execution, isDebugLogEnabled) {
757                 try {
758                         String basicAuthValueDB = UrnPropertiesReader.getVariable("mso.adapters.db.auth", execution)
759                         def encodedString = utils.getBasicAuth(basicAuthValueDB, UrnPropertiesReader.getVariable("mso.msoKey", execution))
760                         execution.setVariable("BasicAuthHeaderValueDB",encodedString)
761                 } catch (IOException ex) {
762                         String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()
763                         utils.log("DEBUG", dataErrorMessage, isDebugLogEnabled)
764                         (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
765                 }
766         }
767     public AAIResourcesClient getAAIClient(){
768         return  new AAIResourcesClient();
769     }
770 }