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.util;
10 import java.io.IOException;
12 import org.opendaylight.controller.netconf.api.NetconfMessage;
13 import org.opendaylight.controller.netconf.api.NetconfSession;
14 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
15 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
16 import org.opendaylight.controller.netconf.util.handler.NetconfEXICodec;
17 import org.opendaylight.controller.netconf.util.xml.EXIParameters;
18 import org.opendaylight.controller.netconf.util.xml.XmlElement;
19 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
20 import org.opendaylight.protocol.framework.AbstractProtocolSession;
21 import org.openexi.proc.common.EXIOptionsException;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import io.netty.channel.Channel;
26 import io.netty.channel.ChannelFuture;
27 import io.netty.channel.ChannelHandler;
29 public abstract class AbstractNetconfSession<S extends NetconfSession, L extends NetconfSessionListener<S>> extends AbstractProtocolSession<NetconfMessage> implements NetconfSession, NetconfExiSession {
30 private static final Logger logger = LoggerFactory.getLogger(AbstractNetconfSession.class);
31 private final L sessionListener;
32 private final long sessionId;
33 private boolean up = false;
35 private ChannelHandler delayedEncoder;
37 private final Channel channel;
39 protected AbstractNetconfSession(final L sessionListener, final Channel channel, final long sessionId) {
40 this.sessionListener = sessionListener;
41 this.channel = channel;
42 this.sessionId = sessionId;
43 logger.debug("Session {} created", sessionId);
46 protected abstract S thisInstance();
52 sessionListener.onSessionTerminated(thisInstance(), new NetconfTerminationReason("Session closed"));
56 protected void handleMessage(final NetconfMessage netconfMessage) {
57 logger.debug("handling incoming message");
58 sessionListener.onMessage(thisInstance(), netconfMessage);
62 public ChannelFuture sendMessage(final NetconfMessage netconfMessage) {
63 final ChannelFuture future = channel.writeAndFlush(netconfMessage);
64 if (delayedEncoder != null) {
65 replaceMessageEncoder(delayedEncoder);
66 delayedEncoder = null;
73 protected void endOfInput() {
74 logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
77 this.sessionListener.onSessionDown(thisInstance(), new IOException("End of input detected. Close the session."));
82 protected void sessionUp() {
83 logger.debug("Session {} up", toString());
84 sessionListener.onSessionUp(thisInstance());
89 public String toString() {
90 final StringBuffer sb = new StringBuffer(getClass().getSimpleName() + "{");
91 sb.append("sessionId=").append(sessionId);
96 protected final void replaceMessageDecoder(final ChannelHandler handler) {
97 replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, handler);
100 protected final void replaceMessageEncoder(final ChannelHandler handler) {
101 replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, handler);
104 protected final void replaceMessageEncoderAfterNextMessage(final ChannelHandler handler) {
105 this.delayedEncoder = handler;
108 protected final void replaceChannelHandler(final String handlerName, final ChannelHandler handler) {
109 channel.pipeline().replace(handlerName, handlerName, handler);
113 public final void startExiCommunication(final NetconfMessage startExiMessage) {
114 final EXIParameters exiParams;
116 exiParams = EXIParameters.fromXmlElement(XmlElement.fromDomDocument(startExiMessage.getDocument()));
117 } catch (final EXIOptionsException e) {
118 logger.warn("Unable to parse EXI parameters from {} om session {}", XmlUtil.toString(startExiMessage.getDocument()), this, e);
119 throw new IllegalArgumentException(e);
121 final NetconfEXICodec exiCodec = new NetconfEXICodec(exiParams.getOptions());
122 addExiHandlers(exiCodec);
123 logger.debug("Session {} EXI handlers added to pipeline", this);
126 protected abstract void addExiHandlers(NetconfEXICodec exiCodec);
128 public final boolean isUp() {
132 public final long getSessionId() {