From 2c2225edc5c24a5c90967403900c6c922651bdc2 Mon Sep 17 00:00:00 2001 From: Rudolf Brisuda Date: Wed, 17 Aug 2016 14:03:04 +0200 Subject: [PATCH] Bug 6023 - Adress for config subsystem netconf endpoint is not configurable - added NetconfConfigurationActivator for loading ManagedService - updating configuration from karaf netconf.cfg file - edited NetconfTCPActivator, NetconfSSHActivator for run with cfg values Change-Id: I5069a9f307a9a9b7c2d79723dc6c1cca0b2cc201 Signed-off-by: Rudolf Brisuda --- .../netconf/src/main/features/features.xml | 3 + netconf/netconf-ssh/pom.xml | 12 ++-- .../netconf/ssh/osgi/NetconfSSHActivator.java | 26 +++----- netconf/netconf-tcp/pom.xml | 4 ++ .../netconf/tcp/osgi/NetconfTCPActivator.java | 11 ++-- netconf/netconf-util/pom.xml | 35 ++++++++++ .../netconf/util/osgi/NetconfConfigUtil.java | 21 ++++++ .../util/osgi/NetconfConfiguration.java | 66 +++++++++++++++++++ .../osgi/NetconfConfigurationActivator.java | 41 ++++++++++++ .../util/osgi/NetconfConfigurationHolder.java | 37 +++++++++++ .../src/main/resources/netconf.cfg | 11 ++++ 11 files changed, 238 insertions(+), 29 deletions(-) create mode 100644 netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfiguration.java create mode 100644 netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivator.java create mode 100644 netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationHolder.java create mode 100644 netconf/netconf-util/src/main/resources/netconf.cfg diff --git a/features/netconf/src/main/features/features.xml b/features/netconf/src/main/features/features.xml index 645f985282..b43653d2a0 100644 --- a/features/netconf/src/main/features/features.xml +++ b/features/netconf/src/main/features/features.xml @@ -44,6 +44,7 @@ odl-netconf-mapping-api odl-yangtools-yang-data mvn:org.opendaylight.netconf/netconf-util/{{VERSION}} + mvn:org.opendaylight.netconf/netconf-util/{{VERSION}}/cfg/config @@ -113,12 +114,14 @@ + odl-netconf-util odl-netconf-tcp odl-aaa-netconf-plugin mvn:org.opendaylight.netconf/netconf-ssh/{{VERSION}} + odl-netconf-util odl-netconf-impl odl-config-netty mvn:org.opendaylight.netconf/netconf-tcp/{{VERSION}} diff --git a/netconf/netconf-ssh/pom.xml b/netconf/netconf-ssh/pom.xml index fe563f4883..0225bdbf6f 100644 --- a/netconf/netconf-ssh/pom.xml +++ b/netconf/netconf-ssh/pom.xml @@ -93,6 +93,10 @@ netconf-client test + + org.osgi + org.osgi.compendium + @@ -118,10 +122,10 @@ - - org.opendaylight.yangtools - yang-maven-plugin - + + org.opendaylight.yangtools + yang-maven-plugin + 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/osgi/NetconfSSHActivator.java index 9cbfac1580..14cbae81e1 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/osgi/NetconfSSHActivator.java @@ -7,7 +7,6 @@ */ package org.opendaylight.netconf.ssh.osgi; -import com.google.common.base.Optional; import io.netty.channel.local.LocalAddress; import io.netty.channel.nio.NioEventLoopGroup; import java.io.IOException; @@ -21,7 +20,7 @@ import org.apache.sshd.server.keyprovider.PEMGeneratorHostKeyProvider; 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.NetconfConfigUtil.InfixProp; +import org.opendaylight.netconf.util.osgi.NetconfConfiguration; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.slf4j.Logger; @@ -79,25 +78,17 @@ public class NetconfSSHActivator implements BundleActivator { } private SshProxyServer startSSHServer(final BundleContext bundleContext) throws IOException { - final Optional maybeSshSocketAddress = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, InfixProp.ssh); - if (!maybeSshSocketAddress.isPresent()) { - LOG.warn("SSH bridge not configured. Using default value {}", NetconfConfigUtil.DEFAULT_SSH_SERVER_ADRESS); - } - final InetSocketAddress sshSocketAddress = maybeSshSocketAddress - .or(NetconfConfigUtil.DEFAULT_SSH_SERVER_ADRESS); - LOG.info("Starting netconf SSH bridge at {}", sshSocketAddress); + final NetconfConfiguration netconfConfiguration = NetconfConfigUtil.getNetconfConfigurationService(bundleContext). + orElseThrow(() -> new IllegalStateException("Configuration for SSH not found.")); - final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress(); + final InetSocketAddress sshSocketAddress = netconfConfiguration.getSshServerAddress(); + LOG.info("Starting netconf SSH server at {}", sshSocketAddress); + final LocalAddress localAddress = NetconfConfigUtil.getNetconfLocalAddress(); authProviderTracker = new AuthProviderTracker(bundleContext); - final Optional maybePath = NetconfConfigUtil.getPrivateKeyPath(bundleContext); - if(!maybePath.isPresent()) { - LOG.warn("Private key path not configured. Using default value {}", - NetconfConfigUtil.DEFAULT_PRIVATE_KEY_PATH); - } - final String path = maybePath.or(NetconfConfigUtil.DEFAULT_PRIVATE_KEY_PATH); - LOG.trace("Starting netconf SSH bridge with path to ssh private key {}", path); + final String path = netconfConfiguration.getPrivateKeyPath(); + LOG.trace("Starting netconf SSH server with path to ssh private key {}", path); final SshProxyServer sshProxyServer = new SshProxyServer(minaTimerExecutor, clientGroup, nioExecutor); sshProxyServer.bind( @@ -110,5 +101,4 @@ public class NetconfSSHActivator implements BundleActivator { .createSshProxyServerConfiguration()); return sshProxyServer; } - } diff --git a/netconf/netconf-tcp/pom.xml b/netconf/netconf-tcp/pom.xml index f6c8c05f05..0e45504a58 100644 --- a/netconf/netconf-tcp/pom.xml +++ b/netconf/netconf-tcp/pom.xml @@ -60,6 +60,10 @@ mockito-configuration test + + org.osgi + org.osgi.compendium + 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/osgi/NetconfTCPActivator.java index 1f4188d827..d972a32ba7 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/osgi/NetconfTCPActivator.java @@ -8,11 +8,11 @@ package org.opendaylight.netconf.tcp.osgi; -import com.google.common.base.Optional; import java.net.InetSocketAddress; import org.opendaylight.netconf.tcp.netty.ProxyServer; import org.opendaylight.netconf.util.osgi.NetconfConfigUtil; import org.opendaylight.netconf.util.osgi.NetconfConfigUtil.InfixProp; +import org.opendaylight.netconf.util.osgi.NetconfConfiguration; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.slf4j.Logger; @@ -27,13 +27,10 @@ public class NetconfTCPActivator implements BundleActivator { @Override public void start(BundleContext context) { - final Optional maybeAddress = NetconfConfigUtil.extractNetconfServerAddress(context, InfixProp.tcp); - if (maybeAddress.isPresent() == false) { - LOG.warn("Netconf tcp server is not configured. Using default value {}", - NetconfConfigUtil.DEFAULT_TCP_SERVER_ADRESS); - } + final NetconfConfiguration netconfConfiguration = NetconfConfigUtil.getNetconfConfigurationService(context). + orElseThrow(() -> new IllegalStateException("Configuration for TCP not found.")); - InetSocketAddress address = maybeAddress.or(NetconfConfigUtil.DEFAULT_TCP_SERVER_ADRESS); + 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 {} to 127.0.0.1", diff --git a/netconf/netconf-util/pom.xml b/netconf/netconf-util/pom.xml index 56108b9d0d..110872bf88 100644 --- a/netconf/netconf-util/pom.xml +++ b/netconf/netconf-util/pom.xml @@ -88,10 +88,45 @@ org.opendaylight.yangtools yang-data-api + + org.osgi + org.osgi.compendium + + + org.apache.felix + maven-bundle-plugin + + + org.opendaylight.netconf.util.osgi.NetconfConfigurationActivator + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + attach-artifacts + + attach-artifact + + package + + + + ${project.build.directory}/classes/netconf.cfg + cfg + config + + + + + + org.apache.maven.plugins maven-jar-plugin 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 index 9a6ff2e054..9d4b5a1238 100644 --- 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 @@ -11,8 +11,12 @@ package org.opendaylight.netconf.util.osgi; import com.google.common.base.Optional; import io.netty.channel.local.LocalAddress; import java.net.InetSocketAddress; +import java.util.Collection; import java.util.concurrent.TimeUnit; 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; @@ -109,4 +113,21 @@ public final class NetconfConfigUtil { } return Optional.fromNullable(value); } + + public static java.util.Optional getNetconfConfigurationService(BundleContext bundleContext) { + final Collection> serviceReferences; + try { + serviceReferences = bundleContext.getServiceReferences(ManagedService.class, null); + for (final ServiceReference serviceReference : serviceReferences) { + ManagedService service = bundleContext.getService(serviceReference); + if (service instanceof NetconfConfiguration){ + return java.util.Optional.of((NetconfConfiguration) service); + } + } + } catch (InvalidSyntaxException e) { + LOG.error("Unable to retrieve references for ManagedService: {}", e); + } + LOG.error("Unable to retrieve NetconfConfiguration service. Not found. Bundle netconf-util probably failed."); + return java.util.Optional.empty(); + } } 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/osgi/NetconfConfiguration.java new file mode 100644 index 0000000000..e33f7e2e71 --- /dev/null +++ b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfiguration.java @@ -0,0 +1,66 @@ +/* + * 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.net.InetSocketAddress; +import java.util.Dictionary; +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); + + private static final NetconfConfiguration instance = new NetconfConfiguration(); + private NetconfConfigurationHolder netconfConfiguration; + + public static final String KEY_SSH_ADDRESS = "ssh-address"; + public static final String KEY_SSH_PORT = "ssh-port"; + public static final String KEY_TCP_ADDRESS = "tcp-address"; + public static final String KEY_TCP_PORT = "tcp-port"; + public static final String KEY_SSH_PK_PATH = "ssh-pk-path"; + + public static NetconfConfiguration getInstance() { + return instance; + } + + private NetconfConfiguration() { + netconfConfiguration = new NetconfConfigurationHolder(NetconfConfigUtil.DEFAULT_TCP_SERVER_ADRESS, + NetconfConfigUtil.DEFAULT_SSH_SERVER_ADRESS, NetconfConfigUtil.DEFAULT_PRIVATE_KEY_PATH); + } + + @Override + public void updated(final Dictionary dictionaryConfig) { + if (dictionaryConfig == null) { + LOG.warn("Netconf configuration cannot be updated."); + return; + } + final InetSocketAddress sshServerAddress = new InetSocketAddress((String) dictionaryConfig.get(KEY_SSH_ADDRESS), + Integer.parseInt((String) dictionaryConfig.get(KEY_SSH_PORT))); + final InetSocketAddress tcpServerAddress = new InetSocketAddress((String) dictionaryConfig.get(KEY_TCP_ADDRESS), + Integer.parseInt((String) dictionaryConfig.get(KEY_TCP_PORT))); + + netconfConfiguration = new NetconfConfigurationHolder(tcpServerAddress, sshServerAddress, + (String) dictionaryConfig.get(KEY_SSH_PK_PATH)); + + LOG.info("Netconf configuration was updated: {}", dictionaryConfig.toString()); + } + + public InetSocketAddress getSshServerAddress(){ + return netconfConfiguration.getSshServerAddress(); + } + + public InetSocketAddress getTcpServerAddress(){ + return netconfConfiguration.getTcpServerAddress(); + } + + public String getPrivateKeyPath() { + return netconfConfiguration.getPrivateKeyPath(); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000000..a752322d50 --- /dev/null +++ b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivator.java @@ -0,0 +1,41 @@ +/* + * 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; + +public class NetconfConfigurationActivator implements BundleActivator { + private static final String CONFIG_PID = "netconf"; + private ServiceRegistration configService; + + @Override + public void start(BundleContext bundleContext) { + configService = bundleContext.registerService(ManagedService.class, + NetconfConfiguration.getInstance(), getNetconfConfigProperties()); + } + + @Override + public void stop(BundleContext bundleContext) { + if (configService != null) { + configService.unregister(); + configService = null; + } + } + + private Hashtable getNetconfConfigProperties(){ + Hashtable properties = new Hashtable<>(); + properties.put(Constants.SERVICE_PID, CONFIG_PID); + return properties; + } +} 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/osgi/NetconfConfigurationHolder.java new file mode 100644 index 0000000000..74b3a089fa --- /dev/null +++ b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationHolder.java @@ -0,0 +1,37 @@ +/* + * 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.net.InetSocketAddress; + +final class NetconfConfigurationHolder { + + private final InetSocketAddress tcpServerAddress; + private final InetSocketAddress sshServerAddress; + private final String privateKeyPath; + + NetconfConfigurationHolder(InetSocketAddress tcpServerAddress, InetSocketAddress sshServerAddress, String privateKeyPath){ + this.tcpServerAddress = tcpServerAddress; + this.sshServerAddress = sshServerAddress; + this.privateKeyPath = privateKeyPath; + } + + String getPrivateKeyPath() { + return privateKeyPath; + } + + InetSocketAddress getSshServerAddress() { + return sshServerAddress; + } + + InetSocketAddress getTcpServerAddress() { + return tcpServerAddress; + } + +} diff --git a/netconf/netconf-util/src/main/resources/netconf.cfg b/netconf/netconf-util/src/main/resources/netconf.cfg new file mode 100644 index 0000000000..01437d891a --- /dev/null +++ b/netconf/netconf-util/src/main/resources/netconf.cfg @@ -0,0 +1,11 @@ +# netconf-tcp: + +tcp-address=127.0.0.1 +tcp-port=8383 + +# netconf-ssh: + +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 -- 2.36.6