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