08fd707c7d4e2fb1ca22e829a7df8d02e57a5474
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Intel. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.handling.sdc;
22
23 import java.util.List;
24 import java.util.UUID;
25
26 import org.onap.policy.common.parameters.GroupValidationResult;
27 import org.onap.policy.common.parameters.ValidationStatus;
28 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
29 import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup;
30
31 /**
32  * This class handles reading, parsing and validating of the Policy SDC Service Distribution parameters from Json
33  * format, which strictly adheres to the interface:IConfiguration, defined by SDC SDK.
34  */
35 public class SdcReceptionHandlerConfigurationParameterGroup extends ReceptionHandlerConfigurationParameterGroup {
36
37     // Interface of IConfiguration item
38     private String asdcAddress;
39     private List<String> messageBusAddress;
40     private String user;
41     private String password;
42     private int pollingInterval;
43     private int pollingTimeout;
44     private String consumerId;
45     private List<String> artifactTypes;
46     private String consumerGroup;
47     private String environmentName;
48     private String keyStorePath;
49     private String keyStorePassword;
50     private boolean activeServerTlsAuth;
51     private boolean isFilterInEmptyResources;
52     private boolean isUseHttpsWithDmaap;
53
54     /**
55      * The constructor for instantiating {@link SdcReceptionHandlerConfigurationParameterGroup} class.
56      *
57      * @param builder the SDC configuration builder
58      */
59     private SdcReceptionHandlerConfigurationParameterGroup(final SdcConfigurationBuilder builder) {
60         asdcAddress = builder.asdcAddress;
61         messageBusAddress = builder.messageBusAddress;
62         user = builder.user;
63         password = builder.password;
64         pollingInterval = builder.pollingInterval;
65         pollingTimeout = builder.pollingTimeout;
66         consumerId = builder.consumerId;
67         artifactTypes = builder.artifactTypes;
68         consumerGroup = builder.consumerGroup;
69         environmentName = builder.environmentName;
70         keyStorePath = builder.keystorePath;
71         keyStorePassword = builder.keystorePassword;
72         activeServerTlsAuth = builder.activeserverTlsAuth;
73         isFilterInEmptyResources = builder.isFilterinEmptyResources;
74         isUseHttpsWithDmaap = builder.isUseHttpsWithDmaap;
75
76     }
77
78     public String getAsdcAddress() {
79         return asdcAddress;
80     }
81
82     public List<String> getMessageBusAddress() {
83         return messageBusAddress;
84     }
85
86     public String getUser() {
87         return user;
88     }
89
90     public String getPassword() {
91         return password;
92     }
93
94     public int getPollingInterval() {
95         return pollingInterval;
96     }
97
98     public int getPollingTimeout() {
99         return pollingTimeout;
100     }
101
102     public String getConsumerId() {
103         return consumerId;
104     }
105
106     public List<String> getArtifactTypes() {
107         return artifactTypes;
108     }
109
110     public String getConsumerGroup() {
111         return consumerGroup;
112     }
113
114     public String getEnvironmentName() {
115         return environmentName;
116     }
117
118     public String getKeyStorePassword() {
119         return keyStorePassword;
120     }
121
122     public boolean isActiveServerTlsAuth() {
123         return activeServerTlsAuth;
124     }
125
126     public String getKeyStorePath() {
127         return keyStorePath;
128     }
129
130     public boolean isFilterInEmptyResources() {
131         return isFilterInEmptyResources;
132     }
133
134     public boolean isUseHttpsWithDmaap() {
135         return isUseHttpsWithDmaap;
136     }
137
138     /**
139      * Set the name of this group.
140      *
141      * @param name the name to set.
142      */
143     @Override
144     public void setName(final String name) {
145         super.setName(name + "_" + UUID.randomUUID().toString());
146     }
147
148     /**
149      * Inner static class is to used as a Builder.
150      *
151      */
152     public static class SdcConfigurationBuilder {
153         private String asdcAddress;
154         private List<String> messageBusAddress;
155         private String user;
156         private String password;
157         private int pollingInterval;
158         private int pollingTimeout;
159         private String consumerId;
160         private List<String> artifactTypes;
161         private String consumerGroup;
162         private String environmentName;
163         private String keystorePath;
164         private String keystorePassword;
165         private boolean activeserverTlsAuth;
166         private boolean isFilterinEmptyResources;
167         private Boolean isUseHttpsWithDmaap;
168
169         public SdcConfigurationBuilder setAsdcAddress(final String val) {
170             asdcAddress = val;
171             return this;
172         }
173
174         public SdcConfigurationBuilder setMessageBusAddress(final List<String> val) {
175             messageBusAddress = val;
176             return this;
177         }
178
179         public SdcConfigurationBuilder setUser(final String val) {
180             user = val;
181             return this;
182         }
183
184         public SdcConfigurationBuilder setPassword(final String val) {
185             password = val;
186             return this;
187         }
188
189         public SdcConfigurationBuilder setPollingInterval(final int val) {
190             pollingInterval = val;
191             return this;
192         }
193
194         public SdcConfigurationBuilder setPollingTimeout(final int val) {
195             pollingTimeout = val;
196             return this;
197         }
198
199         public SdcConfigurationBuilder setConsumerId(final String val) {
200             consumerId = val;
201             return this;
202         }
203
204         public SdcConfigurationBuilder setArtifactTypes(final List<String> val) {
205             artifactTypes = val;
206             return this;
207         }
208
209         public SdcConfigurationBuilder setConsumerGroup(final String val) {
210             consumerGroup = val;
211             return this;
212         }
213
214         public SdcConfigurationBuilder setEnvironmentName(final String val) {
215             environmentName = val;
216             return this;
217         }
218
219         public SdcConfigurationBuilder setKeystorePath(final String val) {
220             keystorePath = val;
221             return this;
222         }
223
224         public SdcConfigurationBuilder setKeystorePassword(final String val) {
225             keystorePassword = val;
226             return this;
227         }
228
229         public SdcConfigurationBuilder setActiveserverTlsAuth(final boolean val) {
230             activeserverTlsAuth = val;
231             return this;
232         }
233
234         public SdcConfigurationBuilder setIsFilterinEmptyResources(final boolean val) {
235             isFilterinEmptyResources = val;
236             return this;
237         }
238
239         public SdcConfigurationBuilder setIsUseHttpsWithDmaap(final Boolean val) {
240             isUseHttpsWithDmaap = val;
241             return this;
242         }
243
244         /**
245          * Creates an instance of {@link SdcReceptionHandlerConfigurationParameterGroup}.
246          */
247         public SdcReceptionHandlerConfigurationParameterGroup build() {
248             return new SdcReceptionHandlerConfigurationParameterGroup(this);
249         }
250     }
251
252     /**
253      * {@inheritDoc}
254      */
255     @Override
256     public String toString() {
257         return "name =" + getName() + ",TestParameters:[asdcAddress = " + asdcAddress + ", messageBusAddress = "
258                 + messageBusAddress + ", user = " + user + "]";
259     }
260
261     /**
262      * {@inheritDoc}
263      */
264     @Override
265     public GroupValidationResult validate() {
266         final GroupValidationResult validationResult = new GroupValidationResult(this);
267         validateStringElement(validationResult, asdcAddress, "asdcAddress");
268         validateStringElement(validationResult, user, "user");
269         validateStringElement(validationResult, consumerId, "consumerId");
270         validateStringElement(validationResult, consumerGroup, "consumerGroup");
271         validateStringElement(validationResult, keyStorePath, "keyStorePath");
272         validateStringElement(validationResult, keyStorePassword, "keyStorePassword");
273         validateIntElement(validationResult, pollingInterval, "pollingInterval");
274         validateIntElement(validationResult, pollingTimeout, "pollingTimeout");
275         validateStringListElement(validationResult, messageBusAddress, "messageBusAddress");
276         validateStringListElement(validationResult, artifactTypes, "artifactTypes");
277         return validationResult;
278     }
279
280     /**
281      * Validate the integer Element.
282      *
283      * @param validationResult the result object
284      * @param element the element to validate
285      * @param elementName the element name for error message
286      */
287     private void validateIntElement(final GroupValidationResult validationResult, final int element,
288             final String elementName) {
289         if (!ParameterValidationUtils.validateIntParameter(element)) {
290             validationResult.setResult(elementName, ValidationStatus.INVALID,
291                     elementName + " must be a positive integer");
292         }
293     }
294
295     /**
296      * Validate the String List Element.
297      *
298      * @param validationResult the result object
299      * @param element the element to validate
300      * @param elementName the element name for error message
301      */
302     private void validateStringListElement(final GroupValidationResult validationResult, final List<String> element,
303             final String elementName) {
304         if (element == null) {
305             validationResult.setResult(elementName, ValidationStatus.INVALID,
306                     elementName + " must be a list of non-blank string");
307         } else {
308             for (final String temp : element) {
309                 if (!ParameterValidationUtils.validateStringParameter(temp)) {
310                     validationResult.setResult(elementName, ValidationStatus.INVALID,
311                             "the string of " + elementName + "must be a non-blank string");
312                 }
313             }
314         }
315     }
316
317     /**
318      * Validate the string element.
319      *
320      * @param validationResult the result object
321      * @param element the element to validate
322      * @param elementName the element name for error message
323      */
324     private void validateStringElement(final GroupValidationResult validationResult, final String element,
325             final String elementName) {
326         if (!ParameterValidationUtils.validateStringParameter(element)) {
327             validationResult.setResult(elementName, ValidationStatus.INVALID,
328                     elementName + " must be a non-blank string");
329         }
330     }
331 }
332