5850e64a0591a4d58c320eb67330e1dc0aa55cae
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / mapping / AbstractNetconfOperation.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.util.mapping;
10
11 import java.util.Map;
12
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
15 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
16 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
17 import org.opendaylight.controller.netconf.util.xml.XmlElement;
18 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
20 import org.w3c.dom.Attr;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23
24 public abstract class AbstractNetconfOperation implements NetconfOperation {
25     private final String netconfSessionIdForReporting;
26
27     protected AbstractNetconfOperation(String netconfSessionIdForReporting) {
28         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
29     }
30
31     public String getNetconfSessionIdForReporting() {
32         return netconfSessionIdForReporting;
33     }
34
35     @Override
36     public HandlingPriority canHandle(Document message) {
37         OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(message);
38         return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
39     }
40
41     public static class OperationNameAndNamespace {
42         private final String operationName, namespace;
43
44         public OperationNameAndNamespace(Document message) {
45             XmlElement requestElement = getRequestElementWithCheck(message);
46
47             XmlElement operationElement = requestElement.getOnlyChildElement();
48             operationName = operationElement.getName();
49             namespace = operationElement.getNamespace();
50         }
51
52         public String getOperationName() {
53             return operationName;
54         }
55
56         public String getNamespace() {
57             return namespace;
58         }
59     }
60
61     protected static XmlElement getRequestElementWithCheck(Document message) {
62         return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
63                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
64     }
65
66     protected abstract HandlingPriority canHandle(String operationName, String netconfOperationNamespace);
67
68     @Override
69     public Document handle(Document message, NetconfOperationRouter opRouter) throws NetconfDocumentedException {
70
71         XmlElement requestElement = getRequestElementWithCheck(message);
72
73         Document document = XmlUtil.newDocument();
74
75         XmlElement operationElement = requestElement.getOnlyChildElement();
76         Map<String, Attr> attributes = requestElement.getAttributes();
77
78         Element response = handle(document, operationElement, opRouter);
79
80         Element rpcReply = document.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
81                 XmlNetconfConstants.RPC_REPLY_KEY);
82         rpcReply.appendChild(response);
83
84         for (String attrName : attributes.keySet()) {
85             rpcReply.setAttribute(attrName, attributes.get(attrName).getNodeValue());
86         }
87
88         document.appendChild(rpcReply);
89         return document;
90     }
91
92     protected abstract Element handle(Document document, XmlElement operationElement, NetconfOperationRouter opRouter)
93             throws NetconfDocumentedException;
94
95     @Override
96     public String toString() {
97         return getClass() + "{" + netconfSessionIdForReporting + '}';
98     }
99 }