50a611e9662553e8fac80c04ed48d82ffb349912
[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.beans.DMaaPContext;
25 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
26 import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
27 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
28 import org.onap.dmaap.dmf.mr.utils.Utils;
29
30 class ErrorResponseProvider {
31
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;
39
40     private ErrorResponseProvider() {
41
42     }
43
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);
50     }
51
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);
57     }
58
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 "
63                 + permission,
64             null, Utils.getFormattedDate(new Date()), topicName, publisherId, publisherIp, consumerGroup + "/" + clientId,
65             remoteHost);
66     }
67
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);
73     }
74
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);
80     }
81
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);
87     }
88
89     public static class Builder {
90
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;
98
99         Builder withErrorMessages(DMaaPErrorMessages errorMessages) {
100             this.errorMessages = errorMessages;
101             return this;
102         }
103
104         Builder withTopic(String topic) {
105             this.topicName = topic;
106             return this;
107         }
108
109         Builder withClient(String client) {
110             this.clientId = client;
111             return this;
112         }
113
114         Builder withConsumerGroup(String consumerGroup) {
115             this.consumerGroup = consumerGroup;
116             return this;
117         }
118
119         Builder withRemoteHost(String remoteHost) {
120             this.remoteHost = remoteHost;
121             return this;
122         }
123
124         Builder withPublisherId(String publisherId) {
125             this.publisherId = publisherId;
126             return this;
127         }
128
129         Builder withPublisherIp(String publisherIp) {
130             this.publisherIp = publisherIp;
131             return this;
132         }
133
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;
145         }
146     }
147 }