Fix RuntimeRpc.canHandle() 66/108166/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 2 Oct 2023 17:25:48 +0000 (19:25 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 2 Oct 2023 17:28:32 +0000 (19:28 +0200)
We were disregarding the result from the RPC search, fix that.

Change-Id: Ice6e1d4cc7aa381c21fe640dbd1b5044c68cf6c7
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugins/netconf-server-mdsal/src/main/java/org/opendaylight/netconf/server/mdsal/operations/RuntimeRpc.java

index be6ccc68e175244e8312a71200167ffac91f4642..db2b4fe2d19c1cb1216cef5e12452419be0bdc4c 100644 (file)
@@ -74,15 +74,13 @@ public final class RuntimeRpc extends AbstractSingletonNetconfOperation {
 
     @Override
     protected HandlingPriority canHandle(final String netconfOperationName, final String namespace) {
-        final XMLNamespace namespaceURI = createNsUri(namespace);
-        final Optional<? extends Module> module = getModule(namespaceURI);
-
-        if (module.isEmpty()) {
+        final var xmlNamespace = XMLNamespace.of(namespace);
+        final var rpcDef = getModule(xmlNamespace)
+            .flatMap(module -> getRpcDefinitionFromModule(module, xmlNamespace, netconfOperationName));
+        if (rpcDef.isEmpty()) {
             LOG.debug("Cannot handle rpc: {}, {}", netconfOperationName, namespace);
             return HandlingPriority.CANNOT_HANDLE;
         }
-
-        getRpcDefinitionFromModule(module.orElseThrow(), namespaceURI, netconfOperationName);
         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
     }
 
@@ -91,11 +89,6 @@ public final class RuntimeRpc extends AbstractSingletonNetconfOperation {
         throw new UnsupportedOperationException("Runtime rpc does not have a stable name");
     }
 
-    private static XMLNamespace createNsUri(final String namespace) {
-        // May throw IllegalArgumentException, but that should never happen, as the namespace comes from parsed XML
-        return XMLNamespace.of(namespace);
-    }
-
     //this returns module with the newest revision if more then 1 module with same namespace is found
     private Optional<? extends Module> getModule(final XMLNamespace namespace) {
         return schemaContext.getCurrentContext().findModules(namespace).stream().findFirst();
@@ -103,8 +96,9 @@ public final class RuntimeRpc extends AbstractSingletonNetconfOperation {
 
     private static Optional<RpcDefinition> getRpcDefinitionFromModule(final Module module, final XMLNamespace namespace,
             final String name) {
-        for (final RpcDefinition rpcDef : module.getRpcs()) {
-            if (rpcDef.getQName().getNamespace().equals(namespace) && rpcDef.getQName().getLocalName().equals(name)) {
+        for (var rpcDef : module.getRpcs()) {
+            final var qname = rpcDef.getQName();
+            if (qname.getNamespace().equals(namespace) && qname.getLocalName().equals(name)) {
                 return Optional.of(rpcDef);
             }
         }
@@ -125,7 +119,7 @@ public final class RuntimeRpc extends AbstractSingletonNetconfOperation {
                     ErrorType.PROTOCOL, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
         }
 
-        final XMLNamespace namespaceURI = createNsUri(netconfOperationNamespace);
+        final XMLNamespace namespaceURI = XMLNamespace.of(netconfOperationNamespace);
         final Optional<? extends Module> moduleOptional = getModule(namespaceURI);
 
         if (moduleOptional.isEmpty()) {