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