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
9 package org.opendaylight.controller.netconf.impl;
11 import io.netty.channel.Channel;
12 import org.opendaylight.controller.netconf.api.NetconfMessage;
13 import org.opendaylight.controller.netconf.api.NetconfSession;
14 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
15 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
16 import org.opendaylight.protocol.framework.SessionListener;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import java.io.IOException;
22 public class NetconfServerSession extends NetconfSession {
24 private final SessionListener sessionListener;
25 private final Channel channel;
27 private static final Logger logger = LoggerFactory.getLogger(NetconfServerSession.class);
28 private final long sessionId;
29 private boolean up = false;
31 public NetconfServerSession(SessionListener sessionListener, Channel channel, long sessionId) {
32 this.sessionListener = sessionListener;
33 this.channel = channel;
34 this.sessionId = sessionId;
35 logger.debug("Session {} created", toString());
41 sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Session closed"));
45 protected void handleMessage(NetconfMessage netconfMessage) {
46 logger.debug("Session {} received message {}", toString(), XmlUtil.toString(netconfMessage.getDocument()));
47 sessionListener.onMessage(this, netconfMessage);
50 public void sendMessage(NetconfMessage netconfMessage) {
51 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(this, new IOException("End of input detected. Close the session."));
64 protected void sessionUp() {
65 logger.debug("Session {} up", toString());
66 sessionListener.onSessionUp(this);
71 public String toString() {
72 final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
73 sb.append("sessionId=").append(sessionId);
78 public boolean isUp() {
82 public long getSessionId() {