Upgrade sshd to 2.6.0
[netconf.git] / netconf / callhome-protocol / src / main / java / org / opendaylight / netconf / callhome / protocol / CallHomeSessionContext.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems 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.netconf.callhome.protocol;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.util.concurrent.GlobalEventExecutor;
15 import io.netty.util.concurrent.Promise;
16 import java.io.IOException;
17 import java.net.InetSocketAddress;
18 import java.net.SocketAddress;
19 import java.security.PublicKey;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.netconf.client.NetconfClientSession;
24 import org.opendaylight.netconf.client.NetconfClientSessionListener;
25 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
26 import org.opendaylight.netconf.shaded.sshd.client.channel.ClientChannel;
27 import org.opendaylight.netconf.shaded.sshd.client.future.AuthFuture;
28 import org.opendaylight.netconf.shaded.sshd.client.future.OpenFuture;
29 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
30 import org.opendaylight.netconf.shaded.sshd.common.future.SshFutureListener;
31 import org.opendaylight.netconf.shaded.sshd.common.session.Session;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 class CallHomeSessionContext implements CallHomeProtocolSessionContext {
36
37     private static final Logger LOG = LoggerFactory.getLogger(CallHomeSessionContext.class);
38     static final Session.AttributeKey<CallHomeSessionContext> SESSION_KEY = new Session.AttributeKey<>();
39
40     private static final String NETCONF = "netconf";
41
42     private final ClientSession sshSession;
43     private final CallHomeAuthorization authorization;
44     private final Factory factory;
45
46     private volatile boolean activated;
47
48     private final InetSocketAddress remoteAddress;
49     private final PublicKey serverKey;
50
51     CallHomeSessionContext(final ClientSession sshSession, final CallHomeAuthorization authorization,
52                            final SocketAddress remoteAddress, final Factory factory) {
53         this.authorization = requireNonNull(authorization, "authorization");
54         checkArgument(this.authorization.isServerAllowed(), "Server was not allowed.");
55         this.factory = requireNonNull(factory, "factory");
56         this.sshSession = requireNonNull(sshSession, "sshSession");
57         this.sshSession.setAttribute(SESSION_KEY, this);
58         this.remoteAddress = (InetSocketAddress) this.sshSession.getIoSession().getRemoteAddress();
59         this.serverKey = this.sshSession.getServerKey();
60     }
61
62     static CallHomeSessionContext getFrom(final ClientSession sshSession) {
63         return sshSession.getAttribute(SESSION_KEY);
64     }
65
66     AuthFuture authorize() throws IOException {
67         authorization.applyTo(sshSession);
68         return sshSession.auth();
69     }
70
71     void openNetconfChannel() {
72         LOG.debug("Opening NETCONF Subsystem on {}", sshSession);
73         try {
74             final ClientChannel netconfChannel = sshSession.createSubsystemChannel(NETCONF);
75             netconfChannel.setStreaming(ClientChannel.Streaming.Async);
76             netconfChannel.open().addListener(newSshFutureListener(netconfChannel));
77         } catch (IOException e) {
78             throw new IllegalStateException(e);
79         }
80     }
81
82     SshFutureListener<OpenFuture> newSshFutureListener(final ClientChannel netconfChannel) {
83         return future -> {
84             if (future.isOpened()) {
85                 factory.getChannelOpenListener().onNetconfSubsystemOpened(this,
86                     listener -> doActivate(netconfChannel, listener));
87             } else {
88                 channelOpenFailed(future.getException());
89             }
90         };
91     }
92
93     @Override
94     public void terminate() {
95         sshSession.close(false);
96         removeSelf();
97     }
98
99     @Override
100     public TransportType getTransportType() {
101         return TransportType.SSH;
102     }
103
104     private void channelOpenFailed(final Throwable throwable) {
105         LOG.error("Unable to open netconf subsystem, disconnecting.", throwable);
106         sshSession.close(false);
107     }
108
109     private synchronized Promise<NetconfClientSession> doActivate(final ClientChannel netconfChannel,
110             final NetconfClientSessionListener listener) {
111         if (activated) {
112             return newSessionPromise().setFailure(new IllegalStateException("Session already activated."));
113         }
114
115         activated = true;
116         LOG.info("Activating Netconf channel for {} with {}", getRemoteAddress(), listener);
117         Promise<NetconfClientSession> activationPromise = newSessionPromise();
118         final MinaSshNettyChannel nettyChannel = newMinaSshNettyChannel(netconfChannel);
119         factory.getChannelInitializer(listener).initialize(nettyChannel, activationPromise);
120         factory.getNettyGroup().register(nettyChannel).awaitUninterruptibly(500);
121         return activationPromise;
122     }
123
124     protected MinaSshNettyChannel newMinaSshNettyChannel(final ClientChannel netconfChannel) {
125         return new MinaSshNettyChannel(this, sshSession, netconfChannel);
126     }
127
128     private static Promise<NetconfClientSession> newSessionPromise() {
129         return GlobalEventExecutor.INSTANCE.newPromise();
130     }
131
132     @Override
133     public PublicKey getRemoteServerKey() {
134         return serverKey;
135     }
136
137     @Override
138     public InetSocketAddress getRemoteAddress() {
139         return remoteAddress;
140     }
141
142     @Override
143     public String getSessionId() {
144         return authorization.getSessionName();
145     }
146
147     void removeSelf() {
148         factory.remove(this);
149     }
150
151     static class Factory {
152
153         private final EventLoopGroup nettyGroup;
154         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
155         private final CallHomeNetconfSubsystemListener subsystemListener;
156         private final ConcurrentMap<String, CallHomeSessionContext> sessions = new ConcurrentHashMap<>();
157
158         Factory(final EventLoopGroup nettyGroup, final NetconfClientSessionNegotiatorFactory negotiatorFactory,
159                 final CallHomeNetconfSubsystemListener subsystemListener) {
160             this.nettyGroup = requireNonNull(nettyGroup, "nettyGroup");
161             this.negotiatorFactory = requireNonNull(negotiatorFactory, "negotiatorFactory");
162             this.subsystemListener = requireNonNull(subsystemListener);
163         }
164
165         void remove(final CallHomeSessionContext session) {
166             sessions.remove(session.getSessionId(), session);
167         }
168
169         ReverseSshChannelInitializer getChannelInitializer(final NetconfClientSessionListener listener) {
170             return ReverseSshChannelInitializer.create(negotiatorFactory, listener);
171         }
172
173         CallHomeNetconfSubsystemListener getChannelOpenListener() {
174             return this.subsystemListener;
175         }
176
177         @Nullable CallHomeSessionContext createIfNotExists(final ClientSession sshSession,
178                 final CallHomeAuthorization authorization, final SocketAddress remoteAddress) {
179             CallHomeSessionContext session = new CallHomeSessionContext(sshSession, authorization,
180                     remoteAddress, this);
181             CallHomeSessionContext preexisting = sessions.putIfAbsent(session.getSessionId(), session);
182             // If preexisting is null - session does not exist, so we can safely create new one, otherwise we return
183             // null and incoming connection will be rejected.
184             return preexisting == null ? session : null;
185         }
186
187         EventLoopGroup getNettyGroup() {
188             return nettyGroup;
189         }
190     }
191 }