Merge "Remove duplicate dependency declaration"
[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 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;
26
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;
32
33     private ChannelHandler delayedEncoder;
34
35     private final Channel channel;
36
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);
42     }
43
44     protected abstract S thisInstance();
45
46     @Override
47     public void close() {
48         channel.close();
49         up = false;
50         sessionListener.onSessionTerminated(thisInstance(), new NetconfTerminationReason("Session closed"));
51     }
52
53     @Override
54     protected void handleMessage(final NetconfMessage netconfMessage) {
55         LOG.debug("handling incoming message");
56         sessionListener.onMessage(thisInstance(), netconfMessage);
57     }
58
59     @Override
60     public ChannelFuture sendMessage(final NetconfMessage netconfMessage) {
61         final ChannelFuture future = channel.writeAndFlush(netconfMessage);
62         if (delayedEncoder != null) {
63             replaceMessageEncoder(delayedEncoder);
64             delayedEncoder = null;
65         }
66
67         return future;
68     }
69
70     @Override
71     protected void endOfInput() {
72         LOG.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
73                 : "initialized");
74         if (isUp()) {
75             this.sessionListener.onSessionDown(thisInstance(), new IOException("End of input detected. Close the session."));
76         }
77     }
78
79     @Override
80     protected void sessionUp() {
81         LOG.debug("Session {} up", toString());
82         sessionListener.onSessionUp(thisInstance());
83         this.up = true;
84     }
85
86     @Override
87     public String toString() {
88         final StringBuffer sb = new StringBuffer(getClass().getSimpleName() + "{");
89         sb.append("sessionId=").append(sessionId);
90         sb.append('}');
91         return sb.toString();
92     }
93
94     protected final void replaceMessageDecoder(final ChannelHandler handler) {
95         replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, handler);
96     }
97
98     protected final void replaceMessageEncoder(final ChannelHandler handler) {
99         replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, handler);
100     }
101
102     protected final void replaceMessageEncoderAfterNextMessage(final ChannelHandler handler) {
103         this.delayedEncoder = handler;
104     }
105
106     protected final void replaceChannelHandler(final String handlerName, final ChannelHandler handler) {
107         channel.pipeline().replace(handlerName, handlerName, handler);
108     }
109
110     @Override
111     public final void startExiCommunication(final NetconfMessage startExiMessage) {
112         final EXIParameters exiParams;
113         try {
114             exiParams = EXIParameters.fromXmlElement(XmlElement.fromDomDocument(startExiMessage.getDocument()));
115         } catch (final EXIOptionsException e) {
116             LOG.warn("Unable to parse EXI parameters from {} on session {}", startExiMessage, this, e);
117             throw new IllegalArgumentException("Cannot parse options", e);
118         }
119         final NetconfEXICodec exiCodec = new NetconfEXICodec(exiParams.getOptions());
120         addExiHandlers(exiCodec);
121         LOG.debug("Session {} EXI handlers added to pipeline", this);
122     }
123
124     protected abstract void addExiHandlers(NetconfEXICodec exiCodec);
125
126     public final boolean isUp() {
127         return up;
128     }
129
130     public final long getSessionId() {
131         return sessionId;
132     }
133 }