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