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();
55 sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Session closed"));
59 protected void handleMessage(NetconfMessage netconfMessage) {
60 logger.debug("handlign incomming message");
61 sessionListener.onMessage(this, netconfMessage);
64 public void sendMessage(NetconfMessage netconfMessage) {
65 channel.writeAndFlush(netconfMessage);
66 if (exiEncoder!=null){
67 if (channel.pipeline().get(exiEncoderName)== null){
68 channel.pipeline().addBefore(pmeName, exiEncoderName, exiEncoder);
71 if (removeAfterMessageSentname!=null){
72 channel.pipeline().remove(removeAfterMessageSentname);
73 removeAfterMessageSentname = null;
78 protected void endOfInput() {
79 logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
82 this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
87 protected void sessionUp() {
88 logger.debug("Session {} up", toString());
89 sessionListener.onSessionUp(this);
94 public String toString() {
95 final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
96 sb.append("sessionId=").append(sessionId);
101 public boolean isUp() {
105 public long getSessionId() {
109 public <T extends ChannelHandler> T remove(Class<T> handlerType) {
110 return channel.pipeline().remove(handlerType);
113 public <T extends ChannelHandler> T getHandler(Class<T> handlerType) {
114 return channel.pipeline().get(handlerType);
117 public void addFirst(ChannelHandler handler, String name){
118 channel.pipeline().addFirst(name, handler);
120 public void addLast(ChannelHandler handler, String name){
121 channel.pipeline().addLast(name, handler);
124 public void addExiDecoder(String name,ChannelHandler handler){
125 if (channel.pipeline().get(name)== null){
126 channel.pipeline().addBefore(pmdName, name, handler);
129 public void addExiEncoderAfterMessageSent(String name, ChannelHandler handler){
130 this.exiEncoder = handler;
131 this.exiEncoderName = name;
134 public void addExiEncoder(String name, ChannelHandler handler){
135 channel.pipeline().addBefore(pmeName, name, handler);
138 public void removeAfterMessageSent(String handlerName){
139 this.removeAfterMessageSentname = handlerName;