Merge "Bug 2358: Fixed warnings in Restconf"
[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 com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableMap;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
17 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
18 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
19 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
20 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
21 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
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 public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
30
31     private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionListener.class);
32     private final NetconfMonitoringService monitoringService;
33     private final NetconfOperationRouter operationRouter;
34     private final AutoCloseable onSessionDownCloseable;
35
36     public NetconfServerSessionListener(final NetconfOperationRouter operationRouter, NetconfMonitoringService monitoringService,
37                                         final AutoCloseable onSessionDownCloseable) {
38         this.operationRouter = operationRouter;
39         this.monitoringService = monitoringService;
40         this.onSessionDownCloseable = onSessionDownCloseable;
41     }
42
43     @Override
44     public void onSessionUp(final NetconfServerSession netconfNetconfServerSession) {
45         monitoringService.onSessionUp(netconfNetconfServerSession);
46         // FIXME monitoring service should be also notified about all the other changes to netconf session (from ietf-netconf-monitoring point of view)
47         // This means also notifying after every message is processed
48     }
49
50     @Override
51     public void onSessionDown(final NetconfServerSession netconfNetconfServerSession, final Exception cause) {
52         LOG.debug("Session {} down, reason: {}", netconfNetconfServerSession, cause.getMessage());
53         onDown(netconfNetconfServerSession);
54     }
55
56     public void onDown(final NetconfServerSession netconfNetconfServerSession) {
57         monitoringService.onSessionDown(netconfNetconfServerSession);
58
59         try {
60             operationRouter.close();
61         } catch (Exception closingEx) {
62             LOG.debug("Ignoring exception while closing operationRouter", closingEx);
63         }
64         try {
65             onSessionDownCloseable.close();
66         } catch(Exception ex){
67             LOG.debug("Ignoring exception while closing onSessionDownCloseable", ex);
68         }
69     }
70
71     @Override
72     public void onSessionTerminated(final NetconfServerSession netconfNetconfServerSession,
73             final NetconfTerminationReason netconfTerminationReason) {
74         LOG.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
75                 netconfTerminationReason.getErrorMessage());
76         onDown(netconfNetconfServerSession);
77     }
78
79     @Override
80     public void onMessage(final NetconfServerSession session, final NetconfMessage netconfMessage) {
81         try {
82
83             Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
84             // FIXME: there is no validation since the document may contain yang
85             // schemas
86             final NetconfMessage message = processDocument(netconfMessage,
87                     session);
88             LOG.debug("Responding with message {}", message);
89             session.sendMessage(message);
90         } catch (final RuntimeException e) {
91             // TODO: should send generic error or close session?
92             LOG.error("Unexpected exception", e);
93             session.onIncommingRpcFail();
94             throw new IllegalStateException("Unable to process incoming message " + netconfMessage, e);
95         } catch (NetconfDocumentedException e) {
96             LOG.trace("Error occurred while processing message",e);
97             session.onOutgoingRpcError();
98             session.onIncommingRpcFail();
99             SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
100         }
101     }
102
103     private NetconfMessage processDocument(final NetconfMessage netconfMessage, final NetconfServerSession session)
104             throws NetconfDocumentedException {
105
106         final Document incomingDocument = netconfMessage.getDocument();
107         final Node rootNode = incomingDocument.getDocumentElement();
108
109         if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
110             final Document responseDocument = XmlUtil.newDocument();
111             checkMessageId(rootNode);
112
113             Document rpcReply = operationRouter.onNetconfMessage(incomingDocument, session);
114
115             rpcReply = SubtreeFilter.applySubtreeFilter(incomingDocument, rpcReply);
116
117             session.onIncommingRpcSuccess();
118
119             responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
120             return new NetconfMessage(responseDocument);
121         } else {
122             // unknown command, send RFC 4741 p.70 unknown-element
123             /*
124              * Tag: unknown-element Error-type: rpc, protocol, application
125              * Severity: error Error-info: <bad-element> : name of the
126              * unexpected element Description: An unexpected element is present.
127              */
128             // TODO add message to error info
129             throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
130                     NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
131                     NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
132                             rootNode.getNodeName()));
133         }
134     }
135
136     private void checkMessageId(final Node rootNode) throws NetconfDocumentedException {
137
138         NamedNodeMap attributes = rootNode.getAttributes();
139
140         if(attributes.getNamedItemNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlNetconfConstants.MESSAGE_ID)!=null) {
141             return;
142         }
143
144         if(attributes.getNamedItem(XmlNetconfConstants.MESSAGE_ID)!=null) {
145             return;
146         }
147
148         throw new NetconfDocumentedException("Missing attribute" + rootNode.getNodeName(),
149                 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.missing_attribute,
150                 NetconfDocumentedException.ErrorSeverity.error,
151                 ImmutableMap.of(NetconfDocumentedException.ErrorTag.missing_attribute.toString(),
152                         XmlNetconfConstants.MESSAGE_ID));
153     }
154 }