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