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