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