Merge "Refactor Subnet.isSubnetOf - reduce number of 'if' statements. Added unitests."
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / NetconfSSHServer.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.netconf.ssh;
9
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;
20
21 @ThreadSafe
22 public class NetconfSSHServer implements Runnable {
23
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;
30
31     private NetconfSSHServer(int serverPort,InetSocketAddress clientAddress, AuthProvider authProvider) throws Exception{
32
33         logger.trace("Creating SSH server socket on port {}",serverPort);
34         this.ss = new ServerSocket(serverPort);
35         if (!ss.isBound()){
36             throw new Exception("Socket can't be bound to requested port :"+serverPort);
37         }
38         logger.trace("Server socket created.");
39         this.clientAddress = clientAddress;
40         this.authProvider = authProvider;
41         this.up = true;
42     }
43
44     public static NetconfSSHServer start(int serverPort, InetSocketAddress clientAddress,AuthProvider authProvider) throws Exception {
45         return new NetconfSSHServer(serverPort, clientAddress,authProvider);
46     }
47
48     public void stop() throws Exception {
49         up = false;
50         logger.trace("Closing SSH server socket.");
51         ss.close();
52         logger.trace("SSH server socket closed.");
53     }
54
55     public void removeUserManagerService(){
56         this.authProvider.removeUserManagerService();
57     }
58
59     public void addUserManagerService(IUserManager userManagerService){
60         this.authProvider.addUserManagerService(userManagerService);
61     }
62     public boolean isUp(){
63         return this.up;
64     }
65     @Override
66     public void run() {
67         while (up) {
68             logger.trace("Starting new socket thread.");
69             try {
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.
73             }
74         }
75     }
76 }