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