Merge "BUG-731: do not catch Throwable"
[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
12 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
13 import org.opendaylight.controller.netconf.api.NetconfMessage;
14 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
15 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
16 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
17 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
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.NamedNodeMap;
27 import org.w3c.dom.Node;
28
29 import com.google.common.base.Preconditions;
30 import com.google.common.collect.ImmutableMap;
31
32 public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
33
34     static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionListener.class);
35     private final SessionMonitoringService monitoringService;
36     private final NetconfOperationRouter operationRouter;
37     private final AutoCloseable onSessionDownCloseable;
38
39     public NetconfServerSessionListener(NetconfOperationRouter operationRouter, SessionMonitoringService monitoringService,
40                                         AutoCloseable onSessionDownCloseable) {
41         this.operationRouter = operationRouter;
42         this.monitoringService = monitoringService;
43         this.onSessionDownCloseable = onSessionDownCloseable;
44     }
45
46     @Override
47     public void onSessionUp(NetconfServerSession netconfNetconfServerSession) {
48         monitoringService.onSessionUp(netconfNetconfServerSession);
49     }
50
51     @Override
52     public void onSessionDown(NetconfServerSession netconfNetconfServerSession, Exception cause) {
53         logger.debug("Session {} down, reason: {}", netconfNetconfServerSession, cause.getMessage());
54         onDown(netconfNetconfServerSession);
55     }
56
57     public void onDown(NetconfServerSession netconfNetconfServerSession) {
58         monitoringService.onSessionDown(netconfNetconfServerSession);
59
60         try {
61             operationRouter.close();
62         } catch (Exception closingEx) {
63             logger.debug("Ignoring exception while closing operationRouter", closingEx);
64         }
65         try {
66             onSessionDownCloseable.close();
67         } catch(Exception ex){
68             logger.debug("Ignoring exception while closing onSessionDownCloseable", ex);
69         }
70     }
71
72     @Override
73     public void onSessionTerminated(NetconfServerSession netconfNetconfServerSession,
74             NetconfTerminationReason netconfTerminationReason) {
75         logger.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
76                 netconfTerminationReason.getErrorMessage());
77         onDown(netconfNetconfServerSession);
78     }
79
80     @Override
81     public void onMessage(NetconfServerSession session, NetconfMessage netconfMessage) {
82         try {
83
84             Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
85             // FIXME: there is no validation since the document may contain yang
86             // schemas
87             final NetconfMessage message = processDocument(netconfMessage,
88                     session);
89             logger.debug("Responding with message {}", XmlUtil.toString(message.getDocument()));
90             session.sendMessage(message);
91
92             if (isCloseSession(netconfMessage)) {
93                 closeNetconfSession(session);
94             }
95
96         } catch (final RuntimeException e) {
97             // TODO: should send generic error or close session?
98             logger.error("Unexpected exception", e);
99             session.onIncommingRpcFail();
100             throw new RuntimeException("Unable to process incoming message " + netconfMessage, e);
101         } catch (NetconfDocumentedException e) {
102             session.onOutgoingRpcError();
103             session.onIncommingRpcFail();
104             SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
105         }
106     }
107
108     private void closeNetconfSession(NetconfServerSession session) {
109         // destroy NetconfOperationService
110         session.close();
111         logger.info("Session {} closed successfully", session.getSessionId());
112     }
113
114     private NetconfMessage processDocument(final NetconfMessage netconfMessage, NetconfServerSession session)
115             throws NetconfDocumentedException {
116
117         final Document incomingDocument = netconfMessage.getDocument();
118         final Node rootNode = incomingDocument.getDocumentElement();
119
120         if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
121             final Document responseDocument = XmlUtil.newDocument();
122             checkMessageId(rootNode);
123
124             Document rpcReply = operationRouter.onNetconfMessage(incomingDocument, session);
125
126             session.onIncommingRpcSuccess();
127
128             responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
129             return new NetconfMessage(responseDocument);
130         } else {
131             // unknown command, send RFC 4741 p.70 unknown-element
132             /*
133              * Tag: unknown-element Error-type: rpc, protocol, application
134              * Severity: error Error-info: <bad-element> : name of the
135              * unexpected element Description: An unexpected element is present.
136              */
137             // TODO add message to error info
138             throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
139                     NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
140                     NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
141                             rootNode.getNodeName()));
142         }
143     }
144
145     private void checkMessageId(Node rootNode) throws NetconfDocumentedException {
146             NamedNodeMap attributes = rootNode.getAttributes();
147         if(attributes.getNamedItemNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlNetconfConstants.MESSAGE_ID)!=null) {
148             return;
149         }
150
151         if(attributes.getNamedItem(XmlNetconfConstants.MESSAGE_ID)!=null) {
152             return;
153         }
154
155         throw new NetconfDocumentedException("Missing attribute" + rootNode.getNodeName(),
156                 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.missing_attribute,
157                 NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of(NetconfDocumentedException.ErrorTag.missing_attribute.toString(),
158                 XmlNetconfConstants.MESSAGE_ID));
159     }
160
161     private static boolean isCloseSession(final NetconfMessage incomingDocument) {
162         final Document document = incomingDocument.getDocument();
163         XmlElement rpcElement = XmlElement.fromDomDocument(document);
164         if (rpcElement.getOnlyChildElementOptionally(DefaultCloseSession.CLOSE_SESSION).isPresent()) {
165             return true;
166         }
167
168         return false;
169     }
170 }