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