2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.asdc.client;
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;
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;
40 import com.fasterxml.jackson.databind.JsonNode;
42 public class ASDCConfiguration implements IConfiguration {
44 private MsoPropertiesFactory msoPropertiesFactory;
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
49 private String configKey;
51 private MsoJsonProperties msoProperties;
53 private String asdcControllerName;
54 private String PASSWORD_ATTRIBUTE_NAME;
55 private String KEY_STORE_PASSWORD;
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";
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";
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";
84 private static MsoLogger msoLogger = MsoLogger.getMsoLogger (MsoLogger.Catalog.GENERAL);
86 private static final String[] SUPPORTED_ARTIFACT_TYPES = {HEAT,
96 public static final List<String> SUPPORTED_ARTIFACT_TYPES_LIST = Collections.unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));
99 * Default constructor, the mso.properties is searched in the classpath (for testing)
100 * Or in /etc/ecomp/mso/config/mso.properties
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
106 public ASDCConfiguration (String controllerName) throws ASDCParametersException, IOException {
108 Properties keyProp = new Properties ();
109 this.asdcControllerName = controllerName;
111 keyProp.load (Thread.currentThread ().getContextClassLoader ().getResourceAsStream ("config-key.properties"));
112 configKey = (String) keyProp.get ("asdc.config.key");
114 // This structure contains static values initialized by servlet initializer
115 this.msoPropertiesFactory = new MsoPropertiesFactory ();
117 refreshASDCConfig ();
122 public java.lang.Boolean isUseHttpsWithDmaap() {
127 public boolean isConsumeProduceStatusTopic(){
132 public List<String> getMsgBusAddress(){
134 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
135 if (masterConfigNode != null && masterConfigNode.get (MSG_BUS_ADDRESS_ATTRIBUTE_NAME) != null) {
136 List<String> msgAddressList = new ArrayList<>();
138 Iterator<JsonNode> config = masterConfigNode.get(MSG_BUS_ADDRESS_ATTRIBUTE_NAME).elements();
140 while( config.hasNext() ) {
141 String key = (String)config.next().asText();
142 msgAddressList.add(key);
145 if ("NULL".equals (msgAddressList) || msgAddressList.isEmpty ()) {
148 return msgAddressList;
157 public String getAsdcControllerName () {
158 return asdcControllerName;
161 private JsonNode getASDCControllerConfigJsonNode () {
162 if (this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
163 return this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN).get (this.asdcControllerName);
171 * This method reload the config if needed.
173 * @return true if config has been reloaded, false otherwise
174 * @throws ASDCParametersException In case the parameters validation fails
175 * @throws MsoPropertiesException
177 public boolean refreshASDCConfig () throws ASDCParametersException {
180 if (msoPropertiesFactory.propertiesHaveChanged (MSO_PROP_ASDC, msoProperties)) {
181 msoProperties = msoPropertiesFactory.getMsoJsonProperties (MSO_PROP_ASDC);
183 this.testAllParameters ();
188 } catch (MsoPropertiesException e) {
189 throw new ASDCParametersException ("mso.asdc.json not initialized properly, ASDC config cannot be reloaded",
195 * This method is useful to check whether a mso properties config has been changed.
197 * @return true is a new config is availabe, false otherwise
198 * @throws ASDCParametersException
199 * @throws MsoPropertiesException
201 public boolean hasASDCConfigChanged () throws ASDCParametersException {
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);
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 ();
215 if ("NULL".equals (config) || config.isEmpty ()) {
225 public int getWatchDogTimeout () {
226 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
227 if (masterConfigNode != null && masterConfigNode.get (WATCHDOG_TIMEOUT_NAME) != null) {
229 return masterConfigNode.get (WATCHDOG_TIMEOUT_NAME).asInt ();
236 public String getConsumerID () {
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 ();
242 if (config.isEmpty ()) {
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 ();
258 if (config.isEmpty ()) {
269 public String getPassword () {
270 Properties keyProp = new Properties ();
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),
280 if (config.isEmpty ()) {
288 } catch (IOException e) {
289 msoLogger.debug("IOException occured", e);
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 ();
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);
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 ();
316 if (config.isEmpty ()) {
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 ();
332 if (config.isEmpty ()) {
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 ();
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);
363 public String getKeyStorePassword() {
364 Properties keyProp = new Properties ();
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,
373 if (config.isEmpty()) {
381 } catch (IOException e) {
382 msoLogger.debug("IOException occured", e);
388 public String getKeyStorePath() {
390 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
391 if (masterConfigNode != null && masterConfigNode.get(KEY_STORE_PATH) != null) {
392 String config = masterConfigNode.get(KEY_STORE_PATH).asText();
394 if (config.isEmpty()) {
405 public void testAllParameters () throws ASDCParametersException {
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");
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");
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");
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");
430 if (this.getPassword () == null || this.getPassword ().isEmpty ()) {
431 throw new ASDCParametersException (PASSWORD_ATTRIBUTE_NAME
432 + " parameter cannot be found in config mso.properties");
435 if (this.getPollingInterval () == 0) {
436 throw new ASDCParametersException (POLLING_INTERVAL_ATTRIBUTE_NAME
437 + " parameter cannot be found in config mso.properties");
440 if (this.getPollingTimeout () == 0) {
441 throw new ASDCParametersException (POLLING_TIMEOUT_ATTRIBUTE_NAME
442 + " parameter cannot be found in config mso.properties");
445 if (this.getWatchDogTimeout() == 0) {
446 throw new ASDCParametersException (WATCHDOG_TIMEOUT_NAME
447 + " parameter cannot be found in config mso.properties");
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");
455 if (this.getUser () == null || this.getUser ().isEmpty ()) {
456 throw new ASDCParametersException (USER_ATTRIBUTE_NAME
457 + " parameter cannot be found in config mso.properties");
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");
468 * This method triggers the MsoPropertiesFactory to get the ASDC config from the cache and extracts all controllers
471 * @return A list of controller Names defined in the cache config
472 * @throws ASDCParametersException In cas of issues with the cache
474 public static List <String> getAllDefinedControllers () throws ASDCParametersException {
476 MsoJsonProperties msoProp;
478 List <String> result = new ArrayList<>();
479 msoProp = new MsoPropertiesFactory ().getMsoJsonProperties (MSO_PROP_ASDC);
481 if (msoProp.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
482 Iterator <Entry <String, JsonNode>> it = msoProp.getJsonRootNode ()
483 .get (PARAMETER_PATTERN)
486 Entry <String, JsonNode> entry;
487 while (it.hasNext ()) {
489 result.add (entry.getKey ());
494 } catch (MsoPropertiesException e) {
495 throw new ASDCParametersException ("Unable to get the JSON Properties in cache:" + MSO_PROP_ASDC, e);
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.
504 public boolean isFilterInEmptyResources() {