From: Ryan Goulding Date: Tue, 24 Oct 2017 21:23:10 +0000 (-0400) Subject: Convert netconf-util, netconf-ssh, netconf-tcp to blueprint X-Git-Tag: release/fluorine~150^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=7072e63a98485bbbfa686a639d28002377972aa0;p=netconf.git Convert netconf-util, netconf-ssh, netconf-tcp to blueprint 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 Signed-off-by: Robert Varga --- diff --git a/features/netconf/odl-netconf-ssh/pom.xml b/features/netconf/odl-netconf-ssh/pom.xml index 4f3849eb8b..500dd1bac5 100644 --- a/features/netconf/odl-netconf-ssh/pom.xml +++ b/features/netconf/odl-netconf-ssh/pom.xml @@ -26,6 +26,13 @@ OpenDaylight :: Netconf Connector :: SSH + + ${project.groupId} + odl-aaa-netconf-plugin + ${project.version} + xml + features + ${project.groupId} odl-netconf-util diff --git a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfImplActivator.java b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfImplActivator.java index df45459e1e..8529da681d 100644 --- a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfImplActivator.java +++ b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfImplActivator.java @@ -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; diff --git a/netconf/netconf-ssh/pom.xml b/netconf/netconf-ssh/pom.xml index 58a0f7d927..9f07f70e16 100644 --- a/netconf/netconf-ssh/pom.xml +++ b/netconf/netconf-ssh/pom.xml @@ -117,11 +117,6 @@ org.apache.felix maven-bundle-plugin - - - org.opendaylight.netconf.ssh.osgi.NetconfSSHActivator - - org.apache.maven.plugins diff --git a/netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/osgi/NetconfSSHActivator.java b/netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/NetconfSSHProvider.java 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 0a15fdb608..971e50631e 100644 --- a/netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/osgi/NetconfSSHActivator.java +++ b/netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/NetconfSSHProvider.java @@ -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 index 124e7e7ac1..0000000000 --- a/netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/osgi/AuthProviderTracker.java +++ /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 { - private static final Logger LOG = LoggerFactory.getLogger(AuthProviderTracker.class); - - private final BundleContext bundleContext; - - private final ServiceTracker 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 reference) { - LOG.trace("Service {} added", reference); - this.authProvider = bundleContext.getService(reference); - return authProvider; - } - - @Override - public void modifiedService(final ServiceReference 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 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 index 0000000000..2f504c064c --- /dev/null +++ b/netconf/netconf-ssh/src/main/resources/org/opendaylight/blueprint/netconf-ssh-blueprint.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + diff --git a/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/EchoServer.java b/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/EchoServer.java index c660ffd02b..fed42f255b 100644 --- a/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/EchoServer.java +++ b/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/EchoServer.java @@ -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; diff --git a/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/ProxyServer.java b/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/ProxyServer.java index 3b693512ea..b465e381f2 100644 --- a/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/ProxyServer.java +++ b/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/ProxyServer.java @@ -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; diff --git a/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/SSHTest.java b/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/SSHTest.java index 38907bcf50..a708f4f02f 100644 --- a/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/SSHTest.java +++ b/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/netty/SSHTest.java @@ -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; diff --git a/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/ssh/authentication/SSHServerTest.java b/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/ssh/authentication/SSHServerTest.java index e660628a67..5b07598b5d 100644 --- a/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/ssh/authentication/SSHServerTest.java +++ b/netconf/netconf-ssh/src/test/java/org/opendaylight/netconf/ssh/authentication/SSHServerTest.java @@ -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; diff --git a/netconf/netconf-tcp/pom.xml b/netconf/netconf-tcp/pom.xml index 04cce67209..c9e8ee179b 100644 --- a/netconf/netconf-tcp/pom.xml +++ b/netconf/netconf-tcp/pom.xml @@ -85,11 +85,6 @@ org.apache.felix maven-bundle-plugin - - - org.opendaylight.netconf.tcp.osgi.NetconfTCPActivator - - org.apache.maven.plugins diff --git a/netconf/netconf-tcp/src/main/java/org/opendaylight/netconf/tcp/osgi/NetconfTCPActivator.java b/netconf/netconf-tcp/src/main/java/org/opendaylight/netconf/tcp/NetconfTCPProvider.java 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 d95d2b84a2..d53cf40916 100644 --- a/netconf/netconf-tcp/src/main/java/org/opendaylight/netconf/tcp/osgi/NetconfTCPActivator.java +++ b/netconf/netconf-tcp/src/main/java/org/opendaylight/netconf/tcp/NetconfTCPProvider.java @@ -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 index 0000000000..54237315f9 --- /dev/null +++ b/netconf/netconf-tcp/src/main/resources/org/opendaylight/blueprint/netconf-tcp-blueprint.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + diff --git a/netconf/netconf-util/pom.xml b/netconf/netconf-util/pom.xml index 3c6c3930d1..d9a0f43315 100644 --- a/netconf/netconf-util/pom.xml +++ b/netconf/netconf-util/pom.xml @@ -111,11 +111,6 @@ org.apache.felix maven-bundle-plugin - - - org.opendaylight.netconf.util.osgi.NetconfConfigurationActivator - - org.codehaus.mojo 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 index 0000000000..6cada7f7ee --- /dev/null +++ b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfConfiguration.java @@ -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(); +} diff --git a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationHolder.java b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfConfigurationHolder.java similarity index 96% rename from netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationHolder.java rename to netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfConfigurationHolder.java index 9eb88c4cb2..28f2b87c48 100644 --- a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationHolder.java +++ b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfConfigurationHolder.java @@ -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; diff --git a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfiguration.java b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfConfigurationImpl.java 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 c67675a910..42c7b58871 100644 --- a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfiguration.java +++ b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfConfigurationImpl.java @@ -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 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 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 index 64537f336f..0000000000 --- a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigUtil.java +++ /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> serviceReferences - = bundleContext.getServiceReferences(ManagedService.class, null); - for (final ServiceReference 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 index 3c1a9c5cb0..0000000000 --- a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivator.java +++ /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 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; - } - } -} diff --git a/netconf/netconf-util/src/main/resources/netconf.cfg b/netconf/netconf-util/src/main/resources/netconf.cfg index 01437d891a..01d92ec636 100644 --- a/netconf/netconf-util/src/main/resources/netconf.cfg +++ b/netconf/netconf-util/src/main/resources/netconf.cfg @@ -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 index 0000000000..99cab21ca1 --- /dev/null +++ b/netconf/netconf-util/src/main/resources/org/opendaylight/blueprint/netconf-util-blueprint.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NetconfConfigurationImplTest.java 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 46414f7dc3..99b89ac877 100644 --- a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationTest.java +++ b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/NetconfConfigurationImplTest.java @@ -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 index 64bf74fec5..0000000000 --- a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigUtilTest.java +++ /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 serviceRef; - - @Mock - private ServiceReference netconfConfigurationRef; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - } - - @Test - public void testGetNetconfConfigurationService() throws Exception { - final Collection> 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> 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 index 70fadc2314..0000000000 --- a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivatorTest.java +++ /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 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