X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-ssh%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fssh%2FNetconfSSHServer.java;h=c6974d4982db051f1cb6a66d3f67394ee2d57817;hp=dad149fd60b762704fbf4c5f4d79882c51b8b676;hb=510561642a3d699673140da8879d4eb5bc88534e;hpb=e4f8f32b7e19c72c3de074431b47674adade6336 diff --git a/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/NetconfSSHServer.java b/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/NetconfSSHServer.java index dad149fd60..c6974d4982 100644 --- a/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/NetconfSSHServer.java +++ b/opendaylight/netconf/netconf-ssh/src/main/java/org/opendaylight/controller/netconf/ssh/NetconfSSHServer.java @@ -1,32 +1,85 @@ -/* - * 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 +/* + * 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.netconf.ssh; +import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider; +import org.opendaylight.controller.netconf.ssh.threads.SocketThread; +import org.opendaylight.controller.usermanager.IUserManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.concurrent.ThreadSafe; +import java.io.IOException; +import java.net.InetSocketAddress; import java.net.ServerSocket; +import java.util.concurrent.atomic.AtomicLong; -public class NetconfSSHServer { +@ThreadSafe +public final class NetconfSSHServer implements Runnable { - private static boolean acceptMore = true; - private static final int SERVER_PORT = 830; private ServerSocket ss = null; + private static final Logger logger = LoggerFactory.getLogger(NetconfSSHServer.class); + private static final AtomicLong sesssionId = new AtomicLong(); + private final InetSocketAddress clientAddress; + private final AuthProvider authProvider; + private volatile boolean up = false; + + private NetconfSSHServer(int serverPort,InetSocketAddress clientAddress, AuthProvider authProvider) throws IllegalStateException, IOException { - private NetconfSSHServer() throws Exception{ - this.ss = new ServerSocket(SERVER_PORT); - while (acceptMore) { - SocketThread.start(ss.accept()); + logger.trace("Creating SSH server socket on port {}",serverPort); + this.ss = new ServerSocket(serverPort); + if (!ss.isBound()){ + throw new IllegalStateException("Socket can't be bound to requested port :"+serverPort); } + logger.trace("Server socket created."); + this.clientAddress = clientAddress; + this.authProvider = authProvider; + this.up = true; } - public static NetconfSSHServer start() throws Exception { - return new NetconfSSHServer(); + + public static NetconfSSHServer start(int serverPort, InetSocketAddress clientAddress,AuthProvider authProvider) throws IllegalStateException, IOException { + return new NetconfSSHServer(serverPort, clientAddress,authProvider); + } + + public void stop() throws IOException { + up = false; + logger.trace("Closing SSH server socket."); + ss.close(); + logger.trace("SSH server socket closed."); } - public void stop() throws Exception { - ss.close(); + public void removeUserManagerService(){ + this.authProvider.removeUserManagerService(); } + public void addUserManagerService(IUserManager userManagerService){ + this.authProvider.addUserManagerService(userManagerService); + } + public boolean isUp(){ + return this.up; + } + @Override + public void run() { + while (up) { + logger.trace("Starting new socket thread."); + try { + SocketThread.start(ss.accept(), clientAddress, sesssionId.incrementAndGet(), authProvider); + } + catch (IOException e) { + if( up ) { + logger.error("Exception occurred during socket thread initialization", e); + } + else { + // We're shutting down so an exception is expected as the socket's been closed. + // Log to debug. + logger.debug("Shutting down - got expected exception: " + e); + } + } + } + } }