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