Fixed tag for <nexus.repository.snapshot>
[controller.git] / opendaylight / netconf / netconf-netty-util / src / main / java / org / opendaylight / controller / netconf / nettyutil / AbstractNetconfSession.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.netconf.nettyutil;
9
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.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public abstract class AbstractNetconfSession<S extends NetconfSession, L extends NetconfSessionListener<S>> extends AbstractProtocolSession<NetconfMessage> implements NetconfSession, NetconfExiSession {
32     private static final Logger LOG = LoggerFactory.getLogger(AbstractNetconfSession.class);
33     private final L sessionListener;
34     private final long sessionId;
35     private boolean up = false;
36
37     private ChannelHandler delayedEncoder;
38
39     private final Channel channel;
40
41     protected AbstractNetconfSession(final L sessionListener, final Channel channel, final long sessionId) {
42         this.sessionListener = sessionListener;
43         this.channel = channel;
44         this.sessionId = sessionId;
45         LOG.debug("Session {} created", sessionId);
46     }
47
48     protected abstract S thisInstance();
49
50     @Override
51     public void close() {
52         channel.close();
53         up = false;
54         sessionListener.onSessionTerminated(thisInstance(), new NetconfTerminationReason("Session closed"));
55     }
56
57     @Override
58     protected void handleMessage(final NetconfMessage netconfMessage) {
59         LOG.debug("handling incoming message");
60         sessionListener.onMessage(thisInstance(), netconfMessage);
61     }
62
63     @Override
64     public ChannelFuture sendMessage(final NetconfMessage netconfMessage) {
65         final ChannelFuture future = channel.writeAndFlush(netconfMessage);
66         if (delayedEncoder != null) {
67             replaceMessageEncoder(delayedEncoder);
68             delayedEncoder = null;
69         }
70
71         return future;
72     }
73
74     @Override
75     protected void endOfInput() {
76         LOG.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
77                 : "initialized");
78         if (isUp()) {
79             this.sessionListener.onSessionDown(thisInstance(), new IOException("End of input detected. Close the session."));
80         }
81     }
82
83     @Override
84     protected void sessionUp() {
85         LOG.debug("Session {} up", toString());
86         sessionListener.onSessionUp(thisInstance());
87         this.up = true;
88     }
89
90     @Override
91     public String toString() {
92         final StringBuffer sb = new StringBuffer(getClass().getSimpleName() + "{");
93         sb.append("sessionId=").append(sessionId);
94         sb.append(", channel=").append(channel);
95         sb.append('}');
96         return sb.toString();
97     }
98
99     protected final void replaceMessageDecoder(final ChannelHandler handler) {
100         replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, handler);
101     }
102
103     protected final void replaceMessageEncoder(final ChannelHandler handler) {
104         replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, handler);
105     }
106
107     protected final void replaceMessageEncoderAfterNextMessage(final ChannelHandler handler) {
108         this.delayedEncoder = handler;
109     }
110
111     protected final void replaceChannelHandler(final String handlerName, final ChannelHandler handler) {
112         channel.pipeline().replace(handlerName, handlerName, handler);
113     }
114
115     @Override
116     public final void startExiCommunication(final NetconfMessage startExiMessage) {
117         final EXIParameters exiParams;
118         try {
119             exiParams = EXIParameters.fromXmlElement(XmlElement.fromDomDocument(startExiMessage.getDocument()));
120         } catch (final EXIOptionsException e) {
121             LOG.warn("Unable to parse EXI parameters from {} on session {}", startExiMessage, this, e);
122             throw new IllegalArgumentException("Cannot parse options", e);
123         }
124
125         final NetconfEXICodec exiCodec = new NetconfEXICodec(exiParams.getOptions());
126         final NetconfMessageToEXIEncoder exiEncoder = new NetconfMessageToEXIEncoder(exiCodec);
127         final NetconfEXIToMessageDecoder exiDecoder = new NetconfEXIToMessageDecoder(exiCodec);
128         addExiHandlers(exiDecoder, exiEncoder);
129
130         LOG.debug("Session {} EXI handlers added to pipeline", this);
131     }
132
133     /**
134      * Add a set encoder/decoder tuple into the channel pipeline as appropriate.
135      *
136      * @param decoder EXI decoder
137      * @param encoder EXI encoder
138      */
139     protected abstract void addExiHandlers(ByteToMessageDecoder decoder, MessageToByteEncoder<NetconfMessage> encoder);
140
141     public final boolean isUp() {
142         return up;
143     }
144
145     public final long getSessionId() {
146         return sessionId;
147     }
148 }