Containerization feature of SO
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.asdc.client;
22
23
24 import java.security.GeneralSecurityException;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28
29 import org.onap.sdc.api.consumer.IConfiguration;
30 import org.onap.so.logger.MsoLogger;
31 import org.onap.so.utils.CryptoUtils;
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 -nosalt | xxd
42
43    
44
45     private String asdcControllerName;
46
47     public static final String MSO_PROP_ASDC = "MSO_PROP_ASDC";
48     public static final String PARAMETER_PATTERN = "asdc-connections";
49     public static final String MSG_BUS_ADDRESS_ATTRIBUTE_NAME = "messageBusAddress";
50     public static final String WATCHDOG_TIMEOUT_NAME = "watchDogTimeout";
51
52     public static final String CONSUMER_GROUP_ATTRIBUTE_NAME = "consumerGroup";
53     public static final String CONSUMER_ID_ATTRIBUTE_NAME = "consumerId";
54     public static final String ENVIRONMENT_NAME_ATTRIBUTE_NAME = "environmentName";
55     public static final String PASSWORD_ATTRIBUTE_NAME = "password";
56     public static final String POLLING_INTERVAL_ATTRIBUTE_NAME = "pollingInterval";
57     public static final String RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME = "relevantArtifactTypes";
58     public static final String USER_ATTRIBUTE_NAME = "user";
59     public static final String ASDC_ADDRESS_ATTRIBUTE_NAME = "asdcAddress";
60     public static final String POLLING_TIMEOUT_ATTRIBUTE_NAME = "pollingTimeout";
61     public static final String ACTIVATE_SERVER_TLS_AUTH = "activateServerTLSAuth";
62     public static final String KEY_STORE_PASSWORD = "keyStorePassword";
63     public static final String KEY_STORE_PATH = "keyStorePath";
64
65     public static final String HEAT="HEAT";
66     public static final String HEAT_ARTIFACT="HEAT_ARTIFACT";
67     public static final String HEAT_ENV="HEAT_ENV";
68     public static final String HEAT_NESTED="HEAT_NESTED";
69     public static final String HEAT_NET="HEAT_NET";
70     public static final String HEAT_VOL="HEAT_VOL";
71     public static final String OTHER="OTHER";
72     public static final String TOSCA_CSAR="TOSCA_CSAR";
73     public static final String VF_MODULES_METADATA="VF_MODULES_METADATA";
74     private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.ASDC,ASDCConfiguration.class);
75     
76    
77
78     private static final String[] SUPPORTED_ARTIFACT_TYPES = {HEAT,
79                 HEAT_ARTIFACT,
80                 HEAT_ENV,
81                 HEAT_NESTED,
82                 HEAT_NET,
83                 HEAT_VOL,
84                 OTHER,
85                 TOSCA_CSAR,
86                 VF_MODULES_METADATA};
87
88     public static final List<String>  SUPPORTED_ARTIFACT_TYPES_LIST =  Collections.unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));
89
90     @Autowired
91     private Environment env;
92      
93     @Value("${mso.asdc.config.key}")
94     private String configKey;
95     
96     @Value("${mso.asdc-connections.asdc-controller1.messageBusAddress}")    
97     private String[] messageBusAddress;
98     
99     
100     public void setAsdcControllerName(String asdcControllerName) {
101                 this.asdcControllerName = asdcControllerName;
102         }
103
104
105         @Override
106     public java.lang.Boolean isUseHttpsWithDmaap() {
107         return false;
108     }
109     
110     @Override
111     public boolean isConsumeProduceStatusTopic(){
112         return true;
113     }
114     
115     @Override
116     public List<String> getMsgBusAddress(){      
117         if (messageBusAddress.length > 0) {
118            return Arrays.asList(messageBusAddress);
119         } else {
120             return Collections.emptyList();
121         } 
122         
123         
124     }
125   
126     public String getAsdcControllerName () {
127         return asdcControllerName;
128     }
129
130
131         @Override
132         public String getConsumerGroup() {
133                 return getPropertyOrNull("mso.asdc-connections.asdc-controller1.consumerGroup");
134         }
135     
136     public int getWatchDogTimeout () {
137         return getIntegerPropertyOrZero("mso.asdc-connections.asdc-controller1.watchDogTimeout");     
138                 
139     }
140
141     @Override
142     public String getConsumerID () {
143         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.consumerId");
144     }
145     
146     public int getIntegerPropertyOrZero (String propertyName) {
147         String property = env.getProperty(propertyName);
148                 if (property == null || "NULL".equals(property) || property.isEmpty()) {
149                         return 0;
150                 } else {
151                         try {
152                                 return Integer.parseInt(property);
153                         } catch (NumberFormatException e) {                             
154                                 return 0;
155                         }
156                 }
157     }
158     
159     public String getPropertyOrNull (String propertyName) {
160         String config = env.getProperty(propertyName);
161                 if (config==null || "NULL".equals(config) || config.isEmpty()) {
162                         return null;
163                 } else {
164                         return config;
165                 }
166     }
167     
168     public String getEncryptedPropertyOrNull (String propertyName) {
169         String decryptedKey;
170         String config = env.getProperty(propertyName);
171         
172         if (config==null || "NULL".equals(config) || config.isEmpty()) {
173                         return null;
174                 } 
175
176                 try {
177                                 decryptedKey = CryptoUtils.decrypt(config, this.configKey);
178                         } catch (GeneralSecurityException e) {
179                                 msoLogger.debug("Exception while decrypting property: " + propertyName, e);
180                                 return null;
181                         }
182                         
183                         if (decryptedKey.isEmpty ()) {
184                 return null;
185             } else {
186                 return decryptedKey;
187             }
188         }
189     
190     public boolean getBooleanPropertyWithDefault (String propertyName, boolean defaultValue) {
191         String config = env.getProperty(propertyName);
192                 if (config == null || "NULL".equals(config) || config.isEmpty()) {
193                         return defaultValue;
194                 } else {
195                         try {
196                                 return Boolean.valueOf(config);
197                         } catch (Exception e) {                 
198                                 return defaultValue;
199                         }
200                 }
201     }
202
203     @Override
204     public String getEnvironmentName () {
205         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.environmentName");
206     }
207
208     @Override
209     public String getPassword () {
210         return getEncryptedPropertyOrNull("mso.asdc-connections.asdc-controller1.password");
211     }
212
213     @Override
214     public int getPollingInterval () {
215         return getIntegerPropertyOrZero("mso.asdc-connections.asdc-controller1.pollingInterval");
216     }
217
218     @Override
219     public List <String> getRelevantArtifactTypes () {
220         // DO not return the Static List SUPPORTED_ARTIFACT_TYPES_LIST because the ASDC Client will try to modify it !!!
221         return Arrays.asList(SUPPORTED_ARTIFACT_TYPES);
222     }
223
224     @Override
225     public String getUser () {
226         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.user");
227     }
228
229     @Override
230     public String getAsdcAddress () {
231         return getPropertyOrNull("mso.asdc-connections.asdc-controller1.asdcAddress");
232     }
233
234     @Override
235     public int getPollingTimeout () {
236         return getIntegerPropertyOrZero("mso.asdc-connections.asdc-controller1.pollingTimeout");
237     }
238
239         @Override
240         public boolean activateServerTLSAuth() {
241                 return getBooleanPropertyWithDefault("mso.asdc-connections.asdc-controller1.activateServerTLSAuth", true);
242         }
243
244         @Override
245         public String getKeyStorePassword() {
246                 return getPropertyOrNull("mso.asdc-connections.asdc-controller1.keyStorePassword");
247         }
248
249         @Override
250         public String getKeyStorePath() {
251                 return getPropertyOrNull("mso.asdc-connections.asdc-controller1.keyStorePath");
252         }
253
254     /**
255      * The flag allows the client to receive metadata for all resources of the service regardless of the artifacts associated to them.
256      * Setting the flag to false will preserve legacy behavior.
257      */
258         @Override
259     public boolean isFilterInEmptyResources() {
260                 return getBooleanPropertyWithDefault("mso.asdc-connections.asdc-controller1.isFilterInEmptyResources", true);
261     }
262
263 }