aboutsummaryrefslogtreecommitdiffstats
path: root/dcae-analytics-dmaap/src/main/java/org/openecomp/dcae/apod/analytics/dmaap/domain/config/DMaaPMRBaseConfig.java
blob: 32a435734a15a1866afc24dacab28f869f01c14b (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * ===============================LICENSE_START======================================
 *  dcae-analytics
 * ================================================================================
 *    Copyright © 2017 AT&T Intellectual Property. 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.openecomp.dcae.apod.analytics.dmaap.domain.config;

import com.google.common.base.Objects;
import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Locale;

import static org.openecomp.dcae.apod.analytics.common.utils.HTTPUtils.JSON_APPLICATION_TYPE;

/**
 * <p>
 *      Contains common parameters for both DMaaP Message Router Publisher and Subscriber Configs
 * <p>
 * @author Rajiv Singla . Creation Date: 10/12/2016.
 */
public abstract class DMaaPMRBaseConfig implements DMaaPMRConfig {

    protected static final Logger LOG = LoggerFactory.getLogger(DMaaPMRBaseConfig.class);

    protected String hostName;
    protected Integer portNumber;
    protected String topicName;
    protected String protocol;
    protected String userName;
    protected String userPassword;
    protected String contentType;

    /**
     * Provides host name e.g. mrlocal-mtnjftle01.homer.com
     *
     * @return host name
     */
    public String getHostName() {
        return hostName;
    }


    /**
     * Provides Port Number of DMaaP MR Topic Host. Defaults to 80
     *
     * @return host port number
     */
    public Integer getPortNumber() {
        return portNumber;
    }

    /**
     * Provides topic name e.g. com.dcae.dmaap.mtnje2.DcaeTestVES
     *
     * @return topic name
     */
    public String getTopicName() {
        return topicName;
    }

    /**
     * Provides protocol type e.g. http or https
     *
     * @return protocol type
     */
    public String getProtocol() {
        return protocol;
    }

    /**
     * Provides content type e.g. application/json
     *
     * @return content type
     */
    public String getContentType() {
        return contentType;
    }


    /**
     * Provides User name for the DMaaP MR Topic authentication
     *
     * @return user name
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Provides User password for the DMaaP MR Topic authentication
     *
     * @return user Password
     */
    public String getUserPassword() {
        return userPassword;
    }


    /**
     * Trims, adjusts casing and validates user input String for protocol selection
     *
     * @param protocol - User input for protocol String
     * @return - network protocol e.g http or https
     */
    protected static String normalizeValidateProtocol(final String protocol) {
        // validate that only http and https are supported protocols are Supported for DMaaP MR
        String normalizedProtocolString = protocol.trim().toLowerCase(Locale.ENGLISH);
        if (normalizedProtocolString.isEmpty() ||
                !("http".equals(normalizedProtocolString) || "https".equals(normalizedProtocolString))) {

            final String errorMessage =
                    "Unsupported protocol selection. Only HTTPS and HTTPS are currently supported for DMaaP MR";

            throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, new IllegalArgumentException(errorMessage));
        }
        return normalizedProtocolString;
    }


    /**
     * Trims, adjust casing and validates content type is supported by DMaaP.
     *
     * NOTE: DMaaP currently only support application/json content type
     *
     * @param contentType content type that needs to checked for DMaaP MR support
     * @return true if content type is supported by DMaaP MR
     */
    protected static String normalizeValidateContentType(final String contentType) {
        // Current DMaaP MR is only supporting "application/json" content type
        String normalizedContentType = contentType.trim().toLowerCase(Locale.ENGLISH);
        final boolean isSupported = contentType.equals(JSON_APPLICATION_TYPE);
        if (!isSupported) {
            final String errorMessage =
                    "Unsupported content type selection. Only application/json is currently supported for DMaaP MR";

            throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, new IllegalArgumentException(errorMessage));
        }
        return normalizedContentType;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof DMaaPMRBaseConfig)) {
            return false;
        }
        DMaaPMRBaseConfig that = (DMaaPMRBaseConfig) o;
        return Objects.equal(hostName, that.hostName) &&
                Objects.equal(portNumber, that.portNumber) &&
                Objects.equal(topicName, that.topicName) &&
                Objects.equal(protocol, that.protocol) &&
                Objects.equal(userName, that.userName) &&
                Objects.equal(userPassword, that.userPassword) &&
                Objects.equal(contentType, that.contentType);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(hostName, portNumber, topicName, protocol, userName, userPassword, contentType);
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
                .add("hostName", hostName)
                .add("portNumber", portNumber)
                .add("topicName", topicName)
                .add("protocol", protocol)
                .add("userName", userName)
                .add("contentType", contentType)
                .toString();
    }
}