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