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 java.io.IOException;
14 import org.opendaylight.controller.netconf.api.NetconfExiSession;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.controller.netconf.api.NetconfSession;
17 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
18 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
19 import org.opendaylight.controller.netconf.nettyutil.handler.NetconfEXICodec;
20 import org.opendaylight.controller.netconf.nettyutil.handler.exi.EXIParameters;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.protocol.framework.AbstractProtocolSession;
23 import org.openexi.proc.common.EXIOptionsException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public abstract class AbstractNetconfSession<S extends NetconfSession, L extends NetconfSessionListener<S>> extends AbstractProtocolSession<NetconfMessage> implements NetconfSession, NetconfExiSession {
28 private static final Logger LOG = LoggerFactory.getLogger(AbstractNetconfSession.class);
29 private final L sessionListener;
30 private final long sessionId;
31 private boolean up = false;
33 private ChannelHandler delayedEncoder;
35 private final Channel channel;
37 protected AbstractNetconfSession(final L sessionListener, final Channel channel, final long sessionId) {
38 this.sessionListener = sessionListener;
39 this.channel = channel;
40 this.sessionId = sessionId;
41 LOG.debug("Session {} created", sessionId);
44 protected abstract S thisInstance();
50 sessionListener.onSessionTerminated(thisInstance(), new NetconfTerminationReason("Session closed"));
54 protected void handleMessage(final NetconfMessage netconfMessage) {
55 LOG.debug("handling incoming message");
56 sessionListener.onMessage(thisInstance(), netconfMessage);
60 public ChannelFuture sendMessage(final NetconfMessage netconfMessage) {
61 final ChannelFuture future = channel.writeAndFlush(netconfMessage);
62 if (delayedEncoder != null) {
63 replaceMessageEncoder(delayedEncoder);
64 delayedEncoder = null;
71 protected void endOfInput() {
72 LOG.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
75 this.sessionListener.onSessionDown(thisInstance(), new IOException("End of input detected. Close the session."));
80 protected void sessionUp() {
81 LOG.debug("Session {} up", toString());
82 sessionListener.onSessionUp(thisInstance());
87 public String toString() {
88 final StringBuffer sb = new StringBuffer(getClass().getSimpleName() + "{");
89 sb.append("sessionId=").append(sessionId);
90 sb.append(", channel=").append(channel);
95 protected final void replaceMessageDecoder(final ChannelHandler handler) {
96 replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, handler);
99 protected final void replaceMessageEncoder(final ChannelHandler handler) {
100 replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, handler);
103 protected final void replaceMessageEncoderAfterNextMessage(final ChannelHandler handler) {
104 this.delayedEncoder = handler;
107 protected final void replaceChannelHandler(final String handlerName, final ChannelHandler handler) {
108 channel.pipeline().replace(handlerName, handlerName, handler);
112 public final void startExiCommunication(final NetconfMessage startExiMessage) {
113 final EXIParameters exiParams;
115 exiParams = EXIParameters.fromXmlElement(XmlElement.fromDomDocument(startExiMessage.getDocument()));
116 } catch (final EXIOptionsException e) {
117 LOG.warn("Unable to parse EXI parameters from {} on session {}", startExiMessage, this, e);
118 throw new IllegalArgumentException("Cannot parse options", e);
120 final NetconfEXICodec exiCodec = new NetconfEXICodec(exiParams.getOptions());
121 addExiHandlers(exiCodec);
122 LOG.debug("Session {} EXI handlers added to pipeline", this);
125 protected abstract void addExiHandlers(NetconfEXICodec exiCodec);
127 public final boolean isUp() {
131 public final long getSessionId() {