2 * Copyright (c) 2016 Brocade Communication Systems 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.netconf.callhome.protocol;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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 javax.annotation.concurrent.GuardedBy;
23 import org.apache.sshd.client.channel.ClientChannel;
24 import org.apache.sshd.client.future.AuthFuture;
25 import org.apache.sshd.client.future.OpenFuture;
26 import org.apache.sshd.client.session.ClientSession;
27 import org.apache.sshd.client.session.ClientSessionImpl;
28 import org.apache.sshd.common.future.SshFutureListener;
29 import org.apache.sshd.common.session.Session;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.opendaylight.netconf.client.NetconfClientSession;
32 import org.opendaylight.netconf.client.NetconfClientSessionListener;
33 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 class CallHomeSessionContext implements CallHomeProtocolSessionContext {
39 private static final Logger LOG = LoggerFactory.getLogger(CallHomeSessionContext.class);
40 static final Session.AttributeKey<CallHomeSessionContext> SESSION_KEY = new Session.AttributeKey<>();
42 private static final String NETCONF = "netconf";
44 private final ClientSessionImpl sshSession;
45 private final CallHomeAuthorization authorization;
46 private final Factory factory;
48 private volatile MinaSshNettyChannel nettyChannel = null;
49 private volatile boolean activated;
51 private final InetSocketAddress remoteAddress;
52 private final PublicKey serverKey;
54 CallHomeSessionContext(final ClientSession sshSession, final CallHomeAuthorization authorization,
55 final SocketAddress remoteAddress, final Factory factory) {
56 this.authorization = requireNonNull(authorization, "authorization");
57 checkArgument(this.authorization.isServerAllowed(), "Server was not allowed.");
58 checkArgument(sshSession instanceof ClientSessionImpl,
59 "sshSession must implement ClientSessionImpl");
60 this.factory = requireNonNull(factory, "factory");
61 this.sshSession = (ClientSessionImpl) sshSession;
62 this.sshSession.setAttribute(SESSION_KEY, this);
63 this.remoteAddress = (InetSocketAddress) this.sshSession.getIoSession().getRemoteAddress();
64 this.serverKey = this.sshSession.getKex().getServerKey();
67 static CallHomeSessionContext getFrom(final ClientSession sshSession) {
68 return sshSession.getAttribute(SESSION_KEY);
71 AuthFuture authorize() throws IOException {
72 authorization.applyTo(sshSession);
73 return sshSession.auth();
76 void openNetconfChannel() {
77 LOG.debug("Opening NETCONF Subsystem on {}", sshSession);
79 final ClientChannel netconfChannel = sshSession.createSubsystemChannel(NETCONF);
80 netconfChannel.setStreaming(ClientChannel.Streaming.Async);
81 netconfChannel.open().addListener(newSshFutureListener(netconfChannel));
82 } catch (IOException e) {
83 throw new IllegalStateException(e);
87 SshFutureListener<OpenFuture> newSshFutureListener(final ClientChannel netconfChannel) {
89 if (future.isOpened()) {
90 netconfChannelOpened(netconfChannel);
92 channelOpenFailed(future.getException());
97 private void channelOpenFailed(final Throwable throwable) {
98 LOG.error("Unable to open netconf subsystem, disconnecting.", throwable);
99 sshSession.close(false);
102 private void netconfChannelOpened(final ClientChannel netconfChannel) {
103 nettyChannel = newMinaSshNettyChannel(netconfChannel);
104 factory.getChannelOpenListener().onNetconfSubsystemOpened(
105 CallHomeSessionContext.this, this::doActivate);
109 private synchronized Promise<NetconfClientSession> doActivate(final NetconfClientSessionListener listener) {
111 return newSessionPromise().setFailure(new IllegalStateException("Session already activated."));
114 LOG.info("Activating Netconf channel for {} with {}", getRemoteAddress(), listener);
115 Promise<NetconfClientSession> activationPromise = newSessionPromise();
116 factory.getChannelInitializer(listener).initialize(nettyChannel, activationPromise);
117 factory.getNettyGroup().register(nettyChannel).awaitUninterruptibly(500);
118 return activationPromise;
121 protected MinaSshNettyChannel newMinaSshNettyChannel(final ClientChannel netconfChannel) {
122 return new MinaSshNettyChannel(this, sshSession, netconfChannel);
125 private static Promise<NetconfClientSession> newSessionPromise() {
126 return GlobalEventExecutor.INSTANCE.newPromise();
130 public PublicKey getRemoteServerKey() {
135 public String getRemoteServerVersion() {
136 return sshSession.getServerVersion();
140 public InetSocketAddress getRemoteAddress() {
141 return remoteAddress;
145 public String getSessionName() {
146 return authorization.getSessionName();
150 factory.remove(this);
153 static class Factory {
155 private final EventLoopGroup nettyGroup;
156 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
157 private final CallHomeNetconfSubsystemListener subsystemListener;
158 private final ConcurrentMap<String, CallHomeSessionContext> sessions = new ConcurrentHashMap<>();
160 Factory(final EventLoopGroup nettyGroup, final NetconfClientSessionNegotiatorFactory negotiatorFactory,
161 final CallHomeNetconfSubsystemListener subsystemListener) {
162 this.nettyGroup = requireNonNull(nettyGroup, "nettyGroup");
163 this.negotiatorFactory = requireNonNull(negotiatorFactory, "negotiatorFactory");
164 this.subsystemListener = requireNonNull(subsystemListener);
167 void remove(final CallHomeSessionContext session) {
168 sessions.remove(session.getSessionName(), session);
171 ReverseSshChannelInitializer getChannelInitializer(final NetconfClientSessionListener listener) {
172 return ReverseSshChannelInitializer.create(negotiatorFactory, listener);
175 CallHomeNetconfSubsystemListener getChannelOpenListener() {
176 return this.subsystemListener;
179 @Nullable CallHomeSessionContext createIfNotExists(final ClientSession sshSession,
180 final CallHomeAuthorization authorization, final SocketAddress remoteAddress) {
181 CallHomeSessionContext session = new CallHomeSessionContext(sshSession, authorization,
182 remoteAddress, this);
183 CallHomeSessionContext preexisting = sessions.putIfAbsent(session.getSessionName(), session);
184 // If preexisting is null - session does not exist, so we can safely create new one, otherwise we return
185 // null and incoming connection will be rejected.
186 return preexisting == null ? session : null;
189 EventLoopGroup getNettyGroup() {