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