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.NetconfSession;
16 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
17 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouterImpl;
18 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.opendaylight.protocol.framework.SessionListener;
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
32 SessionListener<NetconfMessage, NetconfServerSession, NetconfTerminationReason> {
34 static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionListener.class);
35 public static final String MESSAGE_ID = "message-id";
37 private NetconfOperationRouterImpl operationRouter;
39 public NetconfServerSessionListener(NetconfOperationRouterImpl operationRouter) {
40 this.operationRouter = operationRouter;
44 public void onSessionUp(NetconfServerSession netconfNetconfServerSession) {
49 public void onSessionDown(NetconfServerSession netconfNetconfServerSession, Exception e) {
50 logger.debug("Session {} down, reason: {}", netconfNetconfServerSession, e.getMessage());
52 operationRouter.close();
56 public void onSessionTerminated(NetconfServerSession netconfNetconfServerSession,
57 NetconfTerminationReason netconfTerminationReason) {
58 logger.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
59 netconfTerminationReason.getErrorMessage());
61 operationRouter.close();
65 public void onMessage(NetconfServerSession session, NetconfMessage netconfMessage) {
68 Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
69 // FIXME: there is no validation since the document may contain yang
71 final NetconfMessage message = processDocument(netconfMessage,
73 logger.debug("Respondign with message {}", XmlUtil.toString(message.getDocument()));
74 session.sendMessage(message);
76 if (isCloseSession(netconfMessage)) {
77 closeNetconfSession(session);
80 } catch (final RuntimeException e) {
81 logger.error("Unexpected exception", e);
82 // TODO: should send generic error or close session?
83 throw new RuntimeException("Unable to process incoming message " + netconfMessage, e);
84 } catch (NetconfDocumentedException e) {
85 SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
89 private void closeNetconfSession(NetconfServerSession session) {
90 // destroy NetconfOperationService
92 logger.info("Session {} closed successfully", session.getSessionId());
95 private NetconfMessage processDocument(final NetconfMessage netconfMessage,
96 NetconfSession session) throws NetconfDocumentedException {
98 final Document incommingDocument = netconfMessage.getDocument();
99 final Node rootNode = incommingDocument.getDocumentElement();
101 if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
102 final String messageId = rootNode.getAttributes().getNamedItem(MESSAGE_ID).getTextContent();
103 checkState(messageId != null);
104 final Document responseDocument = XmlUtil.newDocument();
105 Document rpcReply = operationRouter.onNetconfMessage(
106 incommingDocument, session);
107 responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
108 return new NetconfMessage(responseDocument);
110 // unknown command, send RFC 4741 p.70 unknown-element
112 * Tag: unknown-element Error-type: rpc, protocol, application
113 * Severity: error Error-info: <bad-element> : name of the
114 * unexpected element Description: An unexpected element is present.
116 // TODO add message to error info
117 throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
118 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
119 NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
120 rootNode.getNodeName()));
124 private static boolean isCloseSession(final NetconfMessage incommingDocument) {
125 final Document document = incommingDocument.getDocument();
126 XmlElement rpcElement = XmlElement.fromDomDocument(document);
127 if (rpcElement.getOnlyChildElementOptionally("close-session").isPresent())