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