Merge "Bug 809: Enhancements to the toaster example"
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / 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.util;
9
10 import java.io.IOException;
11
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;
24
25 import io.netty.channel.Channel;
26 import io.netty.channel.ChannelFuture;
27 import io.netty.channel.ChannelHandler;
28
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;
34
35     private ChannelHandler delayedEncoder;
36
37     private final Channel channel;
38
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);
44     }
45
46     protected abstract S thisInstance();
47
48     @Override
49     public void close() {
50         channel.close();
51         up = false;
52         sessionListener.onSessionTerminated(thisInstance(), new NetconfTerminationReason("Session closed"));
53     }
54
55     @Override
56     protected void handleMessage(final NetconfMessage netconfMessage) {
57         logger.debug("handling incoming message");
58         sessionListener.onMessage(thisInstance(), netconfMessage);
59     }
60
61     @Override
62     public ChannelFuture sendMessage(final NetconfMessage netconfMessage) {
63         final ChannelFuture future = channel.writeAndFlush(netconfMessage);
64         if (delayedEncoder != null) {
65                 replaceMessageEncoder(delayedEncoder);
66                 delayedEncoder = null;
67         }
68
69         return future;
70     }
71
72     @Override
73     protected void endOfInput() {
74         logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
75                 : "initialized");
76         if (isUp()) {
77             this.sessionListener.onSessionDown(thisInstance(), new IOException("End of input detected. Close the session."));
78         }
79     }
80
81     @Override
82     protected void sessionUp() {
83         logger.debug("Session {} up", toString());
84         sessionListener.onSessionUp(thisInstance());
85         this.up = true;
86     }
87
88     @Override
89     public String toString() {
90         final StringBuffer sb = new StringBuffer(getClass().getSimpleName() + "{");
91         sb.append("sessionId=").append(sessionId);
92         sb.append('}');
93         return sb.toString();
94     }
95
96     protected final void replaceMessageDecoder(final ChannelHandler handler) {
97         replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, handler);
98     }
99
100     protected final void replaceMessageEncoder(final ChannelHandler handler) {
101         replaceChannelHandler(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER, handler);
102     }
103
104     protected final void replaceMessageEncoderAfterNextMessage(final ChannelHandler handler) {
105         this.delayedEncoder = handler;
106     }
107
108     protected final void replaceChannelHandler(final String handlerName, final ChannelHandler handler) {
109         channel.pipeline().replace(handlerName, handlerName, handler);
110     }
111
112     @Override
113     public final void startExiCommunication(final NetconfMessage startExiMessage) {
114         final EXIParameters exiParams;
115         try {
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);
120         }
121         final NetconfEXICodec exiCodec = new NetconfEXICodec(exiParams.getOptions());
122         addExiHandlers(exiCodec);
123         logger.debug("Session {} EXI handlers added to pipeline", this);
124     }
125
126     protected abstract void addExiHandlers(NetconfEXICodec exiCodec);
127
128     public final boolean isUp() {
129         return up;
130     }
131
132     public final long getSessionId() {
133         return sessionId;
134     }
135 }