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.codehaus.jackson.JsonNode;
35 import org.openecomp.sdc.api.consumer.IConfiguration;
36 import org.openecomp.mso.asdc.client.exceptions.ASDCParametersException;
37 import org.openecomp.mso.properties.MsoJsonProperties;
38 import org.openecomp.mso.properties.MsoPropertiesException;
39 import org.openecomp.mso.properties.MsoPropertiesFactory;
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 VF_MODULES_METADATA="VF_MODULES_METADATA";
80 private static final String[] SUPPORTED_ARTIFACT_TYPES = {HEAT,
89 public static final List<String> SUPPORTED_ARTIFACT_TYPES_LIST = Collections.unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));
92 * Default constructor, the mso.properties is searched in the classpath (for testing)
93 * Or in /etc/ecomp/mso/config/mso.properties
95 * @param controllerName The controllerName of the config JSON tree
96 * @throws ASDCParametersException in case of issues with the parameters
97 * @throws IOException If the key file has not been loaded properly
99 public ASDCConfiguration (String controllerName) throws ASDCParametersException, IOException {
101 Properties keyProp = new Properties ();
102 this.asdcControllerName = controllerName;
104 keyProp.load (Thread.currentThread ().getContextClassLoader ().getResourceAsStream ("config-key.properties"));
105 configKey = (String) keyProp.get ("asdc.config.key");
107 // This structure contains static values initialized by servlet initializer
108 this.msoPropertiesFactory = new MsoPropertiesFactory ();
110 refreshASDCConfig ();
114 public String getAsdcControllerName () {
115 return asdcControllerName;
118 private JsonNode getASDCControllerConfigJsonNode () {
119 if (this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
120 return this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN).get (this.asdcControllerName);
128 * This method reload the config if needed.
130 * @return true if config has been reloaded, false otherwise
131 * @throws ASDCParametersException In case the parameters validation fails
132 * @throws MsoPropertiesException
134 public boolean refreshASDCConfig () throws ASDCParametersException {
137 if (msoPropertiesFactory.propertiesHaveChanged (MSO_PROP_ASDC, msoProperties)) {
138 msoProperties = msoPropertiesFactory.getMsoJsonProperties (MSO_PROP_ASDC);
140 this.testAllParameters ();
145 } catch (MsoPropertiesException e) {
146 throw new ASDCParametersException ("mso.asdc.json not initialized properly, ASDC config cannot be reloaded",
152 * This method is useful to check whether a mso properties config has been changed.
154 * @return true is a new config is availabe, false otherwise
155 * @throws ASDCParametersException
156 * @throws MsoPropertiesException
158 public boolean hasASDCConfigChanged () throws ASDCParametersException {
160 return msoPropertiesFactory.propertiesHaveChanged (MSO_PROP_ASDC, msoProperties);
161 } catch (MsoPropertiesException e) {
162 throw new ASDCParametersException ("mso.asdc.json not initialized properly, ASDC config cannot be read", e);
167 public String getConsumerGroup () {
168 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
169 if (masterConfigNode != null && masterConfigNode.get (CONSUMER_GROUP_ATTRIBUTE_NAME) != null) {
170 String config = masterConfigNode.get (CONSUMER_GROUP_ATTRIBUTE_NAME).asText ();
172 if ("NULL".equals (config) || config.isEmpty ()) {
183 public String getConsumerID () {
185 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
186 if (masterConfigNode != null && masterConfigNode.get (CONSUMER_ID_ATTRIBUTE_NAME) != null) {
187 String config = masterConfigNode.get (CONSUMER_ID_ATTRIBUTE_NAME).asText ();
189 if (config.isEmpty ()) {
200 public String getEnvironmentName () {
201 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
202 if (masterConfigNode != null && masterConfigNode.get (ENVIRONMENT_NAME_ATTRIBUTE_NAME) != null) {
203 String config = masterConfigNode.get (ENVIRONMENT_NAME_ATTRIBUTE_NAME).asText ();
205 if (config.isEmpty ()) {
216 public String getPassword () {
217 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
218 if (masterConfigNode != null && masterConfigNode.get (PASSWORD_ATTRIBUTE_NAME) != null) {
219 String config = this.msoProperties.getEncryptedProperty (masterConfigNode.get (PASSWORD_ATTRIBUTE_NAME),
223 if (config.isEmpty ()) {
234 public int getPollingInterval () {
235 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
236 if (masterConfigNode != null && masterConfigNode.get (POLLING_INTERVAL_ATTRIBUTE_NAME) != null) {
237 return masterConfigNode.get (POLLING_INTERVAL_ATTRIBUTE_NAME).asInt ();
244 public List <String> getRelevantArtifactTypes () {
245 // DO not return the Static List SUPPORTED_ARTIFACT_TYPES_LIST because the ASDC Client will try to modify it !!!
246 return Arrays.asList(SUPPORTED_ARTIFACT_TYPES);
250 public String getUser () {
251 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
252 if (masterConfigNode != null && masterConfigNode.get (USER_ATTRIBUTE_NAME) != null) {
253 String config = masterConfigNode.get (USER_ATTRIBUTE_NAME).asText ();
255 if (config.isEmpty ()) {
266 public String getAsdcAddress () {
267 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
268 if (masterConfigNode != null && masterConfigNode.get (ASDC_ADDRESS_ATTRIBUTE_NAME) != null) {
269 String config = masterConfigNode.get (ASDC_ADDRESS_ATTRIBUTE_NAME).asText ();
271 if (config.isEmpty ()) {
282 public int getPollingTimeout () {
283 JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
284 if (masterConfigNode != null && masterConfigNode.get (POLLING_TIMEOUT_ATTRIBUTE_NAME) != null) {
285 return masterConfigNode.get (POLLING_TIMEOUT_ATTRIBUTE_NAME).asInt ();
292 public boolean activateServerTLSAuth() {
293 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
294 if (masterConfigNode != null && masterConfigNode.get(ACTIVATE_SERVER_TLS_AUTH) != null) {
295 return masterConfigNode.get(ACTIVATE_SERVER_TLS_AUTH).asBoolean(false);
302 public String getKeyStorePassword() {
303 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
304 if (masterConfigNode != null && masterConfigNode.get(KEY_STORE_PASSWORD) != null) {
305 String config = this.msoProperties.getEncryptedProperty(masterConfigNode.get(KEY_STORE_PASSWORD), null,
308 if (config.isEmpty()) {
319 public String getKeyStorePath() {
320 JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
321 if (masterConfigNode != null && masterConfigNode.get(KEY_STORE_PATH) != null) {
322 String config = masterConfigNode.get(KEY_STORE_PATH).asText();
324 if (config.isEmpty()) {
334 public void testAllParameters () throws ASDCParametersException {
336 // Special case for this attribute that can be null from getConsumerGroup
337 if (this.getConsumerGroup () == null
338 && (getASDCControllerConfigJsonNode () == null
339 || !"NULL".equals (getASDCControllerConfigJsonNode ().get (CONSUMER_GROUP_ATTRIBUTE_NAME).asText ()))) {
340 throw new ASDCParametersException (CONSUMER_GROUP_ATTRIBUTE_NAME
341 + " parameter cannot be found in config mso.properties");
344 if (this.getConsumerID () == null || this.getConsumerID ().isEmpty ()) {
345 throw new ASDCParametersException (CONSUMER_ID_ATTRIBUTE_NAME
346 + " parameter cannot be found in config mso.properties");
349 if (this.getEnvironmentName () == null || this.getEnvironmentName ().isEmpty ()) {
350 throw new ASDCParametersException (ENVIRONMENT_NAME_ATTRIBUTE_NAME
351 + " parameter cannot be found in config mso.properties");
354 if (this.getAsdcAddress () == null || this.getAsdcAddress ().isEmpty ()) {
355 throw new ASDCParametersException (ASDC_ADDRESS_ATTRIBUTE_NAME
356 + " parameter cannot be found in config mso.properties");
359 if (this.getPassword () == null || this.getPassword ().isEmpty ()) {
360 throw new ASDCParametersException (PASSWORD_ATTRIBUTE_NAME
361 + " parameter cannot be found in config mso.properties");
364 if (this.getPollingInterval () == 0) {
365 throw new ASDCParametersException (POLLING_INTERVAL_ATTRIBUTE_NAME
366 + " parameter cannot be found in config mso.properties");
369 if (this.getPollingTimeout () == 0) {
370 throw new ASDCParametersException (POLLING_TIMEOUT_ATTRIBUTE_NAME
371 + " parameter cannot be found in config mso.properties");
374 if (this.getRelevantArtifactTypes () == null || this.getRelevantArtifactTypes ().isEmpty ()) {
375 throw new ASDCParametersException (RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME
376 + " parameter cannot be found in config mso.properties");
379 if (this.getUser () == null || this.getUser ().isEmpty ()) {
380 throw new ASDCParametersException (USER_ATTRIBUTE_NAME
381 + " parameter cannot be found in config mso.properties");
386 * This method triggers the MsoPropertiesFactory to get the ASDC config from the cache and extracts all controllers
389 * @return A list of controller Names defined in the cache config
390 * @throws ASDCParametersException In cas of issues with the cache
392 public static List <String> getAllDefinedControllers () throws ASDCParametersException {
394 MsoJsonProperties msoProp;
396 List <String> result = new ArrayList <String> ();
397 msoProp = new MsoPropertiesFactory ().getMsoJsonProperties (MSO_PROP_ASDC);
399 if (msoProp.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
400 Iterator <Entry <String, JsonNode>> it = msoProp.getJsonRootNode ()
401 .get (PARAMETER_PATTERN)
404 Entry <String, JsonNode> entry;
405 while (it.hasNext ()) {
407 result.add (entry.getKey ());
412 } catch (MsoPropertiesException e) {
413 throw new ASDCParametersException ("Unable to get the JSON Properties in cache:" + MSO_PROP_ASDC, e);