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";
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";
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 TOSCA_CSAR="TOSCA_CSAR";
78 public static final String VF_MODULES_METADATA="VF_MODULES_METADATA";
81 private static final String[] SUPPORTED_ARTIFACT_TYPES = {HEAT,
91 public static final List<String> SUPPORTED_ARTIFACT_TYPES_LIST = Collections.unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));
94 * Default constructor, the mso.properties is searched in the classpath (for testing)
95 * Or in /etc/ecomp/mso/config/mso.properties
97 * @param controllerName The controllerName of the config JSON tree
98 * @throws ASDCParametersException in case of issues with the parameters
99 * @throws IOException If the key file has not been loaded properly
101 public ASDCConfiguration (String controllerName) throws ASDCParametersException, IOException {
103 Properties keyProp = new Properties ();
104 this.asdcControllerName = controllerName;
106 keyProp.load (Thread.currentThread ().getContextClassLoader ().getResourceAsStream ("config-key.properties"));
107 configKey = (String) keyProp.get ("asdc.config.key");
109 // This structure contains static values initialized by servlet initializer
110 this.msoPropertiesFactory = new MsoPropertiesFactory ();
112 refreshASDCConfig ();
116 public java.lang.Boolean isUseHttpsWithDmaap() {
120 public String getAsdcControllerName () {
121 return asdcControllerName;
124 private JsonNode getASDCControllerConfigJsonNode () {
125 if (this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
126 return this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN).get (this.asdcControllerName);
134 * This method reload the config if needed.
136 * @return true if config has been reloaded, false otherwise
137 * @throws ASDCParametersException In case the parameters validation fails
138 * @throws MsoPropertiesException
140 public boolean refreshASDCConfig () throws ASDCParametersException {
143 if (msoPropertiesFactory.propertiesHaveChanged (MSO_PROP_ASDC, msoProperties)) {
144 msoProperties = msoPropertiesFactory.getMsoJsonProperties (MSO_PROP_ASDC);
146 this.testAllParameters ();
151 } catch (MsoPropertiesException e) {
152 throw new ASDCParametersException ("mso.asdc.json not initialized properly, ASDC config cannot be reloaded",
158 * This method is useful to check whether a mso properties config has been changed.
160 * @return true is a new config is availabe, false otherwise
161 * @throws ASDCParametersException
162 * @throws MsoPropertiesException
164 public boolean hasASDCConfigChanged () throws ASDCParametersException {
166 return msoPropertiesFactory.propertiesHaveChanged (MSO_PROP_ASDC, msoProperties);
167 } catch (MsoPropertiesException e) {
168 throw new ASDCParametersException ("mso.asdc.json not initialized properly, ASDC config cannot be read", e);
173 public String getConsumerGroup () {
174 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
175 if (masterConfigNode != null && masterConfigNode.get (CONSUMER_GROUP_ATTRIBUTE_NAME) != null) {
176 String config = masterConfigNode.get (CONSUMER_GROUP_ATTRIBUTE_NAME).asText ();
178 if ("NULL".equals (config) || config.isEmpty ()) {
189 public String getConsumerID () {
191 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
192 if (masterConfigNode != null && masterConfigNode.get (CONSUMER_ID_ATTRIBUTE_NAME) != null) {
193 String config = masterConfigNode.get (CONSUMER_ID_ATTRIBUTE_NAME).asText ();
195 if (config.isEmpty ()) {
206 public String getEnvironmentName () {
207 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
208 if (masterConfigNode != null && masterConfigNode.get (ENVIRONMENT_NAME_ATTRIBUTE_NAME) != null) {
209 String config = masterConfigNode.get (ENVIRONMENT_NAME_ATTRIBUTE_NAME).asText ();
211 if (config.isEmpty ()) {
222 public String getPassword () {
223 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
224 if (masterConfigNode != null && masterConfigNode.get (PASSWORD_ATTRIBUTE_NAME) != null) {
225 String config = this.msoProperties.getEncryptedProperty (masterConfigNode.get (PASSWORD_ATTRIBUTE_NAME),
229 if (config.isEmpty ()) {
240 public int getPollingInterval () {
241 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
242 if (masterConfigNode != null && masterConfigNode.get (POLLING_INTERVAL_ATTRIBUTE_NAME) != null) {
243 return masterConfigNode.get (POLLING_INTERVAL_ATTRIBUTE_NAME).asInt ();
250 public List <String> getRelevantArtifactTypes () {
251 // DO not return the Static List SUPPORTED_ARTIFACT_TYPES_LIST because the ASDC Client will try to modify it !!!
252 return Arrays.asList(SUPPORTED_ARTIFACT_TYPES);
256 public String getUser () {
257 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
258 if (masterConfigNode != null && masterConfigNode.get (USER_ATTRIBUTE_NAME) != null) {
259 String config = masterConfigNode.get (USER_ATTRIBUTE_NAME).asText ();
261 if (config.isEmpty ()) {
272 public String getAsdcAddress () {
273 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
274 if (masterConfigNode != null && masterConfigNode.get (ASDC_ADDRESS_ATTRIBUTE_NAME) != null) {
275 String config = masterConfigNode.get (ASDC_ADDRESS_ATTRIBUTE_NAME).asText ();
277 if (config.isEmpty ()) {
288 public int getPollingTimeout () {
289 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
290 if (masterConfigNode != null && masterConfigNode.get (POLLING_TIMEOUT_ATTRIBUTE_NAME) != null) {
291 return masterConfigNode.get (POLLING_TIMEOUT_ATTRIBUTE_NAME).asInt ();
298 public boolean activateServerTLSAuth() {
299 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
300 if (masterConfigNode != null && masterConfigNode.get(ACTIVATE_SERVER_TLS_AUTH) != null) {
301 return masterConfigNode.get(ACTIVATE_SERVER_TLS_AUTH).asBoolean(false);
308 public String getKeyStorePassword() {
309 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
310 if (masterConfigNode != null && masterConfigNode.get(KEY_STORE_PASSWORD) != null) {
311 String config = this.msoProperties.getEncryptedProperty(masterConfigNode.get(KEY_STORE_PASSWORD), null,
314 if (config.isEmpty()) {
325 public String getKeyStorePath() {
326 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
327 if (masterConfigNode != null && masterConfigNode.get(KEY_STORE_PATH) != null) {
328 String config = masterConfigNode.get(KEY_STORE_PATH).asText();
330 if (config.isEmpty()) {
340 public void testAllParameters () throws ASDCParametersException {
342 // Special case for this attribute that can be null from getConsumerGroup
343 if (this.getConsumerGroup () == null
344 && (getASDCControllerConfigJsonNode () == null
345 || !"NULL".equals (getASDCControllerConfigJsonNode ().get (CONSUMER_GROUP_ATTRIBUTE_NAME).asText ()))) {
346 throw new ASDCParametersException (CONSUMER_GROUP_ATTRIBUTE_NAME
347 + " parameter cannot be found in config mso.properties");
350 if (this.getConsumerID () == null || this.getConsumerID ().isEmpty ()) {
351 throw new ASDCParametersException (CONSUMER_ID_ATTRIBUTE_NAME
352 + " parameter cannot be found in config mso.properties");
355 if (this.getEnvironmentName () == null || this.getEnvironmentName ().isEmpty ()) {
356 throw new ASDCParametersException (ENVIRONMENT_NAME_ATTRIBUTE_NAME
357 + " parameter cannot be found in config mso.properties");
360 if (this.getAsdcAddress () == null || this.getAsdcAddress ().isEmpty ()) {
361 throw new ASDCParametersException (ASDC_ADDRESS_ATTRIBUTE_NAME
362 + " parameter cannot be found in config mso.properties");
365 if (this.getPassword () == null || this.getPassword ().isEmpty ()) {
366 throw new ASDCParametersException (PASSWORD_ATTRIBUTE_NAME
367 + " parameter cannot be found in config mso.properties");
370 if (this.getPollingInterval () == 0) {
371 throw new ASDCParametersException (POLLING_INTERVAL_ATTRIBUTE_NAME
372 + " parameter cannot be found in config mso.properties");
375 if (this.getPollingTimeout () == 0) {
376 throw new ASDCParametersException (POLLING_TIMEOUT_ATTRIBUTE_NAME
377 + " parameter cannot be found in config mso.properties");
380 if (this.getRelevantArtifactTypes () == null || this.getRelevantArtifactTypes ().isEmpty ()) {
381 throw new ASDCParametersException (RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME
382 + " parameter cannot be found in config mso.properties");
385 if (this.getUser () == null || this.getUser ().isEmpty ()) {
386 throw new ASDCParametersException (USER_ATTRIBUTE_NAME
387 + " parameter cannot be found in config mso.properties");
392 * This method triggers the MsoPropertiesFactory to get the ASDC config from the cache and extracts all controllers
395 * @return A list of controller Names defined in the cache config
396 * @throws ASDCParametersException In cas of issues with the cache
398 public static List <String> getAllDefinedControllers () throws ASDCParametersException {
400 MsoJsonProperties msoProp;
402 List <String> result = new ArrayList <String> ();
403 msoProp = new MsoPropertiesFactory ().getMsoJsonProperties (MSO_PROP_ASDC);
405 if (msoProp.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
406 Iterator <Entry <String, JsonNode>> it = msoProp.getJsonRootNode ()
407 .get (PARAMETER_PATTERN)
410 Entry <String, JsonNode> entry;
411 while (it.hasNext ()) {
413 result.add (entry.getKey ());
418 } catch (MsoPropertiesException e) {
419 throw new ASDCParametersException ("Unable to get the JSON Properties in cache:" + MSO_PROP_ASDC, e);
425 * The flag allows the client to receive metadata for all resources of the service regardless of the artifacts associated to them.
426 * Setting the flag to false will preserve legacy behavior.
428 public boolean isFilterInEmptyResources() {