Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[controller.git] / opendaylight / netconf / netconf-ssh / src / main / java / org / opendaylight / controller / netconf / ssh / SshProxyServer.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.netconf.ssh;
10
11 import com.google.common.collect.Lists;
12 import io.netty.channel.EventLoopGroup;
13 import java.io.IOException;
14 import java.nio.channels.AsynchronousChannelGroup;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.ScheduledExecutorService;
19 import java.util.concurrent.TimeUnit;
20 import org.apache.sshd.SshServer;
21 import org.apache.sshd.common.FactoryManager;
22 import org.apache.sshd.common.NamedFactory;
23 import org.apache.sshd.common.RuntimeSshException;
24 import org.apache.sshd.common.io.IoAcceptor;
25 import org.apache.sshd.common.io.IoConnector;
26 import org.apache.sshd.common.io.IoHandler;
27 import org.apache.sshd.common.io.IoServiceFactory;
28 import org.apache.sshd.common.io.IoServiceFactoryFactory;
29 import org.apache.sshd.common.io.nio2.Nio2Acceptor;
30 import org.apache.sshd.common.io.nio2.Nio2Connector;
31 import org.apache.sshd.common.io.nio2.Nio2ServiceFactoryFactory;
32 import org.apache.sshd.common.util.CloseableUtils;
33 import org.apache.sshd.server.Command;
34 import org.apache.sshd.server.ServerFactoryManager;
35
36 /**
37  * Proxy SSH server that just delegates decrypted content to a delegate server within same VM.
38  * Implemented using Apache Mina SSH lib.
39  */
40 public class SshProxyServer implements AutoCloseable {
41
42     private final SshServer sshServer;
43     private final ScheduledExecutorService minaTimerExecutor;
44     private final EventLoopGroup clientGroup;
45     private final IoServiceFactoryFactory nioServiceWithPoolFactoryFactory;
46
47     public SshProxyServer(final ScheduledExecutorService minaTimerExecutor, final EventLoopGroup clientGroup, final ExecutorService nioExecutor) {
48         this.minaTimerExecutor = minaTimerExecutor;
49         this.clientGroup = clientGroup;
50         this.nioServiceWithPoolFactoryFactory = new NioServiceWithPoolFactory.NioServiceWithPoolFactoryFactory(nioExecutor);
51         this.sshServer = SshServer.setUpDefaultServer();
52     }
53
54     public void bind(final SshProxyServerConfiguration sshProxyServerConfiguration) throws IOException {
55         sshServer.setHost(sshProxyServerConfiguration.getBindingAddress().getHostString());
56         sshServer.setPort(sshProxyServerConfiguration.getBindingAddress().getPort());
57
58         sshServer.setPasswordAuthenticator(sshProxyServerConfiguration.getAuthenticator());
59         sshServer.setKeyPairProvider(sshProxyServerConfiguration.getKeyPairProvider());
60
61         sshServer.setIoServiceFactoryFactory(nioServiceWithPoolFactoryFactory);
62         sshServer.setScheduledExecutorService(minaTimerExecutor);
63         sshServer.setProperties(getProperties(sshProxyServerConfiguration));
64
65         final RemoteNetconfCommand.NetconfCommandFactory netconfCommandFactory =
66                 new RemoteNetconfCommand.NetconfCommandFactory(clientGroup, sshProxyServerConfiguration.getLocalAddress());
67         sshServer.setSubsystemFactories(Lists.<NamedFactory<Command>>newArrayList(netconfCommandFactory));
68         sshServer.start();
69     }
70
71     private static Map<String, String> getProperties(final SshProxyServerConfiguration sshProxyServerConfiguration) {
72         return new HashMap<String, String>()
73         {{
74             put(ServerFactoryManager.IDLE_TIMEOUT, String.valueOf(sshProxyServerConfiguration.getIdleTimeout()));
75         }};
76     }
77
78     @Override
79     public void close() {
80         try {
81             sshServer.stop(true);
82         } catch (final InterruptedException e) {
83             throw new RuntimeException("Interrupted while stopping sshServer", e);
84         } finally {
85             sshServer.close(true);
86         }
87     }
88
89     /**
90      * Based on Nio2ServiceFactory with one addition: injectable executor
91      */
92     private static final class NioServiceWithPoolFactory extends CloseableUtils.AbstractCloseable implements IoServiceFactory {
93
94         private final FactoryManager manager;
95         private final AsynchronousChannelGroup group;
96
97         public NioServiceWithPoolFactory(final FactoryManager manager, final ExecutorService executor) {
98             this.manager = manager;
99             try {
100                 group = AsynchronousChannelGroup.withThreadPool(executor);
101             } catch (final IOException e) {
102                 throw new RuntimeSshException(e);
103             }
104         }
105
106         public IoConnector createConnector(final IoHandler handler) {
107             return new Nio2Connector(manager, handler, group);
108         }
109
110         public IoAcceptor createAcceptor(final IoHandler handler) {
111             return new Nio2Acceptor(manager, handler, group);
112         }
113
114         @Override
115         protected void doCloseImmediately() {
116             try {
117                 group.shutdownNow();
118                 group.awaitTermination(5, TimeUnit.SECONDS);
119             } catch (final Exception e) {
120                 log.debug("Exception caught while closing channel group", e);
121             } finally {
122                 super.doCloseImmediately();
123             }
124         }
125
126         private static final class NioServiceWithPoolFactoryFactory extends Nio2ServiceFactoryFactory {
127
128             private final ExecutorService nioExecutor;
129
130             private NioServiceWithPoolFactoryFactory(final ExecutorService nioExecutor) {
131                 this.nioExecutor = nioExecutor;
132             }
133
134             @Override
135             public IoServiceFactory create(final FactoryManager manager) {
136                 return new NioServiceWithPoolFactory(manager, nioExecutor);
137             }
138         }
139     }
140
141 }