aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/common/publishing/MessageRouterHttpStatusMapper.java
blob: b5c735b7e9a3ec66743c762831dc51fce55ddd96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * ============LICENSE_START=======================================================
 * VES Collector
 * ================================================================================
 * Copyright (C) 2021 Nokia. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.dcae.common.publishing;

import org.jetbrains.annotations.NotNull;
import org.onap.dcae.common.model.BackwardsCompatibilityException;
import org.onap.dcae.common.model.InternalException;
import org.onap.dcae.common.model.PayloadToLargeException;
import org.onap.dcae.restapi.ApiException;
import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.model.MessageRouterPublishResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

import java.util.Objects;

import static org.onap.dcae.ApplicationSettings.responseCompatibility;

public class MessageRouterHttpStatusMapper {

    private static final Logger log = LoggerFactory.getLogger(MessageRouterHttpStatusMapper.class);

    private MessageRouterHttpStatusMapper() {
    }

    @NotNull
    static HttpStatus getHttpStatus(MessageRouterPublishResponse messageRouterPublishResponse) {
        return responseCompatibility.equals("v7.2") ?
                getHttpStatusBackwardsCompatibility(messageRouterPublishResponse):
                getHttpStatusWithMappedResponseCode(messageRouterPublishResponse);
    }

    @NotNull
    private static HttpStatus getHttpStatusBackwardsCompatibility(MessageRouterPublishResponse messageRouterPublishResponse) {
        if (isHttpOk(messageRouterPublishResponse)) {
            log.info("Successfully send event to MR");
            return HttpStatus.ACCEPTED;
        } else {
            log.error(messageRouterPublishResponse.failReason());
            throw new BackwardsCompatibilityException();
        }
    }

    @NotNull
    private static HttpStatus getHttpStatusWithMappedResponseCode(MessageRouterPublishResponse messageRouterPublishResponse) {
        if (isHttpOk(messageRouterPublishResponse)) {
            log.info("Successfully send event to MR");
            return HttpStatus.OK;
        } else if (isHttp413(messageRouterPublishResponse)) {
            log.error(messageRouterPublishResponse.failReason());
            throw new PayloadToLargeException();
        } else {
            log.error(messageRouterPublishResponse.failReason());
            throw new InternalException(responseBody(resolveHttpCode(messageRouterPublishResponse)));
        }
    }

    @NotNull
    private static String resolveHttpCode(MessageRouterPublishResponse messageRouterPublishResponse) {
        return Objects.requireNonNull(messageRouterPublishResponse.failReason()).substring(0, 3);
    }

    @NotNull
    private static ApiException responseBody(String substring) {
        switch (substring) {
            case "404":
                return ApiException.NOT_FOUND;
            case "408":
                return ApiException.REQUEST_TIMEOUT;
            case "429":
                return ApiException.TOO_MANY_REQUESTS;
            case "502":
                return ApiException.BAD_GATEWAY;
            case "503":
                return ApiException.SERVICE_UNAVAILABLE;
            case "504":
                return ApiException.GATEWAY_TIMEOUT;
            default:
                return ApiException.INTERNAL_SERVER_ERROR;
        }
    }

    private static boolean isHttpOk(MessageRouterPublishResponse messageRouterPublishResponse) {
        return messageRouterPublishResponse.successful();
    }

    private static boolean isHttp413(MessageRouterPublishResponse messageRouterPublishResponse) {
        return Objects.requireNonNull(messageRouterPublishResponse.failReason()).startsWith("413");
    }
}