* See <a href="http://tools.ietf.org/html/rfc6241#section-6">rfc6241</a> for details.
*/
public class SubtreeFilter {
- private static final Logger logger = LoggerFactory.getLogger(SubtreeFilter.class);
+ private static final Logger LOG = LoggerFactory.getLogger(SubtreeFilter.class);
static Document applySubtreeFilter(Document requestDocument, Document rpcReply) throws NetconfDocumentedException {
- // FIXME: rpcReply document must be reread otherwise some nodes do not inherit namespaces. (services/service)
- try {
- rpcReply = XmlUtil.readXmlToDocument(XmlUtil.toString(rpcReply, true));
- } catch (SAXException | IOException e) {
- logger.error("Cannot transform document", e);
- throw new NetconfDocumentedException("Cannot transform document");
- }
-
OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(requestDocument);
if (XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(operationNameAndNamespace.getNamespace()) &&
XmlNetconfConstants.GET.equals(operationNameAndNamespace.getOperationName()) ||
// not implement filtering.
Optional<XmlElement> maybeFilter = operationNameAndNamespace.getOperationElement().getOnlyChildElementOptionally(
XmlNetconfConstants.FILTER, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
- if (maybeFilter.isPresent() && (
- "subtree".equals(maybeFilter.get().getAttribute("type"))||
- "subtree".equals(maybeFilter.get().getAttribute("type", XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0))
- )) {
+ if (!maybeFilter.isPresent()) {
+ return rpcReply;
+ }
+ // FIXME: rpcReply document must be reread otherwise some nodes do not inherit namespaces. (services/service)
+ try {
+ rpcReply = XmlUtil.readXmlToDocument(XmlUtil.toString(rpcReply, true));
+ } catch (SAXException | IOException e) {
+ LOG.error("Cannot transform document", e);
+ throw new NetconfDocumentedException("Cannot transform document" + e);
+ }
+ XmlElement filter = maybeFilter.get();
+ if ("subtree".equals(filter.getAttribute("type"))||
+ "subtree".equals(filter.getAttribute("type", XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0))) {
// do
return filtered(maybeFilter.get(), rpcReply);
}
}
+
return rpcReply; // return identical document
}
if (result == null) {
result = MatchingResult.NO_MATCH;
}
- logger.debug("Matching {} to {} resulted in {}", src, filter, result);
+ LOG.debug("Matching {} to {} resulted in {}", src, filter, result);
return result;
}
private static boolean prefixedContentMatches(final XmlElement filter, final XmlElement src) throws NetconfDocumentedException {
- final Map.Entry<String, String> prefixToNamespaceOfFilter = filter.findNamespaceOfTextContent();
- final Map.Entry<String, String> prefixToNamespaceOfSrc = src.findNamespaceOfTextContent();
+ final Map.Entry<String, String> prefixToNamespaceOfFilter;
+ final Map.Entry<String, String> prefixToNamespaceOfSrc;
+ try {
+ prefixToNamespaceOfFilter = filter.findNamespaceOfTextContent();
+ prefixToNamespaceOfSrc = src.findNamespaceOfTextContent();
+ } catch (IllegalArgumentException e) {
+ //if we can't find namespace of prefix - it's not a prefix, so it doesn't match
+ return false;
+ }
final String prefix = prefixToNamespaceOfFilter.getKey();
// If this is not a prefixed content, we do not need to continue since content do not match
- if(prefix.equals(XmlElement.DEFAULT_NAMESPACE_PREFIX)) {
+ if (prefix.equals(XmlElement.DEFAULT_NAMESPACE_PREFIX)) {
return false;
}
// Namespace mismatch
- if(!prefixToNamespaceOfFilter.getValue().equals(prefixToNamespaceOfSrc.getValue())) {
+ if (!prefixToNamespaceOfFilter.getValue().equals(prefixToNamespaceOfSrc.getValue())) {
return false;
}
- final String unprefixedFilterContent = filter.getTextContent().substring(prefix.length());
- final String unprefixedSrcCOntnet = src.getTextContent().substring(prefix.length());
+ final String unprefixedFilterContent = filter.getTextContent().substring(prefixToNamespaceOfFilter.getKey().length() + 1);
+ final String unprefixedSrcContnet = src.getTextContent().substring(prefixToNamespaceOfSrc.getKey().length() + 1);
// Finally compare unprefixed content
- return unprefixedFilterContent.equals(unprefixedSrcCOntnet);
+ return unprefixedFilterContent.equals(unprefixedSrcContnet);
}
enum MatchingResult {