Merge "Add message to update the schema context of the InMemoryDOMDataStore"
[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
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
18 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
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.impl.osgi.SessionMonitoringService;
23 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
24 import org.opendaylight.controller.netconf.util.xml.XmlElement;
25 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.NamedNodeMap;
30 import org.w3c.dom.Node;
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 IllegalStateException("Unable to process incoming message " + netconfMessage, e);
101         } catch (NetconfDocumentedException e) {
102             logger.trace("Error occurred while processing message",e);
103             session.onOutgoingRpcError();
104             session.onIncommingRpcFail();
105             SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
106         }
107     }
108
109     private void closeNetconfSession(NetconfServerSession session) {
110         // destroy NetconfOperationService
111         session.close();
112         logger.info("Session {} closed successfully", session.getSessionId());
113     }
114
115
116
117     private NetconfMessage processDocument(final NetconfMessage netconfMessage, NetconfServerSession session)
118             throws NetconfDocumentedException {
119
120         final Document incomingDocument = netconfMessage.getDocument();
121         final Node rootNode = incomingDocument.getDocumentElement();
122
123         if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
124             final Document responseDocument = XmlUtil.newDocument();
125             checkMessageId(rootNode);
126
127             Document rpcReply = operationRouter.onNetconfMessage(incomingDocument, session);
128
129             rpcReply = SubtreeFilter.applySubtreeFilter(incomingDocument, rpcReply);
130
131             session.onIncommingRpcSuccess();
132
133             responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
134             return new NetconfMessage(responseDocument);
135         } else {
136             // unknown command, send RFC 4741 p.70 unknown-element
137             /*
138              * Tag: unknown-element Error-type: rpc, protocol, application
139              * Severity: error Error-info: <bad-element> : name of the
140              * unexpected element Description: An unexpected element is present.
141              */
142             // TODO add message to error info
143             throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
144                     NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
145                     NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
146                             rootNode.getNodeName()));
147         }
148     }
149
150     private void checkMessageId(Node rootNode) throws NetconfDocumentedException {
151             NamedNodeMap attributes = rootNode.getAttributes();
152         if(attributes.getNamedItemNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlNetconfConstants.MESSAGE_ID)!=null) {
153             return;
154         }
155
156         if(attributes.getNamedItem(XmlNetconfConstants.MESSAGE_ID)!=null) {
157             return;
158         }
159
160         throw new NetconfDocumentedException("Missing attribute" + rootNode.getNodeName(),
161                 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.missing_attribute,
162                 NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of(NetconfDocumentedException.ErrorTag.missing_attribute.toString(),
163                 XmlNetconfConstants.MESSAGE_ID));
164     }
165
166     private static boolean isCloseSession(final NetconfMessage incomingDocument) {
167         final Document document = incomingDocument.getDocument();
168         XmlElement rpcElement = XmlElement.fromDomDocument(document);
169         if (rpcElement.getOnlyChildElementOptionally(DefaultCloseSession.CLOSE_SESSION).isPresent()) {
170             return true;
171         }
172
173         return false;
174     }
175 }