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;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
18 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
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.impl.osgi.SessionMonitoringService;
23 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
24 import org.opendaylight.controller.netconf.util.xml.XmlElement;
25 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.NamedNodeMap;
30 import org.w3c.dom.Node;
32 public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
34 static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionListener.class);
35 private final SessionMonitoringService monitoringService;
36 private final NetconfOperationRouter operationRouter;
37 private final AutoCloseable onSessionDownCloseable;
39 public NetconfServerSessionListener(NetconfOperationRouter operationRouter, SessionMonitoringService monitoringService,
40 AutoCloseable onSessionDownCloseable) {
41 this.operationRouter = operationRouter;
42 this.monitoringService = monitoringService;
43 this.onSessionDownCloseable = onSessionDownCloseable;
47 public void onSessionUp(NetconfServerSession netconfNetconfServerSession) {
48 monitoringService.onSessionUp(netconfNetconfServerSession);
52 public void onSessionDown(NetconfServerSession netconfNetconfServerSession, Exception cause) {
53 logger.debug("Session {} down, reason: {}", netconfNetconfServerSession, cause.getMessage());
54 onDown(netconfNetconfServerSession);
57 public void onDown(NetconfServerSession netconfNetconfServerSession) {
58 monitoringService.onSessionDown(netconfNetconfServerSession);
61 operationRouter.close();
62 } catch (Exception closingEx) {
63 logger.debug("Ignoring exception while closing operationRouter", closingEx);
66 onSessionDownCloseable.close();
67 } catch(Exception ex){
68 logger.debug("Ignoring exception while closing onSessionDownCloseable", ex);
73 public void onSessionTerminated(NetconfServerSession netconfNetconfServerSession,
74 NetconfTerminationReason netconfTerminationReason) {
75 logger.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
76 netconfTerminationReason.getErrorMessage());
77 onDown(netconfNetconfServerSession);
81 public void onMessage(NetconfServerSession session, NetconfMessage netconfMessage) {
84 Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
85 // FIXME: there is no validation since the document may contain yang
87 final NetconfMessage message = processDocument(netconfMessage,
89 logger.debug("Responding with message {}", XmlUtil.toString(message.getDocument()));
90 session.sendMessage(message);
92 if (isCloseSession(netconfMessage)) {
93 closeNetconfSession(session);
96 } catch (final RuntimeException e) {
97 // TODO: should send generic error or close session?
98 logger.error("Unexpected exception", e);
99 session.onIncommingRpcFail();
100 throw new IllegalStateException("Unable to process incoming message " + netconfMessage, e);
101 } catch (NetconfDocumentedException e) {
102 logger.trace("Error occurred while processing message",e);
103 session.onOutgoingRpcError();
104 session.onIncommingRpcFail();
105 SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
109 private void closeNetconfSession(NetconfServerSession session) {
110 // destroy NetconfOperationService
112 logger.info("Session {} closed successfully", session.getSessionId());
117 private NetconfMessage processDocument(final NetconfMessage netconfMessage, NetconfServerSession session)
118 throws NetconfDocumentedException {
120 final Document incomingDocument = netconfMessage.getDocument();
121 final Node rootNode = incomingDocument.getDocumentElement();
123 if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
124 final Document responseDocument = XmlUtil.newDocument();
125 checkMessageId(rootNode);
127 Document rpcReply = operationRouter.onNetconfMessage(incomingDocument, session);
129 rpcReply = SubtreeFilter.applySubtreeFilter(incomingDocument, rpcReply);
131 session.onIncommingRpcSuccess();
133 responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
134 return new NetconfMessage(responseDocument);
136 // unknown command, send RFC 4741 p.70 unknown-element
138 * Tag: unknown-element Error-type: rpc, protocol, application
139 * Severity: error Error-info: <bad-element> : name of the
140 * unexpected element Description: An unexpected element is present.
142 // TODO add message to error info
143 throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
144 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
145 NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
146 rootNode.getNodeName()));
150 private void checkMessageId(Node rootNode) throws NetconfDocumentedException {
151 NamedNodeMap attributes = rootNode.getAttributes();
152 if(attributes.getNamedItemNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlNetconfConstants.MESSAGE_ID)!=null) {
156 if(attributes.getNamedItem(XmlNetconfConstants.MESSAGE_ID)!=null) {
160 throw new NetconfDocumentedException("Missing attribute" + rootNode.getNodeName(),
161 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.missing_attribute,
162 NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of(NetconfDocumentedException.ErrorTag.missing_attribute.toString(),
163 XmlNetconfConstants.MESSAGE_ID));
166 private static boolean isCloseSession(final NetconfMessage incomingDocument) {
167 final Document document = incomingDocument.getDocument();
168 XmlElement rpcElement = XmlElement.fromDomDocument(document);
169 if (rpcElement.getOnlyChildElementOptionally(DefaultCloseSession.CLOSE_SESSION).isPresent()) {