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