2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.api;
10 import io.netty.channel.Channel;
12 import java.io.IOException;
14 import org.opendaylight.protocol.framework.AbstractProtocolSession;
15 import org.opendaylight.protocol.framework.SessionListener;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
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;
25 protected final Channel channel;
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());
38 sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Session closed"));
42 protected void handleMessage(NetconfMessage netconfMessage) {
43 logger.debug("handling incoming message");
44 sessionListener.onMessage(this, netconfMessage);
47 public void sendMessage(NetconfMessage netconfMessage) {
48 channel.writeAndFlush(netconfMessage);
52 protected void endOfInput() {
53 logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
56 this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
61 protected void sessionUp() {
62 logger.debug("Session {} up", toString());
63 sessionListener.onSessionUp(this);
68 public String toString() {
69 final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
70 sb.append("sessionId=").append(sessionId);
75 public final boolean isUp() {
79 public final long getSessionId() {