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