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