Replace cambria with DmaaP client
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / common / publishing / MessageRouterHttpStatusMapper.java
1 /*
2  * ============LICENSE_START=======================================================
3  * VES Collector
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 package org.onap.dcae.common.publishing;
21
22 import org.jetbrains.annotations.NotNull;
23 import org.onap.dcae.common.model.BackwardsCompatibilityException;
24 import org.onap.dcae.common.model.InternalException;
25 import org.onap.dcae.common.model.PayloadToLargeException;
26 import org.onap.dcae.restapi.ApiException;
27 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.http.HttpStatus;
31
32 import java.util.Objects;
33
34 import static org.onap.dcae.ApplicationSettings.responseCompatibility;
35
36 public class MessageRouterHttpStatusMapper {
37
38     private static final Logger log = LoggerFactory.getLogger(MessageRouterHttpStatusMapper.class);
39
40     private MessageRouterHttpStatusMapper() {
41     }
42
43     @NotNull
44     static HttpStatus getHttpStatus(MessageRouterPublishResponse messageRouterPublishResponse) {
45         return responseCompatibility.equals("v7.2") ?
46                 getHttpStatusBackwardsCompatibility(messageRouterPublishResponse):
47                 getHttpStatusWithMappedResponseCode(messageRouterPublishResponse);
48     }
49
50     @NotNull
51     private static HttpStatus getHttpStatusBackwardsCompatibility(MessageRouterPublishResponse messageRouterPublishResponse) {
52         if (isHttpOk(messageRouterPublishResponse)) {
53             log.info("Successfully send event to MR");
54             return HttpStatus.ACCEPTED;
55         } else {
56             log.error(messageRouterPublishResponse.failReason());
57             throw new BackwardsCompatibilityException();
58         }
59     }
60
61     @NotNull
62     private static HttpStatus getHttpStatusWithMappedResponseCode(MessageRouterPublishResponse messageRouterPublishResponse) {
63         if (isHttpOk(messageRouterPublishResponse)) {
64             log.info("Successfully send event to MR");
65             return HttpStatus.OK;
66         } else if (isHttp413(messageRouterPublishResponse)) {
67             log.error(messageRouterPublishResponse.failReason());
68             throw new PayloadToLargeException();
69         } else {
70             log.error(messageRouterPublishResponse.failReason());
71             throw new InternalException(responseBody(resolveHttpCode(messageRouterPublishResponse)));
72         }
73     }
74
75     @NotNull
76     private static String resolveHttpCode(MessageRouterPublishResponse messageRouterPublishResponse) {
77         return Objects.requireNonNull(messageRouterPublishResponse.failReason()).substring(0, 3);
78     }
79
80     @NotNull
81     private static ApiException responseBody(String substring) {
82         switch (substring) {
83             case "404":
84                 return ApiException.NOT_FOUND;
85             case "408":
86                 return ApiException.REQUEST_TIMEOUT;
87             case "429":
88                 return ApiException.TOO_MANY_REQUESTS;
89             case "502":
90                 return ApiException.BAD_GATEWAY;
91             case "503":
92                 return ApiException.SERVICE_UNAVAILABLE;
93             case "504":
94                 return ApiException.GATEWAY_TIMEOUT;
95             default:
96                 return ApiException.INTERNAL_SERVER_ERROR;
97         }
98     }
99
100     private static boolean isHttpOk(MessageRouterPublishResponse messageRouterPublishResponse) {
101         return messageRouterPublishResponse.successful();
102     }
103
104     private static boolean isHttp413(MessageRouterPublishResponse messageRouterPublishResponse) {
105         return Objects.requireNonNull(messageRouterPublishResponse.failReason()).startsWith("413");
106     }
107 }