Fix to properties load for blueprints-processor

Change-Id: I044587dd702d979708a95f8c98349e008a2d3d49
Issue-ID: CCSDK-1037
Signed-off-by: Alexis de Talhouët <adetalhouet89@gmail.com>
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/pom.xml b/blueprints-processor/adaptors/rest-adaptor-provider/pom.xml
index af272f6..98f1fdb 100644
--- a/blueprints-processor/adaptors/rest-adaptor-provider/pom.xml
+++ b/blueprints-processor/adaptors/rest-adaptor-provider/pom.xml
@@ -69,6 +69,11 @@
 			<version>${eelf.version}</version>
 		</dependency>
 		<dependency>
+			<groupId>org.osgi</groupId>
+			<artifactId>org.osgi.core</artifactId>
+			<scope>provided</scope>
+		</dependency>
+		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>
 			<version>${junit.version}</version>
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/features/rest/adaptor/service/ConfigRestAdaptorServiceImpl.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/features/rest/adaptor/service/ConfigRestAdaptorServiceImpl.java
index 158123a..0646e1f 100644
--- a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/features/rest/adaptor/service/ConfigRestAdaptorServiceImpl.java
+++ b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/features/rest/adaptor/service/ConfigRestAdaptorServiceImpl.java
@@ -1,13 +1,13 @@
 /*

  * Copyright © 2017-2018 AT&T Intellectual Property.

  * Modifications Copyright © 2018 IBM.

- * 

+ *

  * Licensed under the Apache License, Version 2.0 (the "License");

  * you may not use this file except in compliance with the License.

  * You may obtain a copy of the License at

- * 

+ *

  * http://www.apache.org/licenses/LICENSE-2.0

- * 

+ *

  * Unless required by applicable law or agreed to in writing, software

  * distributed under the License is distributed on an "AS IS" BASIS,

  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

@@ -19,6 +19,9 @@
 

 import java.io.File;

 import java.io.FileInputStream;

+import java.io.IOException;

+import java.io.InputStream;

+import java.net.URL;

 import java.util.Map;

 import java.util.Properties;

 import java.util.concurrent.ConcurrentHashMap;

@@ -32,24 +35,29 @@
 import org.onap.ccsdk.features.rest.adaptor.data.RestResponse;

 import com.att.eelf.configuration.EELFLogger;

 import com.att.eelf.configuration.EELFManager;

+import org.osgi.framework.Bundle;

+import org.osgi.framework.BundleContext;

+import org.osgi.framework.FrameworkUtil;

 

 public class ConfigRestAdaptorServiceImpl implements ConfigRestAdaptorService {

 

     private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigRestAdaptorServiceImpl.class);

     private Map<String, String> restProperties = new ConcurrentHashMap<>();

 

-    public ConfigRestAdaptorServiceImpl(String propertyPath) {

-        initializeProperties(propertyPath);

+    public ConfigRestAdaptorServiceImpl() {

+        this(null);

+    }

+

+    public ConfigRestAdaptorServiceImpl(final String propertyFilePath) {

+        loadProps(propertyFilePath);

         try {

             String envType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY

-                    + ConfigRestAdaptorConstants.REST_ADAPTOR_ENV_TYPE);

+                + ConfigRestAdaptorConstants.REST_ADAPTOR_ENV_TYPE);

 

             if (!(ConfigRestAdaptorConstants.PROPERTY_ENV_PROD.equalsIgnoreCase(envType)

-                    || ConfigRestAdaptorConstants.PROPERTY_ENV_SOLO.equalsIgnoreCase(envType))) {

+                || ConfigRestAdaptorConstants.PROPERTY_ENV_SOLO.equalsIgnoreCase(envType))) {

                 ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

-                Runnable task = () -> {

-                    initializeProperties(propertyPath);

-                };

+                Runnable task = () -> loadProps(propertyFilePath);

                 executor.scheduleWithFixedDelay(task, 60, 15, TimeUnit.MINUTES);

             }

         } catch (Exception e) {

@@ -57,52 +65,63 @@
         }

     }

 

-    private void initializeProperties(String propertyPath) {

-        logger.trace("Initialising Config rest adaptor Service with property directory ({})", propertyPath);

-        try {

-            if (StringUtils.isBlank(propertyPath)) {

-                propertyPath = System.getProperty(ConfigRestAdaptorConstants.SDNC_ROOT_DIR_ENV_VAR_KEY);

-            }

 

-            if (StringUtils.isBlank(propertyPath)) {

-                throw new ConfigRestAdaptorException(

-                        String.format("Failed to get the property directory (%s)", propertyPath));

-            }

-

-            // Loading Default config-rest-adaptor.properties

+    // propertyFilePath is only specified in test case.

+    private void loadProps(final String propertyFilePath) {

+        Properties properties = new Properties();

+        if (propertyFilePath != null) {

+            // Loading Default properties

             String propertyFile =

-                    propertyPath + File.separator + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;

+                propertyFilePath + File.separator + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;

+            doLoadFromPath(propertyFile, properties);

+        } else {

+            // Try to load config from dir

+            final String ccsdkConfigDir =

+                System.getProperty(ConfigRestAdaptorConstants.SDNC_ROOT_DIR_ENV_VAR_KEY) + File.separator

+                    + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;

+            try (FileInputStream in = new FileInputStream(ccsdkConfigDir)) {

+                properties.load(in);

+                logger.info("Loaded {} properties from file {}", properties.size(), ccsdkConfigDir);

+            } catch (Exception e) {

+                // Try to load config from jar

+                final Bundle bundle = FrameworkUtil.getBundle(ConfigRestAdaptorServiceImpl.class);

+                final BundleContext ctx = bundle.getBundleContext();

+                final URL url = ctx.getBundle()

+                    .getResource(ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME);

+                doLoadFromPath(url.getPath(), properties);

+            }

+        }

+        restProperties.putAll(properties.entrySet().stream()

+            .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));

+    }

 

-            Properties properties = new Properties();

-            properties.load(new FileInputStream(propertyFile));

-

-            logger.trace("Initializing properties details for property file ({}) properties ({})", propertyFile,

-                    properties);

-            restProperties.putAll(properties.entrySet().stream()

-                    .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));

-

+    private void doLoadFromPath(final String propertyFile, final Properties properties) {

+        try (FileInputStream in = new FileInputStream(propertyFile)) {

+            properties.load(in);

+            logger.info("Loaded {} properties from file {}", properties.size(), propertyFile);

         } catch (Exception e) {

-            logger.error(e.getMessage(), e);

+            logger.error("Failed to load properties for file: {} "

+                + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME, e);

         }

     }

 

     @Override

     public <T> T getResource(String serviceSelector, String path, Class<T> responseType)

-            throws ConfigRestAdaptorException {

+        throws ConfigRestAdaptorException {

         return getRestClientAdapterBySelectorName(serviceSelector).getResource(path, responseType);

     }

 

     @Override

     public <T> T postResource(String serviceSelector, String path, Object request, Class<T> responseType)

-            throws ConfigRestAdaptorException {

+        throws ConfigRestAdaptorException {

         return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request, responseType);

     }

 

     @Override

     public <T> T exchangeResource(String serviceSelector, String path, Object request, Class<T> responseType,

-            String method) throws ConfigRestAdaptorException {

+        String method) throws ConfigRestAdaptorException {

         return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, responseType,

-                method);

+            method);

     }

 

     @Override

@@ -112,20 +131,20 @@
 

     @Override

     public RestResponse postResource(String serviceSelector, String path, Object request)

-            throws ConfigRestAdaptorException {

+        throws ConfigRestAdaptorException {

         return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request);

     }

 

     @Override

     public RestResponse exchangeResource(String serviceSelector, String path, Object request, String method)

-            throws ConfigRestAdaptorException {

+        throws ConfigRestAdaptorException {

         return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, method);

     }

 

     private ConfigRestClientServiceAdapter getRestClientAdapterBySelectorName(String serviceSelector)

-            throws ConfigRestAdaptorException {

+        throws ConfigRestAdaptorException {

         String adoptorType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector

-                + ConfigRestAdaptorConstants.SERVICE_TYPE_PROPERTY);

+            + ConfigRestAdaptorConstants.SERVICE_TYPE_PROPERTY);

         if (StringUtils.isNotBlank(adoptorType)) {

             if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_GENERIC.equalsIgnoreCase(adoptorType)) {

                 return new GenericRestClientAdapterImpl(restProperties, serviceSelector);

@@ -133,12 +152,12 @@
                 return new SSLRestClientAdapterImpl(restProperties, serviceSelector);

             } else {

                 throw new ConfigRestAdaptorException(

-                        String.format("no implementation for rest adoptor type (%s) for the selector (%s).",

-                                adoptorType, serviceSelector));

+                    String.format("no implementation for rest adoptor type (%s) for the selector (%s).",

+                        adoptorType, serviceSelector));

             }

         } else {

             throw new ConfigRestAdaptorException(

-                    String.format("couldn't get rest adoptor type for the selector (%s)", serviceSelector));

+                String.format("couldn't get rest adoptor type for the selector (%s)", serviceSelector));

         }

     }

 

diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/resources/blueprints-processor-adaptor.properties b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/resources/blueprints-processor-adaptor.properties
new file mode 100644
index 0000000..a3dcf67
--- /dev/null
+++ b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/resources/blueprints-processor-adaptor.properties
@@ -0,0 +1,35 @@
+###
+# Copyright � 2017-2018 AT&T Intellectual Property.
+# Modifications Copyright � 2018 IBM.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+###
+#
+# Configuration file for SDNC Controller Module
+#
+
+org.onap.ccsdk.features.blueprints.adaptors.envtype=solo
+
+# Config Generator Microservices
+org.onap.ccsdk.features.blueprints.adaptors.modelservice.type=generic
+org.onap.ccsdk.features.blueprints.adaptors.modelservice.enable=true
+org.onap.ccsdk.features.blueprints.adaptors.modelservice.url=http://controller-blueprints:8080/api/v1/
+org.onap.ccsdk.features.blueprints.adaptors.modelservice.user=ccsdkapps
+org.onap.ccsdk.features.blueprints.adaptors.modelservice.passwd=ccsdkapps
+
+# Generic RESTCONF Adaptor
+org.onap.ccsdk.features.blueprints.adaptors.restconf.type=generic
+org.onap.ccsdk.features.blueprints.adaptors.restconf.enable=true
+org.onap.ccsdk.features.blueprints.adaptors.restconf.user=admin
+org.onap.ccsdk.features.blueprints.adaptors.restconf.passwd=Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U
+org.onap.ccsdk.features.blueprints.adaptors.restconf.url=http://sdnc:8282/restconf/
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml
index 09c9a4c..8788f64 100644
--- a/blueprints-processor/adaptors/rest-adaptor-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml
+++ b/blueprints-processor/adaptors/rest-adaptor-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml
@@ -24,7 +24,6 @@
 

 	<bean id="configRestAdaptorService"

 		class="org.onap.ccsdk.features.rest.adaptor.service.ConfigRestAdaptorServiceImpl">

-		<argument value="/opt/onap/sdnc/data/properties" />

 	</bean>

 

 	<service ref="configRestAdaptorService"