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