- netconf SSH bridge bundle
[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         sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Session closed"));
56     }
57
58     @Override
59     protected void handleMessage(NetconfMessage netconfMessage) {
60         logger.debug("handlign incomming message");
61         sessionListener.onMessage(this, netconfMessage);
62     }
63
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);
69             }
70         }
71         if (removeAfterMessageSentname!=null){
72             channel.pipeline().remove(removeAfterMessageSentname);
73             removeAfterMessageSentname = null;
74         }
75     }
76
77     @Override
78     protected void endOfInput() {
79         logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
80                 : "initialized");
81         if (isUp()) {
82             this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
83         }
84     }
85
86     @Override
87     protected void sessionUp() {
88         logger.debug("Session {} up", toString());
89         sessionListener.onSessionUp(this);
90         this.up = true;
91     }
92
93     @Override
94     public String toString() {
95         final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
96         sb.append("sessionId=").append(sessionId);
97         sb.append('}');
98         return sb.toString();
99     }
100
101     public boolean isUp() {
102         return up;
103     }
104
105     public long getSessionId() {
106         return sessionId;
107     }
108
109     public <T extends ChannelHandler> T remove(Class<T> handlerType) {
110         return channel.pipeline().remove(handlerType);
111     }
112
113     public <T extends ChannelHandler> T getHandler(Class<T> handlerType) {
114         return channel.pipeline().get(handlerType);
115    }
116
117     public void addFirst(ChannelHandler handler, String name){
118         channel.pipeline().addFirst(name, handler);
119     }
120     public void addLast(ChannelHandler handler, String name){
121         channel.pipeline().addLast(name, handler);
122     }
123
124     public void addExiDecoder(String name,ChannelHandler handler){
125         if (channel.pipeline().get(name)== null){
126             channel.pipeline().addBefore(pmdName, name, handler);
127         }
128     }
129     public void addExiEncoderAfterMessageSent(String name, ChannelHandler handler){
130         this.exiEncoder = handler;
131         this.exiEncoderName = name;
132     }
133
134     public void addExiEncoder(String name, ChannelHandler handler){
135         channel.pipeline().addBefore(pmeName, name, handler);
136     }
137
138     public void removeAfterMessageSent(String handlerName){
139         this.removeAfterMessageSentname = handlerName;
140     }
141
142 }
143