Reuse NetconfClientBuilder in NetconfCallHomeServer
[netconf.git] / netconf / callhome-protocol / src / main / java / org / opendaylight / netconf / callhome / protocol / NetconfCallHomeServerBuilder.java
index 7814842a1d32667dbe9ea313aea3ff28daa43851..136adf351a6c388ed855679e8b4fe22f81ab40f9 100644 (file)
@@ -5,25 +5,22 @@
  * 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.callhome.protocol;
 
-import com.google.common.base.Optional;
+import io.netty.channel.DefaultEventLoopGroup;
 import io.netty.channel.EventLoopGroup;
-import io.netty.channel.local.LocalEventLoopGroup;
 import io.netty.util.HashedWheelTimer;
 import java.net.InetSocketAddress;
+import java.util.Optional;
 import java.util.concurrent.TimeUnit;
-import org.apache.sshd.SshClient;
-import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
-import org.opendaylight.netconf.callhome.protocol.CallHomeSessionContext.Factory;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
-import org.opendaylight.yangtools.concepts.Builder;
-
-public class NetconfCallHomeServerBuilder implements Builder<NetconfCallHomeServer> {
+import org.opendaylight.netconf.nettyutil.handler.ssh.client.NetconfClientBuilder;
+import org.opendaylight.netconf.shaded.sshd.client.SshClient;
 
+public class NetconfCallHomeServerBuilder {
     private static final long DEFAULT_SESSION_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(5);
-    private static final int DEFAULT_CALL_HOME_PORT = 6666;
+    private static final int DEFAULT_CALL_HOME_PORT = 4334;
 
     private SshClient sshClient;
     private EventLoopGroup nettyGroup;
@@ -34,25 +31,24 @@ public class NetconfCallHomeServerBuilder implements Builder<NetconfCallHomeServ
     private final CallHomeNetconfSubsystemListener subsystemListener;
     private final StatusRecorder recorder;
 
-    public NetconfCallHomeServerBuilder(CallHomeAuthorizationProvider authProvider,
-                                        CallHomeNetconfSubsystemListener subsystemListener, StatusRecorder recorder) {
+    public NetconfCallHomeServerBuilder(final CallHomeAuthorizationProvider authProvider,
+            final CallHomeNetconfSubsystemListener subsystemListener, final StatusRecorder recorder) {
         this.authProvider = authProvider;
         this.subsystemListener = subsystemListener;
         this.recorder = recorder;
     }
 
-    @Override
-    public NetconfCallHomeServer build() {
-        Factory factory =
-                new CallHomeSessionContext.Factory(nettyGroup(), negotiatorFactory(), subsystemListener());
-        return new NetconfCallHomeServer(sshClient(), authProvider(), factory, bindAddress(), this.recorder);
+    public @NonNull NetconfCallHomeServer build() {
+        return new NetconfCallHomeServer(sshClient(), authProvider(),
+            new CallHomeSessionContext.Factory(nettyGroup(), negotiatorFactory(), subsystemListener()),
+            bindAddress(), recorder);
     }
 
     public SshClient getSshClient() {
         return sshClient;
     }
 
-    public void setSshClient(SshClient sshClient) {
+    public void setSshClient(final SshClient sshClient) {
         this.sshClient = sshClient;
     }
 
@@ -60,7 +56,7 @@ public class NetconfCallHomeServerBuilder implements Builder<NetconfCallHomeServ
         return nettyGroup;
     }
 
-    public void setNettyGroup(EventLoopGroup nettyGroup) {
+    public void setNettyGroup(final EventLoopGroup nettyGroup) {
         this.nettyGroup = nettyGroup;
     }
 
@@ -68,7 +64,7 @@ public class NetconfCallHomeServerBuilder implements Builder<NetconfCallHomeServ
         return negotiationFactory;
     }
 
-    public void setNegotiationFactory(NetconfClientSessionNegotiatorFactory negotiationFactory) {
+    public void setNegotiationFactory(final NetconfClientSessionNegotiatorFactory negotiationFactory) {
         this.negotiationFactory = negotiationFactory;
     }
 
@@ -76,7 +72,7 @@ public class NetconfCallHomeServerBuilder implements Builder<NetconfCallHomeServ
         return bindAddress;
     }
 
-    public void setBindAddress(InetSocketAddress bindAddress) {
+    public void setBindAddress(final InetSocketAddress bindAddress) {
         this.bindAddress = bindAddress;
     }
 
@@ -108,20 +104,20 @@ public class NetconfCallHomeServerBuilder implements Builder<NetconfCallHomeServ
         return sshClient != null ? sshClient : defaultSshClient();
     }
 
-    private SshClient defaultSshClient() {
-        return SshClient.setUpDefaultClient();
+    private static SshClient defaultSshClient() {
+        return new NetconfClientBuilder().build();
     }
 
-    private NetconfClientSessionNegotiatorFactory defaultNegotiationFactory() {
+    private static NetconfClientSessionNegotiatorFactory defaultNegotiationFactory() {
         return new NetconfClientSessionNegotiatorFactory(new HashedWheelTimer(),
-                Optional.<NetconfHelloMessageAdditionalHeader>absent(), DEFAULT_SESSION_TIMEOUT_MILLIS);
+                                                         Optional.empty(), DEFAULT_SESSION_TIMEOUT_MILLIS);
     }
 
-    private EventLoopGroup defaultNettyGroup() {
-        return new LocalEventLoopGroup();
+    private static EventLoopGroup defaultNettyGroup() {
+        return new DefaultEventLoopGroup();
     }
 
-    private InetSocketAddress defaultBindAddress() {
+    private static InetSocketAddress defaultBindAddress() {
         return new InetSocketAddress(DEFAULT_CALL_HOME_PORT);
     }
 }