Fix to properties load for blueprints-processor 44/78244/2
authorAlexis de Talhouët <adetalhouet89@gmail.com>
Mon, 11 Feb 2019 18:45:30 +0000 (13:45 -0500)
committerAlexis de Talhouët <adetalhouet89@gmail.com>
Tue, 12 Feb 2019 13:43:06 +0000 (08:43 -0500)
Change-Id: I044587dd702d979708a95f8c98349e008a2d3d49
Issue-ID: CCSDK-1037
Signed-off-by: Alexis de Talhouët <adetalhouet89@gmail.com>
blueprints-processor/adaptors/rest-adaptor-provider/pom.xml
blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/features/rest/adaptor/service/ConfigRestAdaptorServiceImpl.java
blueprints-processor/adaptors/rest-adaptor-provider/src/main/resources/blueprints-processor-adaptor.properties [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml

index af272f6..98f1fdb 100644 (file)
                        <artifactId>eelf-core</artifactId>
                        <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>
index 158123a..0646e1f 100644 (file)
@@ -1,13 +1,13 @@
 /*\r
  * Copyright © 2017-2018 AT&T Intellectual Property.\r
  * Modifications Copyright © 2018 IBM.\r
- * \r
+ *\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
- * \r
+ *\r
  * http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
+ *\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
@@ -19,6 +19,9 @@ package org.onap.ccsdk.features.rest.adaptor.service;
 \r
 import java.io.File;\r
 import java.io.FileInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.net.URL;\r
 import java.util.Map;\r
 import java.util.Properties;\r
 import java.util.concurrent.ConcurrentHashMap;\r
@@ -32,24 +35,29 @@ import org.onap.ccsdk.features.rest.adaptor.ConfigRestAdaptorException;
 import org.onap.ccsdk.features.rest.adaptor.data.RestResponse;\r
 import com.att.eelf.configuration.EELFLogger;\r
 import com.att.eelf.configuration.EELFManager;\r
+import org.osgi.framework.Bundle;\r
+import org.osgi.framework.BundleContext;\r
+import org.osgi.framework.FrameworkUtil;\r
 \r
 public class ConfigRestAdaptorServiceImpl implements ConfigRestAdaptorService {\r
 \r
     private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigRestAdaptorServiceImpl.class);\r
     private Map<String, String> restProperties = new ConcurrentHashMap<>();\r
 \r
-    public ConfigRestAdaptorServiceImpl(String propertyPath) {\r
-        initializeProperties(propertyPath);\r
+    public ConfigRestAdaptorServiceImpl() {\r
+        this(null);\r
+    }\r
+\r
+    public ConfigRestAdaptorServiceImpl(final String propertyFilePath) {\r
+        loadProps(propertyFilePath);\r
         try {\r
             String envType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY\r
-                    + ConfigRestAdaptorConstants.REST_ADAPTOR_ENV_TYPE);\r
+                + ConfigRestAdaptorConstants.REST_ADAPTOR_ENV_TYPE);\r
 \r
             if (!(ConfigRestAdaptorConstants.PROPERTY_ENV_PROD.equalsIgnoreCase(envType)\r
-                    || ConfigRestAdaptorConstants.PROPERTY_ENV_SOLO.equalsIgnoreCase(envType))) {\r
+                || ConfigRestAdaptorConstants.PROPERTY_ENV_SOLO.equalsIgnoreCase(envType))) {\r
                 ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);\r
-                Runnable task = () -> {\r
-                    initializeProperties(propertyPath);\r
-                };\r
+                Runnable task = () -> loadProps(propertyFilePath);\r
                 executor.scheduleWithFixedDelay(task, 60, 15, TimeUnit.MINUTES);\r
             }\r
         } catch (Exception e) {\r
@@ -57,52 +65,63 @@ public class ConfigRestAdaptorServiceImpl implements ConfigRestAdaptorService {
         }\r
     }\r
 \r
-    private void initializeProperties(String propertyPath) {\r
-        logger.trace("Initialising Config rest adaptor Service with property directory ({})", propertyPath);\r
-        try {\r
-            if (StringUtils.isBlank(propertyPath)) {\r
-                propertyPath = System.getProperty(ConfigRestAdaptorConstants.SDNC_ROOT_DIR_ENV_VAR_KEY);\r
-            }\r
-\r
-            if (StringUtils.isBlank(propertyPath)) {\r
-                throw new ConfigRestAdaptorException(\r
-                        String.format("Failed to get the property directory (%s)", propertyPath));\r
-            }\r
 \r
-            // Loading Default config-rest-adaptor.properties\r
+    // propertyFilePath is only specified in test case.\r
+    private void loadProps(final String propertyFilePath) {\r
+        Properties properties = new Properties();\r
+        if (propertyFilePath != null) {\r
+            // Loading Default properties\r
             String propertyFile =\r
-                    propertyPath + File.separator + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;\r
-\r
-            Properties properties = new Properties();\r
-            properties.load(new FileInputStream(propertyFile));\r
-\r
-            logger.trace("Initializing properties details for property file ({}) properties ({})", propertyFile,\r
-                    properties);\r
-            restProperties.putAll(properties.entrySet().stream()\r
-                    .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));\r
+                propertyFilePath + File.separator + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;\r
+            doLoadFromPath(propertyFile, properties);\r
+        } else {\r
+            // Try to load config from dir\r
+            final String ccsdkConfigDir =\r
+                System.getProperty(ConfigRestAdaptorConstants.SDNC_ROOT_DIR_ENV_VAR_KEY) + File.separator\r
+                    + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;\r
+            try (FileInputStream in = new FileInputStream(ccsdkConfigDir)) {\r
+                properties.load(in);\r
+                logger.info("Loaded {} properties from file {}", properties.size(), ccsdkConfigDir);\r
+            } catch (Exception e) {\r
+                // Try to load config from jar\r
+                final Bundle bundle = FrameworkUtil.getBundle(ConfigRestAdaptorServiceImpl.class);\r
+                final BundleContext ctx = bundle.getBundleContext();\r
+                final URL url = ctx.getBundle()\r
+                    .getResource(ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME);\r
+                doLoadFromPath(url.getPath(), properties);\r
+            }\r
+        }\r
+        restProperties.putAll(properties.entrySet().stream()\r
+            .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));\r
+    }\r
 \r
+    private void doLoadFromPath(final String propertyFile, final Properties properties) {\r
+        try (FileInputStream in = new FileInputStream(propertyFile)) {\r
+            properties.load(in);\r
+            logger.info("Loaded {} properties from file {}", properties.size(), propertyFile);\r
         } catch (Exception e) {\r
-            logger.error(e.getMessage(), e);\r
+            logger.error("Failed to load properties for file: {} "\r
+                + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME, e);\r
         }\r
     }\r
 \r
     @Override\r
     public <T> T getResource(String serviceSelector, String path, Class<T> responseType)\r
-            throws ConfigRestAdaptorException {\r
+        throws ConfigRestAdaptorException {\r
         return getRestClientAdapterBySelectorName(serviceSelector).getResource(path, responseType);\r
     }\r
 \r
     @Override\r
     public <T> T postResource(String serviceSelector, String path, Object request, Class<T> responseType)\r
-            throws ConfigRestAdaptorException {\r
+        throws ConfigRestAdaptorException {\r
         return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request, responseType);\r
     }\r
 \r
     @Override\r
     public <T> T exchangeResource(String serviceSelector, String path, Object request, Class<T> responseType,\r
-            String method) throws ConfigRestAdaptorException {\r
+        String method) throws ConfigRestAdaptorException {\r
         return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, responseType,\r
-                method);\r
+            method);\r
     }\r
 \r
     @Override\r
@@ -112,20 +131,20 @@ public class ConfigRestAdaptorServiceImpl implements ConfigRestAdaptorService {
 \r
     @Override\r
     public RestResponse postResource(String serviceSelector, String path, Object request)\r
-            throws ConfigRestAdaptorException {\r
+        throws ConfigRestAdaptorException {\r
         return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request);\r
     }\r
 \r
     @Override\r
     public RestResponse exchangeResource(String serviceSelector, String path, Object request, String method)\r
-            throws ConfigRestAdaptorException {\r
+        throws ConfigRestAdaptorException {\r
         return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, method);\r
     }\r
 \r
     private ConfigRestClientServiceAdapter getRestClientAdapterBySelectorName(String serviceSelector)\r
-            throws ConfigRestAdaptorException {\r
+        throws ConfigRestAdaptorException {\r
         String adoptorType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector\r
-                + ConfigRestAdaptorConstants.SERVICE_TYPE_PROPERTY);\r
+            + ConfigRestAdaptorConstants.SERVICE_TYPE_PROPERTY);\r
         if (StringUtils.isNotBlank(adoptorType)) {\r
             if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_GENERIC.equalsIgnoreCase(adoptorType)) {\r
                 return new GenericRestClientAdapterImpl(restProperties, serviceSelector);\r
@@ -133,12 +152,12 @@ public class ConfigRestAdaptorServiceImpl implements ConfigRestAdaptorService {
                 return new SSLRestClientAdapterImpl(restProperties, serviceSelector);\r
             } else {\r
                 throw new ConfigRestAdaptorException(\r
-                        String.format("no implementation for rest adoptor type (%s) for the selector (%s).",\r
-                                adoptorType, serviceSelector));\r
+                    String.format("no implementation for rest adoptor type (%s) for the selector (%s).",\r
+                        adoptorType, serviceSelector));\r
             }\r
         } else {\r
             throw new ConfigRestAdaptorException(\r
-                    String.format("couldn't get rest adoptor type for the selector (%s)", serviceSelector));\r
+                String.format("couldn't get rest adoptor type for the selector (%s)", serviceSelector));\r
         }\r
     }\r
 \r
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 (file)
index 0000000..a3dcf67
--- /dev/null
@@ -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/
index 09c9a4c..8788f64 100644 (file)
@@ -24,7 +24,6 @@
 \r
        <bean id="configRestAdaptorService"\r
                class="org.onap.ccsdk.features.rest.adaptor.service.ConfigRestAdaptorServiceImpl">\r
-               <argument value="/opt/onap/sdnc/data/properties" />\r
        </bean>\r
 \r
        <service ref="configRestAdaptorService"\r