Optimize NetconfMessageUtil.isOKMessage() 54/105654/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 25 Apr 2023 13:32:45 +0000 (15:32 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 25 Apr 2023 13:35:05 +0000 (15:35 +0200)
We are acquiring child elements twice, which is rather suboptimal.
Retain the list in local variable, which means we do not need to worry
about exceptions being thrown.

Change-Id: Iee2fbd9ea169ef9ab209643ef3a07518af7f04b0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
protocol/netconf-util/src/main/java/org/opendaylight/netconf/util/messages/NetconfMessageUtil.java

index 2481c75149f0622551b7f6f29a3e29d305cfa204..a863a85278c39d5a034e9c9887f87f090918fda5 100644 (file)
@@ -24,7 +24,7 @@ public final class NetconfMessageUtil {
     private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageUtil.class);
 
     private NetconfMessageUtil() {
-
+        // Hidden on purpose
     }
 
     public static boolean isOKMessage(final NetconfMessage message) throws NetconfDocumentedException {
@@ -36,14 +36,8 @@ public final class NetconfMessageUtil {
     }
 
     public static boolean isOKMessage(final XmlElement xmlElement) throws NetconfDocumentedException {
-        if (xmlElement.getChildElements().size() != 1) {
-            return false;
-        }
-        try {
-            return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK);
-        } catch (DocumentedException e) {
-            throw new NetconfDocumentedException(e);
-        }
+        final var children = xmlElement.getChildElements();
+        return children.size() == 1 && children.get(0).getName().equals(XmlNetconfConstants.OK);
     }
 
     public static boolean isErrorMessage(final NetconfMessage message) throws NetconfDocumentedException {