2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
28 package org.onap.appc.adapter.restHealthcheck.impl;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 public class RequestContext {
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.
38 private Integer retryDelay;
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.
44 private Integer retryLimit;
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.
52 private Long timeToLive;
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.
59 private long accumulatedTime;
62 * The total number of retries attempted so far
67 * The time when the stopwatch was started
69 private long startTime = -1;
72 * The service logic (DG) context from the SLI
74 private SvcLogicContext svcLogicContext;
82 * Set to true whenever the retry limit has been exceeded, reset to false when reset() is called.
84 private boolean retryFailed;
87 * Creates the request context
90 * The service logic (SLI) context associated with the current DG
92 public RequestContext(SvcLogicContext context) {
93 setSvcLogicContext(context);
97 * @return The retry delay, in seconds. If zero, then no retry is to be performed
99 public int getRetryDelay() {
100 if (retryDelay == null) {
102 retryDelay = Integer.valueOf(value);
105 return retryDelay.intValue();
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.
112 public void delay() {
113 long time = getRetryDelay() * 1000L;
114 long future = System.currentTimeMillis() + time;
116 while (System.currentTimeMillis() < future && time > 0) {
119 } catch (InterruptedException e) {
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.
125 time = future - System.currentTimeMillis();
132 * @return The number of retries that are allowed per connection
134 public int getRetryLimit() {
135 if (retryLimit == null) {
137 retryLimit = Integer.valueOf(value);
140 return retryLimit.intValue();
144 * Check and count the connection attempt.
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.
149 public boolean attempt() {
150 if (retryFailed || attempt >= getRetryLimit()) {
160 * @return The number of retry attempts so far
162 public int getAttempts() {
167 * @return True if the retry limit has been exceeded, false otherwise
169 public boolean isFailed() {
174 * This method both checks the time to live to see if it has been exceeded and accumulates the total time used so
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.
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.
186 public boolean isAlive() {
187 long now = System.currentTimeMillis();
188 if (startTime == -1) {
192 accumulatedTime += (now - startTime);
194 if (accumulatedTime > timeToLive) {
201 * @return The total amount of time used, in milliseconds.
203 public long getTotalDuration() {
204 return accumulatedTime;
208 * This method is called to reset the retry counters. It has no effect on the time to live accumulator.
210 public void reset() {
215 * Sets the time to live to the value, expressed in seconds
218 * The time to live, in seconds
220 public void setTimeToLiveSeconds(int time) {
221 setTimeToLiveMS(time * 1000L);
225 * Sets the time to live to the value, expressed in milliseconds
228 * The time to live, in milliseconds
230 public void setTimeToLiveMS(long time) {
231 this.timeToLive = time;
235 * @return The service logic context associated with this request
237 public SvcLogicContext getSvcLogicContext() {
238 return svcLogicContext;
242 * @param svcLogicContext
243 * The service logic context to be associated with this request
245 public void setSvcLogicContext(SvcLogicContext svcLogicContext) {
246 this.svcLogicContext = svcLogicContext;