2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.netconf.ssh;
10 import java.io.IOException;
11 import java.net.InetSocketAddress;
12 import java.net.ServerSocket;
13 import java.util.concurrent.atomic.AtomicLong;
14 import javax.annotation.concurrent.ThreadSafe;
15 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
16 import org.opendaylight.controller.netconf.ssh.threads.SocketThread;
17 import org.opendaylight.controller.usermanager.IUserManager;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 public class NetconfSSHServer implements Runnable {
24 private ServerSocket ss = null;
25 private static final Logger logger = LoggerFactory.getLogger(NetconfSSHServer.class);
26 private static final AtomicLong sesssionId = new AtomicLong();
27 private final InetSocketAddress clientAddress;
28 private final AuthProvider authProvider;
29 private boolean up = false;
31 private NetconfSSHServer(int serverPort,InetSocketAddress clientAddress, AuthProvider authProvider) throws Exception{
33 logger.trace("Creating SSH server socket on port {}",serverPort);
34 this.ss = new ServerSocket(serverPort);
36 throw new Exception("Socket can't be bound to requested port :"+serverPort);
38 logger.trace("Server socket created.");
39 this.clientAddress = clientAddress;
40 this.authProvider = authProvider;
44 public static NetconfSSHServer start(int serverPort, InetSocketAddress clientAddress,AuthProvider authProvider) throws Exception {
45 return new NetconfSSHServer(serverPort, clientAddress,authProvider);
48 public void stop() throws Exception {
50 logger.trace("Closing SSH server socket.");
52 logger.trace("SSH server socket closed.");
55 public void removeUserManagerService(){
56 this.authProvider.removeUserManagerService();
59 public void addUserManagerService(IUserManager userManagerService){
60 this.authProvider.addUserManagerService(userManagerService);
62 public boolean isUp(){
68 logger.trace("Starting new socket thread.");
70 SocketThread.start(ss.accept(), clientAddress, sesssionId.incrementAndGet(),authProvider);
71 } catch (IOException e) {
72 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.