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.properties.MsoJsonProperties;
35 import org.openecomp.mso.properties.MsoPropertiesException;
36 import org.openecomp.mso.properties.MsoPropertiesFactory;
37 import org.openecomp.sdc.api.consumer.IConfiguration;
39 import com.fasterxml.jackson.databind.JsonNode;
41 public class ASDCConfiguration implements IConfiguration {
43 private MsoPropertiesFactory msoPropertiesFactory;
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
48 private String configKey;
50 private MsoJsonProperties msoProperties;
52 private String asdcControllerName;
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";
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";
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";
84 private static final String[] SUPPORTED_ARTIFACT_TYPES = {HEAT,
94 public static final List<String> SUPPORTED_ARTIFACT_TYPES_LIST = Collections.unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));
97 * Default constructor, the mso.properties is searched in the classpath (for testing)
98 * Or in /etc/ecomp/mso/config/mso.properties
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
104 public ASDCConfiguration (String controllerName) throws ASDCParametersException, IOException {
106 Properties keyProp = new Properties ();
107 this.asdcControllerName = controllerName;
109 keyProp.load (Thread.currentThread ().getContextClassLoader ().getResourceAsStream ("config-key.properties"));
110 configKey = (String) keyProp.get ("asdc.config.key");
112 // This structure contains static values initialized by servlet initializer
113 this.msoPropertiesFactory = new MsoPropertiesFactory ();
115 refreshASDCConfig ();
120 public java.lang.Boolean isUseHttpsWithDmaap() {
125 public boolean isConsumeProduceStatusTopic(){
130 public List<String> getMsgBusAddress(){
132 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
133 if (masterConfigNode != null && masterConfigNode.get (MSG_BUS_ADDRESS_ATTRIBUTE_NAME) != null) {
134 List<String> msgAddressList = new ArrayList<>();
136 Iterator<JsonNode> config = masterConfigNode.get(MSG_BUS_ADDRESS_ATTRIBUTE_NAME).elements();
138 while( config.hasNext() ) {
139 String key = (String)config.next().asText();
140 msgAddressList.add(key);
143 if ("NULL".equals (msgAddressList) || msgAddressList.isEmpty ()) {
146 return msgAddressList;
155 public String getAsdcControllerName () {
156 return asdcControllerName;
159 private JsonNode getASDCControllerConfigJsonNode () {
160 if (this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
161 return this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN).get (this.asdcControllerName);
169 * This method reload the config if needed.
171 * @return true if config has been reloaded, false otherwise
172 * @throws ASDCParametersException In case the parameters validation fails
173 * @throws MsoPropertiesException
175 public boolean refreshASDCConfig () throws ASDCParametersException {
178 if (msoPropertiesFactory.propertiesHaveChanged (MSO_PROP_ASDC, msoProperties)) {
179 msoProperties = msoPropertiesFactory.getMsoJsonProperties (MSO_PROP_ASDC);
181 this.testAllParameters ();
186 } catch (MsoPropertiesException e) {
187 throw new ASDCParametersException ("mso.asdc.json not initialized properly, ASDC config cannot be reloaded",
193 * This method is useful to check whether a mso properties config has been changed.
195 * @return true is a new config is availabe, false otherwise
196 * @throws ASDCParametersException
197 * @throws MsoPropertiesException
199 public boolean hasASDCConfigChanged () throws ASDCParametersException {
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);
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 ();
213 if ("NULL".equals (config) || config.isEmpty ()) {
223 public int getWatchDogTimeout () {
224 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
225 if (masterConfigNode != null && masterConfigNode.get (WATCHDOG_TIMEOUT_NAME) != null) {
227 return masterConfigNode.get (WATCHDOG_TIMEOUT_NAME).asInt ();
234 public String getConsumerID () {
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 ();
240 if (config.isEmpty ()) {
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 ();
256 if (config.isEmpty ()) {
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),
274 if (config.isEmpty ()) {
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 ();
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);
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 ();
306 if (config.isEmpty ()) {
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 ();
322 if (config.isEmpty ()) {
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 ();
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);
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,
359 if (config.isEmpty()) {
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();
375 if (config.isEmpty()) {
385 public void testAllParameters () throws ASDCParametersException {
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");
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");
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");
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");
410 if (this.getPassword () == null || this.getPassword ().isEmpty ()) {
411 throw new ASDCParametersException (PASSWORD_ATTRIBUTE_NAME
412 + " parameter cannot be found in config mso.properties");
415 if (this.getPollingInterval () == 0) {
416 throw new ASDCParametersException (POLLING_INTERVAL_ATTRIBUTE_NAME
417 + " parameter cannot be found in config mso.properties");
420 if (this.getPollingTimeout () == 0) {
421 throw new ASDCParametersException (POLLING_TIMEOUT_ATTRIBUTE_NAME
422 + " parameter cannot be found in config mso.properties");
425 if (this.getWatchDogTimeout() == 0) {
426 throw new ASDCParametersException (WATCHDOG_TIMEOUT_NAME
427 + " parameter cannot be found in config mso.properties");
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");
435 if (this.getUser () == null || this.getUser ().isEmpty ()) {
436 throw new ASDCParametersException (USER_ATTRIBUTE_NAME
437 + " parameter cannot be found in config mso.properties");
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");
448 * This method triggers the MsoPropertiesFactory to get the ASDC config from the cache and extracts all controllers
451 * @return A list of controller Names defined in the cache config
452 * @throws ASDCParametersException In cas of issues with the cache
454 public static List <String> getAllDefinedControllers () throws ASDCParametersException {
456 MsoJsonProperties msoProp;
458 List <String> result = new ArrayList<>();
459 msoProp = new MsoPropertiesFactory ().getMsoJsonProperties (MSO_PROP_ASDC);
461 if (msoProp.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
462 Iterator <Entry <String, JsonNode>> it = msoProp.getJsonRootNode ()
463 .get (PARAMETER_PATTERN)
466 Entry <String, JsonNode> entry;
467 while (it.hasNext ()) {
469 result.add (entry.getKey ());
474 } catch (MsoPropertiesException e) {
475 throw new ASDCParametersException ("Unable to get the JSON Properties in cache:" + MSO_PROP_ASDC, e);
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.
484 public boolean isFilterInEmptyResources() {