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.netconf.impl;
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;
33 public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
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;
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;
48 public void onSessionUp(final NetconfServerSession netconfNetconfServerSession) {
49 monitoringSessionListener.onSessionUp(netconfNetconfServerSession);
53 public void onSessionDown(final NetconfServerSession netconfNetconfServerSession, final Exception cause) {
54 LOG.debug("Session {} down, reason: {}", netconfNetconfServerSession, cause.getMessage());
55 onDown(netconfNetconfServerSession);
58 public void onDown(final NetconfServerSession netconfNetconfServerSession) {
59 monitoringSessionListener.onSessionDown(netconfNetconfServerSession);
62 operationRouter.close();
63 } catch (Exception closingEx) {
64 LOG.debug("Ignoring exception while closing operationRouter", closingEx);
67 onSessionDownCloseable.close();
68 } catch(Exception ex){
69 LOG.debug("Ignoring exception while closing onSessionDownCloseable", ex);
74 public void onSessionTerminated(final NetconfServerSession netconfNetconfServerSession,
75 final NetconfTerminationReason netconfTerminationReason) {
76 LOG.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
77 netconfTerminationReason.getErrorMessage());
78 onDown(netconfNetconfServerSession);
82 public void onMessage(final NetconfServerSession session, final NetconfMessage netconfMessage) {
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,
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);
108 public void onNotification(final NetconfServerSession session, final NetconfNotification notification) {
109 monitoringSessionListener.onSessionEvent(SessionEvent.notification(session));
112 private NetconfMessage processDocument(final NetconfMessage netconfMessage, final NetconfServerSession session)
113 throws DocumentedException {
115 final Document incomingDocument = netconfMessage.getDocument();
116 final Node rootNode = incomingDocument.getDocumentElement();
118 if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
119 final Document responseDocument = XmlUtil.newDocument();
120 checkMessageId(rootNode);
122 Document rpcReply = operationRouter.onNetconfMessage(incomingDocument, session);
124 rpcReply = SubtreeFilter.applyRpcSubtreeFilter(incomingDocument, rpcReply);
126 session.onIncommingRpcSuccess();
128 responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
129 return new NetconfMessage(responseDocument);
131 // unknown command, send RFC 4741 p.70 unknown-element
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.
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()));
144 private static void checkMessageId(final Node rootNode) throws DocumentedException {
146 NamedNodeMap attributes = rootNode.getAttributes();
148 if(attributes.getNamedItemNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlNetconfConstants.MESSAGE_ID)!=null) {
152 if(attributes.getNamedItem(XmlNetconfConstants.MESSAGE_ID)!=null) {
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));