Merge "Remove deprecated API elements from protocol framework"
[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.ChannelFuture;
12
13 import java.io.IOException;
14
15 import org.opendaylight.protocol.framework.AbstractProtocolSession;
16 import org.opendaylight.protocol.framework.SessionListener;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public abstract class NetconfSession extends AbstractProtocolSession<NetconfMessage> {
21     private static final Logger logger = LoggerFactory.getLogger(NetconfSession.class);
22     private final SessionListener<NetconfMessage, NetconfSession, NetconfTerminationReason> sessionListener;
23     private final long sessionId;
24     private boolean up = false;
25
26     protected final Channel channel;
27
28     protected NetconfSession(SessionListener<NetconfMessage, NetconfSession, NetconfTerminationReason> sessionListener, Channel channel, long sessionId) {
29         this.sessionListener = sessionListener;
30         this.channel = channel;
31         this.sessionId = sessionId;
32         logger.debug("Session {} created", toString());
33     }
34
35     @Override
36     public void close() {
37         channel.close();
38         up = false;
39         sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Session closed"));
40     }
41
42     @Override
43     protected void handleMessage(NetconfMessage netconfMessage) {
44         logger.debug("handling incoming message");
45         sessionListener.onMessage(this, netconfMessage);
46     }
47
48     public ChannelFuture sendMessage(NetconfMessage netconfMessage) {
49         return channel.writeAndFlush(netconfMessage);
50     }
51
52     @Override
53     protected void endOfInput() {
54         logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
55                 : "initialized");
56         if (isUp()) {
57             this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
58         }
59     }
60
61     @Override
62     protected void sessionUp() {
63         logger.debug("Session {} up", toString());
64         sessionListener.onSessionUp(this);
65         this.up = true;
66     }
67
68     @Override
69     public String toString() {
70         final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
71         sb.append("sessionId=").append(sessionId);
72         sb.append('}');
73         return sb.toString();
74     }
75
76     public final boolean isUp() {
77         return up;
78     }
79
80     public final long getSessionId() {
81         return sessionId;
82     }
83 }
84