1 /*******************************************************************************
2 * ============LICENSE_START===================================================
4 * ============================================================================
5 * Copyright © 2019 Nokia Intellectual Property. 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 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=====================================================
18 ******************************************************************************/
19 package org.onap.dmaap.dmf.mr.service.impl;
21 import com.google.common.base.Preconditions;
22 import org.apache.http.HttpStatus;
23 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
24 import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
25 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
26 import org.onap.dmaap.dmf.mr.utils.Utils;
28 import java.util.Date;
30 class ErrorResponseProvider {
32 private String clientId;
33 private String topicName;
34 private String consumerGroup;
35 private String remoteHost;
36 private String publisherId;
37 private String publisherIp;
38 private DMaaPErrorMessages errorMessages;
40 private ErrorResponseProvider() {
44 ErrorResponse getIpBlacklistedError(String remoteAddr) {
45 return new ErrorResponse(HttpStatus.SC_FORBIDDEN,
46 DMaaPResponseCode.ACCESS_NOT_PERMITTED.getResponseCode(),
47 "Source address [" + remoteAddr + "] is blacklisted. Please contact the cluster management team.",
48 null, Utils.getFormattedDate(new Date()), topicName, publisherId,
49 publisherIp, consumerGroup + "/" + clientId, remoteHost);
52 ErrorResponse getTopicNotFoundError() {
53 return new ErrorResponse(HttpStatus.SC_NOT_FOUND,
54 DMaaPResponseCode.RESOURCE_NOT_FOUND.getResponseCode(),
55 errorMessages.getTopicNotExist() + "-[" + topicName + "]", null, Utils.getFormattedDate(new Date()),
56 topicName, publisherId, publisherIp, consumerGroup + "/" + clientId, remoteHost);
59 ErrorResponse getAafAuthorizationError(String permission, String action) {
60 return new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
61 DMaaPResponseCode.ACCESS_NOT_PERMITTED.getResponseCode(),
62 errorMessages.getNotPermitted1() + action + errorMessages.getNotPermitted2() + topicName + " on "
64 null, Utils.getFormattedDate(new Date()), topicName, publisherId, publisherIp, consumerGroup + "/" + clientId,
68 ErrorResponse getServiceUnavailableError(String msg) {
69 return new ErrorResponse(HttpStatus.SC_SERVICE_UNAVAILABLE,
70 DMaaPResponseCode.SERVER_UNAVAILABLE.getResponseCode(),
71 errorMessages.getServerUnav() + msg, null, Utils.getFormattedDate(new Date()), topicName,
72 publisherId, publisherIp, consumerGroup + "/" + clientId, remoteHost);
75 ErrorResponse getConcurrentModificationError() {
76 return new ErrorResponse(HttpStatus.SC_CONFLICT,
77 DMaaPResponseCode.TOO_MANY_REQUESTS.getResponseCode(),
78 "Couldn't respond to client, possible of consumer requests from more than one server. Please contact MR team if you see this issue occurs continously", null,
79 Utils.getFormattedDate(new Date()), topicName, publisherId, publisherIp, consumerGroup + "/" + clientId, remoteHost);
82 ErrorResponse getGenericError(String msg) {
83 return new ErrorResponse(HttpStatus.SC_SERVICE_UNAVAILABLE,
84 DMaaPResponseCode.SERVER_UNAVAILABLE.getResponseCode(),
85 "Couldn't respond to client, closing cambria consumer" + msg, null,
86 Utils.getFormattedDate(new Date()), topicName, publisherId, publisherIp, consumerGroup + "/" + clientId, remoteHost);
89 public static class Builder {
91 private String clientId;
92 private String topicName;
93 private String consumerGroup;
94 private String remoteHost;
95 private String publisherId;
96 private String publisherIp;
97 DMaaPErrorMessages errorMessages;
99 Builder withErrorMessages(DMaaPErrorMessages errorMessages) {
100 this.errorMessages = errorMessages;
104 Builder withTopic(String topic) {
105 this.topicName = topic;
109 Builder withClient(String client) {
110 this.clientId = client;
114 Builder withConsumerGroup(String consumerGroup) {
115 this.consumerGroup = consumerGroup;
119 Builder withRemoteHost(String remoteHost) {
120 this.remoteHost = remoteHost;
124 Builder withPublisherId(String publisherId) {
125 this.publisherId = publisherId;
129 Builder withPublisherIp(String publisherIp) {
130 this.publisherIp = publisherIp;
134 public ErrorResponseProvider build() {
135 Preconditions.checkArgument(errorMessages!=null);
136 ErrorResponseProvider errRespProvider = new ErrorResponseProvider();
137 errRespProvider.errorMessages = this.errorMessages;
138 errRespProvider.clientId = this.clientId;
139 errRespProvider.consumerGroup = this.consumerGroup;
140 errRespProvider.topicName = this.topicName;
141 errRespProvider.remoteHost = this.remoteHost;
142 errRespProvider.publisherId = this.publisherId;
143 errRespProvider.publisherIp = this.publisherIp;
144 return errRespProvider;