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