Allow HTTP with SDC
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / config / ModelLoaderConfig.java
index 6a45f28..88735d4 100644 (file)
@@ -2,8 +2,8 @@
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
- * Copyright © 2017-2018 European Software Marketing Ltd.
+ * Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (c) 2017-2019 European Software Marketing Ltd.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
+
 package org.onap.aai.modelloader.config;
 
 import java.io.File;
@@ -26,6 +27,7 @@ import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
+import org.apache.commons.lang3.StringUtils;
 import org.eclipse.jetty.util.security.Password;
 import org.onap.sdc.api.consumer.IConfiguration;
 
@@ -54,6 +56,7 @@ public class ModelLoaderConfig implements IConfiguration {
     protected static final String PROP_ML_DISTRIBUTION_ASDC_CONNECTION_DISABLED =
             PREFIX_DISTRIBUTION_CLIENT + "ASDC_CONNECTION_DISABLE";
     protected static final String PROP_ML_DISTRIBUTION_ASDC_ADDRESS = PREFIX_DISTRIBUTION_CLIENT + "ASDC_ADDRESS";
+    protected static final String PROP_ML_DISTRIBUTION_ASDC_USE_HTTPS = PREFIX_DISTRIBUTION_CLIENT + "ASDC_USE_HTTPS";
     protected static final String PROP_ML_DISTRIBUTION_CONSUMER_GROUP = PREFIX_DISTRIBUTION_CLIENT + "CONSUMER_GROUP";
     protected static final String PROP_ML_DISTRIBUTION_CONSUMER_ID = PREFIX_DISTRIBUTION_CLIENT + "CONSUMER_ID";
     protected static final String PROP_ML_DISTRIBUTION_ENVIRONMENT_NAME =
@@ -97,8 +100,8 @@ public class ModelLoaderConfig implements IConfiguration {
     private static String configHome;
     private Properties modelLoaderProperties = null;
     private String certLocation = ".";
-    private List<String> artifactTypes = null;
-    private List<String> msgBusAddrs = null;
+    private List<String> artifactTypes = new ArrayList<>();
+    private List<String> msgBusAddrs = new ArrayList<>();
     private String modelVersion = null;
 
     public ModelLoaderConfig(Properties configProperties) {
@@ -108,27 +111,27 @@ public class ModelLoaderConfig implements IConfiguration {
     /**
      * Original constructor
      *
-     * @param modelLoaderProperties properties needed to be configured for the model loader
-     * @param certLocation location of the certificate
+     * @param modelLoaderProperties
+     *            properties needed to be configured for the model loader
+     * @param certLocation
+     *            location of the certificate
      */
     public ModelLoaderConfig(Properties modelLoaderProperties, String certLocation) {
         this.modelLoaderProperties = modelLoaderProperties;
         this.certLocation = certLocation;
 
-        // Get list of artifacts
-        artifactTypes = new ArrayList<>();
-        if (modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_ARTIFACT_TYPES) != null) {
-            String[] artTypeList = modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_ARTIFACT_TYPES).split(",");
-            for (String artType : artTypeList) {
+        // Get list of artifact types
+        String types = get(PROP_ML_DISTRIBUTION_ARTIFACT_TYPES);
+        if (types != null) {
+            for (String artType : types.split(",")) {
                 artifactTypes.add(artType);
             }
         }
 
         // Get list of message bus addresses
-        msgBusAddrs = new ArrayList<>();
-        if (modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_MSG_BUS_ADDRESSES) != null) {
-            String[] msgBusList = modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_MSG_BUS_ADDRESSES).split(",");
-            for (String addr : msgBusList) {
+        String addresses = get(PROP_ML_DISTRIBUTION_MSG_BUS_ADDRESSES);
+        if (addresses != null) {
+            for (String addr : addresses.split(",")) {
                 msgBusAddrs.add(addr);
             }
         }
@@ -144,53 +147,63 @@ public class ModelLoaderConfig implements IConfiguration {
 
     @Override
     public boolean activateServerTLSAuth() {
-        String value = modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_ACTIVE_SERVER_TLS_AUTH);
+        String value = get(PROP_ML_DISTRIBUTION_ACTIVE_SERVER_TLS_AUTH);
         return value != null && Boolean.parseBoolean(value);
     }
 
     @Override
     public String getAsdcAddress() {
-        return modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_ASDC_ADDRESS);
+        return get(PROP_ML_DISTRIBUTION_ASDC_ADDRESS);
+    }
+
+    @Override
+    public Boolean isUseHttpsWithSDC() {
+        /* if PROP_ML_DISTRIBUTION_ASDC_USE_HTTPS is null, https will be used, as before */
+        String value = get(PROP_ML_DISTRIBUTION_ASDC_USE_HTTPS);
+        if (value == null) {
+          return true;
+        }
+        return Boolean.parseBoolean(value);
     }
 
     @Override
     public String getConsumerGroup() {
-        return modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_CONSUMER_GROUP);
+        return get(PROP_ML_DISTRIBUTION_CONSUMER_GROUP);
     }
 
     @Override
     public String getConsumerID() {
-        return modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_CONSUMER_ID);
+        return get(PROP_ML_DISTRIBUTION_CONSUMER_ID);
     }
 
     @Override
     public String getEnvironmentName() {
-        return modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_ENVIRONMENT_NAME);
+        return get(PROP_ML_DISTRIBUTION_ENVIRONMENT_NAME);
     }
 
     @Override
     public String getKeyStorePassword() {
-        return Password.deobfuscate(modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_KEYSTORE_PASSWORD));
+        return getDeobfuscatedValue(get(PROP_ML_DISTRIBUTION_KEYSTORE_PASSWORD));
     }
 
     @Override
     public String getKeyStorePath() {
-        return certLocation + modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_KEYSTORE_FILE);
+        return certLocation + get(PROP_ML_DISTRIBUTION_KEYSTORE_FILE);
     }
 
     @Override
     public String getPassword() {
-        return Password.deobfuscate(modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_PASSWORD));
+        return getDeobfuscatedValue(get(PROP_ML_DISTRIBUTION_PASSWORD));
     }
 
     @Override
     public int getPollingInterval() {
-        return Integer.parseInt(modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_POLLING_INTERVAL));
+        return Integer.parseInt(get(PROP_ML_DISTRIBUTION_POLLING_INTERVAL));
     }
 
     @Override
     public int getPollingTimeout() {
-        return Integer.parseInt(modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_POLLING_TIMEOUT));
+        return Integer.parseInt(get(PROP_ML_DISTRIBUTION_POLLING_TIMEOUT));
     }
 
     @Override
@@ -200,7 +213,7 @@ public class ModelLoaderConfig implements IConfiguration {
 
     @Override
     public String getUser() {
-        return modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_USER);
+        return get(PROP_ML_DISTRIBUTION_USER);
     }
 
     @Override
@@ -210,7 +223,7 @@ public class ModelLoaderConfig implements IConfiguration {
 
     @Override
     public Boolean isUseHttpsWithDmaap() {
-        String useHTTPS = modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_HTTPS_WITH_DMAAP);
+        String useHTTPS = get(PROP_ML_DISTRIBUTION_HTTPS_WITH_DMAAP);
         return useHTTPS != null && Boolean.valueOf(useHTTPS);
     }
 
@@ -224,7 +237,7 @@ public class ModelLoaderConfig implements IConfiguration {
     }
 
     public String getBabelKeyStorePath() {
-        String filename = modelLoaderProperties.getProperty(PROP_BABEL_KEYSTORE_FILE);
+        String filename = get(PROP_BABEL_KEYSTORE_FILE);
         if (filename == null) {
             return null;
         } else {
@@ -233,15 +246,15 @@ public class ModelLoaderConfig implements IConfiguration {
     }
 
     public String getAaiKeyStorePassword() {
-        return Password.deobfuscate(modelLoaderProperties.getProperty(PROP_AAI_KEYSTORE_PASSWORD));
+        return getDeobfuscatedValue(get(PROP_AAI_KEYSTORE_PASSWORD));
     }
 
     public String getBabelKeyStorePassword() {
-        return Password.deobfuscate(modelLoaderProperties.getProperty(PROP_BABEL_KEYSTORE_PASSWORD));
+        return getDeobfuscatedValue(get(PROP_BABEL_KEYSTORE_PASSWORD));
     }
 
     public String getBabelTrustStorePath() {
-        String filename = modelLoaderProperties.getProperty(PROP_BABEL_TRUSTSTORE_FILE);
+        String filename = get(PROP_BABEL_TRUSTSTORE_FILE);
         if (filename == null) {
             return null;
         } else {
@@ -250,36 +263,36 @@ public class ModelLoaderConfig implements IConfiguration {
     }
 
     public String getBabelTrustStorePassword() {
-        return Password.deobfuscate(modelLoaderProperties.getProperty(PROP_BABEL_TRUSTSTORE_PASSWORD));
+        return getDeobfuscatedValue(get(PROP_BABEL_TRUSTSTORE_PASSWORD));
     }
 
     public String getAaiBaseUrl() {
-        return modelLoaderProperties.getProperty(PROP_AAI_BASE_URL);
+        return get(PROP_AAI_BASE_URL);
     }
 
     public String getBabelBaseUrl() {
-        return modelLoaderProperties.getProperty(PROP_BABEL_BASE_URL);
+        return get(PROP_BABEL_BASE_URL);
     }
 
     public String getBabelGenerateArtifactsUrl() {
-        return modelLoaderProperties.getProperty(PROP_BABEL_GENERATE_RESOURCE_URL);
+        return get(PROP_BABEL_GENERATE_RESOURCE_URL);
     }
 
     public String getAaiModelUrl(String version) {
         setModelVersion(version);
-        return updatePropertyOXMVersion(modelLoaderProperties, PROP_AAI_MODEL_RESOURCE_URL, version);
+        return updatePropertyOXMVersion(PROP_AAI_MODEL_RESOURCE_URL, version);
     }
 
     public String getAaiNamedQueryUrl(String version) {
-        return updatePropertyOXMVersion(modelLoaderProperties, PROP_AAI_NAMED_QUERY_RESOURCE_URL, version);
+        return updatePropertyOXMVersion(PROP_AAI_NAMED_QUERY_RESOURCE_URL, version);
     }
 
     public String getAaiVnfImageUrl() {
-        return updatePropertyOXMVersion(modelLoaderProperties, PROP_AAI_VNF_IMAGE_RESOURCE_URL, getModelVersion());
+        return updatePropertyOXMVersion(PROP_AAI_VNF_IMAGE_RESOURCE_URL, getModelVersion());
     }
 
     public String getAaiAuthenticationUser() {
-        return modelLoaderProperties.getProperty(PROP_AAI_AUTHENTICATION_USER);
+        return get(PROP_AAI_AUTHENTICATION_USER);
     }
 
     public String getModelVersion() {
@@ -291,20 +304,15 @@ public class ModelLoaderConfig implements IConfiguration {
     }
 
     public boolean useGizmo() {
-        String useGizmo = modelLoaderProperties.getProperty(PROP_AAI_USE_GIZMO);
-
-        if ( (useGizmo == null) || (!useGizmo.equalsIgnoreCase("true")) ) {
-            return false;
-        }
-
-        return true;
+        String useGizmo = get(PROP_AAI_USE_GIZMO);
+        return useGizmo != null && useGizmo.equalsIgnoreCase("true");
     }
 
     /**
      * @return password for AAI authentication that has been reverse-engineered from its obfuscated form.
      */
     public String getAaiAuthenticationPassword() {
-        String password = Password.deobfuscate(modelLoaderProperties.getProperty(PROP_AAI_AUTHENTICATION_PASSWORD));
+        String password = getDeobfuscatedValue(get(PROP_AAI_AUTHENTICATION_PASSWORD));
 
         if (password != null && password.isEmpty()) {
             password = null;
@@ -317,27 +325,48 @@ public class ModelLoaderConfig implements IConfiguration {
      * @return a boolean value indicating whether the simulator is enabled.
      */
     public boolean getIngestSimulatorEnabled() {
-        String propValue = modelLoaderProperties.getProperty(PROP_DEBUG_INGEST_SIMULATOR);
+        String propValue = get(PROP_DEBUG_INGEST_SIMULATOR);
         return propValue != null && "enabled".equalsIgnoreCase(propValue);
     }
 
     /**
-     * @return a String value of the defined property with the oxm version
+     * Read the value of the property and replace any wildcard OXM version "v*" with the supplied default OXM version
+     *
+     * @param propertyName
+     *            the name of the property storing the OXM version (possibly containing v*)
+     * @param version
+     *            the default OXM version
+     * @return the String value of the defined property (with any wildcard OXM version defaulted)
      */
-    private String updatePropertyOXMVersion(Properties modelLoaderProperties, String propertyName, String version) {
-        if (version != null)
-            return modelLoaderProperties.getProperty(propertyName).replace("v*", version);
-        else
-            return modelLoaderProperties.getProperty(propertyName);
+    private String updatePropertyOXMVersion(String propertyName, String version) {
+        String value = get(propertyName);
+        if (version != null && value != null) {
+            value = value.replace("v*", version);
+        }
+        return value;
     }
 
     /**
      * @return a boolean value indicating whether model loader is connected to ASDC.
      */
     public boolean getASDCConnectionDisabled() {
-        String propValue = modelLoaderProperties.getProperty(PROP_ML_DISTRIBUTION_ASDC_CONNECTION_DISABLED);
+        String propValue = get(PROP_ML_DISTRIBUTION_ASDC_CONNECTION_DISABLED);
         return propValue != null && "true".equalsIgnoreCase(propValue);
+    }
 
+    private String getDeobfuscatedValue(String property) {
+        if (property != null && property.startsWith("OBF:")) {
+            return Password.deobfuscate(property);
+        }
+        return property;
     }
 
+    private String get(String key) {
+        String value = modelLoaderProperties.getProperty(key);
+
+        if (value != null && value.startsWith("ENV:")) {
+            value = System.getenv(StringUtils.removeStart(value, "ENV:"));
+        }
+        return value;
+    }
 }