removed code smells
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / main / java / org / onap / appc / sdc / listener / SdcConfig.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.appc.sdc.listener;
24
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Properties;
35 import org.apache.commons.lang.StringUtils;
36 import org.onap.sdc.api.consumer.IConfiguration;
37
38 public class SdcConfig implements IConfiguration {
39
40     private String host;
41     private String consumer;
42     private String consumerId;
43     private String env;
44     private String keystorePath;
45     private String keystorePass;
46     /**
47      * Polling internal is time between listening sessions
48      */
49     private int pollingInterval;
50     /**
51      * Polling timeout is the time to listen for (dmaap timeout url param)/1000
52      */
53     private int pollingTimeout;
54     private List<String> types = new ArrayList<>();
55     private String user;
56     private String pass;
57     private URI storeOp;
58     private Properties props;
59     private final EELFLogger logger = EELFManager.getInstance().getLogger(SdcConfig.class);
60
61     SdcConfig(Properties props) throws URISyntaxException {
62         this.props = props;
63         init();
64     }
65
66     private void init() throws URISyntaxException {
67         if (props == null) {
68             logger.error("SdcConfig init is skipped due to properties is null");
69             return;
70         }
71         // Keystore for ca cert
72         keystorePath = props.getProperty("appc.sdc.keystore.path");
73         keystorePass = props.getProperty("appc.sdc.keystore.pass");
74
75         // ASDC host
76         host = props.getProperty("appc.sdc.host");
77         env = props.getProperty("appc.sdc.env");
78         user = props.getProperty("appc.sdc.user");
79         pass = props.getProperty("appc.sdc.pass");
80
81         // DMaaP properties
82         consumer = props.getProperty("appc.sdc.consumer");
83         consumerId = props.getProperty("appc.sdc.consumer.id");
84
85         pollingInterval = Integer.valueOf(props.getProperty("interval", "60"));
86
87         // Client uses cambriaClient-0.2.4 which throws non relevant (wrong)
88         // exceptions with times > 30s
89         pollingTimeout = Integer.valueOf(props.getProperty("timeout", "25"));
90
91         // Anything less than 60 and we risk 429 Too Many Requests
92         if (pollingInterval < 60) {
93             pollingInterval = 60;
94         }
95
96         if (pollingInterval > pollingTimeout) {
97             logger.warn(String.format(
98                 "Message acknowledgement may be delayed by %ds in the ADSC listener. [Listening Time: %s, Poll Period: %s]",
99                 pollingInterval - pollingTimeout, pollingTimeout, pollingInterval));
100         }
101
102         logParams();
103
104         // Download type
105         /*
106           This types seems redundant, as it looks from the code that they are not being used anywhere
107         */
108         types.add("APPC_CONFIG");
109         types.add("VF_LICENSE");
110         // types.add("TOSCA_CSAR"); commenting it out as we are not listening to TOSCA_CSAR
111
112         storeOp = new URI(props.getProperty("appc.sdc.provider.url"));
113     }
114
115     @Override
116     public boolean activateServerTLSAuth() {
117         return false;
118     }
119
120     @Override
121     public boolean isFilterInEmptyResources() {
122         return false;
123     }
124
125     @Override
126     public String getAsdcAddress() {
127         return host;
128     }
129
130     @Override
131     public String getConsumerGroup() {
132         return consumer;
133     }
134
135     @Override
136     public String getConsumerID() {
137         return consumerId;
138     }
139
140     @Override
141     public String getEnvironmentName() {
142         return env;
143     }
144
145     @Override
146     public String getKeyStorePassword() {
147         return keystorePass;
148     }
149
150     @Override
151     public String getKeyStorePath() {
152         return keystorePath;
153     }
154
155     @Override
156     public String getPassword() {
157         return pass;
158     }
159
160     @Override
161     public int getPollingInterval() {
162         return pollingInterval;
163     }
164
165     @Override
166     public int getPollingTimeout() {
167         return pollingTimeout;
168     }
169
170     @Override
171     public List<String> getRelevantArtifactTypes() {
172         return types;
173     }
174
175     @Override
176     public String getUser() {
177         return user;
178     }
179
180     @Override
181     public Boolean isUseHttpsWithDmaap() {
182         return true;
183     }
184
185     @Override
186     public List<String> getMsgBusAddress() {
187         return getMsgBusProperties();
188     }
189
190     public List<String> getMsgBusProperties() {
191         List<String> uebAddresses;
192         String uebAddressesList;
193         if (null != this.props) {
194             uebAddressesList = this.props.getProperty("appc.ClosedLoop.poolMembers");
195         } else {
196             logger.info("SdcConfig:SdcConfig:getMsgBusProperties()::props is null for SdcConfig");
197             return Collections.emptyList();
198         }
199         if (null == uebAddressesList) {
200             logger.info("SdcConfig:SdcConfig:getMsgBusProperties()::uebAddressesList is null for SdcConfig");
201             return Collections.emptyList();
202         }
203         logger.debug("SdcConfig:SdcConfig:getMsgBusProperties()::uebAddressesList is=" + uebAddressesList);
204         String[] sList = uebAddressesList.split(",");
205         uebAddresses = formatAddresses(sList);
206         logger.debug("SdcConfig:getMsgBusProperties:::Returning addresses as " + uebAddresses.toString());
207         return uebAddresses;
208     }
209
210     URI getStoreOpURI() {
211         return storeOp;
212     }
213
214     /**
215      * Logs the relevant parameters
216      */
217     private void logParams() {
218         Map<String, String> params = new HashMap<>();
219         params.put("SDC Host", getAsdcAddress());
220         params.put("SDC Environment", getEnvironmentName());
221         params.put("Consumer Name", getConsumerGroup());
222         params.put("Consumer ID", getConsumerID());
223         params.put("Poll Active Wait", String.valueOf(getPollingInterval()));
224         params.put("Poll Timeout", String.valueOf(getPollingTimeout()));
225
226         logger.info(String.format("SDC Params: %s", params));
227     }
228
229     private List<String> formatAddresses(String[] sList) {
230         List<String> uebAddresses = new ArrayList<>();
231         for (String fqdnPort : sList) {
232             if (fqdnPort.startsWith("http")) {
233                 fqdnPort = StringUtils.substringAfter(fqdnPort, "://");
234             }
235             if (fqdnPort.contains(":")) {
236                 fqdnPort = StringUtils.substringBefore(fqdnPort, ":");
237             }
238             logger.debug("SdcConfig:formatAddresses:: " + fqdnPort);
239             uebAddresses.add(fqdnPort);
240         }
241         return uebAddresses;
242     }
243 }