Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / common / workflow / context / WorkflowContext.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  * 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.Delayed;
25 import java.util.concurrent.TimeUnit;
26
27
28 /**
29  * @version 1.0
30  * Workflow context object used to send timeout response, if workflow instance does not write the response in time
31  */
32 public class WorkflowContext implements Delayed {
33         private final String processKey;
34         private final String requestId;
35
36         private final long startTime;
37         private final long timeout;
38         
39         private final WorkflowResponse workflowResponse;
40         
41         public WorkflowContext(String processKey, String requestId, long timeout, WorkflowResponse workflowResponse) {
42                 this.processKey = processKey;
43                 this.requestId = requestId;             
44                 this.timeout = timeout;
45                 this.startTime = System.currentTimeMillis(); 
46                 this.workflowResponse = workflowResponse;
47         }
48         
49         public String getRequestId() {
50                 return requestId;
51         }
52
53         public String getProcessKey() {
54                 return processKey;
55         }
56
57
58         public long getTimeout() {
59                 return timeout;
60         }
61
62         public long getStartTime() {
63                 return startTime;
64         }
65         
66         public WorkflowResponse getWorkflowResponse() {
67                 return workflowResponse;
68         }
69
70         /**
71          * Required implementation by Delay queue
72          * Returns the elapsed time for this context
73          */
74         @Override
75         public long getDelay(TimeUnit unit) {
76                 // 0 or negative means this object is considered to be expired
77                 return unit.convert(startTime + timeout - System.currentTimeMillis(), unit);
78         }
79
80         /**
81          * Required implementation by Delay queue
82          * Compares the object to determine whether the object can be marked as expired
83          */
84         @Override
85         public int compareTo(Delayed object) {
86                 WorkflowContext that = (WorkflowContext) object;
87                 long thisEndTime = startTime + timeout;
88                 long thatEndTime = that.startTime + that.timeout;
89
90                 if (thisEndTime < thatEndTime) {
91                         return -1;
92                 } else if (thisEndTime > thatEndTime) {
93                         return 1;
94                 } else {
95                         return 0;
96                 }
97         }
98 }