Change the header to SO
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / 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.openecomp.mso.asdc.client;
22
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map.Entry;
31 import java.util.Properties;
32
33 import org.openecomp.mso.asdc.client.exceptions.ASDCParametersException;
34 import org.openecomp.mso.properties.MsoJsonProperties;
35 import org.openecomp.mso.properties.MsoPropertiesException;
36 import org.openecomp.mso.properties.MsoPropertiesFactory;
37 import org.openecomp.sdc.api.consumer.IConfiguration;
38
39 import com.fasterxml.jackson.databind.JsonNode;
40
41 public class ASDCConfiguration implements IConfiguration {
42
43     private MsoPropertiesFactory msoPropertiesFactory;
44
45     // SHell command to obtain the same encryption, 128 bits key, key must be HEX
46     // echo -n "This is a test string" | openssl aes-128-ecb -e -K 546573746F736973546573746F736973 -nosalt | xxd
47
48     private String configKey;
49
50     private MsoJsonProperties msoProperties;
51
52     private String asdcControllerName;
53
54     public static final String MSO_PROP_ASDC = "MSO_PROP_ASDC";
55     public static final String PARAMETER_PATTERN = "asdc-connections";
56
57     public static final String CONSUMER_GROUP_ATTRIBUTE_NAME = "consumerGroup";
58     public static final String CONSUMER_ID_ATTRIBUTE_NAME = "consumerId";
59     public static final String ENVIRONMENT_NAME_ATTRIBUTE_NAME = "environmentName";
60     public static final String PASSWORD_ATTRIBUTE_NAME = "password";
61     public static final String POLLING_INTERVAL_ATTRIBUTE_NAME = "pollingInterval";
62     public static final String RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME = "relevantArtifactTypes";
63     public static final String USER_ATTRIBUTE_NAME = "user";
64     public static final String ASDC_ADDRESS_ATTRIBUTE_NAME = "asdcAddress";
65     public static final String POLLING_TIMEOUT_ATTRIBUTE_NAME = "pollingTimeout";
66     public static final String ACTIVATE_SERVER_TLS_AUTH = "activateServerTLSAuth";
67     public static final String KEY_STORE_PASSWORD = "keyStorePassword";
68     public static final String KEY_STORE_PATH = "keyStorePath";
69
70     public static final String HEAT="HEAT";
71     public static final String HEAT_ARTIFACT="HEAT_ARTIFACT";
72     public static final String HEAT_ENV="HEAT_ENV";
73     public static final String HEAT_NESTED="HEAT_NESTED";
74     public static final String HEAT_NET="HEAT_NET";
75     public static final String HEAT_VOL="HEAT_VOL";
76     public static final String OTHER="OTHER";
77     public static final String TOSCA_CSAR="TOSCA_CSAR";
78     public static final String VF_MODULES_METADATA="VF_MODULES_METADATA";
79
80
81     private static final String[] SUPPORTED_ARTIFACT_TYPES = {HEAT,
82                 HEAT_ARTIFACT,
83                 HEAT_ENV,
84                 HEAT_NESTED,
85                 HEAT_NET,
86                 HEAT_VOL,
87                 OTHER,
88                 TOSCA_CSAR,
89                 VF_MODULES_METADATA};
90
91     public static final List<String>  SUPPORTED_ARTIFACT_TYPES_LIST =  Collections.unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));
92
93     /**
94      * Default constructor, the mso.properties is searched in the classpath (for testing)
95      * Or in /etc/ecomp/mso/config/mso.properties
96      *
97      * @param controllerName The controllerName of the config JSON tree
98      * @throws ASDCParametersException in case of issues with the parameters
99      * @throws IOException If the key file has not been loaded properly
100      */
101     public ASDCConfiguration (String controllerName) throws ASDCParametersException, IOException {
102
103         Properties keyProp = new Properties ();
104         this.asdcControllerName = controllerName;
105
106         keyProp.load (Thread.currentThread ().getContextClassLoader ().getResourceAsStream ("config-key.properties"));
107         configKey = (String) keyProp.get ("asdc.config.key");
108
109         // This structure contains static values initialized by servlet initializer
110         this.msoPropertiesFactory = new MsoPropertiesFactory ();
111
112         refreshASDCConfig ();
113
114     }
115     
116     public java.lang.Boolean isUseHttpsWithDmaap() {
117         return false;
118     }
119
120     public String getAsdcControllerName () {
121         return asdcControllerName;
122     }
123
124     private JsonNode getASDCControllerConfigJsonNode () {
125         if (this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
126             return this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN).get (this.asdcControllerName);
127         } else {
128             return null;
129         }
130
131     }
132
133     /**
134      * This method reload the config if needed.
135      *
136      * @return true if config has been reloaded, false otherwise
137      * @throws ASDCParametersException In case the parameters validation fails
138      * @throws MsoPropertiesException
139      */
140     public boolean refreshASDCConfig () throws ASDCParametersException {
141
142         try {
143             if (msoPropertiesFactory.propertiesHaveChanged (MSO_PROP_ASDC, msoProperties)) {
144                 msoProperties = msoPropertiesFactory.getMsoJsonProperties (MSO_PROP_ASDC);
145
146                 this.testAllParameters ();
147                 return true;
148             } else {
149                 return false;
150             }
151         } catch (MsoPropertiesException e) {
152             throw new ASDCParametersException ("mso.asdc.json not initialized properly, ASDC config cannot be reloaded",
153                                                e);
154         }
155     }
156
157     /**
158      * This method is useful to check whether a mso properties config has been changed.
159      *
160      * @return true is a new config is availabe, false otherwise
161      * @throws ASDCParametersException
162      * @throws MsoPropertiesException
163      */
164     public boolean hasASDCConfigChanged () throws ASDCParametersException {
165         try {
166             return msoPropertiesFactory.propertiesHaveChanged (MSO_PROP_ASDC, msoProperties);
167         } catch (MsoPropertiesException e) {
168             throw new ASDCParametersException ("mso.asdc.json not initialized properly, ASDC config cannot be read", e);
169         }
170     }
171
172     @Override
173     public String getConsumerGroup () {
174         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
175         if (masterConfigNode != null && masterConfigNode.get (CONSUMER_GROUP_ATTRIBUTE_NAME) != null) {
176             String config = masterConfigNode.get (CONSUMER_GROUP_ATTRIBUTE_NAME).asText ();
177
178             if ("NULL".equals (config) || config.isEmpty ()) {
179                 return null;
180             } else {
181                 return config;
182             }
183         } else {
184             return null;
185         }
186     }
187
188     @Override
189     public String getConsumerID () {
190
191         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
192         if (masterConfigNode != null && masterConfigNode.get (CONSUMER_ID_ATTRIBUTE_NAME) != null) {
193             String config = masterConfigNode.get (CONSUMER_ID_ATTRIBUTE_NAME).asText ();
194
195             if (config.isEmpty ()) {
196                 return null;
197             } else {
198                 return config;
199             }
200         } else {
201             return null;
202         }
203     }
204
205     @Override
206     public String getEnvironmentName () {
207         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
208         if (masterConfigNode != null && masterConfigNode.get (ENVIRONMENT_NAME_ATTRIBUTE_NAME) != null) {
209             String config = masterConfigNode.get (ENVIRONMENT_NAME_ATTRIBUTE_NAME).asText ();
210
211             if (config.isEmpty ()) {
212                 return null;
213             } else {
214                 return config;
215             }
216         } else {
217             return null;
218         }
219     }
220
221     @Override
222     public String getPassword () {
223         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
224         if (masterConfigNode != null && masterConfigNode.get (PASSWORD_ATTRIBUTE_NAME) != null) {
225             String config = this.msoProperties.getEncryptedProperty (masterConfigNode.get (PASSWORD_ATTRIBUTE_NAME),
226                                                                      null,
227                                                                      this.configKey);
228
229             if (config.isEmpty ()) {
230                 return null;
231             } else {
232                 return config;
233             }
234         } else {
235             return null;
236         }
237     }
238
239     @Override
240     public int getPollingInterval () {
241         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
242         if (masterConfigNode != null && masterConfigNode.get (POLLING_INTERVAL_ATTRIBUTE_NAME) != null) {
243             return masterConfigNode.get (POLLING_INTERVAL_ATTRIBUTE_NAME).asInt ();
244         } else {
245             return 0;
246         }
247     }
248
249     @Override
250     public List <String> getRelevantArtifactTypes () {
251         // DO not return the Static List SUPPORTED_ARTIFACT_TYPES_LIST because the ASDC Client will try to modify it !!!
252         return Arrays.asList(SUPPORTED_ARTIFACT_TYPES);
253     }
254
255     @Override
256     public String getUser () {
257         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
258         if (masterConfigNode != null && masterConfigNode.get (USER_ATTRIBUTE_NAME) != null) {
259             String config = masterConfigNode.get (USER_ATTRIBUTE_NAME).asText ();
260
261             if (config.isEmpty ()) {
262                 return null;
263             } else {
264                 return config;
265             }
266         } else {
267             return null;
268         }
269     }
270
271     @Override
272     public String getAsdcAddress () {
273         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
274         if (masterConfigNode != null && masterConfigNode.get (ASDC_ADDRESS_ATTRIBUTE_NAME) != null) {
275             String config = masterConfigNode.get (ASDC_ADDRESS_ATTRIBUTE_NAME).asText ();
276
277             if (config.isEmpty ()) {
278                 return null;
279             } else {
280                 return config;
281             }
282         } else {
283             return null;
284         }
285     }
286
287     @Override
288     public int getPollingTimeout () {
289         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
290         if (masterConfigNode != null && masterConfigNode.get (POLLING_TIMEOUT_ATTRIBUTE_NAME) != null) {
291             return masterConfigNode.get (POLLING_TIMEOUT_ATTRIBUTE_NAME).asInt ();
292         } else {
293             return 0;
294         }
295     }
296
297         @Override
298         public boolean activateServerTLSAuth() {
299                 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
300                 if (masterConfigNode != null && masterConfigNode.get(ACTIVATE_SERVER_TLS_AUTH) != null) {
301                         return masterConfigNode.get(ACTIVATE_SERVER_TLS_AUTH).asBoolean(false);
302                 } else {
303                         return false;
304                 }
305         }
306
307         @Override
308         public String getKeyStorePassword() {
309                 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
310                 if (masterConfigNode != null && masterConfigNode.get(KEY_STORE_PASSWORD) != null) {
311                         String config = this.msoProperties.getEncryptedProperty(masterConfigNode.get(KEY_STORE_PASSWORD), null,
312                                         this.configKey);
313
314                         if (config.isEmpty()) {
315                                 return null;
316                         } else {
317                                 return config;
318                         }
319                 } else {
320                         return null;
321                 }
322         }
323
324         @Override
325         public String getKeyStorePath() {
326                 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
327                 if (masterConfigNode != null && masterConfigNode.get(KEY_STORE_PATH) != null) {
328                         String config = masterConfigNode.get(KEY_STORE_PATH).asText();
329
330                         if (config.isEmpty()) {
331                                 return null;
332                         } else {
333                                 return config;
334                         }
335                 } else {
336                         return null;
337                 }
338         }
339
340     public void testAllParameters () throws ASDCParametersException {
341
342         // Special case for this attribute that can be null from getConsumerGroup
343         if (this.getConsumerGroup () == null
344             && (getASDCControllerConfigJsonNode () == null
345                 || !"NULL".equals (getASDCControllerConfigJsonNode ().get (CONSUMER_GROUP_ATTRIBUTE_NAME).asText ()))) {
346             throw new ASDCParametersException (CONSUMER_GROUP_ATTRIBUTE_NAME
347                                                + " parameter cannot be found in config mso.properties");
348         }
349
350         if (this.getConsumerID () == null || this.getConsumerID ().isEmpty ()) {
351             throw new ASDCParametersException (CONSUMER_ID_ATTRIBUTE_NAME
352                                                + " parameter cannot be found in config mso.properties");
353         }
354
355         if (this.getEnvironmentName () == null || this.getEnvironmentName ().isEmpty ()) {
356             throw new ASDCParametersException (ENVIRONMENT_NAME_ATTRIBUTE_NAME
357                                                + " parameter cannot be found in config mso.properties");
358         }
359
360         if (this.getAsdcAddress () == null || this.getAsdcAddress ().isEmpty ()) {
361             throw new ASDCParametersException (ASDC_ADDRESS_ATTRIBUTE_NAME
362                                                + " parameter cannot be found in config mso.properties");
363         }
364
365         if (this.getPassword () == null || this.getPassword ().isEmpty ()) {
366             throw new ASDCParametersException (PASSWORD_ATTRIBUTE_NAME
367                                                + " parameter cannot be found in config mso.properties");
368         }
369
370         if (this.getPollingInterval () == 0) {
371             throw new ASDCParametersException (POLLING_INTERVAL_ATTRIBUTE_NAME
372                                                + " parameter cannot be found in config mso.properties");
373         }
374
375         if (this.getPollingTimeout () == 0) {
376             throw new ASDCParametersException (POLLING_TIMEOUT_ATTRIBUTE_NAME
377                                                + " parameter cannot be found in config mso.properties");
378         }
379
380         if (this.getRelevantArtifactTypes () == null || this.getRelevantArtifactTypes ().isEmpty ()) {
381             throw new ASDCParametersException (RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME
382                                                + " parameter cannot be found in config mso.properties");
383         }
384
385         if (this.getUser () == null || this.getUser ().isEmpty ()) {
386             throw new ASDCParametersException (USER_ATTRIBUTE_NAME
387                                                + " parameter cannot be found in config mso.properties");
388         }
389     }
390
391     /**
392      * This method triggers the MsoPropertiesFactory to get the ASDC config from the cache and extracts all controllers
393      * defined.
394      *
395      * @return A list of controller Names defined in the cache config
396      * @throws ASDCParametersException In cas of issues with the cache
397      */
398     public static List <String> getAllDefinedControllers () throws ASDCParametersException {
399
400         MsoJsonProperties msoProp;
401         try {
402             List <String> result = new ArrayList <String> ();
403             msoProp = new MsoPropertiesFactory ().getMsoJsonProperties (MSO_PROP_ASDC);
404
405             if (msoProp.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
406                 Iterator <Entry <String, JsonNode>> it = msoProp.getJsonRootNode ()
407                                                                 .get (PARAMETER_PATTERN)
408                                                                 .fields();
409
410                 Entry <String, JsonNode> entry;
411                 while (it.hasNext ()) {
412                     entry = it.next ();
413                     result.add (entry.getKey ());
414
415                 }
416             }
417             return result;
418         } catch (MsoPropertiesException e) {
419             throw new ASDCParametersException ("Unable to get the JSON Properties in cache:" + MSO_PROP_ASDC, e);
420         }
421
422     }
423
424     /**
425      * The flag allows the client to receive metadata for all resources of the service regardless of the artifacts associated to them.
426      * Setting the flag to false will preserve legacy behavior.
427      */
428     public boolean isFilterInEmptyResources() {
429            return true;
430     }
431
432 }