Convert netconf-util, netconf-ssh, netconf-tcp to blueprint 18/61718/9
authorRyan Goulding <ryandgoulding@gmail.com>
Tue, 24 Oct 2017 21:23:10 +0000 (17:23 -0400)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 25 Feb 2018 22:17:40 +0000 (23:17 +0100)
netconf-util, netconf-ssh, and netconf-tcp still used a BundleActivator
and accompanying ServiceTracker for service instantiation.  netconf-ssh
and netconf-tcp depend on netconf-util for NetconfConfiguration, so it
was decided to do the conversion of all three bundles in one patch.

netconf-util utilizes blueprint-cm to instantiate NetconfConfiguration
based on the ManagedService identified by the "netconf" PID.

Tests that were aimed at testing bespoke Activator logic were removed,
since the accompanying Activators were removed.  ServiceTrackers were
removed in favor of bean references.  Scaffolding used to locate the
NetconfConfiguration ManagedService was also removed since it is now
directly referenced using blueprint.

JIRA: NETCONF-510
Change-Id: I27452ea509bc50333ffc00c6d1d8de3133e8b850
Signed-off-by: Ryan Goulding <ryandgoulding@gmail.com>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
24 files changed:
features/netconf/odl-netconf-ssh/pom.xml
netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfImplActivator.java
netconf/netconf-ssh/pom.xml
netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/NetconfSSHProvider.java [moved from netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/osgi/NetconfSSHActivator.java with 73% similarity]
netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/osgi/AuthProviderTracker.java [deleted file]
netconf/netconf-ssh/src/main/resources/org/opendaylight/blueprint/netconf-ssh-blueprint.xml [new file with mode: 0644]
netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/EchoServer.java
netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/ProxyServer.java
netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/SSHTest.java
netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/ssh/authentication/SSHServerTest.java
netconf/netconf-tcp/pom.xml
netconf/netconf-tcp/src/main/java/org/opendaylight/netconf/tcp/NetconfTCPProvider.java [moved from netconf/netconf-tcp/src/main/java/org/opendaylight/netconf/tcp/osgi/NetconfTCPActivator.java with 61% similarity]
netconf/netconf-tcp/src/main/resources/org/opendaylight/blueprint/netconf-tcp-blueprint.xml [new file with mode: 0644]
netconf/netconf-util/pom.xml
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfConfiguration.java [new file with mode: 0644]
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfConfigurationHolder.java [moved from netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationHolder.java with 96% similarity]
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfConfigurationImpl.java [moved from netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfiguration.java with 66% similarity]
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigUtil.java [deleted file]
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivator.java [deleted file]
netconf/netconf-util/src/main/resources/netconf.cfg
netconf/netconf-util/src/main/resources/org/opendaylight/blueprint/netconf-util-blueprint.xml [new file with mode: 0644]
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NetconfConfigurationImplTest.java [moved from netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationTest.java with 84% similarity]
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigUtilTest.java [deleted file]
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivatorTest.java [deleted file]

index 4f3849eb8b6aef78e3af791289cacd992f1e2736..500dd1bac5b3d0007c42c7255e74993f59a512d1 100644 (file)
     <name>OpenDaylight :: Netconf Connector :: SSH</name>
 
     <dependencies>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>odl-aaa-netconf-plugin</artifactId>
+            <version>${project.version}</version>
+            <type>xml</type>
+            <classifier>features</classifier>
+        </dependency>
         <dependency>
             <groupId>${project.groupId}</groupId>
             <artifactId>odl-netconf-util</artifactId>
index df45459e1efc75c0c85d84da711b1c47dd252a2f..8529da681dcd438e4840fa79a6bbd43f332c2c21 100644 (file)
@@ -23,7 +23,7 @@ import org.opendaylight.netconf.impl.SessionIdProvider;
 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
 import org.opendaylight.netconf.notifications.BaseNotificationPublisherRegistration;
 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
-import org.opendaylight.netconf.util.osgi.NetconfConfiguration;
+import org.opendaylight.netconf.util.NetconfConfiguration;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
index 58a0f7d927c3f777a4e212cd4b10d194356768c7..9f07f70e16a392713ced51db2e6b1a010c8dbb51 100644 (file)
       <plugin>
         <groupId>org.apache.felix</groupId>
         <artifactId>maven-bundle-plugin</artifactId>
-        <configuration>
-          <instructions>
-            <Bundle-Activator>org.opendaylight.netconf.ssh.osgi.NetconfSSHActivator</Bundle-Activator>
-          </instructions>
-        </configuration>
       </plugin>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
similarity index 73%
rename from netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/osgi/NetconfSSHActivator.java
rename to netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/NetconfSSHProvider.java
index 0a15fdb608c3bde1774ab265ddb9a9ae2ddc9e06..971e50631ed5fc11ddfa8d4b89f7d6104f9c22d8 100644 (file)
@@ -5,7 +5,7 @@
  * 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.netconf.ssh.osgi;
+package org.opendaylight.netconf.ssh;
 
 import io.netty.channel.local.LocalAddress;
 import io.netty.channel.nio.NioEventLoopGroup;
@@ -18,50 +18,53 @@ import java.util.concurrent.ScheduledExecutorService;
 import org.apache.sshd.common.util.security.SecurityUtils;
 import org.apache.sshd.common.util.threads.ThreadUtils;
 import org.apache.sshd.server.keyprovider.AbstractGeneratorHostKeyProvider;
-import org.opendaylight.netconf.ssh.SshProxyServer;
-import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
-import org.opendaylight.netconf.util.osgi.NetconfConfigUtil;
-import org.opendaylight.netconf.util.osgi.NetconfConfiguration;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
+import org.opendaylight.netconf.auth.AuthProvider;
+import org.opendaylight.netconf.util.NetconfConfiguration;
 import org.osgi.framework.InvalidSyntaxException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class NetconfSSHActivator implements BundleActivator {
-    private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHActivator.class);
+public class NetconfSSHProvider {
+    private static final Logger LOG = LoggerFactory.getLogger(NetconfSSHProvider.class);
 
     private static final java.lang.String ALGORITHM = "RSA";
     private static final int KEY_SIZE = 4096;
     public static final int POOL_SIZE = 8;
     private static final int DEFAULT_IDLE_TIMEOUT = Integer.MAX_VALUE;
 
+    private final AuthProvider authProvider;
+    private final NetconfConfiguration netconfConfiguration;
+
     private ScheduledExecutorService minaTimerExecutor;
     private NioEventLoopGroup clientGroup;
     private ExecutorService nioExecutor;
-    private AuthProviderTracker authProviderTracker;
 
     private SshProxyServer server;
 
-    @Override
-    public void start(final BundleContext bundleContext) throws IOException, InvalidSyntaxException {
+    public NetconfSSHProvider(final AuthProvider authProvider,
+                              final NetconfConfiguration netconfConfiguration) {
+
+        this.authProvider = authProvider;
+        this.netconfConfiguration = netconfConfiguration;
+    }
+
+    // Called via blueprint
+    @SuppressWarnings("unused")
+    public void init() throws IOException, InvalidSyntaxException {
         minaTimerExecutor = Executors.newScheduledThreadPool(POOL_SIZE,
             runnable -> new Thread(runnable, "netconf-ssh-server-mina-timers"));
         clientGroup = new NioEventLoopGroup();
         nioExecutor = ThreadUtils.newFixedThreadPool("netconf-ssh-server-nio-group", POOL_SIZE);
-        server = startSSHServer(bundleContext);
+        server = startSSHServer();
     }
 
-    @Override
-    public void stop(final BundleContext context) throws IOException {
+    // Called via blueprint
+    @SuppressWarnings("unused")
+    public void destroy() throws IOException {
         if (server != null) {
             server.close();
         }
 
-        if (authProviderTracker != null) {
-            authProviderTracker.stop();
-        }
-
         if (nioExecutor != null) {
             nioExecutor.shutdownNow();
         }
@@ -75,16 +78,13 @@ public class NetconfSSHActivator implements BundleActivator {
         }
     }
 
-    private SshProxyServer startSSHServer(final BundleContext bundleContext)
+    private SshProxyServer startSSHServer()
             throws IOException, InvalidSyntaxException {
-        final NetconfConfiguration netconfConfiguration =
-                NetconfConfigUtil.getNetconfConfigurationService(bundleContext);
 
         final InetSocketAddress sshSocketAddress = netconfConfiguration.getSshServerAddress();
         LOG.info("Starting netconf SSH server at {}", sshSocketAddress);
 
         final LocalAddress localAddress = NetconfConfiguration.NETCONF_LOCAL_ADDRESS;
-        authProviderTracker = new AuthProviderTracker(bundleContext);
 
         final String path = netconfConfiguration.getPrivateKeyPath();
         LOG.trace("Starting netconf SSH server with path to ssh private key {}", path);
@@ -98,7 +98,7 @@ public class NetconfSSHActivator implements BundleActivator {
                 new SshProxyServerConfigurationBuilder()
                         .setBindingAddress(sshSocketAddress)
                         .setLocalAddress(localAddress)
-                        .setAuthenticator(authProviderTracker)
+                        .setAuthenticator(authProvider)
                         .setKeyPairProvider(keyPairProvider)
                         .setIdleTimeout(DEFAULT_IDLE_TIMEOUT)
                         .createSshProxyServerConfiguration());
diff --git a/netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/osgi/AuthProviderTracker.java b/netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/osgi/AuthProviderTracker.java
deleted file mode 100644 (file)
index 124e7e7..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Copyright (c) 2014 Cisco Systems, 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.netconf.ssh.osgi;
-
-import org.opendaylight.netconf.auth.AuthProvider;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.util.tracker.ServiceTracker;
-import org.osgi.util.tracker.ServiceTrackerCustomizer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-final class AuthProviderTracker implements ServiceTrackerCustomizer<AuthProvider, AuthProvider>, AuthProvider {
-    private static final Logger LOG = LoggerFactory.getLogger(AuthProviderTracker.class);
-
-    private final BundleContext bundleContext;
-
-    private final ServiceTracker<AuthProvider, AuthProvider> listenerTracker;
-    private volatile AuthProvider authProvider;
-
-    AuthProviderTracker(final BundleContext bundleContext) {
-        this.bundleContext = bundleContext;
-        listenerTracker = new ServiceTracker<>(bundleContext, AuthProvider.class, this);
-        listenerTracker.open();
-    }
-
-    @Override
-    public AuthProvider addingService(final ServiceReference<AuthProvider> reference) {
-        LOG.trace("Service {} added", reference);
-        this.authProvider = bundleContext.getService(reference);
-        return authProvider;
-    }
-
-    @Override
-    public void modifiedService(final ServiceReference<AuthProvider> reference, final AuthProvider service) {
-        final AuthProvider authService = bundleContext.getService(reference);
-        LOG.trace("Replacing modified service {} in netconf SSH.", reference);
-        this.authProvider = authService;
-    }
-
-    @Override
-    public void removedService(final ServiceReference<AuthProvider> reference, final AuthProvider service) {
-        LOG.trace("Removing service {} from netconf SSH. {}", reference,
-                " SSH won't authenticate users until AuthProvider service will be started.");
-        this.authProvider = null;
-    }
-
-    public void stop() {
-        listenerTracker.close();
-        this.authProvider = null;
-        // sshThread should finish normally since sshServer.close stops processing
-    }
-
-    @Override
-    public boolean authenticated(final String username, final String password) {
-        if (authProvider == null) {
-            LOG.warn("AuthProvider is missing, failing authentication");
-            return false;
-        }
-        return authProvider.authenticated(username, password);
-    }
-}
diff --git a/netconf/netconf-ssh/src/main/resources/org/opendaylight/blueprint/netconf-ssh-blueprint.xml b/netconf/netconf-ssh/src/main/resources/org/opendaylight/blueprint/netconf-ssh-blueprint.xml
new file mode 100644 (file)
index 0000000..2f504c0
--- /dev/null
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright (c) 2017 Inocybe Technologies 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
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0"
+           odl:use-default-for-reference-types="true">
+
+    <reference id="authProvider"
+               interface="org.opendaylight.netconf.auth.AuthProvider"
+               odl:type="netconf-auth-provider" />
+
+    <reference id="netconfConfiguration"
+               interface="org.opendaylight.netconf.util.NetconfConfiguration" />
+
+    <bean id="netconfSshProvider" class="org.opendaylight.netconf.ssh.NetconfSSHProvider"
+          init-method="init" destroy-method="destroy">
+        <argument ref="authProvider" />
+        <argument ref="netconfConfiguration" />
+    </bean>
+
+</blueprint>
index c660ffd02b0f0643b4a4e088fdaa07c797f589f5..fed42f255b57af182c8312530bbbddee247b4ebc 100644 (file)
@@ -21,7 +21,7 @@ import io.netty.handler.logging.LogLevel;
 import io.netty.handler.logging.LoggingHandler;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
-import org.opendaylight.netconf.util.osgi.NetconfConfiguration;
+import org.opendaylight.netconf.util.NetconfConfiguration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
index 3b693512ea659ec1ae227f3a8f6cec8b903989fa..b465e381f2be3d862a59ba9dd1ce6353065205a0 100644 (file)
@@ -21,7 +21,7 @@ import io.netty.channel.socket.nio.NioServerSocketChannel;
 import io.netty.handler.logging.LogLevel;
 import io.netty.handler.logging.LoggingHandler;
 import java.net.InetSocketAddress;
-import org.opendaylight.netconf.util.osgi.NetconfConfiguration;
+import org.opendaylight.netconf.util.NetconfConfiguration;
 
 public class ProxyServer implements Runnable {
     private final ProxyHandlerFactory proxyHandlerFactory;
index 38907bcf509e2a93ae1b19e020a40fc5f6ccbe56..a708f4f02f5decbffdaa4c9413ab0c84897aa054 100644 (file)
@@ -35,7 +35,7 @@ import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPasswo
 import org.opendaylight.netconf.nettyutil.handler.ssh.client.AsyncSshHandler;
 import org.opendaylight.netconf.ssh.SshProxyServer;
 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
-import org.opendaylight.netconf.util.osgi.NetconfConfiguration;
+import org.opendaylight.netconf.util.NetconfConfiguration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
index e660628a67653d9c1c4bfe39da4277100fa108ae..5b07598b5d4fa3f8badd2d60f160976f59a5c1b4 100644 (file)
@@ -32,7 +32,7 @@ import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.opendaylight.netconf.ssh.SshProxyServer;
 import org.opendaylight.netconf.ssh.SshProxyServerConfigurationBuilder;
-import org.opendaylight.netconf.util.osgi.NetconfConfiguration;
+import org.opendaylight.netconf.util.NetconfConfiguration;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceListener;
 import org.osgi.framework.ServiceReference;
index 04cce67209faacfee552ab4450d4725c32be01d2..c9e8ee179b5d7ae6967f25e5601e4802ec1b2a49 100644 (file)
       <plugin>
         <groupId>org.apache.felix</groupId>
         <artifactId>maven-bundle-plugin</artifactId>
-        <configuration>
-          <instructions>
-            <Bundle-Activator>org.opendaylight.netconf.tcp.osgi.NetconfTCPActivator</Bundle-Activator>
-          </instructions>
-        </configuration>
       </plugin>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
similarity index 61%
rename from netconf/netconf-tcp/src/main/java/org/opendaylight/netconf/tcp/osgi/NetconfTCPActivator.java
rename to netconf/netconf-tcp/src/main/java/org/opendaylight/netconf/tcp/NetconfTCPProvider.java
index d95d2b84a2a793bce5f9b10309385e3695bd3c88..d53cf409162c2024911f22df85191762a04dfaf5 100644 (file)
@@ -6,14 +6,11 @@
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
-package org.opendaylight.netconf.tcp.osgi;
+package org.opendaylight.netconf.tcp;
 
 import java.net.InetSocketAddress;
 import org.opendaylight.netconf.tcp.netty.ProxyServer;
-import org.opendaylight.netconf.util.osgi.NetconfConfigUtil;
-import org.opendaylight.netconf.util.osgi.NetconfConfiguration;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
+import org.opendaylight.netconf.util.NetconfConfiguration;
 import org.osgi.framework.InvalidSyntaxException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -21,26 +18,33 @@ import org.slf4j.LoggerFactory;
 /**
  * Opens TCP port specified in config.ini, creates bridge between this port and local netconf server.
  */
-public class NetconfTCPActivator implements BundleActivator {
-    private static final Logger LOG = LoggerFactory.getLogger(NetconfTCPActivator.class);
+public class NetconfTCPProvider {
+    private static final Logger LOG = LoggerFactory.getLogger(NetconfTCPProvider.class);
+
+    private final NetconfConfiguration netconfConfiguration;
     private ProxyServer proxyServer;
 
-    @Override
-    public void start(BundleContext context) throws InvalidSyntaxException {
-        final NetconfConfiguration netconfConfiguration = NetconfConfigUtil.getNetconfConfigurationService(context);
+    public NetconfTCPProvider(final NetconfConfiguration netconfConfiguration) {
+        this.netconfConfiguration = netconfConfiguration;
+    }
 
+    // Called via blueprint
+    @SuppressWarnings("unused")
+    public void init() throws InvalidSyntaxException {
         final InetSocketAddress address = netconfConfiguration.getTcpServerAddress();
 
         if (address.getAddress().isAnyLocalAddress()) {
-            LOG.warn("Unprotected netconf TCP address is configured to ANY local address. This is a security risk. "
-                    + "Consider changing tcp-address in netconf.cfg to 127.0.0.1");
+            LOG.warn("Unprotected netconf TCP address is configured to ANY "
+                    + "local address. This is a security risk. Consider "
+                    + "changing tcp-address in netconf.cfg to 127.0.0.1");
         }
         LOG.info("Starting TCP netconf server at {}", address);
         proxyServer = new ProxyServer(address, NetconfConfiguration.NETCONF_LOCAL_ADDRESS);
     }
 
-    @Override
-    public void stop(BundleContext context) {
+    // Called via blueprint
+    @SuppressWarnings("unused")
+    public void destroy() {
         if (proxyServer != null) {
             proxyServer.close();
         }
diff --git a/netconf/netconf-tcp/src/main/resources/org/opendaylight/blueprint/netconf-tcp-blueprint.xml b/netconf/netconf-tcp/src/main/resources/org/opendaylight/blueprint/netconf-tcp-blueprint.xml
new file mode 100644 (file)
index 0000000..5423731
--- /dev/null
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright (c) 2017 Inocybe Technologies 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
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0"
+           odl:use-default-for-reference-types="true">
+
+    <reference id="netconfConfiguration"
+               interface="org.opendaylight.netconf.util.NetconfConfiguration" />
+
+    <bean id="netconfTcpProvider" class="org.opendaylight.netconf.tcp.NetconfTCPProvider"
+          init-method="init" destroy-method="destroy">
+        <argument ref="netconfConfiguration" />
+    </bean>
+
+</blueprint>
index 3c6c3930d1a9c341d236d7f9338955b603488aa1..d9a0f4331510c2298ef1e929d080ace0a7b5b83c 100644 (file)
       <plugin>
         <groupId>org.apache.felix</groupId>
         <artifactId>maven-bundle-plugin</artifactId>
-        <configuration>
-          <instructions>
-            <Bundle-Activator>org.opendaylight.netconf.util.osgi.NetconfConfigurationActivator</Bundle-Activator>
-          </instructions>
-        </configuration>
       </plugin>
       <plugin>
         <groupId>org.codehaus.mojo</groupId>
diff --git a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfConfiguration.java b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfConfiguration.java
new file mode 100644 (file)
index 0000000..6cada7f
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2017 Inocybe Technologies 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.netconf.util;
+
+import io.netty.channel.local.LocalAddress;
+import java.net.InetSocketAddress;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Configuration for NETCONF northbound.
+ */
+public interface NetconfConfiguration {
+
+    /**
+     * LocalAddress constant for NETCONF northbound.
+     */
+    LocalAddress NETCONF_LOCAL_ADDRESS = new LocalAddress("netconf");
+
+    /**
+     * Default timeout for NETCONF northbound connections.
+     */
+    long DEFAULT_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(30);
+
+    /**
+     * NETCONF SSH server address.
+     *
+     * @return NETCONF SSH server address
+     */
+    InetSocketAddress getSshServerAddress();
+
+    /**
+     * NETCONF TCP server address.
+     *
+     * @return NETCONF TCP server address.
+     */
+    InetSocketAddress getTcpServerAddress();
+
+    /**
+     * Private key path for NETCONF.
+     *
+     * @return Private key path for NETCONF.
+     */
+    String getPrivateKeyPath();
+}
similarity index 66%
rename from netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfiguration.java
rename to netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfConfigurationImpl.java
index c67675a91062780df065aedbf9f898abbf7df199..42c7b58871681786f58aece834783342ffad4ab9 100644 (file)
@@ -6,20 +6,20 @@
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
-package org.opendaylight.netconf.util.osgi;
+package org.opendaylight.netconf.util;
 
-import io.netty.channel.local.LocalAddress;
 import java.net.InetSocketAddress;
 import java.util.Dictionary;
-import java.util.concurrent.TimeUnit;
+import java.util.Hashtable;
 import org.osgi.service.cm.ManagedService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class NetconfConfiguration implements ManagedService {
-    private static final Logger LOG = LoggerFactory.getLogger(NetconfConfiguration.class);
+public class NetconfConfigurationImpl implements NetconfConfiguration, ManagedService {
 
-    /**
+    private static final Logger LOG = LoggerFactory.getLogger(NetconfConfigurationImpl.class);
+
+    /*
      * Props to access information within the dictionary.
      */
 
@@ -29,30 +29,27 @@ public class NetconfConfiguration implements ManagedService {
     private static final String TCP_PORT_PROP = "tcp-port";
     private static final String SSH_PK_PATH_PROP = "ssh-pk-path";
 
-    /**
-     * Default values used if no dictionary is provided.
-     */
-
-    public static final LocalAddress NETCONF_LOCAL_ADDRESS = new LocalAddress("netconf");
-    public static final long DEFAULT_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(30);
+    private NetconfConfigurationHolder netconfConfiguration;
 
-    private static final String LOCAL_HOST = "127.0.0.1";
-    private static final String INADDR_ANY = "0.0.0.0";
-    private static final String DEFAULT_PRIVATE_KEY_PATH = "./configuration/RSA.pk";
-    private static final InetSocketAddress DEFAULT_TCP_SERVER_ADRESS = new InetSocketAddress(LOCAL_HOST, 8383);
-    private static final InetSocketAddress DEFAULT_SSH_SERVER_ADRESS = new InetSocketAddress(INADDR_ANY, 1830);
+    public NetconfConfigurationImpl(final String tcpServerAddress, final String tcpServerPort,
+                                    final String sshServerAddress, final String sshServerPort,
+                                    final String privateKeyPath) throws NumberFormatException {
 
-    private NetconfConfigurationHolder netconfConfiguration;
+        // isolate configuration to "updated(...)" instead of repeating logic here
+        final Dictionary<String, String> dictionaryConfig = new Hashtable<>();
+        dictionaryConfig.put(TCP_ADDRESS_PROP, tcpServerAddress);
+        dictionaryConfig.put(TCP_PORT_PROP, tcpServerPort);
+        dictionaryConfig.put(SSH_ADDRESS_PROP, sshServerAddress);
+        dictionaryConfig.put(SSH_PORT_PROP, sshServerPort);
+        dictionaryConfig.put(SSH_PK_PATH_PROP, privateKeyPath);
 
-    NetconfConfiguration() {
-        netconfConfiguration = new NetconfConfigurationHolder(DEFAULT_TCP_SERVER_ADRESS,
-                DEFAULT_SSH_SERVER_ADRESS, DEFAULT_PRIVATE_KEY_PATH);
+        updated(dictionaryConfig);
     }
 
     @Override
     public void updated(final Dictionary<String, ?> dictionaryConfig) {
         if (dictionaryConfig == null) {
-            LOG.debug("CSS netconf server configuration cannot be updated as passed dictionary is null");
+            LOG.debug("CSS NETCONF server configuration cannot be updated as passed dictionary is null");
             return;
         }
         final InetSocketAddress sshServerAddress =
@@ -69,14 +66,17 @@ public class NetconfConfiguration implements ManagedService {
         LOG.debug("CSS netconf server configuration was updated: {}", dictionaryConfig.toString());
     }
 
+    @Override
     public InetSocketAddress getSshServerAddress() {
         return netconfConfiguration.getSshServerAddress();
     }
 
+    @Override
     public InetSocketAddress getTcpServerAddress() {
         return netconfConfiguration.getTcpServerAddress();
     }
 
+    @Override
     public String getPrivateKeyPath() {
         return netconfConfiguration.getPrivateKeyPath();
     }
diff --git a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigUtil.java b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigUtil.java
deleted file mode 100644 (file)
index 64537f3..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2013 Cisco Systems, 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.netconf.util.osgi;
-
-import java.util.Collection;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.cm.ManagedService;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public final class NetconfConfigUtil {
-    private static final Logger LOG = LoggerFactory.getLogger(NetconfConfigUtil.class);
-
-    private NetconfConfigUtil() {
-        throw new AssertionError("Utility class should not be instantiated");
-    }
-
-    public static NetconfConfiguration getNetconfConfigurationService(BundleContext bundleContext)
-            throws InvalidSyntaxException {
-        LOG.debug("Trying to retrieve netconf configuration service");
-        final Collection<ServiceReference<ManagedService>> serviceReferences
-                = bundleContext.getServiceReferences(ManagedService.class, null);
-        for (final ServiceReference<ManagedService> serviceReference : serviceReferences) {
-            ManagedService service = bundleContext.getService(serviceReference);
-            if (service instanceof NetconfConfiguration) {
-                LOG.debug("Netconf configuration service found");
-                return (NetconfConfiguration) service;
-            }
-        }
-
-        throw new IllegalStateException("Netconf configuration service not found");
-    }
-}
diff --git a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivator.java b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivator.java
deleted file mode 100644 (file)
index 3c1a9c5..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, 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.netconf.util.osgi;
-
-import java.util.Hashtable;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.cm.ManagedService;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class NetconfConfigurationActivator implements BundleActivator {
-    private static final Logger LOG = LoggerFactory.getLogger(NetconfConfigurationActivator.class);
-
-    // This has to match netconf config filename without .cfg suffix
-    private static final String CONFIG_PID = "netconf";
-    private static final Hashtable<String, String> PROPS = new Hashtable<>(1);
-
-    static {
-        PROPS.put(Constants.SERVICE_PID, CONFIG_PID);
-    }
-
-    private ServiceRegistration<?> configService;
-
-    @Override
-    public void start(final BundleContext bundleContext) {
-        LOG.debug("Starting netconf configuration service");
-        configService = bundleContext.registerService(ManagedService.class,
-                new NetconfConfiguration(), PROPS);
-    }
-
-    @Override
-    public void stop(final BundleContext bundleContext) {
-        if (configService != null) {
-            LOG.debug("Unregistering netconf configuration service");
-            configService.unregister();
-            configService = null;
-        }
-    }
-}
index 01437d891ad9f5c3c1a32c270268a90c117fb5bd..01d92ec6364bfb9029872f788989bce49e3d93af 100644 (file)
@@ -1,11 +1,10 @@
 # netconf-tcp:
-
-tcp-address=127.0.0.1
-tcp-port=8383
+#tcp-address=127.0.0.1
+#tcp-port=8383
 
 # netconf-ssh:
+#ssh-address=0.0.0.0
+#ssh-port=1830
 
-ssh-address=0.0.0.0
-ssh-port=1830
 # Use Linux style path
-ssh-pk-path = ./configuration/RSA.pk
\ No newline at end of file
+#ssh-pk-path = ./configuration/RSA.pk
\ No newline at end of file
diff --git a/netconf/netconf-util/src/main/resources/org/opendaylight/blueprint/netconf-util-blueprint.xml b/netconf/netconf-util/src/main/resources/org/opendaylight/blueprint/netconf-util-blueprint.xml
new file mode 100644 (file)
index 0000000..99cab21
--- /dev/null
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright (c) 2017 Inocybe Technologies 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
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0"
+           xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
+           odl:use-default-for-reference-types="true">
+
+    <!-- The PID "netconf" is used for backwards-compatibility even though it is
+         an ambiguous name. -->
+    <cm:property-placeholder persistent-id="netconf" update-strategy="reload">
+        <cm:default-properties>
+            <cm:property name="tcp-address" value="127.0.0.1" />
+            <cm:property name="tcp-port" value="8383" />
+            <cm:property name="ssh-address" value="0.0.0.0" />
+            <cm:property name="ssh-port" value="1830" />
+            <cm:property name="ssh-pk-path" value="./configuration/RSA.pk" />
+        </cm:default-properties>
+    </cm:property-placeholder>
+
+    <bean id="netconfConfiguration" class="org.opendaylight.netconf.util.NetconfConfigurationImpl">
+        <argument value="${tcp-address}" />
+        <argument value="${tcp-port}" />
+        <argument value="${ssh-address}" />
+        <argument value="${ssh-port}" />
+        <argument value="${ssh-pk-path}" />
+    </bean>
+    <service ref="netconfConfiguration" interface="org.opendaylight.netconf.util.NetconfConfiguration" />
+
+</blueprint>
similarity index 84%
rename from netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationTest.java
rename to netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NetconfConfigurationImplTest.java
index 46414f7dc34c45e992e7425a2f6593a4bf0d027d..99b89ac87796f123d82c561244dfcb4405c2fda2 100644 (file)
@@ -6,7 +6,7 @@
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
-package org.opendaylight.netconf.util.osgi;
+package org.opendaylight.netconf.util;
 
 import java.net.InetSocketAddress;
 import java.util.Dictionary;
@@ -14,11 +14,13 @@ import java.util.Hashtable;
 import org.junit.Assert;
 import org.junit.Test;
 
-public class NetconfConfigurationTest {
+public class NetconfConfigurationImplTest {
 
     @Test
     public void testUpdated() throws Exception {
-        final NetconfConfiguration config = new NetconfConfiguration();
+        final NetconfConfigurationImpl config = new NetconfConfigurationImpl("127.0.0.1", "8383",
+                "0.0.0.0", "1830",
+                "./configuration/RSA.pk");
         Assert.assertEquals(new InetSocketAddress("0.0.0.0", 1830), config.getSshServerAddress());
         Assert.assertEquals(new InetSocketAddress("127.0.0.1", 8383), config.getTcpServerAddress());
         Assert.assertEquals("./configuration/RSA.pk", config.getPrivateKeyPath());
@@ -41,7 +43,9 @@ public class NetconfConfigurationTest {
 
     @Test
     public void testUpdatedNull() throws Exception {
-        final NetconfConfiguration config = new NetconfConfiguration();
+        final NetconfConfigurationImpl config = new NetconfConfigurationImpl("127.0.0.1", "8383",
+                "0.0.0.0", "1830",
+                "./configuration/RSA.pk");
         Assert.assertEquals(new InetSocketAddress("0.0.0.0", 1830), config.getSshServerAddress());
         Assert.assertEquals(new InetSocketAddress("127.0.0.1", 8383), config.getTcpServerAddress());
         Assert.assertEquals("./configuration/RSA.pk", config.getPrivateKeyPath());
diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigUtilTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigUtilTest.java
deleted file mode 100644 (file)
index 64bf74f..0000000
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, 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.netconf.util.osgi;
-
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.doThrow;
-import static org.mockito.Mockito.mock;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.cm.ManagedService;
-
-public class NetconfConfigUtilTest {
-
-    @Mock
-    private ServiceReference<ManagedService> serviceRef;
-
-    @Mock
-    private ServiceReference<ManagedService> netconfConfigurationRef;
-
-    @Before
-    public void setUp() {
-        MockitoAnnotations.initMocks(this);
-    }
-
-    @Test
-    public void testGetNetconfConfigurationService() throws Exception {
-        final Collection<ServiceReference<ManagedService>> services = new ArrayList<>();
-        services.add(serviceRef);
-        services.add(netconfConfigurationRef);
-        final BundleContext context = mock(BundleContext.class);
-        doReturn(services).when(context).getServiceReferences(ManagedService.class, null);
-        final ManagedService service = mock(ManagedService.class);
-        doReturn(service).when(context).getService(serviceRef);
-        NetconfConfiguration netconfConfiguration = new NetconfConfiguration();
-        doReturn(netconfConfiguration).when(context).getService(netconfConfigurationRef);
-        final NetconfConfiguration actualNetconfConfiguration =
-                NetconfConfigUtil.getNetconfConfigurationService(context);
-        Assert.assertEquals(netconfConfiguration, actualNetconfConfiguration);
-
-    }
-
-    @Test
-    public void testGetNetconfConfigurationServiceAbsent() throws Exception {
-        final Collection<ServiceReference<ManagedService>> services = new ArrayList<>();
-        services.add(serviceRef);
-        final BundleContext context = mock(BundleContext.class);
-        doReturn(services).when(context).getServiceReferences(ManagedService.class, null);
-        final ManagedService service = mock(ManagedService.class);
-        doReturn(service).when(context).getService(serviceRef);
-        try {
-            NetconfConfigUtil.getNetconfConfigurationService(context);
-            Assert.fail(IllegalStateException.class + "exception expected");
-        } catch (IllegalStateException e) {
-            assertTrue(e.getMessage().startsWith("Netconf configuration service not found"));
-        }
-    }
-
-    @Test
-    public void testGetNetconfConfigurationServiceInvalidSyntax() throws Exception {
-        final BundleContext context = mock(BundleContext.class);
-        final InvalidSyntaxException exception = new InvalidSyntaxException("Invalid syntax", "filter");
-        doThrow(exception).when(context).getServiceReferences(ManagedService.class, null);
-        try {
-            NetconfConfigUtil.getNetconfConfigurationService(context);
-            Assert.fail(InvalidSyntaxException.class + "exception expected");
-        } catch (InvalidSyntaxException e) {
-            return;
-        }
-    }
-}
diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivatorTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivatorTest.java
deleted file mode 100644 (file)
index 70fadc2..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, 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.netconf.util.osgi;
-
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.verify;
-
-import java.util.Dictionary;
-import java.util.Hashtable;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.cm.ManagedService;
-
-public class NetconfConfigurationActivatorTest {
-
-    @Mock
-    private BundleContext context;
-    @Mock
-    private ServiceRegistration<?> registration;
-
-    @Before
-    public void setUp() throws Exception {
-        MockitoAnnotations.initMocks(this);
-        doReturn(registration).when(context)
-                .registerService(eq(ManagedService.class), any(NetconfConfiguration.class), any());
-        doNothing().when(registration).unregister();
-    }
-
-    @Test
-    public void testStartStop() throws Exception {
-        final NetconfConfigurationActivator activator = new NetconfConfigurationActivator();
-        activator.start(context);
-        final Dictionary<String, String> props = new Hashtable<>();
-        props.put(Constants.SERVICE_PID, "netconf");
-        verify(context).registerService(eq(ManagedService.class), any(NetconfConfiguration.class), eq(props));
-        activator.stop(context);
-        verify(registration).unregister();
-    }
-
-}
\ No newline at end of file