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 org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
11 import org.opendaylight.controller.netconf.ssh.threads.SocketThread;
12 import org.opendaylight.controller.usermanager.IUserManager;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import javax.annotation.concurrent.ThreadSafe;
17 import java.io.IOException;
18 import java.net.InetSocketAddress;
19 import java.net.ServerSocket;
20 import java.util.concurrent.atomic.AtomicLong;
23 public class NetconfSSHServer implements Runnable {
25 private ServerSocket ss = null;
26 private static final Logger logger = LoggerFactory.getLogger(NetconfSSHServer.class);
27 private static final AtomicLong sesssionId = new AtomicLong();
28 private final InetSocketAddress clientAddress;
29 private final AuthProvider authProvider;
30 private boolean up = false;
32 private NetconfSSHServer(int serverPort,InetSocketAddress clientAddress, AuthProvider authProvider) throws Exception{
34 logger.trace("Creating SSH server socket on port {}",serverPort);
35 this.ss = new ServerSocket(serverPort);
37 throw new Exception("Socket can't be bound to requested port :"+serverPort);
39 logger.trace("Server socket created.");
40 this.clientAddress = clientAddress;
41 this.authProvider = authProvider;
45 public static NetconfSSHServer start(int serverPort, InetSocketAddress clientAddress,AuthProvider authProvider) throws Exception {
46 return new NetconfSSHServer(serverPort, clientAddress,authProvider);
49 public void stop() throws Exception {
51 logger.trace("Closing SSH server socket.");
53 logger.trace("SSH server socket closed.");
56 public void removeUserManagerService(){
57 this.authProvider.removeUserManagerService();
60 public void addUserManagerService(IUserManager userManagerService){
61 this.authProvider.addUserManagerService(userManagerService);
63 public boolean isUp(){
69 logger.trace("Starting new socket thread.");
71 SocketThread.start(ss.accept(), clientAddress, sesssionId.incrementAndGet(),authProvider);
72 } catch (IOException e) {
73 logger.error("Exception occurred during socket thread initialization {}",e);