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