- Added exi capability utilities, handlers and necessary modifications
[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 static com.google.common.base.Preconditions.checkState;
12
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.NetconfMessage;
15 import org.opendaylight.controller.netconf.api.NetconfSession;
16 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
17 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouterImpl;
18 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.opendaylight.protocol.framework.SessionListener;
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 com.google.common.base.Preconditions;
29 import com.google.common.collect.ImmutableMap;
30
31 public class NetconfServerSessionListener implements
32         SessionListener<NetconfMessage, NetconfServerSession, NetconfTerminationReason> {
33
34     static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionListener.class);
35     public static final String MESSAGE_ID = "message-id";
36
37     private NetconfOperationRouterImpl operationRouter;
38
39     public NetconfServerSessionListener(NetconfOperationRouterImpl operationRouter) {
40         this.operationRouter = operationRouter;
41     }
42
43     @Override
44     public void onSessionUp(NetconfServerSession netconfNetconfServerSession) {
45
46     }
47
48     @Override
49     public void onSessionDown(NetconfServerSession netconfNetconfServerSession, Exception e) {
50         logger.debug("Session {} down, reason: {}", netconfNetconfServerSession, e.getMessage());
51
52         operationRouter.close();
53     }
54
55     @Override
56     public void onSessionTerminated(NetconfServerSession netconfNetconfServerSession,
57             NetconfTerminationReason netconfTerminationReason) {
58         logger.debug("Session {} terminated, reason: {}", netconfNetconfServerSession,
59                 netconfTerminationReason.getErrorMessage());
60
61         operationRouter.close();
62     }
63
64     @Override
65     public void onMessage(NetconfServerSession session, NetconfMessage netconfMessage) {
66         try {
67
68             Preconditions.checkState(operationRouter != null, "Cannot handle message, session up was not yet received");
69             // FIXME: there is no validation since the document may contain yang
70             // schemas
71             final NetconfMessage message = processDocument(netconfMessage,
72                     session);
73             logger.debug("Respondign with message {}", XmlUtil.toString(message.getDocument()));
74             session.sendMessage(message);
75
76             if (isCloseSession(netconfMessage)) {
77                 closeNetconfSession(session);
78             }
79
80         } catch (final RuntimeException e) {
81             logger.error("Unexpected exception", e);
82             // TODO: should send generic error or close session?
83             throw new RuntimeException("Unable to process incoming message " + netconfMessage, e);
84         } catch (NetconfDocumentedException e) {
85             SendErrorExceptionUtil.sendErrorMessage(session, e, netconfMessage);
86         }
87     }
88
89     private void closeNetconfSession(NetconfServerSession session) {
90         // destroy NetconfOperationService
91         session.close();
92         logger.info("Session {} closed successfully", session.getSessionId());
93     }
94
95     private NetconfMessage processDocument(final NetconfMessage netconfMessage,
96             NetconfSession session) throws NetconfDocumentedException {
97
98         final Document incommingDocument = netconfMessage.getDocument();
99         final Node rootNode = incommingDocument.getDocumentElement();
100
101         if (rootNode.getLocalName().equals(XmlNetconfConstants.RPC_KEY)) {
102             final String messageId = rootNode.getAttributes().getNamedItem(MESSAGE_ID).getTextContent();
103             checkState(messageId != null);
104             final Document responseDocument = XmlUtil.newDocument();
105             Document rpcReply = operationRouter.onNetconfMessage(
106                     incommingDocument, session);
107             responseDocument.appendChild(responseDocument.importNode(rpcReply.getDocumentElement(), true));
108             return new NetconfMessage(responseDocument);
109         } else {
110             // unknown command, send RFC 4741 p.70 unknown-element
111             /*
112              * Tag: unknown-element Error-type: rpc, protocol, application
113              * Severity: error Error-info: <bad-element> : name of the
114              * unexpected element Description: An unexpected element is present.
115              */
116             // TODO add message to error info
117             throw new NetconfDocumentedException("Unknown tag " + rootNode.getNodeName(),
118                     NetconfDocumentedException.ErrorType.protocol, NetconfDocumentedException.ErrorTag.unknown_element,
119                     NetconfDocumentedException.ErrorSeverity.error, ImmutableMap.of("bad-element",
120                             rootNode.getNodeName()));
121         }
122     }
123
124     private static boolean isCloseSession(final NetconfMessage incommingDocument) {
125         final Document document = incommingDocument.getDocument();
126         XmlElement rpcElement = XmlElement.fromDomDocument(document);
127         if (rpcElement.getOnlyChildElementOptionally("close-session").isPresent())
128             return true;
129
130         return false;
131     }
132 }