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;
11 import io.netty.channel.ChannelFuture;
13 import java.io.IOException;
15 import org.opendaylight.protocol.framework.AbstractProtocolSession;
16 import org.opendaylight.protocol.framework.SessionListener;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
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;
26 protected final Channel channel;
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());
39 sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Session closed"));
43 protected void handleMessage(NetconfMessage netconfMessage) {
44 logger.debug("handling incoming message");
45 sessionListener.onMessage(this, netconfMessage);
48 public ChannelFuture sendMessage(NetconfMessage netconfMessage) {
49 return channel.writeAndFlush(netconfMessage);
53 protected void endOfInput() {
54 logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
57 this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
62 protected void sessionUp() {
63 logger.debug("Session {} up", toString());
64 sessionListener.onSessionUp(this);
69 public String toString() {
70 final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
71 sb.append("sessionId=").append(sessionId);
76 public final boolean isUp() {
80 public final long getSessionId() {