1ae3fabbb9a31bf5275b63f1bc27da15e8a72d88
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSession.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.impl;
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
22 public class NetconfServerSession extends NetconfSession {
23
24     private final SessionListener sessionListener;
25     private final Channel channel;
26
27     private static final Logger logger = LoggerFactory.getLogger(NetconfServerSession.class);
28     private final long sessionId;
29     private boolean up = false;
30
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());
36     }
37
38     @Override
39     public void close() {
40         channel.close();
41         sessionListener.onSessionTerminated(this, new NetconfTerminationReason("Session closed"));
42     }
43
44     @Override
45     protected void handleMessage(NetconfMessage netconfMessage) {
46         logger.debug("Session {} received message {}", toString(), XmlUtil.toString(netconfMessage.getDocument()));
47         sessionListener.onMessage(this, netconfMessage);
48     }
49
50     public void sendMessage(NetconfMessage netconfMessage) {
51         channel.writeAndFlush(netconfMessage);
52     }
53
54     @Override
55     protected void endOfInput() {
56         logger.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up"
57                 : "initialized");
58         if (isUp()) {
59             this.sessionListener.onSessionDown(this, new IOException("End of input detected. Close the session."));
60         }
61     }
62
63     @Override
64     protected void sessionUp() {
65         logger.debug("Session {} up", toString());
66         sessionListener.onSessionUp(this);
67         this.up = true;
68     }
69
70     @Override
71     public String toString() {
72         final StringBuffer sb = new StringBuffer("ServerNetconfSession{");
73         sb.append("sessionId=").append(sessionId);
74         sb.append('}');
75         return sb.toString();
76     }
77
78     public boolean isUp() {
79         return up;
80     }
81
82     public long getSessionId() {
83         return sessionId;
84     }
85 }