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.api;
10 import io.netty.channel.Channel;
11 import io.netty.channel.ChannelHandler;
13 import java.io.IOException;
16 import org.opendaylight.protocol.framework.AbstractProtocolSession;
17 import org.opendaylight.protocol.framework.ProtocolMessageDecoder;
18 import org.opendaylight.protocol.framework.ProtocolMessageEncoder;
19 import org.opendaylight.protocol.framework.SessionListener;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public abstract class NetconfSession extends AbstractProtocolSession<NetconfMessage> {
25 private ChannelHandler exiEncoder;
26 private String exiEncoderName;
27 private String removeAfterMessageSentname;
28 private String pmeName,pmdName;
29 private final Channel channel;
30 private final SessionListener sessionListener;
31 private final long sessionId;
32 private boolean up = false;
33 private static final Logger logger = LoggerFactory.getLogger(NetconfSession.class);
34 private static final int T = 0;
36 protected NetconfSession(SessionListener sessionListener, Channel channel, long sessionId) {
37 this.sessionListener = sessionListener;
38 this.channel = channel;
39 this.sessionId = sessionId;
40 logger.debug("Session {} created", toString());
42 ChannelHandler pmd = channel.pipeline().get(ProtocolMessageDecoder.class);
43 ChannelHandler pme = channel.pipeline().get(ProtocolMessageEncoder.class);
45 for (Map.Entry<String, ChannelHandler> entry:channel.pipeline().toMap().entrySet()){
46 if (entry.getValue().equals(pmd)){
47 pmdName = entry.getKey();
49 if (entry.getValue().equals(pme)){
50 pmeName = entry.getKey();
57 sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Session closed"));
61 protected void handleMessage(NetconfMessage netconfMessage) {
62 logger.debug("handlign incomming message");
63 sessionListener.onMessage(this, netconfMessage);
66 public void sendMessage(NetconfMessage netconfMessage) {
67 channel.writeAndFlush(netconfMessage);
68 if (exiEncoder!=null){
69 if (channel.pipeline().get(exiEncoderName)== null){
70 channel.pipeline().addBefore(pmeName, exiEncoderName, exiEncoder);
73 if (removeAfterMessageSentname!=null){
74 channel.pipeline().remove(removeAfterMessageSentname);
75 removeAfterMessageSentname = null;
80 protected void endOfInput() {
81 logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
84 this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
89 protected void sessionUp() {
90 logger.debug("Session {} up", toString());
91 sessionListener.onSessionUp(this);
96 public String toString() {
97 final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
98 sb.append("sessionId=").append(sessionId);
100 return sb.toString();
103 public boolean isUp() {
107 public long getSessionId() {
111 public <T extends ChannelHandler> T remove(Class<T> handlerType) {
112 return channel.pipeline().remove(handlerType);
115 public <T extends ChannelHandler> T getHandler(Class<T> handlerType) {
116 return channel.pipeline().get(handlerType);
119 public void addFirst(ChannelHandler handler, String name){
120 channel.pipeline().addFirst(name, handler);
122 public void addLast(ChannelHandler handler, String name){
123 channel.pipeline().addLast(name, handler);
126 public void addExiDecoder(String name,ChannelHandler handler){
127 if (channel.pipeline().get(name)== null){
128 channel.pipeline().addBefore(pmdName, name, handler);
131 public void addExiEncoderAfterMessageSent(String name, ChannelHandler handler){
132 this.exiEncoder = handler;
133 this.exiEncoderName = name;
136 public void addExiEncoder(String name, ChannelHandler handler){
137 channel.pipeline().addBefore(pmeName, name, handler);
140 public void removeAfterMessageSent(String handlerName){
141 this.removeAfterMessageSentname = handlerName;