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