removed code smells
[dmaap/messagerouter/msgrtr.git] / src / main / java / org / onap / dmaap / dmf / mr / service / impl / ErrorResponseProvider.java
1 /*******************************************************************************
2  *  ============LICENSE_START===================================================
3  *  org.onap.dmaap
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
11  *
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;
20
21 import com.google.common.base.Preconditions;
22 import java.util.Date;
23 import org.apache.http.HttpStatus;
24 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
25 import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
26 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
27 import org.onap.dmaap.dmf.mr.utils.Utils;
28
29 class ErrorResponseProvider {
30
31     private String clientId;
32     private String topicName;
33     private String consumerGroup;
34     private String remoteHost;
35     private String publisherId;
36     private String publisherIp;
37     private DMaaPErrorMessages errorMessages;
38
39     private ErrorResponseProvider() {
40
41     }
42
43     ErrorResponse getIpBlacklistedError(String remoteAddr) {
44         return new ErrorResponse(HttpStatus.SC_FORBIDDEN,
45             DMaaPResponseCode.ACCESS_NOT_PERMITTED.getResponseCode(),
46             "Source address [" + remoteAddr + "] is blacklisted. Please contact the cluster management team.",
47             null, Utils.getFormattedDate(new Date()), topicName, publisherId,
48             publisherIp, consumerGroup + "/" + clientId, remoteHost);
49     }
50
51     ErrorResponse getTopicNotFoundError() {
52         return new ErrorResponse(HttpStatus.SC_NOT_FOUND,
53             DMaaPResponseCode.RESOURCE_NOT_FOUND.getResponseCode(),
54             errorMessages.getTopicNotExist() + "-[" + topicName + "]", null, Utils.getFormattedDate(new Date()),
55             topicName, publisherId, publisherIp, consumerGroup + "/" + clientId, remoteHost);
56     }
57
58     ErrorResponse getAafAuthorizationError(String permission, String action) {
59         return new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
60             DMaaPResponseCode.ACCESS_NOT_PERMITTED.getResponseCode(),
61             errorMessages.getNotPermitted1() + action + errorMessages.getNotPermitted2() + topicName + " on "
62                 + permission,
63             null, Utils.getFormattedDate(new Date()), topicName, publisherId, publisherIp, consumerGroup + "/" + clientId,
64             remoteHost);
65     }
66
67     ErrorResponse getServiceUnavailableError(String msg) {
68         return new ErrorResponse(HttpStatus.SC_SERVICE_UNAVAILABLE,
69             DMaaPResponseCode.SERVER_UNAVAILABLE.getResponseCode(),
70             errorMessages.getServerUnav() + msg, null, Utils.getFormattedDate(new Date()), topicName,
71             publisherId, publisherIp, consumerGroup + "/" + clientId, remoteHost);
72     }
73
74     ErrorResponse getConcurrentModificationError() {
75         return new ErrorResponse(HttpStatus.SC_CONFLICT,
76             DMaaPResponseCode.TOO_MANY_REQUESTS.getResponseCode(),
77             "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,
78             Utils.getFormattedDate(new Date()), topicName, publisherId, publisherIp, consumerGroup + "/" + clientId, remoteHost);
79     }
80
81     ErrorResponse getGenericError(String msg) {
82         return new ErrorResponse(HttpStatus.SC_SERVICE_UNAVAILABLE,
83             DMaaPResponseCode.SERVER_UNAVAILABLE.getResponseCode(),
84             "Couldn't respond to client, closing cambria consumer" + msg, null,
85             Utils.getFormattedDate(new Date()), topicName, publisherId, publisherIp, consumerGroup + "/" + clientId, remoteHost);
86     }
87
88     public static class Builder {
89
90         private String clientId;
91         private String topicName;
92         private String consumerGroup;
93         private String remoteHost;
94         private String publisherId;
95         private String publisherIp;
96         DMaaPErrorMessages errorMessages;
97
98         Builder withErrorMessages(DMaaPErrorMessages errorMessages) {
99             this.errorMessages = errorMessages;
100             return this;
101         }
102
103         Builder withTopic(String topic) {
104             this.topicName = topic;
105             return this;
106         }
107
108         Builder withClient(String client) {
109             this.clientId = client;
110             return this;
111         }
112
113         Builder withConsumerGroup(String consumerGroup) {
114             this.consumerGroup = consumerGroup;
115             return this;
116         }
117
118         Builder withRemoteHost(String remoteHost) {
119             this.remoteHost = remoteHost;
120             return this;
121         }
122
123         Builder withPublisherId(String publisherId) {
124             this.publisherId = publisherId;
125             return this;
126         }
127
128         Builder withPublisherIp(String publisherIp) {
129             this.publisherIp = publisherIp;
130             return this;
131         }
132
133         public ErrorResponseProvider build() {
134             Preconditions.checkArgument(errorMessages!=null);
135             ErrorResponseProvider errRespProvider = new ErrorResponseProvider();
136             errRespProvider.errorMessages = this.errorMessages;
137             errRespProvider.clientId = this.clientId;
138             errRespProvider.consumerGroup = this.consumerGroup;
139             errRespProvider.topicName = this.topicName;
140             errRespProvider.remoteHost = this.remoteHost;
141             errRespProvider.publisherId = this.publisherId;
142             errRespProvider.publisherIp = this.publisherIp;
143             return errRespProvider;
144         }
145     }
146 }