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;
12 import org.opendaylight.protocol.framework.AbstractProtocolSession;
13 import org.opendaylight.protocol.framework.ProtocolMessageDecoder;
14 import org.opendaylight.protocol.framework.ProtocolMessageEncoder;
15 import org.opendaylight.protocol.framework.SessionListener;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import java.io.IOException;
22 public abstract class NetconfSession extends AbstractProtocolSession<NetconfMessage> {
24 private ChannelHandler exiEncoder;
25 private String exiEncoderName;
26 private String removeAfterMessageSentname;
27 private String pmeName,pmdName;
28 protected final Channel channel;
29 private final SessionListener sessionListener;
30 private final long sessionId;
31 private boolean up = false;
32 private static final Logger logger = LoggerFactory.getLogger(NetconfSession.class);
34 protected NetconfSession(SessionListener sessionListener, Channel channel, long sessionId) {
35 this.sessionListener = sessionListener;
36 this.channel = channel;
37 this.sessionId = sessionId;
38 logger.debug("Session {} created", toString());
40 ChannelHandler pmd = channel.pipeline().get(ProtocolMessageDecoder.class);
41 ChannelHandler pme = channel.pipeline().get(ProtocolMessageEncoder.class);
43 for (Map.Entry<String, ChannelHandler> entry:channel.pipeline().toMap().entrySet()){
44 if (entry.getValue().equals(pmd)){
45 pmdName = entry.getKey();
47 if (entry.getValue().equals(pme)){
48 pmeName = entry.getKey();
56 sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Session closed"));
60 protected void handleMessage(NetconfMessage netconfMessage) {
61 logger.debug("handling incoming message");
62 sessionListener.onMessage(this, netconfMessage);
65 public void sendMessage(NetconfMessage netconfMessage) {
66 channel.writeAndFlush(netconfMessage);
67 if (exiEncoder!=null){
68 if (channel.pipeline().get(exiEncoderName)== null){
69 channel.pipeline().addBefore(pmeName, exiEncoderName, exiEncoder);
72 if (removeAfterMessageSentname!=null){
73 channel.pipeline().remove(removeAfterMessageSentname);
74 removeAfterMessageSentname = null;
79 protected void endOfInput() {
80 logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
83 this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
88 protected void sessionUp() {
89 logger.debug("Session {} up", toString());
90 sessionListener.onSessionUp(this);
95 public String toString() {
96 final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
97 sb.append("sessionId=").append(sessionId);
102 public boolean isUp() {
106 public long getSessionId() {
110 public <T extends ChannelHandler> T remove(Class<T> handlerType) {
111 return channel.pipeline().remove(handlerType);
114 public <T extends ChannelHandler> T getHandler(Class<T> handlerType) {
115 return channel.pipeline().get(handlerType);
118 public void addFirst(ChannelHandler handler, String name){
119 channel.pipeline().addFirst(name, handler);
121 public void addLast(ChannelHandler handler, String name){
122 channel.pipeline().addLast(name, handler);
125 public void addExiDecoder(String name,ChannelHandler handler){
126 if (channel.pipeline().get(name)== null){
127 channel.pipeline().addBefore(pmdName, name, handler);
130 public void addExiEncoderAfterMessageSent(String name, ChannelHandler handler){
131 this.exiEncoder = handler;
132 this.exiEncoderName = name;
135 public void addExiEncoder(String name, ChannelHandler handler){
136 channel.pipeline().addBefore(pmeName, name, handler);
139 public void removeAfterMessageSent(String handlerName){
140 this.removeAfterMessageSentname = handlerName;