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.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
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;
25 private final Channel channel;
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);
34 protected abstract S thisInstance();
40 sessionListener.onSessionTerminated(thisInstance(), new NetconfTerminationReason("Session closed"));
44 protected void handleMessage(NetconfMessage netconfMessage) {
45 logger.debug("handling incoming message");
46 sessionListener.onMessage(thisInstance(), netconfMessage);
50 public ChannelFuture sendMessage(NetconfMessage netconfMessage) {
51 return channel.writeAndFlush(netconfMessage);
55 protected void endOfInput() {
56 logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
59 this.sessionListener.onSessionDown(thisInstance(), new IOException("End of input detected. Close the session."));
64 protected void sessionUp() {
65 logger.debug("Session {} up", toString());
66 sessionListener.onSessionUp(thisInstance());
71 public String toString() {
72 final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
73 sb.append("sessionId=").append(sessionId);
78 public final boolean isUp() {
82 public final long getSessionId() {