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 org.opendaylight.protocol.framework.AbstractProtocolSession;
12 import org.opendaylight.protocol.framework.SessionListener;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import java.io.IOException;
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);
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());
35 sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Session closed"));
39 protected void handleMessage(NetconfMessage netconfMessage) {
40 logger.debug("handling incoming message");
41 sessionListener.onMessage(this, netconfMessage);
44 public void sendMessage(NetconfMessage netconfMessage) {
45 channel.writeAndFlush(netconfMessage);
49 protected void endOfInput() {
50 logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
53 this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
58 protected void sessionUp() {
59 logger.debug("Session {} up", toString());
60 sessionListener.onSessionUp(this);
65 public String toString() {
66 final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
67 sb.append("sessionId=").append(sessionId);
72 public boolean isUp() {
76 public long getSessionId() {