Remove hardcoded literals
[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 HEAT = "HEAT";
51     public static final String HEAT_ARTIFACT = "HEAT_ARTIFACT";
52     public static final String HEAT_ENV = "HEAT_ENV";
53     public static final String HEAT_NESTED = "HEAT_NESTED";
54     public static final String HEAT_NET = "HEAT_NET";
55     public static final String HEAT_VOL = "HEAT_VOL";
56     public static final String OTHER = "OTHER";
57     public static final String TOSCA_CSAR = "TOSCA_CSAR";
58     public static final String WORKFLOWS = "Workflows";
59     public static final String VF_MODULES_METADATA = "VF_MODULES_METADATA";
60
61     private static final String[] SUPPORTED_ARTIFACT_TYPES =
62             {HEAT, HEAT_ARTIFACT, HEAT_ENV, HEAT_NESTED, HEAT_NET, HEAT_VOL, OTHER, TOSCA_CSAR, VF_MODULES_METADATA};
63
64     public static final List<String> SUPPORTED_ARTIFACT_TYPES_LIST =
65             Collections.unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));
66
67     @Autowired
68     private Environment env;
69
70     @Value("${mso.asdc.config.key}")
71     private String configKey;
72
73     @Value("${mso.asdc-connections.asdc-controller1.messageBusAddress}")
74     private String[] messageBusAddress;
75
76
77     public void setAsdcControllerName(String asdcControllerName) {
78         this.asdcControllerName = asdcControllerName;
79     }
80
81     @Override
82     public java.lang.Boolean isUseHttpsWithDmaap() {
83         return getBooleanPropertyWithDefault("mso.asdc-connections.asdc-controller1.useHttpsWithDmaap", true);
84     }
85
86     @Override
87     public boolean isConsumeProduceStatusTopic() {
88         return true;
89     }
90
91     @Override
92     public List<String> getMsgBusAddress() {
93         if (messageBusAddress.length > 0) {
94             return Arrays.asList(messageBusAddress);
95         } else {
96             return Collections.emptyList();
97         }
98
99
100     }
101
102     public String getAsdcControllerName() {
103         return asdcControllerName;
104     }
105
106
107     @Override
108     public String getConsumerGroup() {
109         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.consumerGroup");
110     }
111
112     public int getWatchDogTimeout() {
113         return getIntegerPropertyOrZero("mso.asdc-connections.asdc-controller1.watchDogTimeout");
114
115     }
116
117     @Override
118     public String getConsumerID() {
119         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.consumerId");
120     }
121
122     public int getIntegerPropertyOrZero(String propertyName) {
123         String property = env.getProperty(propertyName);
124         if (property == null || "NULL".equals(property) || property.isEmpty()) {
125             return 0;
126         } else {
127             try {
128                 return Integer.parseInt(property);
129             } catch (NumberFormatException e) {
130                 return 0;
131             }
132         }
133     }
134
135     public String getPropertyOrNull(String propertyName) {
136         String config = env.getProperty(propertyName);
137         if (config == null || "NULL".equals(config) || config.isEmpty()) {
138             return null;
139         } else {
140             return config;
141         }
142     }
143
144     public String getEncryptedPropertyOrNull(String propertyName) {
145         String decryptedKey;
146         String config = env.getProperty(propertyName);
147
148         if (config == null || "NULL".equals(config) || config.isEmpty()) {
149             return null;
150         }
151
152         try {
153             decryptedKey = CryptoUtils.decrypt(config, this.configKey);
154         } catch (GeneralSecurityException e) {
155             logger.debug("Exception while decrypting property: {}", propertyName, e);
156             return null;
157         }
158
159         if (decryptedKey.isEmpty()) {
160             return null;
161         } else {
162             return decryptedKey;
163         }
164     }
165
166     public boolean getBooleanPropertyWithDefault(String propertyName, boolean defaultValue) {
167         String config = env.getProperty(propertyName);
168         if (config == null || "NULL".equals(config) || config.isEmpty()) {
169             return defaultValue;
170         } else {
171             try {
172                 return Boolean.valueOf(config);
173             } catch (Exception e) {
174                 return defaultValue;
175             }
176         }
177     }
178
179     @Override
180     public String getEnvironmentName() {
181         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.environmentName");
182     }
183
184     @Override
185     public String getPassword() {
186         return getEncryptedPropertyOrNull("mso.asdc-connections.asdc-controller1.password");
187     }
188
189     @Override
190     public int getPollingInterval() {
191         return getIntegerPropertyOrZero("mso.asdc-connections.asdc-controller1.pollingInterval");
192     }
193
194     @Override
195     public List<String> getRelevantArtifactTypes() {
196         // DO not return the Static List SUPPORTED_ARTIFACT_TYPES_LIST because the ASDC Client will try to modify it !!!
197         return Arrays.asList(SUPPORTED_ARTIFACT_TYPES);
198     }
199
200     @Override
201     public String getUser() {
202         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.user");
203     }
204
205     @Override
206     public String getAsdcAddress() {
207         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.asdcAddress");
208     }
209
210     @Override
211     public int getPollingTimeout() {
212         return getIntegerPropertyOrZero("mso.asdc-connections.asdc-controller1.pollingTimeout");
213     }
214
215     @Override
216     public boolean activateServerTLSAuth() {
217         return getBooleanPropertyWithDefault("mso.asdc-connections.asdc-controller1.activateServerTLSAuth", true);
218     }
219
220     @Override
221     public String getKeyStorePassword() {
222         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.keyStorePassword");
223     }
224
225     @Override
226     public String getKeyStorePath() {
227         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.keyStorePath");
228     }
229
230     /**
231      * The flag allows the client to receive metadata for all resources of the service regardless of the artifacts
232      * associated to them. Setting the flag to false will preserve legacy behavior.
233      */
234     @Override
235     public boolean isFilterInEmptyResources() {
236         return getBooleanPropertyWithDefault("mso.asdc-connections.asdc-controller1.isFilterInEmptyResources", true);
237     }
238
239 }