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.server.api.operations;
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.w3c.dom.Attr;
17 import org.w3c.dom.Document;
18 import org.w3c.dom.Element;
19 import org.w3c.dom.NodeList;
21 public abstract class AbstractNetconfOperation implements NetconfOperation {
22 private final String netconfSessionIdForReporting;
24 protected AbstractNetconfOperation(final String netconfSessionIdForReporting) {
25 this.netconfSessionIdForReporting = netconfSessionIdForReporting;
28 public final String getNetconfSessionIdForReporting() {
29 return netconfSessionIdForReporting;
33 public HandlingPriority canHandle(final Document message) throws DocumentedException {
34 OperationNameAndNamespace operationNameAndNamespace = null;
35 operationNameAndNamespace = new OperationNameAndNamespace(message);
36 return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
39 protected HandlingPriority canHandle(final String operationName, final String operationNamespace) {
40 return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
41 ? getHandlingPriority()
42 : HandlingPriority.CANNOT_HANDLE;
45 public static final class OperationNameAndNamespace {
46 private final String operationName;
47 private final String namespace;
48 private final XmlElement operationElement;
50 public OperationNameAndNamespace(final Document message) throws DocumentedException {
51 final var requestElement = getRequestElementWithCheck(message);
52 operationElement = requestElement.getOnlyChildElement();
53 operationName = operationElement.getName();
54 namespace = operationElement.getNamespace();
57 public String getOperationName() {
61 public String getNamespace() {
65 public XmlElement getOperationElement() {
66 return operationElement;
70 protected static XmlElement getRequestElementWithCheck(final Document message) throws DocumentedException {
71 return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
72 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
75 protected HandlingPriority getHandlingPriority() {
76 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
79 protected String getOperationNamespace() {
80 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
83 protected abstract String getOperationName();
86 public Document handle(final Document requestMessage,
87 final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
89 XmlElement requestElement = getRequestElementWithCheck(requestMessage);
91 Document document = XmlUtil.newDocument();
93 XmlElement operationElement = requestElement.getOnlyChildElement();
94 Map<String, Attr> attributes = requestElement.getAttributes();
96 Element response = handle(document, operationElement, subsequentOperation);
97 Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.RPC_REPLY_KEY,
98 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
100 if (XmlElement.fromDomElement(response).hasNamespace()) {
101 rpcReply.appendChild(response);
103 Element responseNS = XmlUtil.createElement(document, response.getNodeName(),
104 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
105 NodeList list = response.getChildNodes();
106 while (list.getLength() != 0) {
107 responseNS.appendChild(list.item(0));
109 rpcReply.appendChild(responseNS);
112 for (Attr attribute : attributes.values()) {
113 rpcReply.setAttributeNode((Attr) document.importNode(attribute, true));
115 document.appendChild(rpcReply);
119 protected abstract Element handle(Document document, XmlElement message,
120 NetconfOperationChainedExecution subsequentOperation)
121 throws DocumentedException;
124 public String toString() {
125 final StringBuilder sb = new StringBuilder(getClass().getName());
127 sb.append("{name=").append(getOperationName());
128 } catch (UnsupportedOperationException e) {
131 sb.append(", namespace=").append(getOperationNamespace());
132 sb.append(", session=").append(netconfSessionIdForReporting);
134 return sb.toString();