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;
52 private final XmlElement operationElement;
54 public OperationNameAndNamespace(final Document message) throws DocumentedException {
55 XmlElement requestElement = null;
56 requestElement = getRequestElementWithCheck(message);
57 operationElement = requestElement.getOnlyChildElement();
58 operationName = operationElement.getName();
59 namespace = operationElement.getNamespace();
62 public String getOperationName() {
66 public String getNamespace() {
70 public XmlElement getOperationElement() {
71 return operationElement;
76 protected static XmlElement getRequestElementWithCheck(final Document message) throws DocumentedException {
77 return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
78 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
81 protected HandlingPriority getHandlingPriority() {
82 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
85 protected String getOperationNamespace() {
86 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
89 protected abstract String getOperationName();
92 public Document handle(final Document requestMessage,
93 final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
95 XmlElement requestElement = getRequestElementWithCheck(requestMessage);
97 Document document = XmlUtil.newDocument();
99 XmlElement operationElement = requestElement.getOnlyChildElement();
100 Map<String, Attr> attributes = requestElement.getAttributes();
102 Element response = handle(document, operationElement, subsequentOperation);
103 Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.RPC_REPLY_KEY,
104 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
106 if (XmlElement.fromDomElement(response).hasNamespace()) {
107 rpcReply.appendChild(response);
109 Element responseNS = XmlUtil.createElement(document, response.getNodeName(),
110 Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
111 NodeList list = response.getChildNodes();
112 while (list.getLength() != 0) {
113 responseNS.appendChild(list.item(0));
115 rpcReply.appendChild(responseNS);
118 for (Attr attribute : attributes.values()) {
119 rpcReply.setAttributeNode((Attr) document.importNode(attribute, true));
121 document.appendChild(rpcReply);
125 protected abstract Element handle(Document document, XmlElement message,
126 NetconfOperationChainedExecution subsequentOperation)
127 throws DocumentedException;
130 public String toString() {
131 final StringBuilder sb = new StringBuilder(getClass().getName());
133 sb.append("{name=").append(getOperationName());
134 } catch (UnsupportedOperationException e) {
137 sb.append(", namespace=").append(getOperationNamespace());
138 sb.append(", session=").append(netconfSessionIdForReporting);
140 return sb.toString();