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