Merge "Initial implementation of the ClusteredDataStore"
[controller.git] / opendaylight / netconf / netconf-api / src / main / java / org / opendaylight / controller / netconf / api / NetconfSession.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.api;
9
10 import io.netty.channel.Channel;
11 import io.netty.channel.ChannelHandler;
12
13 import java.io.IOException;
14 import java.util.Map;
15
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;
22
23 public abstract class NetconfSession extends AbstractProtocolSession<NetconfMessage> {
24
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;
35
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());
41
42         ChannelHandler pmd = channel.pipeline().get(ProtocolMessageDecoder.class);
43         ChannelHandler pme = channel.pipeline().get(ProtocolMessageEncoder.class);
44
45         for (Map.Entry<String, ChannelHandler> entry:channel.pipeline().toMap().entrySet()){
46             if (entry.getValue().equals(pmd)){
47                 pmdName = entry.getKey();
48             }
49             if (entry.getValue().equals(pme)){
50                 pmeName = entry.getKey();
51             }
52         }
53     }
54     @Override
55     public void close() {
56         channel.close();
57         sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Session closed"));
58     }
59
60     @Override
61     protected void handleMessage(NetconfMessage netconfMessage) {
62         logger.debug("handlign incomming message");
63         sessionListener.onMessage(this, netconfMessage);
64     }
65
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);
71             }
72         }
73         if (removeAfterMessageSentname!=null){
74             channel.pipeline().remove(removeAfterMessageSentname);
75             removeAfterMessageSentname = null;
76         }
77     }
78
79     @Override
80     protected void endOfInput() {
81         logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
82                 : "initialized");
83         if (isUp()) {
84             this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
85         }
86     }
87
88     @Override
89     protected void sessionUp() {
90         logger.debug("Session {} up", toString());
91         sessionListener.onSessionUp(this);
92         this.up = true;
93     }
94
95     @Override
96     public String toString() {
97         final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
98         sb.append("sessionId=").append(sessionId);
99         sb.append('}');
100         return sb.toString();
101     }
102
103     public boolean isUp() {
104         return up;
105     }
106
107     public long getSessionId() {
108         return sessionId;
109     }
110
111     public <T extends ChannelHandler> T remove(Class<T> handlerType) {
112         return channel.pipeline().remove(handlerType);
113     }
114
115     public <T extends ChannelHandler> T getHandler(Class<T> handlerType) {
116         return channel.pipeline().get(handlerType);
117    }
118
119     public void addFirst(ChannelHandler handler, String name){
120         channel.pipeline().addFirst(name, handler);
121     }
122     public void addLast(ChannelHandler handler, String name){
123         channel.pipeline().addLast(name, handler);
124     }
125
126     public void addExiDecoder(String name,ChannelHandler handler){
127         if (channel.pipeline().get(name)== null){
128             channel.pipeline().addBefore(pmdName, name, handler);
129         }
130     }
131     public void addExiEncoderAfterMessageSent(String name, ChannelHandler handler){
132         this.exiEncoder = handler;
133         this.exiEncoderName = name;
134     }
135
136     public void addExiEncoder(String name, ChannelHandler handler){
137         channel.pipeline().addBefore(pmeName, name, handler);
138     }
139
140     public void removeAfterMessageSent(String handlerName){
141         this.removeAfterMessageSentname = handlerName;
142     }
143
144 }
145