Changed to unmaintained
[appc.git] / appc-adapters / appc-rest-healthcheck-adapter / appc-rest-healthcheck-adapter-bundle / src / main / java / org / onap / appc / adapter / restHealthcheck / impl / RequestContext.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26
27
28 package org.onap.appc.adapter.restHealthcheck.impl;
29
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31
32 public class RequestContext {
33     /**
34      * The number of seconds of wait time between successive attempts to connect to the provider. This is used to
35      * recover from provider outages or failures. It is not used to recover from logical errors, such as an invalid
36      * request, server not found, etc.
37      */
38     private Integer retryDelay;
39
40     /**
41      * The number of times we will attempt to connect to the provider. This is used to recover from provider outages or
42      * failures. It is not used to recover from logical errors, such as an invalid request, server not found, etc.
43      */
44     private Integer retryLimit;
45
46     /**
47      * The total time, in milliseconds, that the provider can have to process this request. If the accumulated time
48      * exceeds the time to live, then the request is failed with a timeout exception, regardless of the state of the
49      * provider. Note that the caller may supply this as a value in seconds, in which case it must be converted to
50      * milliseconds for the request context.
51      */
52     private Long timeToLive;
53
54     /**
55      * The accumulated time, in milliseconds, that has been used so far to process the request. This is compared to the
56      * time to live each time it is updated. If the accumulated time exceeds the time to live, then the request is
57      * failed with a timeout exception, regardless of the state of the provider.
58      */
59     private long accumulatedTime;
60
61     /**
62      * The total number of retries attempted so far
63      */
64     private int attempt;
65
66     /**
67      * The time when the stopwatch was started
68      */
69     private long startTime = -1;
70
71     /**
72      * The service logic (DG) context from the SLI
73      */
74     private SvcLogicContext svcLogicContext;
75
76     /**
77      * The configuration
78      */
79
80
81     /**
82      * Set to true whenever the retry limit has been exceeded, reset to false when reset() is called.
83      */
84     private boolean retryFailed;
85
86     /**
87      * Creates the request context
88      * 
89      * @param context
90      *            The service logic (SLI) context associated with the current DG
91      */
92     public RequestContext(SvcLogicContext context) {
93         setSvcLogicContext(context);
94     }
95
96     /**
97      * @return The retry delay, in seconds. If zero, then no retry is to be performed
98      */
99     public int getRetryDelay() {
100         if (retryDelay == null) {
101             int value = 10;
102             retryDelay = Integer.valueOf(value);
103         }
104
105         return retryDelay.intValue();
106     }
107
108     /**
109      * This method is a helper that allows the caller to delay for the retry interval time and not have to handle the
110      * thread interruption, timer handling, etc.
111      */
112     public void delay() {
113         long time = getRetryDelay() * 1000L;
114         long future = System.currentTimeMillis() + time;
115         if (time != 0) {
116             while (System.currentTimeMillis() < future && time > 0) {
117                 try {
118                     Thread.sleep(time);
119                 } catch (InterruptedException e) {
120                     /*
121                      * This is rare, but it can happen if another thread interrupts us while we are sleeping. In that
122                      * case, the thread is resumed before the delay time has actually expired, so re-calculate the
123                      * amount of delay time needed and reenter the sleep until we get to the future time.
124                      */
125                     time = future - System.currentTimeMillis();
126                 }
127             }
128         }
129     }
130
131     /**
132      * @return The number of retries that are allowed per connection
133      */
134     public int getRetryLimit() {
135         if (retryLimit == null) {
136             int value = 10;
137             retryLimit = Integer.valueOf(value);
138         }
139
140         return retryLimit.intValue();
141     }
142
143     /**
144      * Check and count the connection attempt.
145      * 
146      * @return True if the connection should be attempted. False indicates that the number of retries has been exhausted
147      *         and it should NOT be attempted.
148      */
149     public boolean attempt() {
150         if (retryFailed || attempt >= getRetryLimit()) {
151             retryFailed = true;
152             return false;
153         }
154         attempt++;
155
156         return true;
157     }
158
159     /**
160      * @return The number of retry attempts so far
161      */
162     public int getAttempts() {
163         return attempt;
164     }
165
166     /**
167      * @return True if the retry limit has been exceeded, false otherwise
168      */
169     public boolean isFailed() {
170         return retryFailed;
171     }
172
173     /**
174      * This method both checks the time to live to see if it has been exceeded and accumulates the total time used so
175      * far.
176      * <p>
177      * Each time this method is called it accumulates the total duration since the last time it was called to the total
178      * time accumulator. It then checks the total time to the time to live and if greater, it returns false. As long as
179      * the total time used is less than or equal to the time to live limit, the method returns true. It is important to
180      * call this method at the very beginning of the process so that all parts of the process are tracked.
181      * </p>
182      * 
183      * @return True if the total time to live has not been exceeded. False indicates that the total time to live has
184      *         been exceeded and no further processing should be performed.
185      */
186     public boolean isAlive() {
187         long now = System.currentTimeMillis();
188         if (startTime == -1) {
189             startTime = now;
190             return true;
191         }
192         accumulatedTime += (now - startTime);
193         startTime = now;
194         if (accumulatedTime > timeToLive) {
195             return false;
196         }
197         return true;
198     }
199
200     /**
201      * @return The total amount of time used, in milliseconds.
202      */
203     public long getTotalDuration() {
204         return accumulatedTime;
205     }
206
207     /**
208      * This method is called to reset the retry counters. It has no effect on the time to live accumulator.
209      */
210     public void reset() {
211         attempt = 0;
212     }
213
214     /**
215      * Sets the time to live to the value, expressed in seconds
216      * 
217      * @param time
218      *            The time to live, in seconds
219      */
220     public void setTimeToLiveSeconds(int time) {
221         setTimeToLiveMS(time * 1000L);
222     }
223
224     /**
225      * Sets the time to live to the value, expressed in milliseconds
226      * 
227      * @param time
228      *            The time to live, in milliseconds
229      */
230     public void setTimeToLiveMS(long time) {
231         this.timeToLive = time;
232     }
233
234     /**
235      * @return The service logic context associated with this request
236      */
237     public SvcLogicContext getSvcLogicContext() {
238         return svcLogicContext;
239     }
240
241     /**
242      * @param svcLogicContext
243      *            The service logic context to be associated with this request
244      */
245     public void setSvcLogicContext(SvcLogicContext svcLogicContext) {
246         this.svcLogicContext = svcLogicContext;
247     }
248 }