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 <T extends ChannelHandler> T removeHandler(final Class<T> handlerType) {
97 return this.channel.pipeline().remove(handlerType);
100 protected void replaceMessageDecoder(final ChannelHandler handler) {
101 replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, handler);
104 protected void replaceMessageEncoder(final ChannelHandler handler) {
105 replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, handler);
108 protected void replaceMessageEncoderAfterNextMessage(final ChannelHandler handler) {
109 this.delayedEncoder = handler;
112 protected 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 logger.warn("Unable to parse EXI parameters from {} om session {}", XmlUtil.toString(startExiMessage.getDocument()), this, e);
123 throw new IllegalArgumentException(e);
125 final NetconfEXICodec exiCodec = new NetconfEXICodec(exiParams.getOptions());
126 addExiHandlers(exiCodec);
127 logger.debug("EXI handlers added to pipeline on session {}", this);
130 protected abstract void addExiHandlers(NetconfEXICodec exiCodec);
132 public final boolean isUp() {
136 public final long getSessionId() {