Remove duplicate dependency declaration
[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.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
20 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
21 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
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 SessionMonitoringService monitoringService;
35     private final NetconfOperationRouter operationRouter;
36     private final AutoCloseable onSessionDownCloseable;
37
38     public NetconfServerSessionListener(NetconfOperationRouter operationRouter, SessionMonitoringService monitoringService,
39                                         AutoCloseable onSessionDownCloseable) {
40         this.operationRouter = operationRouter;
41         this.monitoringService = monitoringService;
42         this.onSessionDownCloseable = onSessionDownCloseable;
43     }
44
45     @Override
46     public void onSessionUp(NetconfServerSession netconfNetconfServerSession) {
47         monitoringService.onSessionUp(netconfNetconfServerSession);
48     }
49
50     @Override
51     public void onSessionDown(NetconfServerSession netconfNetconfServerSession, Exception cause) {
52         LOG.debug("Session {} down, reason: {}", netconfNetconfServerSession, cause.getMessage());
53         onDown(netconfNetconfServerSession);
54     }
55
56     public void onDown(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(NetconfServerSession netconfNetconfServerSession,
73             NetconfTerminationReason netconfTerminationReason) {
74         LOG.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
75                 netconfTerminationReason.getErrorMessage());
76         onDown(netconfNetconfServerSession);
77     }
78
79     @Override
80     public void onMessage(NetconfServerSession session, 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 {}", XmlUtil.toString(message.getDocument()));
89             session.sendMessage(message);
90
91             if (isCloseSession(netconfMessage)) {
92                 closeNetconfSession(session);
93             }
94
95         } catch (final RuntimeException e) {
96             // TODO: should send generic error or close session?
97             LOG.error("Unexpected exception", e);
98             session.onIncommingRpcFail();
99             throw new IllegalStateException("Unable to process incoming message " + netconfMessage, e);
100         } catch (NetconfDocumentedException e) {
101             LOG.trace("Error occurred while processing message",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         LOG.info("Session {} closed successfully", session.getSessionId());
112     }
113
114
115
116     private NetconfMessage processDocument(final NetconfMessage netconfMessage, NetconfServerSession session)
117             throws NetconfDocumentedException {
118
119         final Document incomingDocument = netconfMessage.getDocument();
120         final Node rootNode = incomingDocument.getDocumentElement();
121
122         if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
123             final Document responseDocument = XmlUtil.newDocument();
124             checkMessageId(rootNode);
125
126             Document rpcReply = operationRouter.onNetconfMessage(incomingDocument, session);
127
128             rpcReply = SubtreeFilter.applySubtreeFilter(incomingDocument, rpcReply);
129
130             session.onIncommingRpcSuccess();
131
132             responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
133             return new NetconfMessage(responseDocument);
134         } else {
135             // unknown command, send RFC 4741 p.70 unknown-element
136             /*
137              * Tag: unknown-element Error-type: rpc, protocol, application
138              * Severity: error Error-info: <bad-element> : name of the
139              * unexpected element Description: An unexpected element is present.
140              */
141             // TODO add message to error info
142             throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
143                     NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
144                     NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
145                             rootNode.getNodeName()));
146         }
147     }
148
149     private void checkMessageId(Node rootNode) throws NetconfDocumentedException {
150
151         NamedNodeMap attributes = rootNode.getAttributes();
152
153         if(attributes.getNamedItemNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, XmlNetconfConstants.MESSAGE_ID)!=null) {
154             return;
155         }
156
157         if(attributes.getNamedItem(XmlNetconfConstants.MESSAGE_ID)!=null) {
158             return;
159         }
160
161         throw new NetconfDocumentedException("Missing attribute" + rootNode.getNodeName(),
162                 NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.missing_attribute,
163                 NetconfDocumentedException.ErrorSeverity.error,
164                 ImmutableMap.of(NetconfDocumentedException.ErrorTag.missing_attribute.toString(),
165                         XmlNetconfConstants.MESSAGE_ID));
166     }
167
168     private static boolean isCloseSession(final NetconfMessage incomingDocument) {
169         final Document document = incomingDocument.getDocument();
170         XmlElement rpcElement = XmlElement.fromDomDocument(document);
171         if (rpcElement.getOnlyChildElementOptionally(DefaultCloseSession.CLOSE_SESSION).isPresent()) {
172             return true;
173         }
174
175         return false;
176     }
177 }

©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.