Initial OpenECOMP MSO commit
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / asdc / client / ASDCConfiguration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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.codehaus.jackson.JsonNode;
34
35 import org.openecomp.sdc.api.consumer.IConfiguration;
36 import org.openecomp.mso.asdc.client.exceptions.ASDCParametersException;
37 import org.openecomp.mso.properties.MsoJsonProperties;
38 import org.openecomp.mso.properties.MsoPropertiesException;
39 import org.openecomp.mso.properties.MsoPropertiesFactory;
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 VF_MODULES_METADATA="VF_MODULES_METADATA";
78     
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                 VF_MODULES_METADATA};   
88     
89     public static final List<String>  SUPPORTED_ARTIFACT_TYPES_LIST =  Collections.unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));   
90     
91     /**
92      * Default constructor, the mso.properties is searched in the classpath (for testing)
93      * Or in /etc/ecomp/mso/config/mso.properties
94      *
95      * @param controllerName The controllerName of the config JSON tree
96      * @throws ASDCParametersException in case of issues with the parameters
97      * @throws IOException If the key file has not been loaded properly
98      */
99     public ASDCConfiguration (String controllerName) throws ASDCParametersException, IOException {
100         
101         Properties keyProp = new Properties ();
102         this.asdcControllerName = controllerName;
103
104         keyProp.load (Thread.currentThread ().getContextClassLoader ().getResourceAsStream ("config-key.properties"));
105         configKey = (String) keyProp.get ("asdc.config.key");
106
107         // This structure contains static values initialized by servlet initializer
108         this.msoPropertiesFactory = new MsoPropertiesFactory ();
109
110         refreshASDCConfig ();
111
112     }
113
114     public String getAsdcControllerName () {
115         return asdcControllerName;
116     }
117
118     private JsonNode getASDCControllerConfigJsonNode () {
119         if (this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
120             return this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN).get (this.asdcControllerName);
121         } else {
122             return null;
123         }
124
125     }
126
127     /**
128      * This method reload the config if needed.
129      *
130      * @return true if config has been reloaded, false otherwise
131      * @throws ASDCParametersException In case the parameters validation fails
132      * @throws MsoPropertiesException
133      */
134     public boolean refreshASDCConfig () throws ASDCParametersException {
135
136         try {
137             if (msoPropertiesFactory.propertiesHaveChanged (MSO_PROP_ASDC, msoProperties)) {
138                 msoProperties = msoPropertiesFactory.getMsoJsonProperties (MSO_PROP_ASDC);
139
140                 this.testAllParameters ();
141                 return true;
142             } else {
143                 return false;
144             }
145         } catch (MsoPropertiesException e) {
146             throw new ASDCParametersException ("mso.asdc.json not initialized properly, ASDC config cannot be reloaded",
147                                                e);
148         }
149     }
150
151     /**
152      * This method is useful to check whether a mso properties config has been changed.
153      *
154      * @return true is a new config is availabe, false otherwise
155      * @throws ASDCParametersException
156      * @throws MsoPropertiesException
157      */
158     public boolean hasASDCConfigChanged () throws ASDCParametersException {
159         try {
160             return msoPropertiesFactory.propertiesHaveChanged (MSO_PROP_ASDC, msoProperties);
161         } catch (MsoPropertiesException e) {
162             throw new ASDCParametersException ("mso.asdc.json not initialized properly, ASDC config cannot be read", e);
163         }
164     }
165
166     @Override
167     public String getConsumerGroup () {
168         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
169         if (masterConfigNode != null && masterConfigNode.get (CONSUMER_GROUP_ATTRIBUTE_NAME) != null) {
170             String config = masterConfigNode.get (CONSUMER_GROUP_ATTRIBUTE_NAME).asText ();
171
172             if ("NULL".equals (config) || config.isEmpty ()) {
173                 return null;
174             } else {
175                 return config;
176             }
177         } else {
178             return null;
179         }
180     }
181
182     @Override
183     public String getConsumerID () {
184
185         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
186         if (masterConfigNode != null && masterConfigNode.get (CONSUMER_ID_ATTRIBUTE_NAME) != null) {
187             String config = masterConfigNode.get (CONSUMER_ID_ATTRIBUTE_NAME).asText ();
188
189             if (config.isEmpty ()) {
190                 return null;
191             } else {
192                 return config;
193             }
194         } else {
195             return null;
196         }
197     }
198
199     @Override
200     public String getEnvironmentName () {
201         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
202         if (masterConfigNode != null && masterConfigNode.get (ENVIRONMENT_NAME_ATTRIBUTE_NAME) != null) {
203             String config = masterConfigNode.get (ENVIRONMENT_NAME_ATTRIBUTE_NAME).asText ();
204
205             if (config.isEmpty ()) {
206                 return null;
207             } else {
208                 return config;
209             }
210         } else {
211             return null;
212         }
213     }
214
215     @Override
216     public String getPassword () {
217         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
218         if (masterConfigNode != null && masterConfigNode.get (PASSWORD_ATTRIBUTE_NAME) != null) {
219             String config = this.msoProperties.getEncryptedProperty (masterConfigNode.get (PASSWORD_ATTRIBUTE_NAME),
220                                                                      null,
221                                                                      this.configKey);
222
223             if (config.isEmpty ()) {
224                 return null;
225             } else {
226                 return config;
227             }
228         } else {
229             return null;
230         }
231     }
232
233     @Override
234     public int getPollingInterval () {
235         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
236         if (masterConfigNode != null && masterConfigNode.get (POLLING_INTERVAL_ATTRIBUTE_NAME) != null) {
237             return masterConfigNode.get (POLLING_INTERVAL_ATTRIBUTE_NAME).asInt ();
238         } else {
239             return 0;
240         }
241     }
242
243     @Override
244     public List <String> getRelevantArtifactTypes () {
245         // DO not return the Static List SUPPORTED_ARTIFACT_TYPES_LIST because the ASDC Client will try to modify it !!!
246         return Arrays.asList(SUPPORTED_ARTIFACT_TYPES);
247     }
248
249     @Override
250     public String getUser () {
251         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
252         if (masterConfigNode != null && masterConfigNode.get (USER_ATTRIBUTE_NAME) != null) {
253             String config = masterConfigNode.get (USER_ATTRIBUTE_NAME).asText ();
254
255             if (config.isEmpty ()) {
256                 return null;
257             } else {
258                 return config;
259             }
260         } else {
261             return null;
262         }
263     }
264
265     @Override
266     public String getAsdcAddress () {
267         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
268         if (masterConfigNode != null && masterConfigNode.get (ASDC_ADDRESS_ATTRIBUTE_NAME) != null) {
269             String config = masterConfigNode.get (ASDC_ADDRESS_ATTRIBUTE_NAME).asText ();
270
271             if (config.isEmpty ()) {
272                 return null;
273             } else {
274                 return config;
275             }
276         } else {
277             return null;
278         }
279     }
280
281     @Override
282     public int getPollingTimeout () {
283         JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
284         if (masterConfigNode != null && masterConfigNode.get (POLLING_TIMEOUT_ATTRIBUTE_NAME) != null) {
285             return masterConfigNode.get (POLLING_TIMEOUT_ATTRIBUTE_NAME).asInt ();
286         } else {
287             return 0;
288         }
289     }
290     
291         @Override
292         public boolean activateServerTLSAuth() {
293                 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
294                 if (masterConfigNode != null && masterConfigNode.get(ACTIVATE_SERVER_TLS_AUTH) != null) {
295                         return masterConfigNode.get(ACTIVATE_SERVER_TLS_AUTH).asBoolean(false);
296                 } else {
297                         return false;
298                 }
299         }
300
301         @Override
302         public String getKeyStorePassword() {
303                 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
304                 if (masterConfigNode != null && masterConfigNode.get(KEY_STORE_PASSWORD) != null) {
305                         String config = this.msoProperties.getEncryptedProperty(masterConfigNode.get(KEY_STORE_PASSWORD), null,
306                                         this.configKey);
307
308                         if (config.isEmpty()) {
309                                 return null;
310                         } else {
311                                 return config;
312                         }
313                 } else {
314                         return null;
315                 }
316         }
317
318         @Override
319         public String getKeyStorePath() {
320                 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
321                 if (masterConfigNode != null && masterConfigNode.get(KEY_STORE_PATH) != null) {
322                         String config = masterConfigNode.get(KEY_STORE_PATH).asText();
323
324                         if (config.isEmpty()) {
325                                 return null;
326                         } else {
327                                 return config;
328                         }
329                 } else {
330                         return null;
331                 }
332         }
333
334     public void testAllParameters () throws ASDCParametersException {
335
336         // Special case for this attribute that can be null from getConsumerGroup
337         if (this.getConsumerGroup () == null
338             && (getASDCControllerConfigJsonNode () == null
339                 || !"NULL".equals (getASDCControllerConfigJsonNode ().get (CONSUMER_GROUP_ATTRIBUTE_NAME).asText ()))) {
340             throw new ASDCParametersException (CONSUMER_GROUP_ATTRIBUTE_NAME
341                                                + " parameter cannot be found in config mso.properties");
342         }
343
344         if (this.getConsumerID () == null || this.getConsumerID ().isEmpty ()) {
345             throw new ASDCParametersException (CONSUMER_ID_ATTRIBUTE_NAME
346                                                + " parameter cannot be found in config mso.properties");
347         }
348
349         if (this.getEnvironmentName () == null || this.getEnvironmentName ().isEmpty ()) {
350             throw new ASDCParametersException (ENVIRONMENT_NAME_ATTRIBUTE_NAME
351                                                + " parameter cannot be found in config mso.properties");
352         }
353
354         if (this.getAsdcAddress () == null || this.getAsdcAddress ().isEmpty ()) {
355             throw new ASDCParametersException (ASDC_ADDRESS_ATTRIBUTE_NAME
356                                                + " parameter cannot be found in config mso.properties");
357         }
358
359         if (this.getPassword () == null || this.getPassword ().isEmpty ()) {
360             throw new ASDCParametersException (PASSWORD_ATTRIBUTE_NAME
361                                                + " parameter cannot be found in config mso.properties");
362         }
363
364         if (this.getPollingInterval () == 0) {
365             throw new ASDCParametersException (POLLING_INTERVAL_ATTRIBUTE_NAME
366                                                + " parameter cannot be found in config mso.properties");
367         }
368
369         if (this.getPollingTimeout () == 0) {
370             throw new ASDCParametersException (POLLING_TIMEOUT_ATTRIBUTE_NAME
371                                                + " parameter cannot be found in config mso.properties");
372         }
373
374         if (this.getRelevantArtifactTypes () == null || this.getRelevantArtifactTypes ().isEmpty ()) {
375             throw new ASDCParametersException (RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME
376                                                + " parameter cannot be found in config mso.properties");
377         }
378
379         if (this.getUser () == null || this.getUser ().isEmpty ()) {
380             throw new ASDCParametersException (USER_ATTRIBUTE_NAME
381                                                + " parameter cannot be found in config mso.properties");
382         }
383     }
384
385     /**
386      * This method triggers the MsoPropertiesFactory to get the ASDC config from the cache and extracts all controllers
387      * defined.
388      *
389      * @return A list of controller Names defined in the cache config
390      * @throws ASDCParametersException In cas of issues with the cache
391      */
392     public static List <String> getAllDefinedControllers () throws ASDCParametersException {
393
394         MsoJsonProperties msoProp;
395         try {
396             List <String> result = new ArrayList <String> ();
397             msoProp = new MsoPropertiesFactory ().getMsoJsonProperties (MSO_PROP_ASDC);
398
399             if (msoProp.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
400                 Iterator <Entry <String, JsonNode>> it = msoProp.getJsonRootNode ()
401                                                                 .get (PARAMETER_PATTERN)
402                                                                 .getFields ();
403
404                 Entry <String, JsonNode> entry;
405                 while (it.hasNext ()) {
406                     entry = it.next ();
407                     result.add (entry.getKey ());
408
409                 }
410             }
411             return result;
412         } catch (MsoPropertiesException e) {
413             throw new ASDCParametersException ("Unable to get the JSON Properties in cache:" + MSO_PROP_ASDC, e);
414         }
415
416     }
417
418 }