Support retry in DCAE-SDK DMaaP-Client
[dcaegen2/services/sdk.git] / rest-services / dmaap-client / src / main / java / org / onap / dcaegen2 / services / sdk / rest / services / dmaap / client / model / config / DmaapRetryConfig.java
1 /*
2  * ============LICENSE_START====================================
3  * DCAEGEN2-SERVICES-SDK
4  * =========================================================
5  * Copyright (C) 2021 Nokia. All rights reserved.
6  * =========================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=====================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.config;
22
23 import io.netty.handler.timeout.ReadTimeoutException;
24 import io.vavr.collection.HashSet;
25 import io.vavr.collection.Set;
26 import org.immutables.value.Value;
27
28 import java.net.ConnectException;
29
30 @Value.Immutable
31 public interface DmaapRetryConfig {
32
33     Set<Class<? extends Throwable>> RETRYABLE_EXCEPTIONS = HashSet.of(ReadTimeoutException.class, ConnectException.class);
34     RuntimeException ON_RETRY_EXHAUSTED_EXCEPTION = ReadTimeoutException.INSTANCE;
35     Set<Integer> RETRYABLE_HTTP_CODES = HashSet.of(404, 408, 413, 429, 500, 502, 503, 504);
36
37     @Value.Default
38     default int retryCount() {
39         return 3;
40     }
41
42     @Value.Default
43     default int retryIntervalInSeconds() {
44         return 1;
45     }
46
47     @Value.Check
48     default void validate() {
49         validateRetryCount();
50         validateRetryInterval();
51     }
52
53     private void validateRetryCount() {
54         int rc = retryCount();
55         if (rc < 0)
56             throw new IllegalArgumentException(String.format("Invalid value: %d, retryCount should be (0-n)", rc));
57     }
58
59     private void validateRetryInterval() {
60         long ri = retryIntervalInSeconds();
61         if (ri < 1)
62             throw new IllegalArgumentException(String.format("Invalid value: %d, retryInterval should be (1-n)", ri));
63     }
64 }