Update Tomas's name
[netconf.git] / protocol / netconf-util / src / main / java / org / opendaylight / 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 package org.opendaylight.netconf.util.mapping;
9
10 import java.util.Map;
11 import java.util.Optional;
12 import org.opendaylight.netconf.api.DocumentedException;
13 import org.opendaylight.netconf.api.xml.XmlElement;
14 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.netconf.api.xml.XmlUtil;
16 import org.opendaylight.netconf.mapping.api.HandlingPriority;
17 import org.opendaylight.netconf.mapping.api.NetconfOperation;
18 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
19 import org.w3c.dom.Attr;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.NodeList;
23
24 public abstract class AbstractNetconfOperation implements NetconfOperation {
25     private final String netconfSessionIdForReporting;
26
27     protected AbstractNetconfOperation(final String netconfSessionIdForReporting) {
28         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
29     }
30
31     public final String getNetconfSessionIdForReporting() {
32         return netconfSessionIdForReporting;
33     }
34
35     @Override
36     public HandlingPriority canHandle(final Document message) throws DocumentedException {
37         OperationNameAndNamespace operationNameAndNamespace = null;
38         operationNameAndNamespace = new OperationNameAndNamespace(message);
39         return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
40     }
41
42     protected HandlingPriority canHandle(final String operationName, final String operationNamespace) {
43         return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
44                 ? getHandlingPriority()
45                 : HandlingPriority.CANNOT_HANDLE;
46     }
47
48     public static final class OperationNameAndNamespace {
49         private final String operationName;
50         private final String namespace;
51         private final XmlElement operationElement;
52
53         public OperationNameAndNamespace(final Document message) throws DocumentedException {
54             final var requestElement = getRequestElementWithCheck(message);
55             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         public XmlElement getOperationElement() {
69             return operationElement;
70         }
71     }
72
73     protected static XmlElement getRequestElementWithCheck(final Document message) throws DocumentedException {
74         return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
75                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
76     }
77
78     protected HandlingPriority getHandlingPriority() {
79         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
80     }
81
82     protected String getOperationNamespace() {
83         return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
84     }
85
86     protected abstract String getOperationName();
87
88     @Override
89     public Document handle(final Document requestMessage,
90             final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
91
92         XmlElement requestElement = getRequestElementWithCheck(requestMessage);
93
94         Document document = XmlUtil.newDocument();
95
96         XmlElement operationElement = requestElement.getOnlyChildElement();
97         Map<String, Attr> attributes = requestElement.getAttributes();
98
99         Element response = handle(document, operationElement, subsequentOperation);
100         Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.RPC_REPLY_KEY,
101                 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(),
107                     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 (Attr attribute : attributes.values()) {
116             rpcReply.setAttributeNode((Attr) document.importNode(attribute, true));
117         }
118         document.appendChild(rpcReply);
119         return document;
120     }
121
122     protected abstract Element handle(Document document, XmlElement message,
123                                       NetconfOperationChainedExecution subsequentOperation)
124             throws DocumentedException;
125
126     @Override
127     public String toString() {
128         final StringBuilder sb = new StringBuilder(getClass().getName());
129         try {
130             sb.append("{name=").append(getOperationName());
131         } catch (UnsupportedOperationException e) {
132             // no problem
133         }
134         sb.append(", namespace=").append(getOperationNamespace());
135         sb.append(", session=").append(netconfSessionIdForReporting);
136         sb.append('}');
137         return sb.toString();
138     }
139 }