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