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;
37 public NetconfServerSessionListener(NetconfOperationRouter operationRouter, SessionMonitoringService monitoringService) {
38 this.operationRouter = operationRouter;
39 this.monitoringService = monitoringService;
43 public void onSessionUp(NetconfServerSession netconfNetconfServerSession) {
44 monitoringService.onSessionUp(netconfNetconfServerSession);
48 public void onSessionDown(NetconfServerSession netconfNetconfServerSession, Exception cause) {
49 logger.debug("Session {} down, reason: {}", netconfNetconfServerSession, cause.getMessage());
50 monitoringService.onSessionDown(netconfNetconfServerSession);
53 operationRouter.close();
54 } catch (Exception closingEx) {
55 logger.debug("Ignoring exception while closing operationRouter", closingEx);
60 public void onSessionTerminated(NetconfServerSession netconfNetconfServerSession,
61 NetconfTerminationReason netconfTerminationReason) {
62 logger.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
63 netconfTerminationReason.getErrorMessage());
64 monitoringService.onSessionDown(netconfNetconfServerSession);
67 operationRouter.close();
68 } catch (Exception closingEx) {
69 logger.debug("Ignoring exception while closing operationRouter", closingEx);
74 public void onMessage(NetconfServerSession session, NetconfMessage netconfMessage) {
77 Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
78 // FIXME: there is no validation since the document may contain yang
80 final NetconfMessage message = processDocument(netconfMessage,
82 logger.debug("Responding with message {}", XmlUtil.toString(message.getDocument()));
83 session.sendMessage(message);
85 if (isCloseSession(netconfMessage)) {
86 closeNetconfSession(session);
89 } catch (final RuntimeException e) {
90 // TODO: should send generic error or close session?
91 logger.error("Unexpected exception", e);
92 session.onIncommingRpcFail();
93 throw new RuntimeException("Unable to process incoming message " + netconfMessage, e);
94 } catch (NetconfDocumentedException e) {
95 session.onOutgoingRpcError();
96 session.onIncommingRpcFail();
97 SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
101 private void closeNetconfSession(NetconfServerSession session) {
102 // destroy NetconfOperationService
104 logger.info("Session {} closed successfully", session.getSessionId());
107 private NetconfMessage processDocument(final NetconfMessage netconfMessage,
108 NetconfServerSession session) throws NetconfDocumentedException {
110 final Document incommingDocument = netconfMessage.getDocument();
111 final Node rootNode = incommingDocument.getDocumentElement();
113 if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
114 final String messageId = rootNode.getAttributes().getNamedItem(MESSAGE_ID).getTextContent();
115 checkState(messageId != null);
116 final Document responseDocument = XmlUtil.newDocument();
117 Document rpcReply = operationRouter.onNetconfMessage(
118 incommingDocument, session);
120 session.onIncommingRpcSuccess();
122 responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
123 return new NetconfMessage(responseDocument);
125 // unknown command, send RFC 4741 p.70 unknown-element
127 * Tag: unknown-element Error-type: rpc, protocol, application
128 * Severity: error Error-info: <bad-element> : name of the
129 * unexpected element Description: An unexpected element is present.
131 // TODO add message to error info
132 throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
133 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
134 NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
135 rootNode.getNodeName()));
139 private static boolean isCloseSession(final NetconfMessage incommingDocument) {
140 final Document document = incommingDocument.getDocument();
141 XmlElement rpcElement = XmlElement.fromDomDocument(document);
142 if (rpcElement.getOnlyChildElementOptionally("close-session").isPresent())