Merge "Add subscription for notifications"
[so.git] / asdc-controller / src / main / java / org / onap / so / asdc / client / ASDCConfiguration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
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.so.asdc.client;
24
25
26 import java.security.GeneralSecurityException;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.List;
30 import org.onap.sdc.api.consumer.IConfiguration;
31 import org.onap.so.utils.CryptoUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.core.env.Environment;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class ASDCConfiguration implements IConfiguration {
41
42     // SHell command to obtain the same encryption, 128 bits key, key must be HEX
43     // echo -n "This is a test string" | openssl aes-128-ecb -e -K 546573746F736973546573746F736973 -nosalt | xxd
44
45     private static Logger logger = LoggerFactory.getLogger(ASDCConfiguration.class);
46
47
48     private String asdcControllerName;
49
50     public static final String MSO_PROP_ASDC = "MSO_PROP_ASDC";
51     public static final String PARAMETER_PATTERN = "asdc-connections";
52     public static final String MSG_BUS_ADDRESS_ATTRIBUTE_NAME = "messageBusAddress";
53     public static final String WATCHDOG_TIMEOUT_NAME = "watchDogTimeout";
54
55     public static final String CONSUMER_GROUP_ATTRIBUTE_NAME = "consumerGroup";
56     public static final String CONSUMER_ID_ATTRIBUTE_NAME = "consumerId";
57     public static final String ENVIRONMENT_NAME_ATTRIBUTE_NAME = "environmentName";
58     public static final String PASSWORD_ATTRIBUTE_NAME = "password";
59     public static final String POLLING_INTERVAL_ATTRIBUTE_NAME = "pollingInterval";
60     public static final String RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME = "relevantArtifactTypes";
61     public static final String USER_ATTRIBUTE_NAME = "user";
62     public static final String ASDC_ADDRESS_ATTRIBUTE_NAME = "asdcAddress";
63     public static final String POLLING_TIMEOUT_ATTRIBUTE_NAME = "pollingTimeout";
64     public static final String ACTIVATE_SERVER_TLS_AUTH = "activateServerTLSAuth";
65     public static final String KEY_STORE_PASSWORD = "keyStorePassword";
66     public static final String KEY_STORE_PATH = "keyStorePath";
67
68     public static final String HEAT = "HEAT";
69     public static final String HEAT_ARTIFACT = "HEAT_ARTIFACT";
70     public static final String HEAT_ENV = "HEAT_ENV";
71     public static final String HEAT_NESTED = "HEAT_NESTED";
72     public static final String HEAT_NET = "HEAT_NET";
73     public static final String HEAT_VOL = "HEAT_VOL";
74     public static final String OTHER = "OTHER";
75     public static final String TOSCA_CSAR = "TOSCA_CSAR";
76     public static final String WORKFLOWS = "Workflows";
77     public static final String VF_MODULES_METADATA = "VF_MODULES_METADATA";
78
79     private static final String[] SUPPORTED_ARTIFACT_TYPES =
80             {HEAT, HEAT_ARTIFACT, HEAT_ENV, HEAT_NESTED, HEAT_NET, HEAT_VOL, OTHER, TOSCA_CSAR, VF_MODULES_METADATA};
81
82     public static final List<String> SUPPORTED_ARTIFACT_TYPES_LIST =
83             Collections.unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));
84
85     @Autowired
86     private Environment env;
87
88     @Value("${mso.asdc.config.key}")
89     private String configKey;
90
91     @Value("${mso.asdc-connections.asdc-controller1.messageBusAddress}")
92     private String[] messageBusAddress;
93
94
95     public void setAsdcControllerName(String asdcControllerName) {
96         this.asdcControllerName = asdcControllerName;
97     }
98
99     @Override
100     public java.lang.Boolean isUseHttpsWithDmaap() {
101         return getBooleanPropertyWithDefault("mso.asdc-connections.asdc-controller1.useHttpsWithDmaap", true);
102     }
103
104     @Override
105     public boolean isConsumeProduceStatusTopic() {
106         return true;
107     }
108
109     @Override
110     public List<String> getMsgBusAddress() {
111         if (messageBusAddress.length > 0) {
112             return Arrays.asList(messageBusAddress);
113         } else {
114             return Collections.emptyList();
115         }
116
117
118     }
119
120     public String getAsdcControllerName() {
121         return asdcControllerName;
122     }
123
124
125     @Override
126     public String getConsumerGroup() {
127         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.consumerGroup");
128     }
129
130     public int getWatchDogTimeout() {
131         return getIntegerPropertyOrZero("mso.asdc-connections.asdc-controller1.watchDogTimeout");
132
133     }
134
135     @Override
136     public String getConsumerID() {
137         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.consumerId");
138     }
139
140     public int getIntegerPropertyOrZero(String propertyName) {
141         String property = env.getProperty(propertyName);
142         if (property == null || "NULL".equals(property) || property.isEmpty()) {
143             return 0;
144         } else {
145             try {
146                 return Integer.parseInt(property);
147             } catch (NumberFormatException e) {
148                 return 0;
149             }
150         }
151     }
152
153     public String getPropertyOrNull(String propertyName) {
154         String config = env.getProperty(propertyName);
155         if (config == null || "NULL".equals(config) || config.isEmpty()) {
156             return null;
157         } else {
158             return config;
159         }
160     }
161
162     public String getEncryptedPropertyOrNull(String propertyName) {
163         String decryptedKey;
164         String config = env.getProperty(propertyName);
165
166         if (config == null || "NULL".equals(config) || config.isEmpty()) {
167             return null;
168         }
169
170         try {
171             decryptedKey = CryptoUtils.decrypt(config, this.configKey);
172         } catch (GeneralSecurityException e) {
173             logger.debug("Exception while decrypting property: {}", propertyName, e);
174             return null;
175         }
176
177         if (decryptedKey.isEmpty()) {
178             return null;
179         } else {
180             return decryptedKey;
181         }
182     }
183
184     public boolean getBooleanPropertyWithDefault(String propertyName, boolean defaultValue) {
185         String config = env.getProperty(propertyName);
186         if (config == null || "NULL".equals(config) || config.isEmpty()) {
187             return defaultValue;
188         } else {
189             try {
190                 return Boolean.valueOf(config);
191             } catch (Exception e) {
192                 return defaultValue;
193             }
194         }
195     }
196
197     @Override
198     public String getEnvironmentName() {
199         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.environmentName");
200     }
201
202     @Override
203     public String getPassword() {
204         return getEncryptedPropertyOrNull("mso.asdc-connections.asdc-controller1.password");
205     }
206
207     @Override
208     public int getPollingInterval() {
209         return getIntegerPropertyOrZero("mso.asdc-connections.asdc-controller1.pollingInterval");
210     }
211
212     @Override
213     public List<String> getRelevantArtifactTypes() {
214         // DO not return the Static List SUPPORTED_ARTIFACT_TYPES_LIST because the ASDC Client will try to modify it !!!
215         return Arrays.asList(SUPPORTED_ARTIFACT_TYPES);
216     }
217
218     @Override
219     public String getUser() {
220         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.user");
221     }
222
223     @Override
224     public String getAsdcAddress() {
225         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.asdcAddress");
226     }
227
228     @Override
229     public int getPollingTimeout() {
230         return getIntegerPropertyOrZero("mso.asdc-connections.asdc-controller1.pollingTimeout");
231     }
232
233     @Override
234     public boolean activateServerTLSAuth() {
235         return getBooleanPropertyWithDefault("mso.asdc-connections.asdc-controller1.activateServerTLSAuth", true);
236     }
237
238     @Override
239     public String getKeyStorePassword() {
240         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.keyStorePassword");
241     }
242
243     @Override
244     public String getKeyStorePath() {
245         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.keyStorePath");
246     }
247
248     /**
249      * The flag allows the client to receive metadata for all resources of the service regardless of the artifacts
250      * associated to them. Setting the flag to false will preserve legacy behavior.
251      */
252     @Override
253     public boolean isFilterInEmptyResources() {
254         return getBooleanPropertyWithDefault("mso.asdc-connections.asdc-controller1.isFilterInEmptyResources", true);
255     }
256
257 }