Merge "Improve logging in NetconfClient, logging-bridge. Increase connection timeout...
authorEd Warnicke <eaw@cisco.com>
Fri, 7 Feb 2014 06:13:46 +0000 (06:13 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 7 Feb 2014 06:13:46 +0000 (06:13 +0000)
opendaylight/logging/bridge/pom.xml
opendaylight/logging/bridge/src/main/java/org/opendaylight/controller/logging/bridge/internal/LogListenerImpl.java
opendaylight/logging/bridge/src/test/java/org/opendaylight/controller/logging/bridge/internal/LogListenerImplTest.java [new file with mode: 0644]
opendaylight/md-sal/sal-netconf-connector/src/main/yang/odl-sal-netconf-connector-cfg.yang
opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClient.java
opendaylight/netconf/netconf-client/src/test/java/org/opendaylight/controller/netconf/client/SSHNetconfClientLiveTest.java

index 0fd26db..3ceb5b1 100644 (file)
@@ -1,55 +1,71 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-  <modelVersion>4.0.0</modelVersion>
-  <parent>
-    <groupId>org.opendaylight.controller</groupId>
-    <artifactId>commons.opendaylight</artifactId>
-    <version>1.4.2-SNAPSHOT</version>
-    <relativePath>../../commons/opendaylight</relativePath>
-  </parent>
-  <scm>
-    <connection>scm:git:ssh://git.opendaylight.org:29418/controller.git</connection>
-    <developerConnection>scm:git:ssh://git.opendaylight.org:29418/controller.git</developerConnection>
-    <url>https://wiki.opendaylight.org/view/OpenDaylight_Controller:Main</url>
-    <tag>HEAD</tag>
-  </scm>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.opendaylight.controller</groupId>
+        <artifactId>commons.opendaylight</artifactId>
+        <version>1.4.2-SNAPSHOT</version>
+        <relativePath>../../commons/opendaylight</relativePath>
+    </parent>
+    <scm>
+        <connection>scm:git:ssh://git.opendaylight.org:29418/controller.git</connection>
+        <developerConnection>scm:git:ssh://git.opendaylight.org:29418/controller.git</developerConnection>
+        <url>https://wiki.opendaylight.org/view/OpenDaylight_Controller:Main</url>
+        <tag>HEAD</tag>
+    </scm>
 
-  <artifactId>logging.bridge</artifactId>
-  <version>0.4.2-SNAPSHOT</version>
-  <packaging>bundle</packaging>
+    <artifactId>logging.bridge</artifactId>
+    <version>0.4.2-SNAPSHOT</version>
+    <packaging>bundle</packaging>
 
-  <dependencies>
-    <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>slf4j-api</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>equinoxSDK381</groupId>
-      <artifactId>org.eclipse.osgi</artifactId>
-    </dependency>
-  </dependencies>
+    <dependencies>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>equinoxSDK381</groupId>
+            <artifactId>org.eclipse.osgi</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.opendaylight.yangtools</groupId>
+            <artifactId>mockito-configuration</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
 
-  <build>
-    <plugins>
-      <plugin>
-        <groupId>org.apache.felix</groupId>
-        <artifactId>maven-bundle-plugin</artifactId>
-        <version>${bundle.plugin.version}</version>
-        <extensions>true</extensions>
-        <configuration>
-          <instructions>
-            <Import-Package>
-              org.slf4j,
-              org.osgi.framework,
-              org.osgi.service.log
-            </Import-Package>
-            <Bundle-Activator>
-              org.opendaylight.controller.logging.bridge.internal.Activator
-            </Bundle-Activator>
-          </instructions>
-          <manifestLocation>${project.basedir}/META-INF</manifestLocation>
-        </configuration>
-      </plugin>
-    </plugins>
-  </build>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <version>${bundle.plugin.version}</version>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Import-Package>
+                            org.slf4j,
+                            org.osgi.framework,
+                            org.osgi.service.log
+                        </Import-Package>
+                        <Bundle-Activator>
+                            org.opendaylight.controller.logging.bridge.internal.Activator
+                        </Bundle-Activator>
+                    </instructions>
+                    <manifestLocation>${project.basedir}/META-INF</manifestLocation>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
 </project>
index 03719d5..2f45c6f 100644 (file)
@@ -26,19 +26,19 @@ public class LogListenerImpl implements LogListener {
         if (this.logger != null) {
             switch (entry.getLevel()) {
             case LogService.LOG_DEBUG:
-                this.logger.debug("Bundle:{} Message:{} Exception:{}", entry.getBundle()
+                this.logger.debug("Bundle:{} Message:{}", entry.getBundle()
                         .getSymbolicName(), entry.getMessage(), entry.getException());
                 break;
             case LogService.LOG_INFO:
-                this.logger.info("Bundle:{} Message:{} Exception:{}", entry.getBundle()
+                this.logger.info("Bundle:{} Message:{}", entry.getBundle()
                         .getSymbolicName(), entry.getMessage(), entry.getException());
                 break;
             case LogService.LOG_WARNING:
-                this.logger.warn("Bundle:{} Message:{} Exception:{}", entry.getBundle()
+                this.logger.warn("Bundle:{} Message:{}", entry.getBundle()
                         .getSymbolicName(), entry.getMessage(), entry.getException());
                 break;
             case LogService.LOG_ERROR:
-                this.logger.error("Bundle:{} Message:{} Exception:{}", entry.getBundle()
+                this.logger.error("Bundle:{} Message:{}", entry.getBundle()
                         .getSymbolicName(), entry.getMessage(), entry.getException());
                 break;
             }
diff --git a/opendaylight/logging/bridge/src/test/java/org/opendaylight/controller/logging/bridge/internal/LogListenerImplTest.java b/opendaylight/logging/bridge/src/test/java/org/opendaylight/controller/logging/bridge/internal/LogListenerImplTest.java
new file mode 100644 (file)
index 0000000..2490c39
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * 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.controller.logging.bridge.internal;
+
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.log.LogEntry;
+import org.osgi.service.log.LogService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+public class LogListenerImplTest {
+    private static final Logger logger = LoggerFactory.getLogger(LogListenerImplTest.class);
+
+    @Test
+    public void test() {
+        LogListenerImpl tested = new LogListenerImpl(logger);
+        tested.logged(getEntry("m1", null));
+        tested.logged(getEntry("m2", new RuntimeException()));
+    }
+
+    private LogEntry getEntry(final String message, final Exception e) {
+        return new LogEntry() {
+            @Override
+            public Bundle getBundle() {
+                Bundle mock = mock(Bundle.class);
+                doReturn(null).when(mock).getSymbolicName();
+                return mock;
+            }
+
+            @Override
+            public ServiceReference getServiceReference() {
+                return null;
+            }
+
+            @Override
+            public int getLevel() {
+                return LogService.LOG_INFO;
+            }
+
+            @Override
+            public String getMessage() {
+                return message;
+            }
+
+            @Override
+            public Throwable getException() {
+                return e;
+            }
+
+            @Override
+            public long getTime() {
+                return 0;
+            }
+        };
+    }
+
+}
index b28e72e..f0fa452 100644 (file)
@@ -92,7 +92,7 @@ module odl-sal-netconf-connector-cfg {
             leaf connection-timeout-millis {
                 description "Specifies timeout in milliseconds after which connection must be established.";
                 type uint32;
-                default 5000;
+                default 20000;
             }
 
             leaf max-connection-attempts {
index 4fcad2c..b8951a4 100644 (file)
@@ -61,18 +61,18 @@ public class NetconfClient implements Closeable {
         try {
             return clientFuture.get();
         } catch (CancellationException e) {
-            throw new RuntimeException("Netconf client interrupted", e);
+            throw new RuntimeException("Cancelling " + this, e);
         } catch (ExecutionException e) {
-            throw new IllegalStateException("Unable to create netconf client", e);
+            throw new IllegalStateException("Unable to create " + this, e);
         }
     }
 
-    public static NetconfClient clientFor(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat, NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
-        return new NetconfClient(clientLabelForLogging,address,strat,netconfClientDispatcher);
+    public static NetconfClient clientFor(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strategy, NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
+        return new NetconfClient(clientLabelForLogging,address,strategy,netconfClientDispatcher);
     }
 
-    public static NetconfClient clientFor(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat, NetconfClientDispatcher netconfClientDispatcher,NetconfClientSessionListener listener) throws InterruptedException {
-        return new NetconfClient(clientLabelForLogging,address,strat,netconfClientDispatcher,listener);
+    public static NetconfClient clientFor(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strategy, NetconfClientDispatcher netconfClientDispatcher,NetconfClientSessionListener listener) throws InterruptedException {
+        return new NetconfClient(clientLabelForLogging,address,strategy,netconfClientDispatcher,listener);
     }
 
     public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectTimeoutMs,
@@ -87,12 +87,12 @@ public class NetconfClient implements Closeable {
                 DEFAULT_CONNECT_TIMEOUT), netconfClientDispatcher);
     }
 
-    public NetconfClient(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat,
+    public NetconfClient(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strategy,
             NetconfClientDispatcher netconfClientDispatcher, NetconfClientSessionListener listener) throws InterruptedException{
         this.label = clientLabelForLogging;
         dispatch = netconfClientDispatcher;
         sessionListener = listener;
-        Future<NetconfClientSession> clientFuture = dispatch.createClient(address, sessionListener, strat);
+        Future<NetconfClientSession> clientFuture = dispatch.createClient(address, sessionListener, strategy);
         this.address = address;
         clientSession = get(clientFuture);
         this.sessionId = clientSession.getSessionId();
index a3cc104..1357201 100644 (file)
@@ -12,38 +12,78 @@ import io.netty.channel.nio.NioEventLoopGroup;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
-import org.opendaylight.controller.netconf.api.NetconfMessage;
-import org.opendaylight.controller.netconf.util.NetconfUtil;
 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.LoginPassword;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
 import java.net.InetSocketAddress;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 
 @Ignore
 public class SSHNetconfClientLiveTest {
+    private static final Logger logger = LoggerFactory.getLogger(SSHNetconfClientLiveTest.class);
 
     NioEventLoopGroup nettyThreadgroup;
     NetconfSshClientDispatcher netconfClientDispatcher;
+    InetSocketAddress address;
+    final int connectionAttempts = 10, attemptMsTimeout = 1000;
+    final int connectionTimeoutMillis = 20000;
 
     @Before
     public void setUp() {
         nettyThreadgroup = new NioEventLoopGroup();
+
         netconfClientDispatcher = new NetconfSshClientDispatcher(new LoginPassword(
                 System.getProperty("username"), System.getProperty("password")),
-                nettyThreadgroup, nettyThreadgroup, 5000);
+                nettyThreadgroup, nettyThreadgroup, connectionTimeoutMillis);
+
+        address = new InetSocketAddress(System.getProperty("host"), Integer.parseInt(System.getProperty("port")));
     }
 
+    @Ignore
     @Test
     public void test() throws Exception {
-        InetSocketAddress address = new InetSocketAddress(System.getProperty("host"), 830);
-        int connectionAttempts = 10, attemptMsTimeout = 1000;
+        //runnable.run();
+    }
 
-        NetconfClient netconfClient = new NetconfClient("client", address, connectionAttempts,
-            attemptMsTimeout, netconfClientDispatcher);
+    @Test
+    public void testInExecutor() throws Exception {
+        int threads = 4;
+        ExecutorService executorService = Executors.newFixedThreadPool(threads);
+        try {
+            for (int i= 0;i< threads;i++) {
+                InetSocketAddress address = new InetSocketAddress(System.getProperty("host"),
+                        Integer.parseInt(System.getProperty("port")));
+                NetconfRunnable runnable = new NetconfRunnable(address);
+                executorService.execute(runnable);
+            }
+            executorService.shutdown();
+            executorService.awaitTermination(1, TimeUnit.MINUTES);
 
-        netconfClient.getCapabilities();
 
-        NetconfMessage netconfMessage = NetconfUtil.createMessage(getClass().getResourceAsStream("/get_schema.xml"));
-        NetconfMessage response = netconfClient.sendMessage(netconfMessage);
-        NetconfUtil.checkIsMessageOk(response);
+        } finally {
+            executorService.shutdownNow();
+        }
     }
+
+    class NetconfRunnable implements Runnable {
+        private final InetSocketAddress address;
+
+        NetconfRunnable(InetSocketAddress address) {
+            this.address = address;
+        }
+
+        @Override
+        public void run() {
+            try (NetconfClient netconfClient = new NetconfClient(address.toString(), address, connectionAttempts,
+                    attemptMsTimeout, netconfClientDispatcher);) {
+                logger.info("OK {}", address);
+            } catch (InterruptedException | IOException e) {
+                logger.error("Failed {}", address, e);
+            }
+        }
+    };
 }