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 static com.google.common.base.Preconditions.checkState;
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 com.google.common.base.Preconditions;
29 import com.google.common.collect.ImmutableMap;
31 public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
32 public static final String MESSAGE_ID = "message-id";
34 static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionListener.class);
35 private final SessionMonitoringService monitoringService;
36 private final NetconfOperationRouter operationRouter;
38 public NetconfServerSessionListener(NetconfOperationRouter operationRouter, SessionMonitoringService monitoringService) {
39 this.operationRouter = operationRouter;
40 this.monitoringService = monitoringService;
44 public void onSessionUp(NetconfServerSession netconfNetconfServerSession) {
45 monitoringService.onSessionUp(netconfNetconfServerSession);
49 public void onSessionDown(NetconfServerSession netconfNetconfServerSession, Exception e) {
50 logger.debug("Session {} down, reason: {}", netconfNetconfServerSession, e.getMessage());
51 monitoringService.onSessionDown(netconfNetconfServerSession);
53 operationRouter.close();
57 public void onSessionTerminated(NetconfServerSession netconfNetconfServerSession,
58 NetconfTerminationReason netconfTerminationReason) {
59 logger.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
60 netconfTerminationReason.getErrorMessage());
61 monitoringService.onSessionDown(netconfNetconfServerSession);
63 operationRouter.close();
67 public void onMessage(NetconfServerSession session, NetconfMessage netconfMessage) {
70 Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
71 // FIXME: there is no validation since the document may contain yang
73 final NetconfMessage message = processDocument(netconfMessage,
75 logger.debug("Responding with message {}", XmlUtil.toString(message.getDocument()));
76 session.sendMessage(message);
78 if (isCloseSession(netconfMessage)) {
79 closeNetconfSession(session);
82 } catch (final RuntimeException e) {
83 // TODO: should send generic error or close session?
84 logger.error("Unexpected exception", e);
85 session.onIncommingRpcFail();
86 throw new RuntimeException("Unable to process incoming message " + netconfMessage, e);
87 } catch (NetconfDocumentedException e) {
88 session.onOutgoingRpcError();
89 session.onIncommingRpcFail();
90 SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
94 private void closeNetconfSession(NetconfServerSession session) {
95 // destroy NetconfOperationService
97 logger.info("Session {} closed successfully", session.getSessionId());
100 private NetconfMessage processDocument(final NetconfMessage netconfMessage,
101 NetconfServerSession session) throws NetconfDocumentedException {
103 final Document incommingDocument = netconfMessage.getDocument();
104 final Node rootNode = incommingDocument.getDocumentElement();
106 if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
107 final String messageId = rootNode.getAttributes().getNamedItem(MESSAGE_ID).getTextContent();
108 checkState(messageId != null);
109 final Document responseDocument = XmlUtil.newDocument();
110 Document rpcReply = operationRouter.onNetconfMessage(
111 incommingDocument, session);
113 session.onIncommingRpcSuccess();
115 responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
116 return new NetconfMessage(responseDocument);
118 // unknown command, send RFC 4741 p.70 unknown-element
120 * Tag: unknown-element Error-type: rpc, protocol, application
121 * Severity: error Error-info: <bad-element> : name of the
122 * unexpected element Description: An unexpected element is present.
124 // TODO add message to error info
125 throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
126 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
127 NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
128 rootNode.getNodeName()));
132 private static boolean isCloseSession(final NetconfMessage incommingDocument) {
133 final Document document = incommingDocument.getDocument();
134 XmlElement rpcElement = XmlElement.fromDomDocument(document);
135 if (rpcElement.getOnlyChildElementOptionally("close-session").isPresent())