Rename NetconfNotification to NotificationMessage
[netconf.git] / protocol / netconf-server / src / main / java / org / opendaylight / netconf / server / NetconfServerSessionListener.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 package org.opendaylight.netconf.server;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import org.opendaylight.netconf.api.DocumentedException;
13 import org.opendaylight.netconf.api.NetconfMessage;
14 import org.opendaylight.netconf.api.NetconfSessionListener;
15 import org.opendaylight.netconf.api.NetconfTerminationReason;
16 import org.opendaylight.netconf.api.messages.NotificationMessage;
17 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
18 import org.opendaylight.netconf.api.monitoring.SessionEvent;
19 import org.opendaylight.netconf.api.monitoring.SessionListener;
20 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
21 import org.opendaylight.netconf.api.xml.XmlUtil;
22 import org.opendaylight.netconf.server.osgi.NetconfOperationRouter;
23 import org.opendaylight.netconf.util.messages.SubtreeFilter;
24 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
25 import org.opendaylight.yangtools.yang.common.ErrorTag;
26 import org.opendaylight.yangtools.yang.common.ErrorType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.NamedNodeMap;
31 import org.w3c.dom.Node;
32
33 public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
34     private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionListener.class);
35
36     private final SessionListener monitoringSessionListener;
37     private final NetconfOperationRouter operationRouter;
38     private final AutoCloseable onSessionDownCloseable;
39
40     public NetconfServerSessionListener(final NetconfOperationRouter operationRouter,
41                                         final NetconfMonitoringService monitoringService,
42                                         final AutoCloseable onSessionDownCloseable) {
43         this.operationRouter = operationRouter;
44         monitoringSessionListener = monitoringService.getSessionListener();
45         this.onSessionDownCloseable = onSessionDownCloseable;
46     }
47
48     @Override
49     public void onSessionUp(final NetconfServerSession netconfNetconfServerSession) {
50         monitoringSessionListener.onSessionUp(netconfNetconfServerSession);
51     }
52
53     @Override
54     public void onSessionDown(final NetconfServerSession netconfNetconfServerSession, final Exception cause) {
55         LOG.debug("Session {} down, reason: {}", netconfNetconfServerSession, cause.getMessage());
56         onDown(netconfNetconfServerSession);
57     }
58
59     @SuppressWarnings("checkstyle:IllegalCatch")
60     public void onDown(final NetconfServerSession netconfNetconfServerSession) {
61         monitoringSessionListener.onSessionDown(netconfNetconfServerSession);
62
63         try {
64             operationRouter.close();
65         } catch (final Exception closingEx) {
66             LOG.debug("Ignoring exception while closing operationRouter", closingEx);
67         }
68         try {
69             onSessionDownCloseable.close();
70         } catch (final Exception ex) {
71             LOG.debug("Ignoring exception while closing onSessionDownCloseable", ex);
72         }
73     }
74
75     @Override
76     public void onSessionTerminated(final NetconfServerSession netconfNetconfServerSession,
77                                     final NetconfTerminationReason netconfTerminationReason) {
78         LOG.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
79                 netconfTerminationReason.getErrorMessage());
80         onDown(netconfNetconfServerSession);
81     }
82
83     @SuppressWarnings("checkstyle:IllegalCatch")
84     @Override
85     public void onMessage(final NetconfServerSession session, final NetconfMessage netconfMessage) {
86         try {
87
88             Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
89             // there is no validation since the document may contain yang schemas
90             final NetconfMessage message = processDocument(netconfMessage, session);
91             LOG.debug("Responding with message {}", message);
92             session.sendMessage(message);
93             monitoringSessionListener.onSessionEvent(SessionEvent.inRpcSuccess(session));
94         } catch (final RuntimeException e) {
95             // TODO: should send generic error or close session?
96             LOG.error("Unexpected exception", e);
97             session.onIncommingRpcFail();
98             monitoringSessionListener.onSessionEvent(SessionEvent.inRpcFail(session));
99             throw new IllegalStateException("Unable to process incoming message " + netconfMessage, e);
100         } catch (final DocumentedException e) {
101             LOG.trace("Error occurred while processing message", e);
102             session.onOutgoingRpcError();
103             session.onIncommingRpcFail();
104             monitoringSessionListener.onSessionEvent(SessionEvent.inRpcFail(session));
105             monitoringSessionListener.onSessionEvent(SessionEvent.outRpcError(session));
106             SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
107         }
108     }
109
110     @Override
111     public void onError(final NetconfServerSession session, final Exception failure) {
112         session.onIncommingRpcFail();
113         monitoringSessionListener.onSessionEvent(SessionEvent.inRpcFail(session));
114         throw new IllegalStateException("Unable to process incoming message", failure);
115     }
116
117     public void onNotification(final NetconfServerSession session, final NotificationMessage notification) {
118         monitoringSessionListener.onSessionEvent(SessionEvent.notification(session));
119     }
120
121     private NetconfMessage processDocument(final NetconfMessage netconfMessage, final NetconfServerSession session)
122             throws DocumentedException {
123
124         final Document incomingDocument = netconfMessage.getDocument();
125         final Node rootNode = incomingDocument.getDocumentElement();
126
127         if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
128             final Document responseDocument = XmlUtil.newDocument();
129             checkMessageId(rootNode);
130
131             Document rpcReply = operationRouter.onNetconfMessage(incomingDocument, session);
132
133             rpcReply = SubtreeFilter.applyRpcSubtreeFilter(incomingDocument, rpcReply);
134
135             session.onIncommingRpcSuccess();
136
137             responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
138             return new NetconfMessage(responseDocument);
139         } else {
140             // unknown command, send RFC 4741 p.70 unknown-element
141             /*
142              * Tag: unknown-element Error-type: rpc, protocol, application
143              * Severity: error Error-info: <bad-element> : name of the
144              * unexpected element Description: An unexpected element is present.
145              */
146             throw new DocumentedException("Unknown tag " + rootNode.getNodeName() + " in message:\n" + netconfMessage,
147                     ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT, ErrorSeverity.ERROR,
148                     ImmutableMap.of("bad-element", rootNode.getNodeName()));
149         }
150     }
151
152     private static void checkMessageId(final Node rootNode) throws DocumentedException {
153
154         final NamedNodeMap attributes = rootNode.getAttributes();
155
156         if (attributes.getNamedItemNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
157                 XmlNetconfConstants.MESSAGE_ID) != null) {
158             return;
159         }
160
161         if (attributes.getNamedItem(XmlNetconfConstants.MESSAGE_ID) != null) {
162             return;
163         }
164
165         throw new DocumentedException("Missing attribute " + rootNode.getNodeName(),
166                 ErrorType.RPC, ErrorTag.MISSING_ATTRIBUTE, ErrorSeverity.ERROR, ImmutableMap.of(
167                     "bad-attribute", XmlNetconfConstants.MESSAGE_ID,
168                     "bad-element", XmlNetconfConstants.RPC_KEY));
169     }
170 }