2 * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.netconf.nettyutil;
10 import io.netty.channel.Channel;
11 import io.netty.channel.ChannelFuture;
12 import io.netty.channel.ChannelHandler;
13 import io.netty.handler.codec.ByteToMessageDecoder;
14 import io.netty.handler.codec.MessageToByteEncoder;
15 import java.io.IOException;
16 import org.opendaylight.controller.netconf.api.NetconfExiSession;
17 import org.opendaylight.controller.netconf.api.NetconfMessage;
18 import org.opendaylight.controller.netconf.api.NetconfSession;
19 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
20 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
21 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfEXICodec;
22 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfEXIToMessageDecoder;
23 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfMessageToEXIEncoder;
24 import org.opendaylight.controller.netconf.nettyutil.handler.exi.EXIParameters;
25 import org.opendaylight.controller.netconf.util.xml.XmlElement;
26 import org.opendaylight.protocol.framework.AbstractProtocolSession;
27 import org.openexi.proc.common.EXIOptionsException;
28 import org.openexi.sax.TransmogrifierException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public abstract class AbstractNetconfSession<S extends NetconfSession, L extends NetconfSessionListener<S>> extends AbstractProtocolSession<NetconfMessage> implements NetconfSession, NetconfExiSession {
33 private static final Logger LOG = LoggerFactory.getLogger(AbstractNetconfSession.class);
34 private final L sessionListener;
35 private final long sessionId;
36 private boolean up = false;
38 private ChannelHandler delayedEncoder;
40 private final Channel channel;
42 protected AbstractNetconfSession(final L sessionListener, final Channel channel, final long sessionId) {
43 this.sessionListener = sessionListener;
44 this.channel = channel;
45 this.sessionId = sessionId;
46 LOG.debug("Session {} created", sessionId);
49 protected abstract S thisInstance();
55 sessionListener.onSessionTerminated(thisInstance(), new NetconfTerminationReason("Session closed"));
59 protected void handleMessage(final NetconfMessage netconfMessage) {
60 LOG.debug("handling incoming message");
61 sessionListener.onMessage(thisInstance(), netconfMessage);
65 public ChannelFuture sendMessage(final NetconfMessage netconfMessage) {
66 final ChannelFuture future = channel.writeAndFlush(netconfMessage);
67 if (delayedEncoder != null) {
68 replaceMessageEncoder(delayedEncoder);
69 delayedEncoder = null;
76 protected void endOfInput() {
77 LOG.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
80 this.sessionListener.onSessionDown(thisInstance(), new IOException("End of input detected. Close the session."));
85 protected void sessionUp() {
86 LOG.debug("Session {} up", toString());
87 sessionListener.onSessionUp(thisInstance());
92 public String toString() {
93 final StringBuffer sb = new StringBuffer(getClass().getSimpleName() + "{");
94 sb.append("sessionId=").append(sessionId);
95 sb.append(", channel=").append(channel);
100 protected final void replaceMessageDecoder(final ChannelHandler handler) {
101 replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, handler);
104 protected final void replaceMessageEncoder(final ChannelHandler handler) {
105 replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, handler);
108 protected final void replaceMessageEncoderAfterNextMessage(final ChannelHandler handler) {
109 this.delayedEncoder = handler;
112 protected final void replaceChannelHandler(final String handlerName, final ChannelHandler handler) {
113 channel.pipeline().replace(handlerName, handlerName, handler);
117 public final void startExiCommunication(final NetconfMessage startExiMessage) {
118 final EXIParameters exiParams;
120 exiParams = EXIParameters.fromXmlElement(XmlElement.fromDomDocument(startExiMessage.getDocument()));
121 } catch (final EXIOptionsException e) {
122 LOG.warn("Unable to parse EXI parameters from {} on session {}", startExiMessage, this, e);
123 throw new IllegalArgumentException("Cannot parse options", e);
126 final NetconfEXICodec exiCodec = new NetconfEXICodec(exiParams.getOptions());
127 final NetconfMessageToEXIEncoder exiEncoder;
129 exiEncoder = NetconfMessageToEXIEncoder.create(exiCodec);
130 } catch (EXIOptionsException | TransmogrifierException e) {
131 LOG.warn("Failed to instantiate EXI encoder for {} on session {}", exiCodec, this, e);
132 throw new IllegalStateException("Cannot instantiate encoder for options", e);
135 final NetconfEXIToMessageDecoder exiDecoder;
137 exiDecoder = NetconfEXIToMessageDecoder.create(exiCodec);
138 } catch (EXIOptionsException e) {
139 LOG.warn("Failed to instantiate EXI decodeer for {} on session {}", exiCodec, this, e);
140 throw new IllegalStateException("Cannot instantiate encoder for options", e);
143 addExiHandlers(exiDecoder, exiEncoder);
144 LOG.debug("Session {} EXI handlers added to pipeline", this);
148 * Add a set encoder/decoder tuple into the channel pipeline as appropriate.
150 * @param decoder EXI decoder
151 * @param encoder EXI encoder
153 protected abstract void addExiHandlers(ByteToMessageDecoder decoder, MessageToByteEncoder<NetconfMessage> encoder);
155 public final boolean isUp() {
159 public final long getSessionId() {