[SO] Release so 1.13.0 image
[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 Workflow context object used to send timeout response, if workflow instance does not write the response
30  *          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 Returns the elapsed time for this context
72      */
73     @Override
74     public long getDelay(TimeUnit unit) {
75         // 0 or negative means this object is considered to be expired
76         return unit.convert(startTime + timeout - System.currentTimeMillis(), unit);
77     }
78
79     /**
80      * Required implementation by Delay queue Compares the object to determine whether the object can be marked as
81      * expired
82      */
83     @Override
84     public int compareTo(Delayed object) {
85         WorkflowContext that = (WorkflowContext) object;
86         long thisEndTime = startTime + timeout;
87         long thatEndTime = that.startTime + that.timeout;
88
89         if (thisEndTime < thatEndTime) {
90             return -1;
91         } else if (thisEndTime > thatEndTime) {
92             return 1;
93         } else {
94             return 0;
95         }
96     }
97 }