a4122ac980eeaac3b37134fe9efb7fb109cca00c
[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 com.google.common.collect.Lists;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Properties;
36 import org.apache.commons.lang.StringUtils;
37 import org.onap.sdc.api.consumer.IConfiguration;
38
39 public class SdcConfig implements IConfiguration {
40
41     private String host;
42     private String consumer;
43     private String consumerId;
44     private String env;
45     private String keystorePath;
46     private String keystorePass;
47     /**
48      * Polling internal is time between listening sessions
49      */
50     private int pollingInterval;
51     /**
52      * Polling timeout is the time to listen for (dmaap timeout url param)/1000
53      */
54     private int pollingTimeout;
55     private List<String> types = new ArrayList<>();
56     private String user;
57     private String pass;
58     private URI storeOp;
59     private Properties props;
60     private final EELFLogger logger = EELFManager.getInstance().getLogger(SdcConfig.class);
61
62     SdcConfig(Properties props) throws URISyntaxException {
63         this.props = props;
64         init();
65     }
66
67     private void init() throws URISyntaxException {
68         if (props == null) {
69             logger.error("SdcConfig init is skipped due to properties is null");
70             return;
71         }
72         // Keystore for ca cert
73         keystorePath = props.getProperty("appc.sdc.keystore.path");
74         keystorePass = props.getProperty("appc.sdc.keystore.pass");
75
76         // ASDC host
77         host = props.getProperty("appc.sdc.host");
78         env = props.getProperty("appc.sdc.env");
79         user = props.getProperty("appc.sdc.user");
80         pass = props.getProperty("appc.sdc.pass");
81
82         // DMaaP properties
83         consumer = props.getProperty("appc.sdc.consumer");
84         consumerId = props.getProperty("appc.sdc.consumer.id");
85
86         pollingInterval = Integer.valueOf(props.getProperty("interval", "60"));
87
88         // Client uses cambriaClient-0.2.4 which throws non relevant (wrong)
89         // exceptions with times > 30s
90         pollingTimeout = Integer.valueOf(props.getProperty("timeout", "25"));
91
92         // Anything less than 60 and we risk 429 Too Many Requests
93         if (pollingInterval < 60) {
94             pollingInterval = 60;
95         }
96
97         if (pollingInterval > pollingTimeout) {
98             logger.warn(String.format(
99                 "Message acknowledgement may be delayed by %ds in the ADSC listener. [Listening Time: %s, Poll Period: %s]",
100                 pollingInterval - pollingTimeout, pollingTimeout, pollingInterval));
101         }
102
103         logParams();
104
105         // Download type
106         /*
107           This types seems redundant, as it looks from the code that they are not being used anywhere
108         */
109         types.add("APPC_CONFIG");
110         types.add("VF_LICENSE");
111         // types.add("TOSCA_CSAR"); commenting it out as we are not listening to TOSCA_CSAR
112
113         storeOp = new URI(props.getProperty("appc.sdc.provider.url"));
114     }
115
116     @Override
117     public boolean activateServerTLSAuth() {
118         return false;
119     }
120
121     @Override
122     public boolean isFilterInEmptyResources() {
123         return false;
124     }
125
126     @Override
127     public String getAsdcAddress() {
128         return host;
129     }
130
131     @Override
132     public String getConsumerGroup() {
133         return consumer;
134     }
135
136     @Override
137     public String getConsumerID() {
138         return consumerId;
139     }
140
141     @Override
142     public String getEnvironmentName() {
143         return env;
144     }
145
146     @Override
147     public String getKeyStorePassword() {
148         return keystorePass;
149     }
150
151     @Override
152     public String getKeyStorePath() {
153         return keystorePath;
154     }
155
156     @Override
157     public String getPassword() {
158         return pass;
159     }
160
161     @Override
162     public int getPollingInterval() {
163         return pollingInterval;
164     }
165
166     @Override
167     public int getPollingTimeout() {
168         return pollingTimeout;
169     }
170
171     @Override
172     public List<String> getRelevantArtifactTypes() {
173         return types;
174     }
175
176     @Override
177     public String getUser() {
178         return user;
179     }
180
181     @Override
182     public Boolean isUseHttpsWithDmaap() {
183         return true;
184     }
185
186     @Override
187     public List<String> getMsgBusAddress() {
188         return getMsgBusProperties();
189     }
190
191     public List<String> getMsgBusProperties() {
192         List<String> uebAddresses;
193         String uebAddressesList;
194         if (null != this.props) {
195             uebAddressesList = this.props.getProperty("appc.ClosedLoop.poolMembers");
196         } else {
197             logger.info("SdcConfig:SdcConfig:getMsgBusProperties()::props is null for SdcConfig");
198             return Collections.emptyList();
199         }
200         if (null == uebAddressesList) {
201             logger.info("SdcConfig:SdcConfig:getMsgBusProperties()::uebAddressesList is null for SdcConfig");
202             return Collections.emptyList();
203         }
204         logger.debug("SdcConfig:SdcConfig:getMsgBusProperties()::uebAddressesList is=" + uebAddressesList);
205         String[] sList = uebAddressesList.split(",");
206         uebAddresses = formatAddresses(sList);
207         logger.debug("SdcConfig:getMsgBusProperties:::Returning addresses as " + uebAddresses.toString());
208         return uebAddresses;
209     }
210
211     URI getStoreOpURI() {
212         return storeOp;
213     }
214
215     /**
216      * Logs the relevant parameters
217      */
218     private void logParams() {
219         Map<String, String> params = new HashMap<>();
220         params.put("SDC Host", getAsdcAddress());
221         params.put("SDC Environment", getEnvironmentName());
222         params.put("Consumer Name", getConsumerGroup());
223         params.put("Consumer ID", getConsumerID());
224         params.put("Poll Active Wait", String.valueOf(getPollingInterval()));
225         params.put("Poll Timeout", String.valueOf(getPollingTimeout()));
226
227         logger.info(String.format("SDC Params: %s", params));
228     }
229
230     private List<String> formatAddresses(String[] sList) {
231         List<String> uebAddresses = new ArrayList<>();
232         for (String fqdnPort : sList) {
233             if (fqdnPort.startsWith("http")) {
234                 fqdnPort = StringUtils.substringAfter(fqdnPort, "://");
235             }
236             if (fqdnPort.contains(":")) {
237                 fqdnPort = StringUtils.substringBefore(fqdnPort, ":");
238             }
239             logger.debug("SdcConfig:formatAddresses:: " + fqdnPort);
240             uebAddresses.add(fqdnPort);
241         }
242         return uebAddresses;
243     }
244 }