52e0988a5c97f10ab162c00311bea4ba7d3ef676
[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 package org.onap.policy.distribution.reception.parameters;
21
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.UUID;
27
28 import org.onap.policy.common.parameters.GroupValidationResult;
29 import org.onap.policy.common.parameters.ParameterGroup;
30 import org.onap.policy.common.parameters.ValidationStatus;
31
32 /**
33  * This class handles reading, parsing and validating of the Policy SDC Service Distribution parameters from Json
34  * format, which strictly adheres to the interface:IConfiguration, defined by SDC SDK.
35  */
36 public class PSSDConfigurationParametersGroup implements ParameterGroup {
37
38     // Policy SDC Service Distribution specified field.
39     private String name;
40
41     // Interface of IConfiguration item
42     private String asdcAddress;
43     private List<String> messageBusAddress;
44     private String user;
45     private String password;
46     private int pollingInterval;
47     private int pollingTimeout;
48     private String consumerId;
49     private List<String> artifactTypes;
50     private String consumerGroup;
51     private String environmentName;
52     private String keystorePath;
53     private String keystorePassword;
54     private boolean activeserverTlsAuth;
55     private boolean isFilterinEmptyResources;
56     private Boolean isUseHttpsWithDmaap;
57
58     /**
59      *Inner static class is to used as a Builder
60      *
61      */
62     public static class PSSDConfigurationBuilder {
63         private String asdcAddress;
64         private List<String> messageBusAddress;
65         private String user;
66         private String password;
67         private int pollingInterval;
68         private int pollingTimeout;
69         private String consumerId;
70         private List<String> artifactTypes;
71         private String consumerGroup;
72         private String environmentName;
73         private String keystorePath;
74         private String keystorePassword;
75         private boolean activeserverTlsAuth;
76         private boolean isFilterinEmptyResources;
77         private Boolean isUseHttpsWithDmaap;
78
79         public PSSDConfigurationBuilder setAsdcAddress(String val)
80             { asdcAddress = val;      return this; }
81         public PSSDConfigurationBuilder setMessageBusAddress(List<String> val)
82             { messageBusAddress = val;           return this; }
83         public PSSDConfigurationBuilder setUser(String val)
84             { user = val;  return this; }
85         public PSSDConfigurationBuilder setPassword(String val)
86             { password = val;        return this; }
87         public PSSDConfigurationBuilder setPollingInterval(int val)
88             { pollingInterval = val;        return this; }
89         public PSSDConfigurationBuilder setPollingTimeout(int val)
90             { pollingTimeout = val;        return this; }
91         public PSSDConfigurationBuilder setConsumerId(String val)
92             { consumerId = val;        return this; }
93         public PSSDConfigurationBuilder setArtifactTypes(List<String>  val)
94             { artifactTypes = val;        return this; }
95         public PSSDConfigurationBuilder setConsumerGroup(String val)
96             { consumerGroup = val;        return this; }
97         public PSSDConfigurationBuilder setEnvironmentName(String val)
98             { environmentName = val;        return this; }
99         public PSSDConfigurationBuilder setKeystorePath(String val)
100             { keystorePath = val;        return this; }
101         public PSSDConfigurationBuilder setKeystorePassword(String val)
102             { keystorePassword = val;        return this; }
103         public PSSDConfigurationBuilder setActiveserverTlsAuth(boolean val)
104             { activeserverTlsAuth = val;        return this; }
105         public PSSDConfigurationBuilder setIsFilterinEmptyResources(boolean val)
106             { isFilterinEmptyResources = val;        return this; }
107         public PSSDConfigurationBuilder setIsUseHttpsWithDmaap(Boolean val)
108             { isUseHttpsWithDmaap = val;        return this; }
109
110         
111         /**
112          * it is to create a new PSSDConfigurationParametersGroup instance.
113          */
114         public PSSDConfigurationParametersGroup build() {
115             return new PSSDConfigurationParametersGroup(this);
116         }
117     }
118
119     /**
120      * The constructor for instantiating PSSDConfigurationParametersGroup it is a private
121      * so that it could ONLY be instantiated by PSSDConfigurationBuilder
122      *
123      * @param builder stores all the values used by PSSDConfigurationParametersGroup
124      */
125     private PSSDConfigurationParametersGroup(PSSDConfigurationBuilder builder) {
126         asdcAddress  = builder.asdcAddress;
127         messageBusAddress     = builder.messageBusAddress;
128         user     = builder.user;
129         password          = builder.password;
130         pollingInterval       = builder.pollingInterval;
131         pollingTimeout = builder.pollingTimeout;
132         consumerId = builder.consumerId;
133         artifactTypes = builder.artifactTypes;
134         consumerGroup = builder.consumerGroup;
135         environmentName = builder.environmentName;
136         keystorePath = builder.keystorePath;
137         keystorePassword = builder.keystorePassword;
138         activeserverTlsAuth = builder.activeserverTlsAuth;
139         isFilterinEmptyResources = builder.isFilterinEmptyResources;
140         isUseHttpsWithDmaap  = builder.isUseHttpsWithDmaap;
141         
142     }
143     
144     public String getAsdcAddress() {
145         return asdcAddress;
146     }
147
148     public List<String> getMsgBusAddress() {
149         return messageBusAddress;
150     }
151
152     public String getUser() {
153         return user;
154     }
155
156     public String getPassword() {
157         return password;
158     }
159
160     public int getPollingInterval() {
161         return pollingInterval;
162     }
163
164     public int getPollingTimeout() {
165         return pollingTimeout;
166     }
167
168     public String getConsumerID() {
169         return consumerId;
170     }
171
172     public List<String> getArtifactTypes() {
173         return artifactTypes;
174     }
175
176     public String getConsumerGroup() {
177         return consumerGroup;
178     }
179
180     public String getEnvironmentName() {
181         return environmentName;
182     }
183
184     public String getKeyStorePassword() {
185         return keystorePassword;
186     }
187
188     public String getKeyStorePath() {
189         return keystorePath;
190     }
191
192     public boolean activateServerTLSAuth() {
193         return activeserverTlsAuth;
194     }
195
196     public boolean isFilterInEmptyResources() {
197         return isFilterinEmptyResources;
198     }
199
200     public Boolean isUseHttpsWithDmaap() {
201         return isUseHttpsWithDmaap;
202     }
203
204     @Override
205     public String toString() {
206         return "name =" + name + ",TestParameters:[asdcAddress = " + asdcAddress + ", messageBusAddress = "
207                 + messageBusAddress + ", user = " + user + "]";
208     }
209
210     @Override
211     public String getName() {
212         return name ;
213     }
214
215     @Override
216     public GroupValidationResult validate() {
217         final GroupValidationResult validationResult = new GroupValidationResult(this);
218
219         if (asdcAddress == null || asdcAddress.trim().length() == 0) {
220             validationResult.setResult("asdcAddress", ValidationStatus.INVALID,
221                     "asdcAddress must be a non-blank string");
222         }
223
224         if (user == null || user.trim().length() == 0) {
225             validationResult.setResult("user", ValidationStatus.INVALID, "user must be a non-blank string");
226         }
227
228         if (consumerId == null || consumerId.trim().length() == 0) {
229             validationResult.setResult("consumerId", ValidationStatus.INVALID, "consumerId must be a non-blank string");
230         }
231
232         if (consumerGroup == null || consumerGroup.trim().length() == 0) {
233             validationResult.setResult("consumerGroup", ValidationStatus.INVALID,
234                     "consumerGroup must be a non-blank string");
235         }
236
237         if (keystorePath == null || keystorePath.trim().length() == 0) {
238             validationResult.setResult("keystorePath", ValidationStatus.INVALID,
239                     "keystorePath must be a non-blank string");
240         }
241
242         if (keystorePassword == null || keystorePassword.trim().length() == 0) {
243             validationResult.setResult("keystorePassword", ValidationStatus.INVALID,
244                     "keystorePassword must be a non-blank string");
245         }
246
247         if (messageBusAddress == null) {
248             validationResult.setResult("messageBusAddress", ValidationStatus.INVALID,
249                     "messageBusAddress must be a list of non-blank string");
250         } else {
251             for (final String temp : messageBusAddress) {
252                 if (temp.trim().length() == 0) {
253                     validationResult.setResult("messageBusAddress", ValidationStatus.INVALID,
254                             "the string of messageBusAddress must be a non-blank string");
255                 }
256             }
257         }
258
259         if (artifactTypes == null) {
260             validationResult.setResult("artifactTypes", ValidationStatus.INVALID,
261                     "artifactTypes must be a list of non-blank string");
262         } else {
263             for (final String temp : artifactTypes) {
264                 if (temp.trim().length() == 0) {
265                     validationResult.setResult("artifactTypes", ValidationStatus.INVALID,
266                             "the string of artifactTypes must be a non-blank string");
267                 }
268             }
269         }
270
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         return validationResult;
282     }
283
284     /**
285      * Set the name of this group.
286      *
287      * @param name the name to set.
288      */
289     public void setName(final String name) {
290         this.name = name + "_" + UUID.randomUUID().toString();
291     }
292 }
293