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