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 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;
35 class CallHomeSessionContext implements CallHomeProtocolSessionContext {
37 private static final Logger LOG = LoggerFactory.getLogger(CallHomeSessionContext.class);
38 static final Session.AttributeKey<CallHomeSessionContext> SESSION_KEY = new Session.AttributeKey<>();
40 private static final String NETCONF = "netconf";
42 private final ClientSession sshSession;
43 private final CallHomeAuthorization authorization;
44 private final Factory factory;
46 private volatile boolean activated;
48 private final InetSocketAddress remoteAddress;
49 private final PublicKey serverKey;
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();
62 static CallHomeSessionContext getFrom(final ClientSession sshSession) {
63 return sshSession.getAttribute(SESSION_KEY);
66 AuthFuture authorize() throws IOException {
67 authorization.applyTo(sshSession);
68 return sshSession.auth();
71 void openNetconfChannel() {
72 LOG.debug("Opening NETCONF Subsystem on {}", sshSession);
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);
82 SshFutureListener<OpenFuture> newSshFutureListener(final ClientChannel netconfChannel) {
84 if (future.isOpened()) {
85 factory.getChannelOpenListener().onNetconfSubsystemOpened(this,
86 listener -> doActivate(netconfChannel, listener));
88 channelOpenFailed(future.getException());
94 public void terminate() {
95 sshSession.close(false);
100 public TransportType getTransportType() {
101 return TransportType.SSH;
104 private void channelOpenFailed(final Throwable throwable) {
105 LOG.error("Unable to open netconf subsystem, disconnecting.", throwable);
106 sshSession.close(false);
109 private synchronized Promise<NetconfClientSession> doActivate(final ClientChannel netconfChannel,
110 final NetconfClientSessionListener listener) {
112 return newSessionPromise().setFailure(new IllegalStateException("Session already activated."));
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;
124 protected MinaSshNettyChannel newMinaSshNettyChannel(final ClientChannel netconfChannel) {
125 return new MinaSshNettyChannel(this, sshSession, netconfChannel);
128 private static Promise<NetconfClientSession> newSessionPromise() {
129 return GlobalEventExecutor.INSTANCE.newPromise();
133 public PublicKey getRemoteServerKey() {
138 public InetSocketAddress getRemoteAddress() {
139 return remoteAddress;
143 public String getSessionId() {
144 return authorization.getSessionName();
148 factory.remove(this);
151 static class Factory {
153 private final EventLoopGroup nettyGroup;
154 private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
155 private final CallHomeNetconfSubsystemListener subsystemListener;
156 private final ConcurrentMap<String, CallHomeSessionContext> sessions = new ConcurrentHashMap<>();
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);
165 void remove(final CallHomeSessionContext session) {
166 sessions.remove(session.getSessionId(), session);
169 ReverseSshChannelInitializer getChannelInitializer(final NetconfClientSessionListener listener) {
170 return ReverseSshChannelInitializer.create(negotiatorFactory, listener);
173 CallHomeNetconfSubsystemListener getChannelOpenListener() {
174 return this.subsystemListener;
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;
187 EventLoopGroup getNettyGroup() {