2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.util.mapping;
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;
24 public abstract class AbstractNetconfOperation implements NetconfOperation {
25 private final String netconfSessionIdForReporting;
27 protected AbstractNetconfOperation(final String netconfSessionIdForReporting) {
28 this.netconfSessionIdForReporting = netconfSessionIdForReporting;
31 public final String getNetconfSessionIdForReporting() {
32 return netconfSessionIdForReporting;
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());
42 protected HandlingPriority canHandle(final String operationName, final String operationNamespace) {
43 return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
44 ? getHandlingPriority()
45 : HandlingPriority.CANNOT_HANDLE;
48 public static final class OperationNameAndNamespace {
49 private final String operationName;
50 private final String namespace;
51 private final XmlElement operationElement;
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();
60 public String getOperationName() {
64 public String getNamespace() {
68 public XmlElement getOperationElement() {
69 return operationElement;
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);
78 protected HandlingPriority getHandlingPriority() {
79 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
82 protected String getOperationNamespace() {
83 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
86 protected abstract String getOperationName();
89 public Document handle(final Document requestMessage,
90 final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
92 XmlElement requestElement = getRequestElementWithCheck(requestMessage);
94 Document document = XmlUtil.newDocument();
96 XmlElement operationElement = requestElement.getOnlyChildElement();
97 Map<String, Attr> attributes = requestElement.getAttributes();
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));
103 if (XmlElement.fromDomElement(response).hasNamespace()) {
104 rpcReply.appendChild(response);
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));
112 rpcReply.appendChild(responseNS);
115 for (Attr attribute : attributes.values()) {
116 rpcReply.setAttributeNode((Attr) document.importNode(attribute, true));
118 document.appendChild(rpcReply);
122 protected abstract Element handle(Document document, XmlElement message,
123 NetconfOperationChainedExecution subsequentOperation)
124 throws DocumentedException;
127 public String toString() {
128 final StringBuilder sb = new StringBuilder(getClass().getName());
130 sb.append("{name=").append(getOperationName());
131 } catch (UnsupportedOperationException e) {
134 sb.append(", namespace=").append(getOperationNamespace());
135 sb.append(", session=").append(netconfSessionIdForReporting);
137 return sb.toString();