f0180cf78d74a8c47a9697f91480c474601a9759
[controller.git] / opendaylight / netconf / netconf-client / src / main / java / org / opendaylight / controller / netconf / client / NetconfClientSession.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.client;
10
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;
19
20 import java.io.IOException;
21 import java.util.Collection;
22
23 public class NetconfClientSession extends NetconfSession {
24
25     private final SessionListener sessionListener;
26     private final long sessionId;
27     private final Channel channel;
28
29     private static final Logger logger = LoggerFactory.getLogger(NetconfClientSession.class);
30     private final Collection<String> capabilities;
31     private boolean up;
32
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());
40     }
41
42     @Override
43     public void close() {
44         channel.close();
45         sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Client Session closed"));
46     }
47
48     @Override
49     protected void handleMessage(NetconfMessage netconfMessage) {
50         logger.debug("Client Session {} received message {}", toString(),
51                 XmlUtil.toString(netconfMessage.getDocument()));
52         sessionListener.onMessage(this, netconfMessage);
53     }
54
55     @Override
56     public void sendMessage(NetconfMessage netconfMessage) {
57         channel.writeAndFlush(netconfMessage);
58     }
59
60     @Override
61     protected void endOfInput() {
62         logger.debug("Client Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
63                 : "initialized");
64         if (isUp()) {
65             this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
66         }
67     }
68
69     @Override
70     protected void sessionUp() {
71         logger.debug("Client Session {} up", toString());
72         sessionListener.onSessionUp(this);
73         this.up = true;
74     }
75
76     @Override
77     public String toString() {
78         final StringBuffer sb = new StringBuffer("ClientNetconfSession{");
79         sb.append("sessionId=").append(sessionId);
80         sb.append('}');
81         return sb.toString();
82     }
83
84     public boolean isUp() {
85         return up;
86     }
87
88     public long getSessionId() {
89         return sessionId;
90     }
91
92     public Collection<String> getServerCapabilities() {
93         return capabilities;
94     }
95 }