Merge "Bug 1029: Remove dead code: p2site"
[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 com.google.common.base.Optional;
12 import java.util.Map;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
16 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
17 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
18 import org.opendaylight.controller.netconf.util.xml.XmlElement;
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 import org.w3c.dom.NodeList;
24
25 public abstract class AbstractNetconfOperation implements NetconfOperation {
26     private final String netconfSessionIdForReporting;
27
28     protected AbstractNetconfOperation(final String netconfSessionIdForReporting) {
29         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
30     }
31
32     public final String getNetconfSessionIdForReporting() {
33         return netconfSessionIdForReporting;
34     }
35
36     @Override
37     public HandlingPriority canHandle(final Document message) throws NetconfDocumentedException {
38         OperationNameAndNamespace operationNameAndNamespace = null;
39         operationNameAndNamespace = new OperationNameAndNamespace(message);
40         return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
41     }
42
43     public static final class OperationNameAndNamespace {
44         private final String operationName, namespace;
45         private final XmlElement operationElement;
46
47         public OperationNameAndNamespace(final Document message) throws NetconfDocumentedException {
48             XmlElement requestElement = null;
49             requestElement = getRequestElementWithCheck(message);
50             operationElement = requestElement.getOnlyChildElement();
51             operationName = operationElement.getName();
52             namespace = operationElement.getNamespace();
53         }
54
55         public String getOperationName() {
56             return operationName;
57         }
58
59         public String getNamespace() {
60             return namespace;
61         }
62
63         public XmlElement getOperationElement() {
64             return operationElement;
65         }
66     }
67
68     protected static XmlElement getRequestElementWithCheck(final Document message) throws NetconfDocumentedException {
69         return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
70                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
71     }
72
73     protected HandlingPriority canHandle(final String operationName, final String operationNamespace) {
74         return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
75                 ? getHandlingPriority()
76                 : HandlingPriority.CANNOT_HANDLE;
77     }
78
79     protected HandlingPriority getHandlingPriority() {
80         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
81     }
82
83     protected String getOperationNamespace() {
84         return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
85     }
86
87     protected abstract String getOperationName();
88
89     @Override
90     public Document handle(final Document requestMessage,
91             final NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
92
93         XmlElement requestElement = getRequestElementWithCheck(requestMessage);
94
95         Document document = XmlUtil.newDocument();
96
97         XmlElement operationElement = requestElement.getOnlyChildElement();
98         Map<String, Attr> attributes = requestElement.getAttributes();
99
100         Element response = handle(document, operationElement, subsequentOperation);
101         Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.RPC_REPLY_KEY, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
102
103         if(XmlElement.fromDomElement(response).hasNamespace()) {
104             rpcReply.appendChild(response);
105         } else {
106             Element responseNS = XmlUtil.createElement(document, response.getNodeName(), Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
107             NodeList list = response.getChildNodes();
108             while(list.getLength()!=0) {
109                 responseNS.appendChild(list.item(0));
110             }
111             rpcReply.appendChild(responseNS);
112         }
113
114         for (Attr attribute : attributes.values()) {
115             rpcReply.setAttributeNode((Attr) document.importNode(attribute, true));
116         }
117         document.appendChild(rpcReply);
118         return document;
119     }
120
121     protected abstract Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
122             throws NetconfDocumentedException;
123
124     @Override
125     public String toString() {
126         final StringBuffer sb = new StringBuffer(getClass().getName());
127         try {
128             sb.append("{name=").append(getOperationName());
129         } catch(UnsupportedOperationException e) {
130             // no problem
131         }
132         sb.append(", namespace=").append(getOperationNamespace());
133         sb.append(", session=").append(netconfSessionIdForReporting);
134         sb.append('}');
135         return sb.toString();
136     }
137 }