2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.common.publishing;
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;
32 import java.util.Objects;
34 import static org.onap.dcae.ApplicationSettings.responseCompatibility;
36 public class MessageRouterHttpStatusMapper {
38 private static final Logger log = LoggerFactory.getLogger(MessageRouterHttpStatusMapper.class);
40 private MessageRouterHttpStatusMapper() {
44 static HttpStatus getHttpStatus(MessageRouterPublishResponse messageRouterPublishResponse) {
45 return responseCompatibility.equals("v7.2") ?
46 getHttpStatusBackwardsCompatibility(messageRouterPublishResponse):
47 getHttpStatusWithMappedResponseCode(messageRouterPublishResponse);
51 private static HttpStatus getHttpStatusBackwardsCompatibility(MessageRouterPublishResponse messageRouterPublishResponse) {
52 if (isHttpOk(messageRouterPublishResponse)) {
53 log.info("Successfully send event to MR");
54 return HttpStatus.ACCEPTED;
56 log.error(messageRouterPublishResponse.failReason());
57 throw new BackwardsCompatibilityException();
62 private static HttpStatus getHttpStatusWithMappedResponseCode(MessageRouterPublishResponse messageRouterPublishResponse) {
63 if (isHttpOk(messageRouterPublishResponse)) {
64 log.info("Successfully send event to MR");
66 } else if (isHttp413(messageRouterPublishResponse)) {
67 log.error(messageRouterPublishResponse.failReason());
68 throw new PayloadToLargeException();
70 log.error(messageRouterPublishResponse.failReason());
71 throw new InternalException(responseBody(resolveHttpCode(messageRouterPublishResponse)));
76 private static String resolveHttpCode(MessageRouterPublishResponse messageRouterPublishResponse) {
77 return Objects.requireNonNull(messageRouterPublishResponse.failReason()).substring(0, 3);
81 private static ApiException responseBody(String substring) {
84 return ApiException.NOT_FOUND;
86 return ApiException.REQUEST_TIMEOUT;
88 return ApiException.TOO_MANY_REQUESTS;
90 return ApiException.BAD_GATEWAY;
92 return ApiException.SERVICE_UNAVAILABLE;
94 return ApiException.GATEWAY_TIMEOUT;
96 return ApiException.INTERNAL_SERVER_ERROR;
100 private static boolean isHttpOk(MessageRouterPublishResponse messageRouterPublishResponse) {
101 return messageRouterPublishResponse.successful();
104 private static boolean isHttp413(MessageRouterPublishResponse messageRouterPublishResponse) {
105 return Objects.requireNonNull(messageRouterPublishResponse.failReason()).startsWith("413");