Bug 6023 - Adress for config subsystem netconf endpoint is not configurable 43/44243/10
authorRudolf Brisuda <rudolf.brisuda@pantheon.tech>
Wed, 17 Aug 2016 12:03:04 +0000 (14:03 +0200)
committerJakub Morvay <jmorvay@cisco.com>
Wed, 19 Oct 2016 08:59:38 +0000 (08:59 +0000)
- 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 <rudolf.brisuda@pantheon.tech>
features/netconf/src/main/features/features.xml
netconf/netconf-ssh/pom.xml
netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/osgi/NetconfSSHActivator.java
netconf/netconf-tcp/pom.xml
netconf/netconf-tcp/src/main/java/org/opendaylight/netconf/tcp/osgi/NetconfTCPActivator.java
netconf/netconf-util/pom.xml
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigUtil.java
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfiguration.java [new file with mode: 0644]
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationActivator.java [new file with mode: 0644]
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/osgi/NetconfConfigurationHolder.java [new file with mode: 0644]
netconf/netconf-util/src/main/resources/netconf.cfg [new file with mode: 0644]

index 645f9852823bfc3e6e72f3992ecaf812f654b02a..b43653d2a05fd2c75bfdc294fe6b2d32c9dac2cf 100644 (file)
@@ -44,6 +44,7 @@
     <feature version='${project.version}'>odl-netconf-mapping-api</feature>
     <feature version='${yangtools.version}'>odl-yangtools-yang-data</feature>
     <bundle>mvn:org.opendaylight.netconf/netconf-util/{{VERSION}}</bundle>
+    <configfile finalname="etc/netconf.cfg">mvn:org.opendaylight.netconf/netconf-util/{{VERSION}}/cfg/config</configfile>
   </feature>
 
   <feature name='odl-netconf-impl' version='${project.version}' description="OpenDaylight :: Netconf :: Impl">
   </feature>
 
   <feature name='odl-netconf-ssh' version='${project.version}' description="OpenDaylight :: Netconf Connector :: SSH">
+    <feature version='${project.version}'>odl-netconf-util</feature>
     <feature version='${project.version}'>odl-netconf-tcp</feature>
     <feature version='${project.version}'>odl-aaa-netconf-plugin</feature>
     <bundle>mvn:org.opendaylight.netconf/netconf-ssh/{{VERSION}}</bundle>
   </feature>
 
   <feature name='odl-netconf-tcp' version='${project.version}' description="OpenDaylight :: Netconf Connector :: TCP">
+    <feature version='${project.version}'>odl-netconf-util</feature>
     <feature version='${project.version}'>odl-netconf-impl</feature>
     <feature version='${config.version}'>odl-config-netty</feature>
     <bundle>mvn:org.opendaylight.netconf/netconf-tcp/{{VERSION}}</bundle>
index fe563f4883f8b96ab078415ed4c99c2f12f5b5f5..0225bdbf6f9f075ac302e473f994f2a380aade71 100644 (file)
       <artifactId>netconf-client</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+        <groupId>org.osgi</groupId>
+        <artifactId>org.osgi.compendium</artifactId>
+    </dependency>
   </dependencies>
 
   <build>
           </execution>
         </executions>
       </plugin>
-        <plugin>
-            <groupId>org.opendaylight.yangtools</groupId>
-            <artifactId>yang-maven-plugin</artifactId>
-        </plugin>
+      <plugin>
+          <groupId>org.opendaylight.yangtools</groupId>
+          <artifactId>yang-maven-plugin</artifactId>
+      </plugin>
     </plugins>
   </build>
 
index 9cbfac1580a3a22a0c3285129f6cee526f300e8b..14cbae81e1891edb357717c5f0dfc88e88dcd545 100644 (file)
@@ -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<InetSocketAddress> 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<String> 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;
     }
-
 }
index f6c8c05f0539260883847f988c463b6b87b301ed..0e45504a588c639ff716e6810064cd9eb74d736e 100644 (file)
       <artifactId>mockito-configuration</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+    </dependency>
   </dependencies>
 
   <build>
index 1f4188d827103fbbd5a2562d615ace2536de7e38..d972a32ba70c9ede29a146cdc572bd610f979862 100644 (file)
@@ -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<InetSocketAddress> 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",
index 56108b9d0d12fd7aba1b439272da19ef84bf871a..110872bf88c085de0276dae2089ffa6bddfc46ff 100644 (file)
       <groupId>org.opendaylight.yangtools</groupId>
       <artifactId>yang-data-api</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+    </dependency>
   </dependencies>
 
   <build>
     <plugins>
+      <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>
+        <artifactId>build-helper-maven-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>attach-artifacts</id>
+            <goals>
+              <goal>attach-artifact</goal>
+            </goals>
+            <phase>package</phase>
+            <configuration>
+              <artifacts>
+                <artifact>
+                  <file>${project.build.directory}/classes/netconf.cfg</file>
+                  <type>cfg</type>
+                  <classifier>config</classifier>
+                </artifact>
+              </artifacts>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
index 9a6ff2e054e7e351ee441e00c8bf10fa028036e7..9d4b5a123839fa39eaa39a119560dc116deb7cb3 100644 (file)
@@ -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<NetconfConfiguration> getNetconfConfigurationService(BundleContext bundleContext) {
+        final Collection<ServiceReference<ManagedService>> serviceReferences;
+        try {
+            serviceReferences = bundleContext.getServiceReferences(ManagedService.class, null);
+            for (final ServiceReference<ManagedService> 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 (file)
index 0000000..e33f7e2
--- /dev/null
@@ -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<String, ?> 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 (file)
index 0000000..a752322
--- /dev/null
@@ -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<String, String> getNetconfConfigProperties(){
+        Hashtable<String, String> 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 (file)
index 0000000..74b3a08
--- /dev/null
@@ -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 (file)
index 0000000..01437d8
--- /dev/null
@@ -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