Add is{Notification,Rpc,RpcReply}Message methods 89/107389/8
authormatus.matok <matus.matok@pantheon.tech>
Fri, 11 Aug 2023 08:34:35 +0000 (10:34 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 29 Aug 2023 12:49:50 +0000 (14:49 +0200)
Added public static methods is{Notification,Rpc,RpcReply}Message(Document) methods
that return true, if the document in the parameter is a NotificationMessage,
RpcMessage or RpcReplyMessage respectively. To be used in the static method
NetconfMessage.of(Document).

JIRA: NETCONF-1014
Change-Id: I61da444487bafcf9bc5e15cf8dbf8b9fa978218e
Signed-off-by: matus.matok <matus.matok@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/messages/HelloMessage.java
protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/messages/NotificationMessage.java
protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/messages/RpcMessage.java
protocol/netconf-api/src/main/java/org/opendaylight/netconf/api/messages/RpcReplyMessage.java
protocol/netconf-api/src/test/java/org/opendaylight/netconf/api/messages/NotificationMessageTest.java
protocol/netconf-api/src/test/java/org/opendaylight/netconf/api/messages/RpcMessageTest.java
protocol/netconf-api/src/test/java/org/opendaylight/netconf/api/messages/RpcReplyMessageTest.java

index 1b6ee7b724225d356502f46c5081bb6a6eb7d0b8..71477b2bef9d24359b154808c59b23c252740efe 100644 (file)
@@ -85,7 +85,7 @@ public final class HelloMessage extends NetconfMessage {
         return isHelloMessage(msg.getDocument());
     }
 
-    private static boolean isHelloMessage(final Document document) {
+    public static boolean isHelloMessage(final Document document) {
         final XmlElement element = XmlElement.fromDomElement(document.getDocumentElement());
         if (!HELLO_TAG.equals(element.getName())) {
             return false;
index da58fcce82d3c684aaddc0598d8d6d3ffad80d60..cec7da8d6c5fa00c0bc3a7878eb7aaec2d2a0985 100644 (file)
@@ -206,4 +206,10 @@ public final class NotificationMessage extends NetconfMessage {
         notificationContent.appendChild(entireNotification);
         return notificationContent;
     }
+
+    public static boolean isNotificationMessage(final Document document) {
+        final var root = document.getDocumentElement();
+        return NamespaceURN.NOTIFICATION.equals(root.getNamespaceURI())
+            && XmlNetconfConstants.NOTIFICATION_ELEMENT_NAME.equals(root.getLocalName());
+    }
 }
index 01fc13df11701afd7865ffb23681a3bb64909791..f3237a8fcf5afc49eb82ab478ecfe3366fc4844f 100644 (file)
@@ -84,4 +84,10 @@ public final class RpcMessage extends NetconfMessage {
     public @NonNull String messageId() {
         return getDocument().getDocumentElement().getAttribute(XmlNetconfConstants.MESSAGE_ID);
     }
+
+    public static boolean isRpcMessage(final Document document) {
+        final var root = document.getDocumentElement();
+        return NamespaceURN.BASE.equals(root.getNamespaceURI())
+            && XmlNetconfConstants.RPC_KEY.equals(root.getLocalName());
+    }
 }
index 071a3ab771f138f1efed28d4fb4656112ee7bfaa..ebf5959fd378d9bdce66e42f4a45ada3900cfeab 100644 (file)
@@ -69,4 +69,10 @@ public final class RpcReplyMessage extends NetconfMessage {
         final var attr = getDocument().getDocumentElement().getAttributeNode(XmlNetconfConstants.MESSAGE_ID);
         return attr == null ? null : attr.getValue();
     }
+
+    public static boolean isRpcReplyMessage(final Document document) {
+        final var root = document.getDocumentElement();
+        return NamespaceURN.BASE.equals(root.getNamespaceURI())
+            && XmlNetconfConstants.RPC_REPLY_KEY.equals(root.getLocalName());
+    }
 }
index 95f0b0840709732efc76e13b0d9ad79e8e0f9ae5..aa37c30c87dc486ba237e6c8094f58da810c4d7f 100644 (file)
@@ -7,46 +7,60 @@
  */
 package org.opendaylight.netconf.api.messages;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.time.Instant;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 import org.opendaylight.yangtools.util.xml.UntrustedXML;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
 
-public class NotificationMessageTest {
-    @Test
-    public void testWrapNotification() throws Exception {
-        final Document document = UntrustedXML.newDocumentBuilder().newDocument();
+class NotificationMessageTest {
+    private static final Instant EVENT_TIME = Instant.ofEpochMilli(10_000_000);
 
-        final Element rootElement = document.createElement("test-root");
-        document.appendChild(rootElement);
+    private final Document document = UntrustedXML.newDocumentBuilder().newDocument();
 
-        final Instant eventTime = Instant.ofEpochMilli(10_000_000);
+    @Test
+    void testWrapNotification() {
+        final var rootElement = document.createElement("test-root");
+        document.appendChild(rootElement);
 
-        final NotificationMessage netconfNotification = new NotificationMessage(document, eventTime);
-        final Document resultDoc = netconfNotification.getDocument();
-        final NodeList nodeList = resultDoc.getElementsByTagNameNS(
+        final var netconfNotification = new NotificationMessage(document, EVENT_TIME);
+        final var resultDoc = netconfNotification.getDocument();
+        final var nodeList = resultDoc.getElementsByTagNameNS(
             "urn:ietf:params:xml:ns:netconf:notification:1.0", "notification");
 
         assertNotNull(nodeList);
         // expected only the one NOTIFICATION tag
         assertEquals(1, nodeList.getLength());
 
-        final Element entireNotification = (Element) nodeList.item(0);
-        final NodeList childNodes = entireNotification.getElementsByTagNameNS(
+        final var entireNotification = (Element) nodeList.item(0);
+        final var childNodes = entireNotification.getElementsByTagNameNS(
             "urn:ietf:params:xml:ns:netconf:notification:1.0", "eventTime");
 
         assertNotNull(childNodes);
         // expected only the one EVENT_TIME tag
         assertEquals(1, childNodes.getLength());
 
-        final Element eventTimeElement = (Element) childNodes.item(0);
+        final var eventTimeElement = (Element) childNodes.item(0);
+
+        assertEquals(EVENT_TIME, NotificationMessage.RFC3339_DATE_PARSER.apply(eventTimeElement.getTextContent()));
+        assertEquals(EVENT_TIME, netconfNotification.getEventTime());
+    }
 
-        assertEquals(eventTime, NotificationMessage.RFC3339_DATE_PARSER.apply(eventTimeElement.getTextContent()));
-        assertEquals(eventTime, netconfNotification.getEventTime());
+    @Test
+    void testIsNotificationMessage() {
+        document.appendChild(document.createElementNS("urn:ietf:params:xml:ns:netconf:notification:1.0",
+            "notification"));
+        assertTrue(NotificationMessage.isNotificationMessage(document));
+    }
+
+    @Test
+    void testIsRpcMessageNegative() {
+        document.appendChild(document.createElementNS("urn:ietf:params:xml:ns:netconf:notification:1.0", "other"));
+        assertFalse(NotificationMessage.isNotificationMessage(document));
     }
 }
index b7b49fbc18e4c250061fd8354da745e394c7311d..5fb0293922d201c8ca6dadcf1d6d51814e9a0f42 100644 (file)
@@ -8,8 +8,10 @@
 package org.opendaylight.netconf.api.messages;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import com.google.common.collect.ImmutableMap;
 import org.junit.jupiter.api.Test;
@@ -96,4 +98,16 @@ class RpcMessageTest {
             </rpc>
             """, msg.toString());
     }
+
+    @Test
+    void testIsRpcMessage() {
+        document.appendChild(document.createElementNS("urn:ietf:params:xml:ns:netconf:base:1.0", "rpc"));
+        assertTrue(RpcMessage.isRpcMessage(document));
+    }
+
+    @Test
+    void testIsRpcMessageNegative() {
+        document.appendChild(document.createElementNS("urn:ietf:params:xml:ns:netconf:base:1.0", "other"));
+        assertFalse(RpcMessage.isRpcMessage(document));
+    }
 }
index 2918e7b1d1ae90439960928ecf0d801772c20921..f8a011c0202b58ead72e5af3753d80830b7848a5 100644 (file)
@@ -8,7 +8,9 @@
 package org.opendaylight.netconf.api.messages;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import com.google.common.collect.ImmutableMap;
 import org.junit.jupiter.api.Test;
@@ -85,4 +87,16 @@ class RpcReplyMessageTest {
         assertEquals(ErrorTag.UNKNOWN_NAMESPACE, ex.getErrorTag());
         assertEquals(ImmutableMap.of("bad-element", "rpc-reply", "bad-namespace", "bad"), ex.getErrorInfo());
     }
+
+    @Test
+    void testIsRpcReplyMessage() {
+        document.appendChild(document.createElementNS("urn:ietf:params:xml:ns:netconf:base:1.0", "rpc-reply"));
+        assertTrue(RpcReplyMessage.isRpcReplyMessage(document));
+    }
+
+    @Test
+    void testIsRpcReplyMessageNegative() {
+        document.appendChild(document.createElementNS("urn:ietf:params:xml:ns:netconf:base:1.0", "other"));
+        assertFalse(RpcReplyMessage.isRpcReplyMessage(document));
+    }
 }