Bug 591: Minimize the use of Xtend in sal-binding-broker-impl
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSessionListener.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.impl;
10
11 import static com.google.common.base.Preconditions.checkState;
12
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;
27
28 import com.google.common.base.Preconditions;
29 import com.google.common.collect.ImmutableMap;
30
31 public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
32     public static final String MESSAGE_ID = "message-id";
33
34     static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionListener.class);
35     private final SessionMonitoringService monitoringService;
36     private final NetconfOperationRouter operationRouter;
37
38     public NetconfServerSessionListener(NetconfOperationRouter operationRouter, SessionMonitoringService monitoringService) {
39         this.operationRouter = operationRouter;
40         this.monitoringService = monitoringService;
41     }
42
43     @Override
44     public void onSessionUp(NetconfServerSession netconfNetconfServerSession) {
45         monitoringService.onSessionUp(netconfNetconfServerSession);
46     }
47
48     @Override
49     public void onSessionDown(NetconfServerSession netconfNetconfServerSession, Exception e) {
50         logger.debug("Session {} down, reason: {}", netconfNetconfServerSession, e.getMessage());
51         monitoringService.onSessionDown(netconfNetconfServerSession);
52
53         operationRouter.close();
54     }
55
56     @Override
57     public void onSessionTerminated(NetconfServerSession netconfNetconfServerSession,
58             NetconfTerminationReason netconfTerminationReason) {
59         logger.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
60                 netconfTerminationReason.getErrorMessage());
61         monitoringService.onSessionDown(netconfNetconfServerSession);
62
63         operationRouter.close();
64     }
65
66     @Override
67     public void onMessage(NetconfServerSession session, NetconfMessage netconfMessage) {
68         try {
69
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
72             // schemas
73             final NetconfMessage message = processDocument(netconfMessage,
74                     session);
75             logger.debug("Responding with message {}", XmlUtil.toString(message.getDocument()));
76             session.sendMessage(message);
77
78             if (isCloseSession(netconfMessage)) {
79                 closeNetconfSession(session);
80             }
81
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);
91         }
92     }
93
94     private void closeNetconfSession(NetconfServerSession session) {
95         // destroy NetconfOperationService
96         session.close();
97         logger.info("Session {} closed successfully", session.getSessionId());
98     }
99
100     private NetconfMessage processDocument(final NetconfMessage netconfMessage,
101             NetconfServerSession session) throws NetconfDocumentedException {
102
103         final Document incommingDocument = netconfMessage.getDocument();
104         final Node rootNode = incommingDocument.getDocumentElement();
105
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);
112
113             session.onIncommingRpcSuccess();
114
115             responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
116             return new NetconfMessage(responseDocument);
117         } else {
118             // unknown command, send RFC 4741 p.70 unknown-element
119             /*
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.
123              */
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()));
129         }
130     }
131
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())
136             return true;
137
138         return false;
139     }
140 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.