5c46049fe8d2e9fd91cfc800aaf473d4688732b9
[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 import org.onap.policy.common.parameters.GroupValidationResult;
26 import org.onap.policy.common.parameters.ValidationStatus;
27 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
28 import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup;
29
30 /**
31  * This class handles reading, parsing and validating of the Policy SDC Service Distribution
32  * parameters from Json format, which strictly adheres to the interface:IConfiguration, defined by
33  * 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      * Inner static class is to used as a Builder.
56      *
57      */
58     public static class PssdConfigurationBuilder {
59         private String asdcAddress;
60         private List<String> messageBusAddress;
61         private String user;
62         private String password;
63         private int pollingInterval;
64         private int pollingTimeout;
65         private String consumerId;
66         private List<String> artifactTypes;
67         private String consumerGroup;
68         private String environmentName;
69         private String keystorePath;
70         private String keystorePassword;
71         private boolean activeserverTlsAuth;
72         private boolean isFilterinEmptyResources;
73         private Boolean isUseHttpsWithDmaap;
74
75         public PssdConfigurationBuilder setAsdcAddress(final String val) {
76             asdcAddress = val;
77             return this;
78         }
79
80         public PssdConfigurationBuilder setMessageBusAddress(final List<String> val) {
81             messageBusAddress = val;
82             return this;
83         }
84
85         public PssdConfigurationBuilder setUser(final String val) {
86             user = val;
87             return this;
88         }
89
90         public PssdConfigurationBuilder setPassword(final String val) {
91             password = val;
92             return this;
93         }
94
95         public PssdConfigurationBuilder setPollingInterval(final int val) {
96             pollingInterval = val;
97             return this;
98         }
99
100         public PssdConfigurationBuilder setPollingTimeout(final int val) {
101             pollingTimeout = val;
102             return this;
103         }
104
105         public PssdConfigurationBuilder setConsumerId(final String val) {
106             consumerId = val;
107             return this;
108         }
109
110         public PssdConfigurationBuilder setArtifactTypes(final List<String> val) {
111             artifactTypes = val;
112             return this;
113         }
114
115         public PssdConfigurationBuilder setConsumerGroup(final String val) {
116             consumerGroup = val;
117             return this;
118         }
119
120         public PssdConfigurationBuilder setEnvironmentName(final String val) {
121             environmentName = val;
122             return this;
123         }
124
125         public PssdConfigurationBuilder setKeystorePath(final String val) {
126             keystorePath = val;
127             return this;
128         }
129
130         public PssdConfigurationBuilder setKeystorePassword(final String val) {
131             keystorePassword = val;
132             return this;
133         }
134
135         public PssdConfigurationBuilder setActiveserverTlsAuth(final boolean val) {
136             activeserverTlsAuth = val;
137             return this;
138         }
139
140         public PssdConfigurationBuilder setIsFilterinEmptyResources(final boolean val) {
141             isFilterinEmptyResources = val;
142             return this;
143         }
144
145         public PssdConfigurationBuilder setIsUseHttpsWithDmaap(final Boolean val) {
146             isUseHttpsWithDmaap = val;
147             return this;
148         }
149
150         /**
151          * Creates a new PssdConfigurationParametersGroup instance.
152          */
153         public SdcReceptionHandlerConfigurationParameterGroup build() {
154             return new SdcReceptionHandlerConfigurationParameterGroup(this);
155         }
156     }
157
158     /**
159      * The constructor for instantiating PssdConfigurationParametersGroup. It is kept private so
160      * that it could only be called by PssdConfigurationBuilder.
161      *
162      * @param builder stores all the values used by PssdConfigurationParametersGroup
163      */
164     private SdcReceptionHandlerConfigurationParameterGroup(final PssdConfigurationBuilder builder) {
165         asdcAddress = builder.asdcAddress;
166         messageBusAddress = builder.messageBusAddress;
167         user = builder.user;
168         password = builder.password;
169         pollingInterval = builder.pollingInterval;
170         pollingTimeout = builder.pollingTimeout;
171         consumerId = builder.consumerId;
172         artifactTypes = builder.artifactTypes;
173         consumerGroup = builder.consumerGroup;
174         environmentName = builder.environmentName;
175         keystorePath = builder.keystorePath;
176         keystorePassword = builder.keystorePassword;
177         activeserverTlsAuth = builder.activeserverTlsAuth;
178         isFilterinEmptyResources = builder.isFilterinEmptyResources;
179         isUseHttpsWithDmaap = builder.isUseHttpsWithDmaap;
180
181     }
182
183     public String getAsdcAddress() {
184         return asdcAddress;
185     }
186
187     public List<String> getMsgBusAddress() {
188         return messageBusAddress;
189     }
190
191     public String getUser() {
192         return user;
193     }
194
195     public String getPassword() {
196         return password;
197     }
198
199     public int getPollingInterval() {
200         return pollingInterval;
201     }
202
203     public int getPollingTimeout() {
204         return pollingTimeout;
205     }
206
207     public String getConsumerID() {
208         return consumerId;
209     }
210
211     public List<String> getArtifactTypes() {
212         return artifactTypes;
213     }
214
215     public String getConsumerGroup() {
216         return consumerGroup;
217     }
218
219     public String getEnvironmentName() {
220         return environmentName;
221     }
222
223     public String getKeyStorePassword() {
224         return keystorePassword;
225     }
226
227     public String getKeyStorePath() {
228         return keystorePath;
229     }
230
231     public boolean activateServerTLSAuth() {
232         return activeserverTlsAuth;
233     }
234
235     public boolean isFilterInEmptyResources() {
236         return isFilterinEmptyResources;
237     }
238
239     public Boolean isUseHttpsWithDmaap() {
240         return isUseHttpsWithDmaap;
241     }
242
243     /**
244      * {@inheritDoc}
245      */
246     @Override
247     public String toString() {
248         return "name =" + getName() + ",TestParameters:[asdcAddress = " + asdcAddress + ", messageBusAddress = "
249                 + messageBusAddress + ", user = " + user + "]";
250     }
251
252     /**
253      * Set the name of this group.
254      *
255      * @param name the name to set.
256      */
257     @Override
258     public void setName(final String name) {
259         super.setName(name + "_" + UUID.randomUUID().toString());
260     }
261
262     /**
263      * {@inheritDoc}
264      */
265     @Override
266     public GroupValidationResult validate() {
267         final GroupValidationResult validationResult = new GroupValidationResult(this);
268         validateStringElement(validationResult, asdcAddress, "asdcAddress");
269         validateStringElement(validationResult, user, "user");
270         validateStringElement(validationResult, consumerId, "consumerId");
271         validateStringElement(validationResult, consumerGroup, "consumerGroup");
272         validateStringElement(validationResult, keystorePath, "keystorePath");
273         validateStringElement(validationResult, keystorePassword, "keystorePassword");
274         validateIntElement(validationResult, pollingInterval, "pollingInterval");
275         validateIntElement(validationResult, pollingTimeout, "pollingTimeout");
276         validateStringListElement(validationResult, messageBusAddress, "messageBusAddress");
277         validateStringListElement(validationResult, artifactTypes, "artifactTypes");
278         return validationResult;
279     }
280
281     /**
282      * Validate the integer Element.
283      *
284      * @param validationResult the result object
285      * @param element the element to validate
286      * @param elementName the element name for error message
287      */
288     private void validateIntElement(final GroupValidationResult validationResult, final int element,
289             final String elementName) {
290         if (!ParameterValidationUtils.validateIntParameter(element)) {
291             validationResult.setResult(elementName, ValidationStatus.INVALID,
292                     elementName + " must be a positive integer");
293         }
294     }
295
296     /**
297      * Validate the String List Element.
298      *
299      * @param validationResult the result object
300      * @param element the element to validate
301      * @param elementName the element name for error message
302      */
303     private void validateStringListElement(final GroupValidationResult validationResult, final List<String> element,
304             final String elementName) {
305         if (element == null) {
306             validationResult.setResult(elementName, ValidationStatus.INVALID,
307                     elementName + " must be a list of non-blank string");
308         } else {
309             for (final String temp : element) {
310                 if (!ParameterValidationUtils.validateStringParameter(temp)) {
311                     validationResult.setResult(elementName, ValidationStatus.INVALID,
312                             "the string of " + elementName + "must be a non-blank string");
313                 }
314             }
315         }
316     }
317
318     /**
319      * Validate the string element.
320      *
321      * @param validationResult the result object
322      * @param element the element to validate
323      * @param elementName the element name for error message
324      */
325     private void validateStringElement(final GroupValidationResult validationResult, final String element,
326             final String elementName) {
327         if (!ParameterValidationUtils.validateStringParameter(asdcAddress)) {
328             validationResult.setResult(elementName, ValidationStatus.INVALID,
329                     elementName + " must be a non-blank string");
330         }
331     }
332 }
333