eb7290b6859b3086a84144213c53518b8bfce1e9
[so.git] /
1 package org.onap.so.bpmn.common.workflow.context;
2 /*-
3  * ============LICENSE_START=======================================================
4  * ONAP - SO
5  * ================================================================================
6  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22
23
24 import java.util.concurrent.DelayQueue;
25 import java.util.concurrent.TimeUnit;
26
27 import org.onap.so.logger.MessageEnum;
28 import org.onap.so.logger.MsoLogger;
29 import org.springframework.stereotype.Component;
30
31 /**
32  * Workflow Context Holder instance which can be accessed elsewhere either in groovy scripts or Java
33  * @version 1.0
34  *
35  */
36
37 @Component
38 public class WorkflowContextHolder {
39
40         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL,WorkflowContextHolder.class);
41         private static final String logMarker = "[WORKFLOW-CONTEXT-HOLDER]";
42         private static WorkflowContextHolder instance = null;
43         
44         
45         private long defaultContextTimeout=60000;
46
47         /**
48          * Delay Queue which holds workflow context holder objects
49          */
50         private final DelayQueue<WorkflowContext> responseQueue = new DelayQueue<>();
51         private final TimeoutThread timeoutThread = new TimeoutThread();
52
53         private WorkflowContextHolder() {
54                 timeoutThread.start();
55         }
56
57         /**
58          * Singleton holder which eliminates hot lock
59          * Since the JVM synchronizes static method there is no synchronization needed for this method
60          * @return
61          */
62         public static synchronized WorkflowContextHolder getInstance() {
63                 if (instance == null) {
64                         instance = new WorkflowContextHolder();
65                 }
66                 return instance;
67         }
68         
69         public void put(WorkflowContext context) {
70                 msoLogger.debug(logMarker + " Adding context to the queue: "
71                         + context.getRequestId());
72                 responseQueue.put(context);
73         }
74         
75         public void remove(WorkflowContext context) {
76                 msoLogger.debug(logMarker + " Removing context from the queue: "
77                         + context.getRequestId());
78                 responseQueue.remove(context);
79         }
80         
81         public WorkflowContext getWorkflowContext(String requestId) {
82                 // Note: DelayQueue interator is threadsafe
83                 for (WorkflowContext context : responseQueue) {
84                         if (requestId.equals(context.getRequestId())) {                         
85                                 return context;
86                         }
87                 }
88                 return null;
89         }
90         
91         /**
92          * Builds the callback response object to respond to client
93          * @param processKey
94          * @param processInstanceId
95          * @param requestId
96          * @param callbackResponse
97          * @return
98          */
99         public void processCallback(String processKey, String processInstanceId,
100                         String requestId, WorkflowCallbackResponse callbackResponse) {          
101                 WorkflowResponse workflowResponse = new WorkflowResponse();
102                 workflowResponse.setResponse(callbackResponse.getResponse());
103                 workflowResponse.setProcessInstanceID(processInstanceId);
104                 workflowResponse.setMessageCode(callbackResponse.getStatusCode());
105                 workflowResponse.setMessage(callbackResponse.getMessage());
106                 WorkflowContext context = new WorkflowContext(processKey, requestId, defaultContextTimeout,workflowResponse);
107                 put(context);
108         }
109
110
111         /**
112          * Timeout thread which monitors the delay queue for expired context and send timeout response
113          * to client
114          *
115          * */
116         private class TimeoutThread extends Thread {
117                 @Override
118                 public void run() {
119                         while (!isInterrupted()) {
120                                 try {
121                                         WorkflowContext requestObject = responseQueue.take();
122                                         MsoLogger.setLogContext(requestObject.getRequestId(), null);
123                                         msoLogger.debug("Time remaining for request id: " + requestObject.getRequestId() + ":" + requestObject.getDelay(TimeUnit.MILLISECONDS));
124                                         msoLogger.debug("Preparing timeout response for " + requestObject.getProcessKey() + ":" + ":" + requestObject.getRequestId());
125                                 } catch (InterruptedException e) {
126                                         Thread.currentThread().interrupt();
127                                 } catch (Exception e) {
128                                         msoLogger.debug("WorkflowContextHolder timeout thread caught exception: " + e);
129                                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION, "BPMN", MsoLogger.getServiceName(), 
130                                                 MsoLogger.ErrorCode.UnknownError, "Error in WorkflowContextHolder timeout thread");
131                                 
132                                 }
133                         }
134                         msoLogger.debug("WorkflowContextHolder timeout thread interrupted, quitting");
135                 }
136         }
137 }