aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOGammaBPMN/src/main/groovy/com/att/bpm/scripts/AbstractServiceTaskProcessor.groovy
blob: 168cf0bc413b8f4830b31912b09186ac64707581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package com.att.bpm.scripts;

import javax.xml.transform.Transformer
import javax.xml.transform.TransformerException;

import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.impl.core.variable.value.ObjectValueImpl
import org.camunda.bpm.engine.runtime.Execution
import org.camunda.bpm.engine.variable.VariableMap



import org.openecomp.mso.bpmn.core.WorkflowException
import org.openecomp.mso.bpmn.gamma.workflow.service.WorkflowCallbackResponse
import org.openecomp.mso.bpmn.gamma.workflow.service.WorkflowContextHolder

import groovy.json.JsonSlurper

import org.openecomp.mso.rest.APIResponse;
import org.openecomp.mso.rest.RESTClient
import org.openecomp.mso.rest.RESTConfig

import org.camunda.bpm.engine.variable.VariableMap;
import org.camunda.bpm.engine.variable.Variables;
import org.camunda.bpm.engine.variable.Variables.SerializationDataFormats;
import org.w3c.dom.Node;

public abstract class AbstractServiceTaskProcessor implements ServiceTaskProcessor {
	public MsoUtils utils = new MsoUtils()


	/**
	 * Logs a message at the ERROR level.
	 * @param message the message
	 */
	public void logError(String message) {
		log('ERROR', message, null, "true")
	}

	/**
	 * Logs a message at the ERROR level.
	 * @param message the message
	 * @param cause the cause (stracktrace will be included in the output)
	 */
	public void logError(String message, Throwable cause) {
		log('ERROR', message, cause, "true")
	}

	/**
	 * Logs a message at the WARN level.
	 * @param message the message
	 */
	public void logWarn(String message) {
		log('WARN', message, null, "true")
	}

	/**
	 * Logs a message at the WARN level.
	 * @param message the message
	 * @param cause the cause (stracktrace will be included in the output)
	 */
	public void logWarn(String message, Throwable cause) {
		log('WARN', message, cause, "true")
	}

	/**
	 * Logs a message at the INFO level.
	 * @param message the message
	 */
	public void logInfo(String message) {
		log('INFO', message, null, "true")
	}

	/**
	 * Logs a message at the INFO level.
	 * @param message the message
	 * @param cause the cause (stracktrace will be included in the output)
	 */
	public void logInfo(String message, Throwable cause) {
		log('INFO', message, cause, "true")
	}

	/**
	 * Logs a message at the DEBUG level.
	 * @param message the message
	 * @param isDebugLogEnabled a flag indicating if DEBUG level is enabled
	 */
	public void logDebug(String message, String isDebugLogEnabled) {
		log('DEBUG', message, null, isDebugLogEnabled)
	}

	/**
	 * Logs a message at the DEBUG level.
	 * @param message the message
	 * @param cause the cause (stracktrace will be included in the output)
	 * @param isDebugLogEnabled a flag indicating if DEBUG level is enabled
	 */
	public void logDebug(String message, Throwable cause, String isDebugLogEnabled) {
		log('DEBUG', message, cause, isDebugLogEnabled)
	}

	/**
	 * Logs a message at the specified level.
	 * @param level the level (DEBUG, INFO, WARN, ERROR)
	 * @param message the message
	 * @param isLevelEnabled a flag indicating if the level is enabled
	 *        (used only at the DEBUG level)
	 */
	public void log(String level, String message, String isLevelEnabled) {
		log(level, message,  null, isLevelEnabled)
	}

	/**
	 * Logs a message at the specified level.
	 * @param level the level (DEBUG, INFO, WARN, ERROR)
	 * @param message the message
	 * @param cause the cause (stracktrace will be included in the output)
	 * @param isLevelEnabled a flag indicating if the level is enabled
	 *        (used only at the DEBUG level)
	 */
	public void log(String level, String message, Throwable cause, String isLevelEnabled) {
		if (cause == null) {
			utils.log(level, message, isLevelEnabled);
		} else {
			StringWriter stringWriter = new StringWriter();
			PrintWriter printWriter = new PrintWriter(stringWriter);
			printWriter.println(message);
			cause.printStackTrace(printWriter);
			utils.log(level, stringWriter.toString(), isLevelEnabled);
			printWriter.close();
		}
	}

	/**
	 * Logs a WorkflowException at the ERROR level with the specified message.
	 * @param execution the execution
	 */
	public void logWorkflowException(Execution execution, String message) {
		def workflowException = execution.getVariable("WorkflowException")

		if (workflowException == null) {
			logError(message);
		} else {
			logError(message + ": " + workflowException)
		}
	}

	/**
	 * Saves the WorkflowException in the execution to the specified variable,
	 * clearing the WorkflowException variable so the workflow can continue
	 * processing (perhaps catching another WorkflowException).
	 * @param execution the execution
	 * @return the name of the destination variable
	 */
	public saveWorkflowException(Execution execution, String variable) {
		if (variable == null) {
			throw new NullPointerException();
		}

		execution.setVariable(variable, execution.getVariable("WorkflowException"))
		execution.setVariable("WorkflowException", null)
	}

	/**
	 * Builds a success response from the specified message content and numeric
	 * response code.  The response code may be an integer or a string representation
	 * of an integer.  The response is stored in the execution where it may be
	 * picked up by the Workflow service.
	 * <p>
	 * IMPORTANT: the activity that executes this method should have an
	 * asynchronous continuation after it to ensure the execution variables
	 * are persisted to the database.
	 * @param execution the execution
	 * @param content the message content
	 * @param responseCode the message response code
	 */
	@Deprecated
	public void buildResponse(Execution execution, String content, Object responseCode) {
		buildResponse(execution, content, responseCode, true)
	}

	/**
	 * Builds a standard error response containing the specified error message and
	 * numeric response code.  The response code may be an integer or a string
	 * representation of an integer.  The response is stored in the execution where
	 * it may be picked up by the Workflow service.
	 * <p>
	 * IMPORTANT: the activity that executes this method should have an
	 * asynchronous continuation after it to ensure the execution variables
	 * are persisted to the database.
	 * @param execution the execution
	 * @param content the message content
	 * @param errorCode the message response code
	 */
	@Deprecated
	public void buildErrorResponse(Execution execution, String errorMessage, Object errorCode) {

		def encErrorMessage = errorMessage.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")

		def content = """
			<aetgt:WorkflowException xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1">
			<aetgt:ErrorMessage>${encErrorMessage}</aetgt:ErrorMessage>
			<aetgt:ErrorCode>${errorCode}</aetgt:ErrorCode>
			</aetgt:WorkflowException>
		  """

		buildResponse(execution, content, errorCode, false)
	}

	// BEGIN LEGACY SUPPORT.  TODO: REMOVE THIS CODE
	/**
	 * Builds a standard error response containing the specified error message
	 * and a numeric response code.  The response code is obtained from the
	 * prefix+"ResponseCode" execution variable. The response is stored in the
	 * execution where it may be picked up by the Workflow service.
	 * <p>
	 * IMPORTANT: the activity that executes this method should have an
	 * asynchronous continuation after it to ensure the execution variables
	 * are persisted to the database.
	 * <p>
	 * This method is deprecated. Methods that accept a response code should
	 * be used instead.
	 * @param execution the execution
	 * @param errorMessage the error message for the error response
	 */
	@Deprecated
	public void buildErrorResponse(Execution execution, String errorMessage) {
		buildErrorResponse(execution, errorMessage, null)
	}
	// END LEGACY SUPPORT.  TODO: REMOVE THIS CODE

	/**
	 * Builds a response from the specified message content and numeric response
	 * code.  The response code may be an integer or a string representation of
	 * an integer.  The response is stored in the execution where it may be
	 * picked up by the Workflow service.
	 * <p>
	 * IMPORTANT: the activity that executes this method should have an
	 * asynchronous continuation after it to ensure the execution variables
	 * are persisted to the database.
	 * @param execution the execution
	 * @param content the message content
	 * @param responseCode the message response code
	 * @param isSuccess true if this is a success response
	 */
	@Deprecated
	protected void buildResponse(Execution execution, String content, Object responseCode,
			boolean isSuccess) {
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')

		String processKey = getProcessKey(execution);
		logDebug("Building " + processKey + " response", isDebugLogEnabled)

		Map<String, Object> responseMap = new HashMap<String, Object>()

		if (isSuccess) {
			responseMap.put("Status", "Success")
		} else {
			responseMap.put("Status", "Fail")
		}

		// BEGIN LEGACY SUPPORT.  TODO: REMOVE THIS CODE
		def prefix = execution.getVariable("prefix")

		if (responseCode == null) {
			responseCode = execution.getVariable(prefix+"ResponseCode")
		} else {
			execution.setVariable(prefix+"ResponseCode", String.valueOf(responseCode))
		}
		// END LEGACY SUPPORT.  TODO: REMOVE THIS CODE

		responseMap.put("ResponseCode", String.valueOf(responseCode))

		if (isSuccess) {
			responseMap.put("Status", "Success")
			// TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead
			execution.setVariable("WorkflowResponse", content)
			// BEGIN LEGACY SUPPORT.  TODO: REMOVE THIS CODE
			execution.setVariable(processKey+"Response", content)
			execution.setVariable(prefix+"ErrorResponse", null)
			// END LEGACY SUPPORT.  TODO: REMOVE THIS CODE
		} else {
			responseMap.put("Status", "Fail")
			// BEGIN LEGACY SUPPORT.  TODO: REMOVE THIS CODE
			execution.setVariable(prefix+"ErrorResponse", content)
			execution.setVariable(prefix+"Response", null)
			// END LEGACY SUPPORT.  TODO: REMOVE THIS CODE
		}

		responseMap.put("Response", content)

		logDebug(processKey
			+ " ResponseCode=" + responseMap.get("ResponseCode")
			+ " Status=" + responseMap.get("Status")
			+ " Response=\n" + responseMap.get("Response"),
			isDebugLogEnabled)

		execution.setVariable(processKey + "ResponseMap", responseMap)
	}

	/**
	 * Builds an error response (if one has not already been built) and throws
	 * a BpmnError of type "MSOWorkflowException" that can be caught as a
	 * boundary event.
	 * @param execution the execution
	 * @param errorMessage the error message for the error response
	 * @param responseCode the message response code
	 */
	@Deprecated
	public void workflowException(Execution execution, String errorMessage, Object responseCode) {
		String processKey = getProcessKey(execution);

		buildErrorResponse(execution, errorMessage, responseCode)
		throw new BpmnError("MSOWorkflowException")
	}

	/**
	 * Puts a WorkflowException into the execution
	 * @param execution the execution
	 * @param errorCode the error code (normally a 4-digit number)
	 * @param errorMessage the error message
	 */
	@Deprecated
	public void newWorkflowException(Execution execution, int errorCode, String errorMessage) {
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		String processKey = getProcessKey(execution);
		logDebug("Building a " + processKey + " WorkflowException", isDebugLogEnabled)

		if (errorCode < 1000) {
			throw new IllegalArgumentException("ErrorCode must be a number greater than 1000");
		}

		WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage);
		execution.setVariable("WorkflowException", exception);
	}

	/**
	 * Puts a WorkflowException into the execution and throws an MSOWorkflowException event.
	 * @param execution the execution
	 * @param errorCode the error code (normally a 4-digit number)
	 * @param errorMessage the error message
	 */
	// TODO: rename this method to be throwWorkflowException
	@Deprecated
	public void createWorkflowException(Execution execution, int errorCode, String errorMessage) {
		newWorkflowException(execution, errorCode, errorMessage)
		throw new BpmnError("MSOWorkflowException", "errorCode:" + errorCode + ", errorMessage:" + errorMessage)
	}

	/**
	 * Puts a WorkflowException into the execution and throws an MSOWorkflowException event.
	 * @param execution the execution
	 * @param errorCode the error code (normally a 4-digit number)
	 * @param errorMessage the error message
	 */
	@Deprecated
	public void commonWorkflowException(Execution execution, int errorCode, String errorMessage) {
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		String processKey = getProcessKey(execution);
		logDebug("Building a " + processKey + " WorkflowException", isDebugLogEnabled)
		logError(errorMessage)
		WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage);
		execution.setVariable("WorkflowException", exception);
		throw new BpmnError("MSOWorkflowException","errorCode:" + errorCode + ",errorMessage:" + errorMessage)
	}

	/**
	 * Puts a WorkflowException into the execution and throws an MSOWorkflowException event.
	 * @param execution the execution
	 * @param errorCode the error code (normally a 4-digit number)
	 * @param errorMessage the error message
	 */
	@Deprecated
	public void commonWorkflowException(Execution execution, String errorCode, String errorMessage) {
		int intRespCode
		try{
			intRespCode = Integer.parseInt(errorCode)
		}catch (Exception e){
			intRespCode = 400
		}
		commonWorkflowException(execution, intRespCode, errorMessage)
	}



	/**
	 * Validates that the request exists and that the att-mso-request-id variable is set.
	 * Additional required variables may be checked by specifying their names.
	 * NOTE: services requiring att-mso-service-instance-id must specify it explicitly!
	 * If a problem is found, buildAndThrowWorkflowException builds a WorkflowException
	 * and throws an MSOWorkflowException.  This method also sets up the log context for
	 * the workflow.
	 *
	 * @param execution the execution
	 * @return the validated request
	 */
	public String validateRequest(Execution execution, String... requiredVariables) {
		ExceptionUtil exceptionUtil = new ExceptionUtil()
		def method = getClass().getSimpleName() + '.validateRequest(' +
			'execution=' + execution.getId() +
			', requredVariables=' + requiredVariables +
			')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)

		String processKey = getProcessKey(execution)
		def prefix = execution.getVariable("prefix")

		if (prefix == null) {
			exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " prefix is null")
		}

		try {
			def request = execution.getVariable(prefix + 'Request')

			if (request == null) {
				request = execution.getVariable(processKey + 'Request')

				if (request == null) {
					request = execution.getVariable('bpmnRequest')
				}

				setVariable(execution, processKey + 'Request', null)
				setVariable(execution, 'bpmnRequest', null)
				setVariable(execution, prefix + 'Request', request)
			}

			if (request == null) {
				exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey + " request is null")
			}

			// All requests must have a request ID.
			// Some requests (e.g. SDN-MOBILITY) do not have a service instance ID.

			String requestId = null
			String serviceInstanceId = null

			List<String> allRequiredVariables = new ArrayList<String>()
			allRequiredVariables.add("att-mso-request-id")

			if (requiredVariables != null) {
				for (String variable : requiredVariables) {
					if (!allRequiredVariables.contains(variable)) {
						allRequiredVariables.add(variable)
					}
				}
			}

			for (String variable : allRequiredVariables) {
				def value = execution.getVariable(variable)
				if (value == null || ((value instanceof CharSequence) && value.length() == 0)) {
					exceptionUtil.buildAndThrowWorkflowException(execution, 1002, processKey +
						" request was received with no '" + variable + "' variable")
				}

				if ("att-mso-request-id".equals(variable)) {
					requestId = (String) value
				} else if ("att-mso-service-instance-id".equals(variable)) {
					serviceInstanceId = (String) value
				}
			}

			if (serviceInstanceId == null) {
				serviceInstanceId = (String) execution.getVariable("att-mso-service-instance-id")
			}

			utils.logContext(requestId, serviceInstanceId)
			logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
			logDebug('Exited ' + method, isDebugLogEnabled)
			return request
		} catch (BpmnError e) {
			throw e
		} catch (Exception e) {
			logError('Caught exception in ' + method, e)
			exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Invalid Message")
		}
	}

	/**
	 * gets vars stored in a JSON object in prefix+Request and returns as a LazyMap
	 * setting log context here too
	 * @param execution the execution
	 * @return the inputVars
	 */
	public Map validateJSONReq(Execution execution) {
		def method = getClass().getSimpleName() + '.validateJSONReq(' +
				'execution=' + execution.getId() +
				')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)

		String processKey = getProcessKey(execution);
		def prefix = execution.getVariable("prefix")

		def requestId =getVariable(execution, "att-mso-request-id")
		def serviceInstanceId = getVariable(execution, "att-mso-service-instance-id")
		if(requestId!=null && serviceInstanceId!=null){
			utils.logContext(requestId, serviceInstanceId)
		}


		def request = getVariable(execution, prefix + 'Request')

		if (request == null) {
			request = getVariable(execution, processKey + 'Request')

			if (request == null) {
				request = getVariable(execution, 'bpmnRequest')
			}
			execution.setVariable(prefix + 'Request', request)
		}

		def jsonSlurper = new JsonSlurper()
		def parsed = jsonSlurper.parseText(request)


		logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
		logDebug('Exited ' + method, isDebugLogEnabled)
		return parsed

	}




	/**
	 * Sends a response to the workflow service that invoked the process.  This method
	 * may only be used by top-level processes that were directly invoked by the
	 * asynchronous workflow service.
	 * @param execution the execution
	 * @param responseCode the response code
	 * @param content the message content
	 * @throws IllegalArgumentException if the response code is invalid
	 *         by HTTP standards
	 * @throws UnsupportedOperationException if not invoked by an asynchronous,
	 *         top-level process
	 * @throws IllegalStateException if a response has already been sent
	 */
	protected void sendWorkflowResponse(Execution execution, Object responseCode, String response) {
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		try {
			String processKey = getProcessKey(execution);

			// isAsyncProcess is injected by the workflow service that started the flow
			if (!String.valueOf(execution.getVariable("isAsyncProcess")).equals("true")) {
				throw new UnsupportedOperationException(processKey + ": " +
					"sendWorkflowResponse is valid only in asynchronous workflows");
			}

			if (String.valueOf(execution.getVariable(processKey + "WorkflowResponseSent")).equals("true")) {
					logDebug("Sync response has already been sent for " + processKey, isDebugLogEnabled)
			}else{

				logDebug("Building " + processKey + " response ", isDebugLogEnabled)

				int intResponseCode;

				try {
					intResponseCode = Integer.parseInt(String.valueOf(responseCode));

					if (intResponseCode < 100 || intResponseCode > 599) {
						throw new NumberFormatException(String.valueOf(responseCode));
					}
				} catch (NumberFormatException e) {
					throw new IllegalArgumentException("Process " + processKey
						+ " provided an invalid HTTP response code: " + responseCode);
				}

				// Only 2XX responses are considered "Success"
				String status = (intResponseCode >= 200 && intResponseCode <= 299) ?
					"Success" : "Fail";

				// TODO: Should deprecate use of processKey+Response variable for the response. Will use "WorkflowResponse" instead
				execution.setVariable(processKey + "ResponseCode", String.valueOf(intResponseCode))
				execution.setVariable(processKey + "Response", response);
				execution.setVariable(processKey + "Status", status);
				execution.setVariable("WorkflowResponse", response)

				logDebug("Sending response for " + processKey
					+ " ResponseCode=" + intResponseCode
					+ " Status=" + status
					+ " Response=\n" + response,
					isDebugLogEnabled)

				// TODO: ensure that this flow was invoked asynchronously?

				WorkflowCallbackResponse callbackResponse = new WorkflowCallbackResponse()
				callbackResponse.setStatusCode(intResponseCode)
				callbackResponse.setMessage(status)
				callbackResponse.setResponse(response)

				// TODO: send this data with HTTP POST

				WorkflowContextHolder.getInstance().processCallback(
					processKey,
					execution.getProcessInstanceId(),
					execution.getVariable("att-mso-request-id"),
					callbackResponse)

				execution.setVariable(processKey + "WorkflowResponseSent", "true");
			}

		} catch (Exception ex) {
			logError("Unable to send workflow response to client ....", ex)
		}
	}

	/**
	 * Returns true if a workflow response has already been sent.
	 * @param execution the execution
	 */
	protected boolean isWorkflowResponseSent(Execution execution) {
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		String processKey = getProcessKey(execution);
		return String.valueOf(execution.getVariable(processKey + "WorkflowResponseSent")).equals("true");
	}

	/**
	 * Returns the process definition key (i.e. the process name) from the
	 * execution.
	 * @param execution the execution
	 */
	public String getProcessKey(Execution execution) {
		def testKey = execution.getVariable("testProcessKey")
		if(testKey!=null){
			return testKey
		}
		return execution.getProcessEngineServices().getRepositoryService()
			.getProcessDefinition(execution.getProcessDefinitionId()).getKey()
	}

	/**
	 * Gets the node for the named element from the given xml. If the element
	 * does not exist in the xml or is empty, a WorkflowException is created
	 * (and as a result, a MSOWorkflowException event is thrown).
	 *
	 * @param execution The flow's execution.
	 * @param xml Xml to search.
	 * @param elementName Name of element to search for.
	 * @return The element node, if found in the xml.
	 */
	protected String getRequiredNodeXml(Execution execution, String xml, String elementName) {
		def element = utils.getNodeXml(xml, elementName, false)
		if (element.trim().isEmpty()) {
			def msg = 'Required element \'' + elementName + '\' is missing or empty'
			logError(msg)
			createWorkflowException(execution, 2000, msg)
		} else {
			return element
		}
	}

	/**
	 * Gets the value of the named element from the given xml. If the element
	 * does not exist in the xml or is empty, a WorkflowException is created
	 * (and as a result, a MSOWorkflowException event is thrown).
	 *
	 * @param execution The flow's execution.
	 * @param xml Xml to search.
	 * @param elementName Name of element to whose value to get.
	 * @return The value of the element, if found in the xml.
	 */
	protected String getRequiredNodeText(Execution execution, String xml, String elementName) {
		def elementText = utils.getNodeText1(xml, elementName)
		if (elementText == null) {
			def msg = 'Required element \'' + elementName + '\' is missing or empty'
			logError(msg)
			createWorkflowException(execution, 2000, msg)
		} else {
			return elementText
		}
	}

	/**
	 * Get the text for the specified element from the specified xml.  If
	 * the element does not exist, return an empty string.
	 *
	 * @param xml Xml from which to get the element's text.
	 * @param elementName Name of element whose text to get.
	 * @return the element's text or an empty string if the element does not
	 * exist in the given xml.
	 */
	protected String getNodeTextForce(String xml, String elementName) {
		def nodeText = utils.getNodeText1(xml, elementName)
		return (nodeText == null) ? '' : nodeText
	}

	/**
	 * Sends the empty, synchronous response back to the API Handler.
	 * @param execution the execution
	 */
	@Deprecated
	public void sendResponse(Execution execution) {
		def method = getClass().getSimpleName() + '.sendResponse(' +
			'execution=' + execution.getId() +
			')'
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		logDebug('Entered ' + method, isDebugLogEnabled)

		try {
			buildResponse(execution, "", 200)
			logDebug('Exited ' + method, isDebugLogEnabled)
		} catch (BpmnError e) {
			throw e;
		} catch (Exception e) {
			logError('Caught exception in ' + method, e)
			workflowException(execution, 'Internal Error', 9999) // TODO: what message and error code?
		}
	}

	/**
	*Store the variable as typed with java serialization type
	*@param execution
	*@param name
	*@param value
	*/
	public void setVariable(Execution execution, String name, Object value) {
		VariableMap variables = Variables.createVariables()
		variables.putValueTyped('payload', Variables.objectValue(value)
		.serializationDataFormat(SerializationDataFormats.JAVA) // tells the engine to use java serialization for persisting the value
		.create())
		execution.setVariable(name,variables)
	}

	//TODO not sure how this will look in Cockpit

	/**
	 * Returns the variable map
	*@param execution
	*@param name
	*@return
	**/
	public String getVariable(Execution execution, String name) {
		def myObj = execution.getVariable(name)
		if(myObj instanceof VariableMap){
			VariableMap serializedObjectMap = (VariableMap) myObj
			ObjectValueImpl payloadObj = serializedObjectMap.getValueTyped('payload')
			return payloadObj.getValue()
		}else{
			return myObj
		}
	}


	/**
	 * Returns true if a value equals one of the provided set. Equality is
	 * determined by using the equals method if the value object and the
	 * object in the provided set have the same class. Otherwise, the objects
	 * are converted to strings and then compared.  Nulls are permitted for
	 * the value as well as in the provided set
	 * Example:
	 * <pre>
	 *     def statusCode = getStatusCode()
	 *     isOneOf(statusCode, 200, 201, 204)
	 * </pre>
	 * @param value the value to test
	 * @param these a set of permissable values
	 * @return true if the value is in the provided set
	 */
	public boolean isOneOf(Object value, Object... these) {
		for (Object thisOne : these) {
			if (thisOne == null) {
				if (value == null) {
					return true
				}
			} else {
				if (value != null) {
					if (value.getClass() == thisOne.getClass()) {
						if (value.equals(thisOne)) {
							return true
						}
					} else {
						if (String.valueOf(value).equals(String.valueOf(thisOne))) {
							return true
						}
					}
				}
			}
		}

		return false
	}

	public void setSuccessIndicator(Execution execution, boolean isSuccess) {
		String prefix = execution.getVariable('prefix')
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')

		logDebug('Entered SetSuccessIndicator Method', isDebugLogEnabled)
		execution.setVariable(prefix+'SuccessIndicator', isSuccess)
		logDebug('Outgoing SuccessIndicator is: ' + execution.getVariable(prefix+'SuccessIndicator') + '', isDebugLogEnabled)
	}


	public void sendSyncError(Execution execution) {
		def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
		String requestId = execution.getVariable("att-mso-request-id")
		logDebug('sendSyncError, requestId: ' + requestId, isDebugEnabled)
		WorkflowException workflowExceptionObj = execution.getVariable("WorkflowException")
		if (workflowExceptionObj != null) {
			String errorMessage = workflowExceptionObj.getErrorMessage()
			def errorCode = workflowExceptionObj.getErrorCode()
			logDebug('sendSyncError, requestId: '  + requestId + ' | errorMessage: ' + errorMessage + ' | errorCode: ' + errorCode, isDebugEnabled)
			sendWorkflowResponse(execution, errorCode, errorMessage)
		}
	}
	
	/**
	 * Create a WorkflowException - uses ExceptionUtil to build a WorkflowException
	 * @param execution
	 * @param errorCode
	 * @param errorMessage
	 * @param isDebugEnabled
	 */
	public void buildWorkflowException(Execution execution, int errorCode, String errorMessage, boolean isDebugEnabled) {
		(new ExceptionUtil()).buildWorkflowException(execution, errorCode, errorMessage)
	}
	
	/**
	 * Executes a named groovy script method in the current object
	 */
	public void executeMethod(String methodName, Object... args) {
		
		if (args != null && args.size() > 0) {
			
			// First argument of method to call is always the execution object
			Execution execution = (Execution) args[0]

			def classAndMethod = getClass().getSimpleName() + '.' + methodName + '(execution=' + execution.getId() + ')'
			def isDebugEnabled =  execution.getVariable('isDebugLogEnabled')

			logDebug('Entered ' + classAndMethod, isDebugEnabled)
			logDebug('Received parameters: ' + args, isDebugEnabled)

			try{
				def methodToCall = this.metaClass.getMetaMethod(methodName, args)
				logDebug('Method to call: ' + methodToCall, isDebugEnabled)
				methodToCall?.invoke(this, args)
			}
			catch(BpmnError bpmnError) {
				logDebug('Rethrowing BpmnError ' + bpmnError.getMessage(), isDebugEnabled)
				throw bpmnError
			}
			catch(Exception e) {
				e.printStackTrace()
				logDebug('Unexpected error encountered - ' + e.getMessage(), isDebugEnabled)
				(new ExceptionUtil()).buildAndThrowWorkflowException(execution, 9999, e.getMessage())
			}
			finally {
				logDebug('Exited ' + classAndMethod, isDebugEnabled)
			}
		}
	}

}