make ConfigurationServiceFactoryImpl independent of OSGi 88/76588/2
authorMichael Vorburger <vorburger@redhat.com>
Wed, 3 Oct 2018 00:33:37 +0000 (02:33 +0200)
committerMichael Vorburger <vorburger@redhat.com>
Wed, 3 Oct 2018 00:47:28 +0000 (02:47 +0200)
so that it can be used in a standalone / JavaSE environment such as
https://github.com/vorburger/opendaylight-simple (or
https://www.lighty.io)

JIRA: OPNFLWPLUG-1037
Change-Id: I2ff54e8d9324b78ee4b0e28485f5bb40e7e40c70
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/configuration/ConfigurationServiceFactory.java
openflowplugin-blueprint-config/src/main/resources/org/opendaylight/blueprint/openflowplugin.xml
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/configuration/ConfigurationServiceFactoryImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/configuration/ConfigurationServiceFactoryOsgiImpl.java [new file with mode: 0644]
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/configuration/OpenFlowProviderConfigImpl.java
openflowplugin-impl/src/main/resources/org/opendaylight/blueprint/commands.xml
openflowplugin-impl/src/main/resources/org/opendaylight/blueprint/openflowplugin-impl.xml
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/configuration/ConfigurationServiceFactoryImplTest.java

index 2a60b12d40c3d67883d8e0acf303729a3d3185db..11bdcf5e7cc983d7dc35d31493c0aed1e47ec5a2 100644 (file)
@@ -8,12 +8,10 @@
 package org.opendaylight.openflowplugin.api.openflow.configuration;
 
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
-import org.osgi.framework.BundleContext;
 
 /**
  * Factory for creating ConfigurationService instances.
  */
 public interface ConfigurationServiceFactory {
-    ConfigurationService newInstance(OpenflowProviderConfig providerConfig,
-                                     BundleContext bundleContext);
-}
\ No newline at end of file
+    ConfigurationService newInstance(OpenflowProviderConfig providerConfig);
+}
index 9d15a7b195aae719740a1b38ff55f50bc53fa899..88022a4dc102d22c7fc30add13dab61e76f89a98 100644 (file)
@@ -35,7 +35,6 @@
           factory-method="newInstance"
           destroy-method="close">
         <argument ref="openflowProviderConfig" />
-        <argument ref="blueprintBundleContext" />
         <cm:managed-properties persistent-id="org.opendaylight.openflowplugin"
                                update-strategy="component-managed"
                                update-method="update"/>
index a1616dddeee740f20beb2b3dabff7c0e2c432e55..567aa85601e5dcd048baab7ad946591d301d509b 100644 (file)
@@ -5,30 +5,22 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.configuration;
 
 import com.google.common.base.Verify;
 import com.google.common.collect.ImmutableMap;
-import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
-import java.util.Optional;
 import java.util.function.Function;
 import javax.annotation.Nonnull;
-import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationProperty;
 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationServiceFactory;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
-import org.osgi.framework.BundleContext;
-import org.osgi.service.cm.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -37,16 +29,15 @@ public class ConfigurationServiceFactoryImpl implements ConfigurationServiceFact
 
     @Override
     public ConfigurationService newInstance(
-            final OpenflowProviderConfig providerConfig,
-            final BundleContext bundleContext) {
-        return new ConfigurationServiceImpl(providerConfig, bundleContext);
+            final OpenflowProviderConfig providerConfig) {
+        return new ConfigurationServiceImpl(providerConfig);
     }
 
     private static final class ConfigurationServiceImpl implements ConfigurationService {
         private final Map<String, String> propertyMap = new HashMap<>();
         private final List<ConfigurationListener> listeners = new ArrayList<>();
 
-        ConfigurationServiceImpl(final OpenflowProviderConfig providerConfig, final BundleContext bundleContext) {
+        ConfigurationServiceImpl(final OpenflowProviderConfig providerConfig) {
             LOG.info("Loading properties from '{}' YANG file", OpenflowProviderConfig.QNAME);
             update(ImmutableMap
                     .<String, String>builder()
@@ -97,32 +88,6 @@ public class ConfigurationServiceFactoryImpl implements ConfigurationServiceFact
                     .put(ConfigurationProperty.DEVICE_CONNECTION_RATE_LIMIT_PER_MIN.toString(),
                             providerConfig.getDeviceConnectionRateLimitPerMin().toString())
                     .build());
-
-            LOG.info("Loading configuration from '{}' configuration file", OFConstants.CONFIG_FILE_ID);
-            Optional.ofNullable(bundleContext.getServiceReference(ConfigurationAdmin.class))
-                    .ifPresent(serviceReference -> {
-                        final ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
-
-                        try {
-                            final Configuration configuration
-                                    = configurationAdmin.getConfiguration(OFConstants.CONFIG_FILE_ID);
-
-                            Optional.ofNullable(configuration.getProperties()).ifPresent(properties -> {
-                                final Enumeration<String> keys = properties.keys();
-                                final Map<String, String> mapProperties = new HashMap<>(properties.size());
-
-                                while (keys.hasMoreElements()) {
-                                    final String key = keys.nextElement();
-                                    final String value = properties.get(key).toString();
-                                    mapProperties.put(key, value);
-                                }
-
-                                update(mapProperties);
-                            });
-                        } catch (IOException e) {
-                            LOG.debug("Failed to load {} configuration file. Error {}", OFConstants.CONFIG_FILE_ID, e);
-                        }
-                    });
         }
 
         @Override
diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/configuration/ConfigurationServiceFactoryOsgiImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/configuration/ConfigurationServiceFactoryOsgiImpl.java
new file mode 100644 (file)
index 0000000..5546ed6
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.openflowplugin.impl.configuration;
+
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ConfigurationServiceFactoryOsgiImpl extends ConfigurationServiceFactoryImpl {
+
+    private static final Logger LOG = LoggerFactory.getLogger(ConfigurationServiceFactoryOsgiImpl.class);
+
+    private final BundleContext bundleContext;
+
+    public ConfigurationServiceFactoryOsgiImpl(BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+    }
+
+    @Override
+    public ConfigurationService newInstance(OpenflowProviderConfig providerConfig) {
+        ConfigurationService cs = super.newInstance(providerConfig);
+        update(cs);
+        return cs;
+    }
+
+    private void update(ConfigurationService configurationService) {
+        LOG.info("Loading configuration from '{}' configuration file", OFConstants.CONFIG_FILE_ID);
+        Optional.ofNullable(bundleContext.getServiceReference(ConfigurationAdmin.class)).ifPresent(serviceReference -> {
+            final ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
+
+            try {
+                final Configuration configuration = configurationAdmin.getConfiguration(OFConstants.CONFIG_FILE_ID);
+
+                Optional.ofNullable(configuration.getProperties()).ifPresent(properties -> {
+                    final Enumeration<String> keys = properties.keys();
+                    final Map<String, String> mapProperties = new HashMap<>(properties.size());
+
+                    while (keys.hasMoreElements()) {
+                        final String key = keys.nextElement();
+                        final String value = properties.get(key).toString();
+                        mapProperties.put(key, value);
+                    }
+
+                    configurationService.update(mapProperties);
+                });
+            } catch (IOException e) {
+                LOG.debug("Failed to load {} configuration file. Error {}", OFConstants.CONFIG_FILE_ID, e);
+            }
+        });
+    }
+}
index 02e7c4060d1837c2b7d49b0d11b28944ae1fc570..f4acb907ac36bdcc9e2bdc609ac0456720464176 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.configuration;
 
 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationProperty;
@@ -184,5 +183,4 @@ public class OpenFlowProviderConfigImpl implements OpenflowProviderConfig {
         return service.getProperty(ConfigurationProperty.DEVICE_CONNECTION_RATE_LIMIT_PER_MIN.toString(),
                 Integer::valueOf);
     }
-
 }
index 4f42c5e0b2f9014d7dbc9fd846cd9e123deff2df..8ec9095170ed477a3c5b16ff61896be9fa672968 100644 (file)
@@ -32,6 +32,4 @@
             <action class="org.opendaylight.openflowplugin.impl.karaf.ResetSessionStatsComandProvider"/>
         </command>
     </command-bundle>
-
-
 </blueprint>
\ No newline at end of file
index ede48ec53516c1bf98a3a878bec0256630a0578c..5231eb0f48ecbe378757f694cdb2339e9bac01ea 100644 (file)
@@ -7,7 +7,9 @@
   <bean id="ofPluginProviderFactory" class="org.opendaylight.openflowplugin.impl.OpenFlowPluginProviderFactoryImpl"/>
   <service ref="ofPluginProviderFactory" interface="org.opendaylight.openflowplugin.api.openflow.OpenFlowPluginProviderFactory"/>
 
-  <bean id="configurationServiceFactory" class="org.opendaylight.openflowplugin.impl.configuration.ConfigurationServiceFactoryImpl"/>
+  <bean id="configurationServiceFactory" class="org.opendaylight.openflowplugin.impl.configuration.ConfigurationServiceFactoryOsgiImpl">
+    <argument ref="blueprintBundleContext"/>
+  </bean>
   <service ref="configurationServiceFactory" interface="org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationServiceFactory"/>
 
   <bean id="mastershipChangeServiceManager" class="org.opendaylight.openflowplugin.impl.mastership.MastershipChangeServiceManagerImpl"/>
index e5010013aead1de46e8febbe91bac6d2c7560676..252380350684893e2c341d6a341a3c9e10229fd4 100644 (file)
@@ -16,24 +16,19 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import com.google.common.collect.ImmutableMap;
-import java.util.Dictionary;
 import java.util.Hashtable;
+import java.util.Map;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.openflowplugin.api.OFConstants;
 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationProperty;
 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint16Type;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.cm.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
 
 @RunWith(MockitoJUnitRunner.class)
 public class ConfigurationServiceFactoryImplTest {
@@ -60,21 +55,9 @@ public class ConfigurationServiceFactoryImplTest {
     @Mock
     private OpenflowProviderConfig config;
 
-    @Mock
-    private BundleContext bundleContext;
-
     @Mock
     private ConfigurationListener configurationListener;
 
-    @Mock
-    private ConfigurationAdmin configurationAdmin;
-
-    @Mock
-    private ServiceReference<ConfigurationAdmin> serviceReference;
-
-    @Mock
-    private Configuration configuration;
-
     private ConfigurationService configurationService;
 
     @Before
@@ -104,16 +87,12 @@ public class ConfigurationServiceFactoryImplTest {
         when(config.getThreadPoolTimeout()).thenReturn(THREAD_POOL_TIMEOUT);
         when(config.getDeviceConnectionRateLimitPerMin()).thenReturn(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN);
 
-        final Dictionary<String, Object> properties = new Hashtable<>();
-        properties.put(ConfigurationProperty.IS_STATISTICS_POLLING_ON.toString(), IS_STATISTICS_POLLING_ON);
+        final Map<String, String> properties = new Hashtable<>();
+        properties.put(ConfigurationProperty.IS_STATISTICS_POLLING_ON.toString(),
+                Boolean.toString(IS_STATISTICS_POLLING_ON));
 
-        when(configuration.getProperties()).thenReturn(properties);
-        when(configurationAdmin.getConfiguration(OFConstants.CONFIG_FILE_ID)).thenReturn(configuration);
-        when(bundleContext.getService(serviceReference)).thenReturn(configurationAdmin);
-        when(bundleContext.getServiceReference(ConfigurationAdmin.class)).thenReturn(serviceReference);
-
-        configurationService = new ConfigurationServiceFactoryImpl()
-                .newInstance(config, bundleContext);
+        configurationService = new ConfigurationServiceFactoryImpl().newInstance(config);
+        configurationService.update(properties);
     }
 
     @Test
@@ -153,5 +132,4 @@ public class ConfigurationServiceFactoryImplTest {
         configurationService.close();
         configurationService.getProperty(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString(), Integer::valueOf);
     }
-
-}
\ No newline at end of file
+}