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.NetconfTerminationReason;
16 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouterImpl;
17 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
18 import org.opendaylight.controller.netconf.util.xml.XmlElement;
19 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
20 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
21 import org.opendaylight.protocol.framework.SessionListener;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Node;
27 import static com.google.common.base.Preconditions.checkState;
29 public class NetconfServerSessionListener implements
30 SessionListener<NetconfMessage, NetconfServerSession, NetconfTerminationReason> {
32 static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionListener.class);
33 public static final String MESSAGE_ID = "message-id";
35 private NetconfOperationRouterImpl operationRouter;
37 public NetconfServerSessionListener(NetconfOperationRouterImpl operationRouter) {
38 this.operationRouter = operationRouter;
42 public void onSessionUp(NetconfServerSession netconfNetconfServerSession) {
47 public void onSessionDown(NetconfServerSession netconfNetconfServerSession, Exception e) {
48 logger.debug("Session {} down, reason: {}", netconfNetconfServerSession, e.getMessage());
50 operationRouter.close();
54 public void onSessionTerminated(NetconfServerSession netconfNetconfServerSession,
55 NetconfTerminationReason netconfTerminationReason) {
56 logger.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
57 netconfTerminationReason.getErrorMessage());
59 operationRouter.close();
63 public void onMessage(NetconfServerSession session, NetconfMessage netconfMessage) {
66 Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
67 // FIXME: there is no validation since the document may contain yang
69 final NetconfMessage message = processDocument(netconfMessage);
70 logger.debug("Respondign with message {}", XmlUtil.toString(message.getDocument()));
71 session.sendMessage(message);
73 if (isCloseSession(netconfMessage)) {
74 closeNetconfSession(session);
77 } catch (final RuntimeException e) {
78 logger.error("Unexpected exception", e);
79 // TODO: should send generic error or close session?
80 throw new RuntimeException("Unable to process incoming message " + netconfMessage, e);
81 } catch (NetconfDocumentedException e) {
82 SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
86 private void closeNetconfSession(NetconfServerSession session) {
87 // destroy NetconfOperationService
89 logger.info("Session {} closed successfully", session.getSessionId());
92 private NetconfMessage processDocument(final NetconfMessage netconfMessage) throws NetconfDocumentedException {
94 final Document incommingDocument = netconfMessage.getDocument();
95 final Node rootNode = incommingDocument.getDocumentElement();
97 if (rootNode.getNodeName().equals(XmlNetconfConstants.RPC_KEY)) {
98 final String messageId = rootNode.getAttributes().getNamedItem(MESSAGE_ID).getTextContent();
99 checkState(messageId != null);
100 final Document responseDocument = XmlUtil.newDocument();
101 Document rpcReply = operationRouter.onNetconfMessage(incommingDocument);
102 responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
103 return new NetconfMessage(responseDocument);
105 // unknown command, send RFC 4741 p.70 unknown-element
107 * Tag: unknown-element Error-type: rpc, protocol, application
108 * Severity: error Error-info: <bad-element> : name of the
109 * unexpected element Description: An unexpected element is present.
111 // TODO add message to error info
112 throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
113 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
114 NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
115 rootNode.getNodeName()));
119 private static boolean isCloseSession(final NetconfMessage incommingDocument) {
120 final Document document = incommingDocument.getDocument();
121 XmlElement rpcElement = XmlElement.fromDomDocument(document);
122 if (rpcElement.getOnlyChildElementOptionally("close-session").isPresent())