Add support for notification subtree filter 32/36432/2
authorAndrej Mak <andmak@cisco.com>
Fri, 18 Mar 2016 10:31:44 +0000 (11:31 +0100)
committerAndrej Mak <andmak@cisco.com>
Fri, 18 Mar 2016 10:55:07 +0000 (10:55 +0000)
Change-Id: Ieae248057d890071c6ac5b05b22f1a05a0a98b0c
Signed-off-by: Andrej Mak <andmak@cisco.com>
55 files changed:
netconf/netconf-api/src/main/java/org/opendaylight/netconf/api/xml/XmlNetconfConstants.java
netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerSessionListener.java
netconf/netconf-notifications-api/src/main/java/org/opendaylight/netconf/notifications/NetconfNotification.java
netconf/netconf-notifications-impl/src/main/java/org/opendaylight/netconf/notifications/impl/ops/CreateSubscription.java
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/messages/SubtreeFilter.java [moved from netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/SubtreeFilter.java with 73% similarity]
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/SubtreeFilterNotificationTest.java [new file with mode: 0644]
netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/SubtreeFilterRpcTest.java [moved from netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/SubtreeFilterTest.java with 86% similarity]
netconf/netconf-util/src/test/resources/subtree/notification/0/filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/0/post-filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/0/pre-filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/1/filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/1/post-filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/1/pre-filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/2/filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/2/post-filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/2/pre-filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/3/filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/3/post-filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/3/pre-filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/4/filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/4/post-filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/notification/4/pre-filter.xml [new file with mode: 0644]
netconf/netconf-util/src/test/resources/subtree/rpc/0/post-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/0/post-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/0/pre-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/0/pre-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/0/request.xml [moved from netconf/netconf-impl/src/test/resources/subtree/0/request.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/1/post-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/1/post-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/1/pre-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/1/pre-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/1/request.xml [moved from netconf/netconf-impl/src/test/resources/subtree/1/request.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/10/post-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/10/post-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/10/pre-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/10/pre-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/10/request.xml [moved from netconf/netconf-impl/src/test/resources/subtree/10/request.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/2/post-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/2/post-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/2/pre-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/2/pre-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/2/request.xml [moved from netconf/netconf-impl/src/test/resources/subtree/2/request.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/3/post-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/3/post-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/3/pre-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/3/pre-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/3/request.xml [moved from netconf/netconf-impl/src/test/resources/subtree/3/request.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/4/post-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/4/post-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/4/pre-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/4/pre-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/4/request.xml [moved from netconf/netconf-impl/src/test/resources/subtree/4/request.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/5/post-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/5/post-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/5/pre-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/5/pre-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/5/request.xml [moved from netconf/netconf-impl/src/test/resources/subtree/5/request.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/6/post-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/6/post-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/6/pre-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/6/pre-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/6/request.xml [moved from netconf/netconf-impl/src/test/resources/subtree/6/request.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/7/post-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/7/post-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/7/pre-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/7/pre-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/7/request.xml [moved from netconf/netconf-impl/src/test/resources/subtree/7/request.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/8/post-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/8/post-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/8/pre-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/8/pre-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/8/request.xml [moved from netconf/netconf-impl/src/test/resources/subtree/8/request.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/9/post-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/9/post-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/9/pre-filter.xml [moved from netconf/netconf-impl/src/test/resources/subtree/9/pre-filter.xml with 100% similarity]
netconf/netconf-util/src/test/resources/subtree/rpc/9/request.xml [moved from netconf/netconf-impl/src/test/resources/subtree/9/request.xml with 100% similarity]

index 7cac46a013e0626474ff88233785884c36008029..33bbef5cd5523cba29567db02e56d5c59b5a7033 100644 (file)
@@ -24,6 +24,7 @@ public final class XmlNetconfConstants {
     public static final String SOURCE_KEY = "source";
     public static final String RPC_KEY = "rpc";
     public static final String NOTIFICATION_ELEMENT_NAME = "notification";
+    public static final String EVENT_TIME = "eventTime";
 
     public static final String MESSAGE_ID = "message-id";
     public static final String SESSION_ID = "session-id";
@@ -35,6 +36,7 @@ public final class XmlNetconfConstants {
     public static final String URN_IETF_PARAMS_NETCONF_BASE_1_0 = "urn:ietf:params:netconf:base:1.0";
     public static final String URN_IETF_PARAMS_NETCONF_BASE_1_1 = "urn:ietf:params:netconf:base:1.1";
     public static final String URN_IETF_PARAMS_XML_NS_NETCONF_EXI_1_0 = "urn:ietf:params:xml:ns:netconf:exi:1.0";
+    public static final String URN_IETF_PARAMS_NETCONF_CAPABILITY_NOTIFICATION_1_0 = "urn:ietf:params:netconf:capability:notification:1.0";
 
     public static final String URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0 = "urn:ietf:params:netconf:capability:exi:1.0";
     public static final String URN_IETF_PARAMS_XML_NS_YANG_IETF_NETCONF_MONITORING = "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring";
index a1a16f426c17c7316a2f6c082644b071bbef8d99..ca61311cdcbd48f046d2be71644be9432e2296ad 100644 (file)
@@ -20,6 +20,7 @@ import org.opendaylight.netconf.api.NetconfTerminationReason;
 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.netconf.impl.osgi.NetconfOperationRouter;
+import org.opendaylight.netconf.util.messages.SubtreeFilter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -112,7 +113,7 @@ public class NetconfServerSessionListener implements NetconfSessionListener<Netc
 
             Document rpcReply = operationRouter.onNetconfMessage(incomingDocument, session);
 
-            rpcReply = SubtreeFilter.applySubtreeFilter(incomingDocument, rpcReply);
+            rpcReply = SubtreeFilter.applyRpcSubtreeFilter(incomingDocument, rpcReply);
 
             session.onIncommingRpcSuccess();
 
index 7111a6cbb3bbd523b035be98dea348650c958748..852682a15e2abc7024cc10b869f0b017ce39d961 100644 (file)
@@ -34,6 +34,8 @@ public final class NetconfNotification extends NetconfMessage {
      */
     public static final Date UNKNOWN_EVENT_TIME = new Date(0);
 
+    private final Date eventTime;
+
     /**
      * Create new notification and capture the timestamp in the constructor
      */
@@ -46,6 +48,14 @@ public final class NetconfNotification extends NetconfMessage {
      */
     public NetconfNotification(final Document notificationContent, final Date eventTime) {
         super(wrapNotification(notificationContent, eventTime));
+        this.eventTime = eventTime;
+    }
+
+    /**
+     * @return notification event time
+     */
+    public Date getEventTime() {
+        return eventTime;
     }
 
     private static Document wrapNotification(final Document notificationContent, final Date eventTime) {
index 3e5a0a8c71ef7c4553f1f6e7959cb2e5d8b5457c..e7211b1c6ede7bf3e416fd9c049b0fc067393e75 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.netconf.notifications.impl.ops;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
+import java.util.Date;
 import java.util.List;
 import org.opendaylight.controller.config.util.xml.DocumentedException;
 import org.opendaylight.controller.config.util.xml.XmlElement;
@@ -24,6 +25,7 @@ import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
 import org.opendaylight.netconf.notifications.NotificationListenerRegistration;
 import org.opendaylight.netconf.notifications.impl.NetconfNotificationManager;
 import org.opendaylight.netconf.util.mapping.AbstractSingletonNetconfOperation;
+import org.opendaylight.netconf.util.messages.SubtreeFilter;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
 import org.slf4j.Logger;
@@ -57,9 +59,7 @@ public class CreateSubscription extends AbstractSingletonNetconfOperation implem
         // FIXME reimplement using CODEC_REGISTRY and parse everything into generated class instance
         // Waiting ofr https://git.opendaylight.org/gerrit/#/c/13763/
 
-        // FIXME filter could be supported same way as netconf server filters get and get-config results
         final Optional<XmlElement> filter = operationElement.getOnlyChildElementWithSameNamespaceOptionally("filter");
-        Preconditions.checkArgument(filter.isPresent() == false, "Filter element not yet supported");
 
         // Replay not supported
         final Optional<XmlElement> startTime = operationElement.getOnlyChildElementWithSameNamespaceOptionally("startTime");
@@ -78,7 +78,7 @@ public class CreateSubscription extends AbstractSingletonNetconfOperation implem
         }
 
         final NotificationListenerRegistration notificationListenerRegistration =
-                notifications.registerNotificationListener(streamNameType, new NotificationSubscription(netconfSession));
+                notifications.registerNotificationListener(streamNameType, new NotificationSubscription(netconfSession, filter));
         subscriptions.add(notificationListenerRegistration);
 
         return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.<String>absent());
@@ -115,14 +115,29 @@ public class CreateSubscription extends AbstractSingletonNetconfOperation implem
 
     private static class NotificationSubscription implements NetconfNotificationListener {
         private final NetconfSession currentSession;
+        private final Optional<XmlElement> filter;
 
-        public NotificationSubscription(final NetconfSession currentSession) {
+        public NotificationSubscription(final NetconfSession currentSession, final Optional<XmlElement> filter) {
             this.currentSession = currentSession;
+            this.filter = filter;
         }
 
         @Override
         public void onNotification(final StreamNameType stream, final NetconfNotification notification) {
-            currentSession.sendMessage(notification);
+            if (filter.isPresent()) {
+                try {
+                    final Optional<Document> filtered = SubtreeFilter.applySubtreeNotificationFilter(this.filter.get(), notification.getDocument());
+                    if (filtered.isPresent()) {
+                        final Date eventTime = notification.getEventTime();
+                        currentSession.sendMessage(new NetconfNotification(filtered.get(), eventTime));
+                    }
+                } catch (DocumentedException e) {
+                    LOG.warn(e.toString());
+                    currentSession.sendMessage(notification);
+                }
+            } else {
+                currentSession.sendMessage(notification);
+            }
         }
     }
 }
similarity index 73%
rename from netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/SubtreeFilter.java
rename to netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/messages/SubtreeFilter.java
index d1466dcdd5a72cc6dfdbdd0739c5bd5ad75457bc..949e63d661a1deea9d1692c50ec997ccd683f1bb 100644 (file)
@@ -6,7 +6,7 @@
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
-package org.opendaylight.netconf.impl;
+package org.opendaylight.netconf.util.messages;
 
 import com.google.common.base.Optional;
 import java.io.IOException;
@@ -14,8 +14,8 @@ import java.util.Map;
 import org.opendaylight.controller.config.util.xml.DocumentedException;
 import org.opendaylight.controller.config.util.xml.XmlElement;
 import org.opendaylight.controller.config.util.xml.XmlUtil;
-import org.opendaylight.netconf.util.mapping.AbstractNetconfOperation.OperationNameAndNamespace;
 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
+import org.opendaylight.netconf.util.mapping.AbstractNetconfOperation.OperationNameAndNamespace;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Attr;
@@ -30,7 +30,7 @@ import org.xml.sax.SAXException;
 public class SubtreeFilter {
     private static final Logger LOG = LoggerFactory.getLogger(SubtreeFilter.class);
 
-    static Document applySubtreeFilter(Document requestDocument, Document rpcReply) throws DocumentedException {
+    public static Document applyRpcSubtreeFilter(Document requestDocument, Document rpcReply) throws DocumentedException {
         OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(requestDocument);
         if (XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(operationNameAndNamespace.getNamespace()) &&
                 XmlNetconfConstants.GET.equals(operationNameAndNamespace.getOperationName()) ||
@@ -43,16 +43,9 @@ public class SubtreeFilter {
                 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 DocumentedException("Cannot transform document" + e);
-            }
+            rpcReply = reReadDocument(rpcReply);
             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))) {
+            if (isSupported(filter)) {
 
                 // do
                 return filtered(maybeFilter.get(), rpcReply);
@@ -62,6 +55,68 @@ public class SubtreeFilter {
         return rpcReply; // return identical document
     }
 
+    /**
+     * Filters notification content. If filter type isn't of type "subtree", returns unchanged notification content.
+     * If no match is found, absent is returned.
+     * @param filter filter
+     * @param notification notification
+     * @return document containing filtered notification content
+     * @throws DocumentedException
+     */
+    public static Optional<Document> applySubtreeNotificationFilter(XmlElement filter, Document notification) throws DocumentedException {
+        notification = reReadDocument(notification);
+        removeEventTimeNode(notification);
+        if (isSupported(filter)) {
+            return Optional.fromNullable(filteredNotification(filter, notification));
+        }
+        return Optional.of(extractNotificationContent(notification));
+    }
+
+    private static Document reReadDocument(Document notification) throws DocumentedException {
+        // FIXME: rpcReply document must be reread otherwise some nodes do not inherit namespaces. (services/service)
+        try {
+            notification = XmlUtil.readXmlToDocument(XmlUtil.toString(notification, true));
+        } catch (SAXException | IOException e) {
+            LOG.error("Cannot transform document", e);
+            throw new DocumentedException("Cannot transform document" + e);
+        }
+        return notification;
+    }
+
+    private static void removeEventTimeNode(Document document) {
+        final Node eventTimeNode = document.getDocumentElement().getElementsByTagNameNS(
+                XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_NOTIFICATION_1_0, XmlNetconfConstants.EVENT_TIME).item(0);
+        document.getDocumentElement().removeChild(eventTimeNode);
+    }
+
+    private static boolean isSupported(XmlElement filter) {
+        return "subtree".equals(filter.getAttribute("type"))||
+                "subtree".equals(filter.getAttribute("type", XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
+    }
+
+    private static Document extractNotificationContent(Document notification) throws DocumentedException {
+        XmlElement root = XmlElement.fromDomElement(notification.getDocumentElement());
+        XmlElement content = root.getOnlyChildElement();
+        notification.removeChild(root.getDomElement());
+        notification.appendChild(content.getDomElement());
+        return notification;
+    }
+
+    private static Document filteredNotification(XmlElement filter, Document originalNotification) throws DocumentedException {
+        Document result = XmlUtil.newDocument();
+        XmlElement dataSrc = XmlElement.fromDomDocument(originalNotification);
+        Element dataDst = (Element) result.importNode(dataSrc.getDomElement(), false);
+        for (XmlElement filterChild : filter.getChildElements()) {
+            addSubtree2(filterChild, dataSrc.getOnlyChildElement(), XmlElement.fromDomElement(dataDst));
+        }
+        if(dataDst.getFirstChild() != null) {
+            result.appendChild(dataDst.getFirstChild());
+            return result;
+        } else {
+            return null;
+        }
+    }
+
     private static Document filtered(XmlElement filter, Document originalReplyDocument) throws DocumentedException {
         Document result = XmlUtil.newDocument();
         // even if filter is empty, copy /rpc/data
diff --git a/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/SubtreeFilterNotificationTest.java b/netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/SubtreeFilterNotificationTest.java
new file mode 100644 (file)
index 0000000..ac7d83c
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.netconf.util.messages;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.base.Optional;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.custommonkey.xmlunit.Diff;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+import org.opendaylight.controller.config.util.xml.XmlElement;
+import org.opendaylight.controller.config.util.xml.XmlUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+@RunWith(value = Parameterized.class)
+public class SubtreeFilterNotificationTest {
+    private static final Logger LOG = LoggerFactory.getLogger(SubtreeFilterRpcTest.class);
+
+    private final int directoryIndex;
+
+    @Parameters
+    public static Collection<Object[]> data() {
+        List<Object[]> result = new ArrayList<>();
+        for (int i = 0; i < 5; i++) {
+            result.add(new Object[]{i});
+        }
+        return result;
+    }
+
+    public SubtreeFilterNotificationTest(int directoryIndex) {
+        this.directoryIndex = directoryIndex;
+    }
+
+    @Before
+    public void setUp(){
+        XMLUnit.setIgnoreWhitespace(true);
+    }
+
+    @Test
+    public void testFilterNotification() throws Exception {
+        XmlElement filter = XmlElement.fromDomDocument(getDocument("filter.xml"));
+        Document preFilterDocument = getDocument("pre-filter.xml");
+        Document postFilterDocument = getDocument("post-filter.xml");
+        Optional<Document> actualPostFilterDocumentOpt = SubtreeFilter.applySubtreeNotificationFilter(filter, preFilterDocument);
+        if(actualPostFilterDocumentOpt.isPresent()) {
+            Document actualPostFilterDocument = actualPostFilterDocumentOpt.get();
+            LOG.info("Actual document: {}", XmlUtil.toString(actualPostFilterDocument));
+            Diff diff = XMLUnit.compareXML(postFilterDocument, actualPostFilterDocument);
+            assertTrue(diff.toString(), diff.similar());
+        } else {
+            assertEquals("empty", XmlElement.fromDomDocument(postFilterDocument).getName());
+        }
+    }
+
+    public Document getDocument(String fileName) throws SAXException, IOException {
+        return XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/subtree/notification/" + directoryIndex + "/" +
+                fileName));
+    }
+}
similarity index 86%
rename from netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/SubtreeFilterTest.java
rename to netconf/netconf-util/src/test/java/org/opendaylight/netconf/util/messages/SubtreeFilterRpcTest.java
index 45ca8c3c36ba48bd31032d180c6fd151b73b2708..e486c75f2e0d8d6abc8f3e29ec4f420c212de433 100644 (file)
@@ -6,7 +6,7 @@
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
-package org.opendaylight.netconf.impl;
+package org.opendaylight.netconf.util.messages;
 
 import static org.junit.Assert.assertTrue;
 
@@ -28,8 +28,8 @@ import org.w3c.dom.Document;
 import org.xml.sax.SAXException;
 
 @RunWith(value = Parameterized.class)
-public class SubtreeFilterTest {
-    private static final Logger LOG = LoggerFactory.getLogger(SubtreeFilterTest.class);
+public class SubtreeFilterRpcTest {
+    private static final Logger LOG = LoggerFactory.getLogger(SubtreeFilterRpcTest.class);
 
     private final int directoryIndex;
 
@@ -42,7 +42,7 @@ public class SubtreeFilterTest {
         return result;
     }
 
-    public SubtreeFilterTest(int directoryIndex) {
+    public SubtreeFilterRpcTest(int directoryIndex) {
         this.directoryIndex = directoryIndex;
     }
 
@@ -56,7 +56,7 @@ public class SubtreeFilterTest {
         Document requestDocument = getDocument("request.xml");
         Document preFilterDocument = getDocument("pre-filter.xml");
         Document postFilterDocument = getDocument("post-filter.xml");
-        Document actualPostFilterDocument = SubtreeFilter.applySubtreeFilter(requestDocument, preFilterDocument);
+        Document actualPostFilterDocument = SubtreeFilter.applyRpcSubtreeFilter(requestDocument, preFilterDocument);
         LOG.info("Actual document: {}", XmlUtil.toString(actualPostFilterDocument));
         Diff diff = XMLUnit.compareXML(postFilterDocument, actualPostFilterDocument);
         assertTrue(diff.toString(), diff.similar());
@@ -64,7 +64,7 @@ public class SubtreeFilterTest {
     }
 
     public Document getDocument(String fileName) throws SAXException, IOException {
-        return XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/subtree/" + directoryIndex + "/" +
+        return XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/subtree/rpc/" + directoryIndex + "/" +
                 fileName));
     }
 }
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/0/filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/0/filter.xml
new file mode 100644 (file)
index 0000000..83b3fcb
--- /dev/null
@@ -0,0 +1,13 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<filter xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0" type="subtree">
+    <netconf-session-end xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+        <username/>
+        <session-id/>
+    </netconf-session-end>
+</filter>
\ No newline at end of file
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/0/post-filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/0/post-filter.xml
new file mode 100644 (file)
index 0000000..9f1667a
--- /dev/null
@@ -0,0 +1,14 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<netconf-session-end xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+    <username>admin</username>
+    <session-id>2</session-id>
+</netconf-session-end>
+
+
+
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/0/pre-filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/0/pre-filter.xml
new file mode 100644 (file)
index 0000000..af5368e
--- /dev/null
@@ -0,0 +1,15 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<notification xmlns="urn:ietf:params:netconf:capability:notification:1.0">
+    <netconf-session-end xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+        <username>admin</username>
+        <session-id>2</session-id>
+        <source-host>127.0.0.1</source-host>
+    </netconf-session-end>
+    <eventTime>2016-03-17T13:15:12+01:00</eventTime>
+</notification>
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/1/filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/1/filter.xml
new file mode 100644 (file)
index 0000000..ca976c8
--- /dev/null
@@ -0,0 +1,13 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<filter xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0" type="unsupported">
+    <netconf-session-end xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+        <username/>
+        <session-id/>
+    </netconf-session-end>
+</filter>
\ No newline at end of file
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/1/post-filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/1/post-filter.xml
new file mode 100644 (file)
index 0000000..92c9db8
--- /dev/null
@@ -0,0 +1,12 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<netconf-session-end xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+    <username>admin</username>
+    <session-id>2</session-id>
+    <source-host>127.0.0.1</source-host>
+</netconf-session-end>
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/1/pre-filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/1/pre-filter.xml
new file mode 100644 (file)
index 0000000..af5368e
--- /dev/null
@@ -0,0 +1,15 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<notification xmlns="urn:ietf:params:netconf:capability:notification:1.0">
+    <netconf-session-end xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+        <username>admin</username>
+        <session-id>2</session-id>
+        <source-host>127.0.0.1</source-host>
+    </netconf-session-end>
+    <eventTime>2016-03-17T13:15:12+01:00</eventTime>
+</notification>
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/2/filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/2/filter.xml
new file mode 100644 (file)
index 0000000..ca976c8
--- /dev/null
@@ -0,0 +1,13 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<filter xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0" type="unsupported">
+    <netconf-session-end xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+        <username/>
+        <session-id/>
+    </netconf-session-end>
+</filter>
\ No newline at end of file
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/2/post-filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/2/post-filter.xml
new file mode 100644 (file)
index 0000000..3755dd4
--- /dev/null
@@ -0,0 +1,12 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<netconf-session-start xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+    <username>admin</username>
+    <session-id>2</session-id>
+    <source-host>127.0.0.1</source-host>
+</netconf-session-start>
\ No newline at end of file
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/2/pre-filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/2/pre-filter.xml
new file mode 100644 (file)
index 0000000..66e7b56
--- /dev/null
@@ -0,0 +1,15 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<notification xmlns="urn:ietf:params:netconf:capability:notification:1.0">
+    <netconf-session-start xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+        <username>admin</username>
+        <session-id>2</session-id>
+        <source-host>127.0.0.1</source-host>
+    </netconf-session-start>
+    <eventTime>2016-03-17T13:15:04+01:00</eventTime>
+</notification>
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/3/filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/3/filter.xml
new file mode 100644 (file)
index 0000000..83b3fcb
--- /dev/null
@@ -0,0 +1,13 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<filter xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0" type="subtree">
+    <netconf-session-end xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+        <username/>
+        <session-id/>
+    </netconf-session-end>
+</filter>
\ No newline at end of file
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/3/post-filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/3/post-filter.xml
new file mode 100644 (file)
index 0000000..4c9b412
--- /dev/null
@@ -0,0 +1,8 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<empty/>
\ No newline at end of file
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/3/pre-filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/3/pre-filter.xml
new file mode 100644 (file)
index 0000000..66e7b56
--- /dev/null
@@ -0,0 +1,15 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<notification xmlns="urn:ietf:params:netconf:capability:notification:1.0">
+    <netconf-session-start xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+        <username>admin</username>
+        <session-id>2</session-id>
+        <source-host>127.0.0.1</source-host>
+    </netconf-session-start>
+    <eventTime>2016-03-17T13:15:04+01:00</eventTime>
+</notification>
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/4/filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/4/filter.xml
new file mode 100644 (file)
index 0000000..d8c908c
--- /dev/null
@@ -0,0 +1,10 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<filter xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0" type="subtree">
+    <netconf-session-end xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications"/>
+</filter>
\ No newline at end of file
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/4/post-filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/4/post-filter.xml
new file mode 100644 (file)
index 0000000..026f797
--- /dev/null
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<netconf-session-end xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+    <username>admin</username>
+    <session-id>2</session-id>
+    <source-host>127.0.0.1</source-host>
+</netconf-session-end>
\ No newline at end of file
diff --git a/netconf/netconf-util/src/test/resources/subtree/notification/4/pre-filter.xml b/netconf/netconf-util/src/test/resources/subtree/notification/4/pre-filter.xml
new file mode 100644 (file)
index 0000000..10f229d
--- /dev/null
@@ -0,0 +1,15 @@
+<!--
+  ~ Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+  ~
+  ~ This program and the accompanying materials are made available under the
+  ~ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+  ~ and is available at http://www.eclipse.org/legal/epl-v10.html
+  -->
+<notification xmlns="urn:ietf:params:netconf:capability:notification:1.0">
+    <netconf-session-end xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications">
+        <username>admin</username>
+        <session-id>2</session-id>
+        <source-host>127.0.0.1</source-host>
+    </netconf-session-end>
+    <eventTime>2016-03-17T13:15:12+01:00</eventTime>
+</notification>
\ No newline at end of file