Merge "Resolve Bug:445 Remove freemarker from config code generator."
[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 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableMap;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.NetconfMessage;
15 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
16 import org.opendaylight.controller.netconf.api.NetconfSessionListener;
17 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
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.Node;
27
28 import static com.google.common.base.Preconditions.checkState;
29
30 public class NetconfServerSessionListener implements NetconfSessionListener<NetconfServerSession> {
31     public static final String MESSAGE_ID = "message-id";
32
33     static final Logger logger = 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         logger.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             logger.debug("Ignoring exception while closing operationRouter", closingEx);
63         }
64         try {
65             onSessionDownCloseable.close();
66         } catch(Exception ex){
67             logger.debug("Ignoring exception while closing onSessionDownCloseable", ex);
68         }
69     }
70
71     @Override
72     public void onSessionTerminated(NetconfServerSession netconfNetconfServerSession,
73             NetconfTerminationReason netconfTerminationReason) {
74         logger.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             logger.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             logger.error("Unexpected exception", e);
98             session.onIncommingRpcFail();
99             throw new RuntimeException("Unable to process incoming message " + netconfMessage, e);
100         } catch (NetconfDocumentedException e) {
101             session.onOutgoingRpcError();
102             session.onIncommingRpcFail();
103             SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
104         }
105     }
106
107     private void closeNetconfSession(NetconfServerSession session) {
108         // destroy NetconfOperationService
109         session.close();
110         logger.info("Session {} closed successfully", session.getSessionId());
111     }
112
113     private NetconfMessage processDocument(final NetconfMessage netconfMessage,
114             NetconfServerSession session) throws NetconfDocumentedException {
115
116         final Document incommingDocument = netconfMessage.getDocument();
117         final Node rootNode = incommingDocument.getDocumentElement();
118
119         if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
120             final String messageId = rootNode.getAttributes().getNamedItem(MESSAGE_ID).getTextContent();
121             checkState(messageId != null);
122             final Document responseDocument = XmlUtil.newDocument();
123             Document rpcReply = operationRouter.onNetconfMessage(
124                     incommingDocument, 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 static boolean isCloseSession(final NetconfMessage incommingDocument) {
146         final Document document = incommingDocument.getDocument();
147         XmlElement rpcElement = XmlElement.fromDomDocument(document);
148         if (rpcElement.getOnlyChildElementOptionally("close-session").isPresent())
149             return true;
150
151         return false;
152     }
153 }