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;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableMap;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.NetconfMessage;
15 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
16 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
17 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
18 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
19 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
20 import org.opendaylight.controller.netconf.util.xml.XmlElement;
21 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
22 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Node;
28 import static com.google.common.base.Preconditions.checkState;
30 public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
31 public static final String MESSAGE_ID = "message-id";
33 static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionListener.class);
34 private final SessionMonitoringService monitoringService;
35 private final NetconfOperationRouter operationRouter;
36 private final AutoCloseable onSessionDownCloseable;
38 public NetconfServerSessionListener(NetconfOperationRouter operationRouter, SessionMonitoringService monitoringService,
39 AutoCloseable onSessionDownCloseable) {
40 this.operationRouter = operationRouter;
41 this.monitoringService = monitoringService;
42 this.onSessionDownCloseable = onSessionDownCloseable;
46 public void onSessionUp(NetconfServerSession netconfNetconfServerSession) {
47 monitoringService.onSessionUp(netconfNetconfServerSession);
51 public void onSessionDown(NetconfServerSession netconfNetconfServerSession, Exception cause) {
52 logger.debug("Session {} down, reason: {}", netconfNetconfServerSession, cause.getMessage());
53 onDown(netconfNetconfServerSession);
56 public void onDown(NetconfServerSession netconfNetconfServerSession) {
57 monitoringService.onSessionDown(netconfNetconfServerSession);
60 operationRouter.close();
61 } catch (Exception closingEx) {
62 logger.debug("Ignoring exception while closing operationRouter", closingEx);
65 onSessionDownCloseable.close();
66 } catch(Exception ex){
67 logger.debug("Ignoring exception while closing onSessionDownCloseable", ex);
72 public void onSessionTerminated(NetconfServerSession netconfNetconfServerSession,
73 NetconfTerminationReason netconfTerminationReason) {
74 logger.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
75 netconfTerminationReason.getErrorMessage());
76 onDown(netconfNetconfServerSession);
80 public void onMessage(NetconfServerSession session, NetconfMessage netconfMessage) {
83 Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
84 // FIXME: there is no validation since the document may contain yang
86 final NetconfMessage message = processDocument(netconfMessage,
88 logger.debug("Responding with message {}", XmlUtil.toString(message.getDocument()));
89 session.sendMessage(message);
91 if (isCloseSession(netconfMessage)) {
92 closeNetconfSession(session);
95 } catch (final RuntimeException e) {
96 // TODO: should send generic error or close session?
97 logger.error("Unexpected exception", e);
98 session.onIncommingRpcFail();
99 throw new RuntimeException("Unable to process incoming message " + netconfMessage, e);
100 } catch (NetconfDocumentedException e) {
101 session.onOutgoingRpcError();
102 session.onIncommingRpcFail();
103 SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
107 private void closeNetconfSession(NetconfServerSession session) {
108 // destroy NetconfOperationService
110 logger.info("Session {} closed successfully", session.getSessionId());
113 private NetconfMessage processDocument(final NetconfMessage netconfMessage,
114 NetconfServerSession session) throws NetconfDocumentedException {
116 final Document incommingDocument = netconfMessage.getDocument();
117 final Node rootNode = incommingDocument.getDocumentElement();
119 if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
120 final String messageId = rootNode.getAttributes().getNamedItem(MESSAGE_ID).getTextContent();
121 checkState(messageId != null);
122 final Document responseDocument = XmlUtil.newDocument();
123 Document rpcReply = operationRouter.onNetconfMessage(
124 incommingDocument, session);
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 // TODO add message to error info
138 throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
139 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
140 NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
141 rootNode.getNodeName()));
145 private static boolean isCloseSession(final NetconfMessage incommingDocument) {
146 final Document document = incommingDocument.getDocument();
147 XmlElement rpcElement = XmlElement.fromDomDocument(document);
148 if (rpcElement.getOnlyChildElementOptionally("close-session").isPresent())