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.controller.netconf.impl;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableMap;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
17 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
18 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
19 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
20 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
21 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
22 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
23 import org.opendaylight.controller.netconf.util.xml.XmlElement;
24 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.NamedNodeMap;
29 import org.w3c.dom.Node;
31 public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
33 private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionListener.class);
34 private final NetconfMonitoringService monitoringService;
35 private final NetconfOperationRouter operationRouter;
36 private final AutoCloseable onSessionDownCloseable;
38 public NetconfServerSessionListener(final NetconfOperationRouter operationRouter, NetconfMonitoringService monitoringService,
39 final AutoCloseable onSessionDownCloseable) {
40 this.operationRouter = operationRouter;
41 this.monitoringService = monitoringService;
42 this.onSessionDownCloseable = onSessionDownCloseable;
46 public void onSessionUp(final NetconfServerSession netconfNetconfServerSession) {
47 monitoringService.onSessionUp(netconfNetconfServerSession);
48 // FIXME monitoring service should be also notified about all the other changes to netconf session (from ietf-netconf-monitoring point of view)
49 // This means also notifying after every message is processed
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 monitoringService.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 // FIXME: there is no validation since the document may contain yang
88 final NetconfMessage message = processDocument(netconfMessage,
90 LOG.debug("Responding with message {}", message);
91 session.sendMessage(message);
93 if (isCloseSession(netconfMessage)) {
94 closeNetconfSession(session);
97 } catch (final RuntimeException e) {
98 // TODO: should send generic error or close session?
99 LOG.error("Unexpected exception", e);
100 session.onIncommingRpcFail();
101 throw new IllegalStateException("Unable to process incoming message " + netconfMessage, e);
102 } catch (NetconfDocumentedException e) {
103 LOG.trace("Error occurred while processing message",e);
104 session.onOutgoingRpcError();
105 session.onIncommingRpcFail();
106 SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
110 private void closeNetconfSession(final NetconfServerSession session) {
111 // destroy NetconfOperationService
113 LOG.info("Session {} closed successfully", session.getSessionId());
118 private NetconfMessage processDocument(final NetconfMessage netconfMessage, final NetconfServerSession session)
119 throws NetconfDocumentedException {
121 final Document incomingDocument = netconfMessage.getDocument();
122 final Node rootNode = incomingDocument.getDocumentElement();
124 if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
125 final Document responseDocument = XmlUtil.newDocument();
126 checkMessageId(rootNode);
128 Document rpcReply = operationRouter.onNetconfMessage(incomingDocument, session);
130 rpcReply = SubtreeFilter.applySubtreeFilter(incomingDocument, rpcReply);
132 session.onIncommingRpcSuccess();
134 responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
135 return new NetconfMessage(responseDocument);
137 // unknown command, send RFC 4741 p.70 unknown-element
139 * Tag: unknown-element Error-type: rpc, protocol, application
140 * Severity: error Error-info: <bad-element> : name of the
141 * unexpected element Description: An unexpected element is present.
143 // TODO add message to error info
144 throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
145 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
146 NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
147 rootNode.getNodeName()));
151 private void checkMessageId(final Node rootNode) throws NetconfDocumentedException {
153 NamedNodeMap attributes = rootNode.getAttributes();
155 if(attributes.getNamedItemNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlNetconfConstants.MESSAGE_ID)!=null) {
159 if(attributes.getNamedItem(XmlNetconfConstants.MESSAGE_ID)!=null) {
163 throw new NetconfDocumentedException("Missing attribute" + rootNode.getNodeName(),
164 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.missing_attribute,
165 NetconfDocumentedException.ErrorSeverity.error,
166 ImmutableMap.of(NetconfDocumentedException.ErrorTag.missing_attribute.toString(),
167 XmlNetconfConstants.MESSAGE_ID));
170 private static boolean isCloseSession(final NetconfMessage incomingDocument) {
171 final Document document = incomingDocument.getDocument();
172 XmlElement rpcElement = XmlElement.fromDomDocument(document);
173 if (rpcElement.getOnlyChildElementOptionally(DefaultCloseSession.CLOSE_SESSION).isPresent()) {