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.client;
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;
21 import java.util.Collection;
23 public class NetconfClientSession extends NetconfSession {
25 private final SessionListener sessionListener;
26 private final long sessionId;
27 private final Channel channel;
29 private static final Logger logger = LoggerFactory.getLogger(NetconfClientSession.class);
30 private final Collection<String> capabilities;
33 public NetconfClientSession(SessionListener sessionListener, Channel channel, long sessionId,
34 Collection<String> capabilities) {
35 this.sessionListener = sessionListener;
36 this.channel = channel;
37 this.sessionId = sessionId;
38 this.capabilities = capabilities;
39 logger.debug("Client Session {} created", toString());
45 sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Client Session closed"));
49 protected void handleMessage(NetconfMessage netconfMessage) {
50 logger.debug("Client Session {} received message {}", toString(),
51 XmlUtil.toString(netconfMessage.getDocument()));
52 sessionListener.onMessage(this, netconfMessage);
56 public void sendMessage(NetconfMessage netconfMessage) {
57 channel.writeAndFlush(netconfMessage);
61 protected void endOfInput() {
62 logger.debug("Client Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
65 this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
70 protected void sessionUp() {
71 logger.debug("Client Session {} up", toString());
72 sessionListener.onSessionUp(this);
77 public String toString() {
78 final StringBuffer sb = new StringBuffer("ClientNetconfSession{");
79 sb.append("sessionId=").append(sessionId);
84 public boolean isUp() {
88 public long getSessionId() {
92 public Collection<String> getServerCapabilities() {