2 * Copyright (c) 2014 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
9 package org.opendaylight.controller.netconf.impl;
11 import com.google.common.base.Optional;
12 import java.io.IOException;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
16 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation.OperationNameAndNamespace;
17 import org.opendaylight.controller.netconf.util.xml.XmlElement;
18 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.w3c.dom.Attr;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24 import org.w3c.dom.Node;
25 import org.xml.sax.SAXException;
28 * See <a href="http://tools.ietf.org/html/rfc6241#section-6">rfc6241</a> for details.
30 public class SubtreeFilter {
31 private static final Logger LOG = LoggerFactory.getLogger(SubtreeFilter.class);
33 static Document applySubtreeFilter(Document requestDocument, Document rpcReply) throws NetconfDocumentedException {
34 OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(requestDocument);
35 if (XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(operationNameAndNamespace.getNamespace()) &&
36 XmlNetconfConstants.GET.equals(operationNameAndNamespace.getOperationName()) ||
37 XmlNetconfConstants.GET_CONFIG.equals(operationNameAndNamespace.getOperationName())) {
38 // process subtree filtering here, in case registered netconf operations do
39 // not implement filtering.
40 Optional<XmlElement> maybeFilter = operationNameAndNamespace.getOperationElement().getOnlyChildElementOptionally(
41 XmlNetconfConstants.FILTER, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
42 if (!maybeFilter.isPresent()) {
46 // FIXME: rpcReply document must be reread otherwise some nodes do not inherit namespaces. (services/service)
48 rpcReply = XmlUtil.readXmlToDocument(XmlUtil.toString(rpcReply, true));
49 } catch (SAXException | IOException e) {
50 LOG.error("Cannot transform document", e);
51 throw new NetconfDocumentedException("Cannot transform document" + e);
53 XmlElement filter = maybeFilter.get();
54 if ("subtree".equals(filter.getAttribute("type"))||
55 "subtree".equals(filter.getAttribute("type", XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0))) {
58 return filtered(maybeFilter.get(), rpcReply);
62 return rpcReply; // return identical document
65 private static Document filtered(XmlElement filter, Document originalReplyDocument) throws NetconfDocumentedException {
66 Document result = XmlUtil.newDocument();
67 // even if filter is empty, copy /rpc/data
68 Element rpcReply = originalReplyDocument.getDocumentElement();
69 Node rpcReplyDst = result.importNode(rpcReply, false);
70 result.appendChild(rpcReplyDst);
71 XmlElement dataSrc = XmlElement.fromDomElement(rpcReply).getOnlyChildElement("data", XmlNetconfConstants.RFC4741_TARGET_NAMESPACE);
72 Element dataDst = (Element) result.importNode(dataSrc.getDomElement(), false);
73 rpcReplyDst.appendChild(dataDst);
74 addSubtree(filter, dataSrc, XmlElement.fromDomElement(dataDst));
79 private static void addSubtree(XmlElement filter, XmlElement src, XmlElement dst) throws NetconfDocumentedException {
80 for (XmlElement srcChild : src.getChildElements()) {
81 for (XmlElement filterChild : filter.getChildElements()) {
82 addSubtree2(filterChild, srcChild, dst);
87 private static MatchingResult addSubtree2(XmlElement filter, XmlElement src, XmlElement dstParent) throws NetconfDocumentedException {
88 Document document = dstParent.getDomElement().getOwnerDocument();
89 MatchingResult matches = matches(src, filter);
90 if (matches != MatchingResult.NO_MATCH && matches != MatchingResult.CONTENT_MISMATCH) {
91 // copy srcChild to dst
92 boolean filterHasChildren = filter.getChildElements().isEmpty() == false;
93 // copy to depth if this is leaf of filter tree
94 Element copied = (Element) document.importNode(src.getDomElement(), filterHasChildren == false);
95 boolean shouldAppend = filterHasChildren == false;
96 if (filterHasChildren) { // this implies TAG_MATCH
97 // do the same recursively
98 int numberOfTextMatchingChildren = 0;
99 for (XmlElement srcChild : src.getChildElements()) {
100 for (XmlElement filterChild : filter.getChildElements()) {
101 MatchingResult childMatch = addSubtree2(filterChild, srcChild, XmlElement.fromDomElement(copied));
102 if (childMatch == MatchingResult.CONTENT_MISMATCH) {
103 return MatchingResult.NO_MATCH;
105 if (childMatch == MatchingResult.CONTENT_MATCH) {
106 numberOfTextMatchingChildren++;
108 shouldAppend |= childMatch != MatchingResult.NO_MATCH;
111 // if only text matching child filters are specified..
112 if (numberOfTextMatchingChildren == filter.getChildElements().size()) {
113 // force all children to be added (to depth). This is done by copying parent node to depth.
114 // implies shouldAppend == true
115 copied = (Element) document.importNode(src.getDomElement(), true);
119 dstParent.getDomElement().appendChild(copied);
126 * Shallow compare src node to filter: tag name and namespace must match.
127 * If filter node has no children and has text content, it also must match.
129 private static MatchingResult matches(XmlElement src, XmlElement filter) throws NetconfDocumentedException {
130 boolean tagMatch = src.getName().equals(filter.getName()) &&
131 src.getNamespaceOptionally().equals(filter.getNamespaceOptionally());
132 MatchingResult result = null;
134 // match text content
135 Optional<String> maybeText = filter.getOnlyTextContentOptionally();
136 if (maybeText.isPresent()) {
137 if (maybeText.equals(src.getOnlyTextContentOptionally()) || prefixedContentMatches(filter, src)) {
138 result = MatchingResult.CONTENT_MATCH;
140 result = MatchingResult.CONTENT_MISMATCH;
143 // match attributes, combination of content and tag is not supported
144 if (result == null) {
145 for (Attr attr : filter.getAttributes().values()) {
146 // ignore namespace declarations
147 if (XmlUtil.XMLNS_URI.equals(attr.getNamespaceURI()) == false ) {
148 // find attr with matching localName(), namespaceURI(), == value() in src
149 String found = src.getAttribute(attr.getLocalName(), attr.getNamespaceURI());
150 if (attr.getValue().equals(found) && result != MatchingResult.NO_MATCH) {
151 result = MatchingResult.TAG_MATCH;
153 result = MatchingResult.NO_MATCH;
158 if (result == null) {
159 result = MatchingResult.TAG_MATCH;
162 if (result == null) {
163 result = MatchingResult.NO_MATCH;
165 LOG.debug("Matching {} to {} resulted in {}", src, filter, result);
169 private static boolean prefixedContentMatches(final XmlElement filter, final XmlElement src) throws NetconfDocumentedException {
170 final Map.Entry<String, String> prefixToNamespaceOfFilter = filter.findNamespaceOfTextContent();
171 final Map.Entry<String, String> prefixToNamespaceOfSrc = src.findNamespaceOfTextContent();
173 final String prefix = prefixToNamespaceOfFilter.getKey();
174 // If this is not a prefixed content, we do not need to continue since content do not match
175 if(prefix.equals(XmlElement.DEFAULT_NAMESPACE_PREFIX)) {
178 // Namespace mismatch
179 if(!prefixToNamespaceOfFilter.getValue().equals(prefixToNamespaceOfSrc.getValue())) {
183 final String unprefixedFilterContent = filter.getTextContent().substring(prefixToNamespaceOfFilter.getKey().length() + 1);
184 final String unprefixedSrcContnet = src.getTextContent().substring(prefixToNamespaceOfSrc.getKey().length() + 1);
185 // Finally compare unprefixed content
186 return unprefixedFilterContent.equals(unprefixedSrcContnet);
189 enum MatchingResult {
190 NO_MATCH, TAG_MATCH, CONTENT_MATCH, CONTENT_MISMATCH