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