Remove AbstractDOMRpcRoutingTableEntry.invokeRpc 24/74624/2
authorJie Han <han.jie@zte.com.cn>
Mon, 30 Jul 2018 06:20:07 +0000 (14:20 +0800)
committerRobert Varga <nite@hq.sk>
Mon, 30 Jul 2018 10:55:51 +0000 (10:55 +0000)
Make routing entry not directly tied to invocation model and
add an inner static class RpcInvocation in DOMRpcRouter
to be used to invoke rpc with routing entries.

Change-Id: I55fa669bff8a18eb182a579b9dd636294d54e460
Signed-off-by: Jie Han <han.jie@zte.com.cn>
dom/mdsal-dom-broker/src/main/java/org/opendaylight/mdsal/dom/broker/AbstractDOMRpcRoutingTableEntry.java
dom/mdsal-dom-broker/src/main/java/org/opendaylight/mdsal/dom/broker/DOMRpcRouter.java
dom/mdsal-dom-broker/src/main/java/org/opendaylight/mdsal/dom/broker/GlobalDOMRpcRoutingTableEntry.java
dom/mdsal-dom-broker/src/main/java/org/opendaylight/mdsal/dom/broker/RoutedDOMRpcRoutingTableEntry.java
dom/mdsal-dom-broker/src/main/java/org/opendaylight/mdsal/dom/broker/UnknownDOMRpcRoutingTableEntry.java
dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/GlobalDOMRpcRoutingTableEntryTest.java
dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/RoutedDOMRpcRoutingTableEntryTest.java
dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/UnknownDOMRpcRoutingTableEntryTest.java

index 04a90d7d61b6e2de5d4110247f2f249d5a107bf2..32f8ba6053d1e1895111d800abd8c5e8b6bf348a 100644 (file)
@@ -12,7 +12,6 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
 import com.google.common.collect.Maps;
-import com.google.common.util.concurrent.FluentFuture;
 import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.List;
@@ -20,24 +19,27 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
+import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
-import org.opendaylight.mdsal.dom.api.DOMRpcResult;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 abstract class AbstractDOMRpcRoutingTableEntry {
     private final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> implementations;
-    private final SchemaPath schemaPath;
+    private final DOMRpcIdentifier rpcId;
 
-    AbstractDOMRpcRoutingTableEntry(final SchemaPath schemaPath, final Map<YangInstanceIdentifier,
+    AbstractDOMRpcRoutingTableEntry(final DOMRpcIdentifier rpcId, final Map<YangInstanceIdentifier,
             List<DOMRpcImplementation>> implementations) {
-        this.schemaPath = Preconditions.checkNotNull(schemaPath);
+        this.rpcId = Preconditions.checkNotNull(rpcId);
         this.implementations = Preconditions.checkNotNull(implementations);
     }
 
     final SchemaPath getSchemaPath() {
-        return schemaPath;
+        return rpcId.getType();
+    }
+
+    final DOMRpcIdentifier getRpcId() {
+        return rpcId;
     }
 
     final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
@@ -116,8 +118,6 @@ abstract class AbstractDOMRpcRoutingTableEntry {
         return v.isEmpty() ? null : newInstance(v);
     }
 
-    protected abstract FluentFuture<DOMRpcResult> invokeRpc(NormalizedNode<?, ?> input);
-
     protected abstract AbstractDOMRpcRoutingTableEntry newInstance(
             Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);
 }
index b27dd95a6b5ee418dbc0ea524ed8128f643dc848..af7a8bf96aad8cc54e3e7a39f7925252e92bd685 100644 (file)
@@ -24,8 +24,10 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -47,9 +49,12 @@ import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class DOMRpcRouter extends AbstractRegistration implements SchemaContextListener {
     private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat(
@@ -222,7 +227,7 @@ public final class DOMRpcRouter extends AbstractRegistration implements SchemaCo
                     new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", type));
             }
 
-            return entry.invokeRpc(input);
+            return RpcInvocation.invoke(entry, input);
         }
 
         @Override
@@ -268,4 +273,75 @@ public final class DOMRpcRouter extends AbstractRegistration implements SchemaCo
             };
         }
     }
+
+    static final class RpcInvocation {
+        private static final Logger LOG = LoggerFactory.getLogger(RpcInvocation.class);
+
+        static FluentFuture<DOMRpcResult> invoke(final AbstractDOMRpcRoutingTableEntry entry,
+            final NormalizedNode<?, ?> input) {
+            if (entry instanceof UnknownDOMRpcRoutingTableEntry) {
+                return FluentFutures.immediateFailedFluentFuture(
+                    new DOMRpcImplementationNotAvailableException("SchemaPath %s is not resolved to an RPC",
+                        entry.getSchemaPath()));
+            } else if (entry instanceof RoutedDOMRpcRoutingTableEntry) {
+                return invokeRoutedRpc((RoutedDOMRpcRoutingTableEntry) entry, input);
+            } else if (entry instanceof GlobalDOMRpcRoutingTableEntry) {
+                return invokeGlobalRpc((GlobalDOMRpcRoutingTableEntry) entry, input);
+            }
+
+            return FluentFutures.immediateFailedFluentFuture(
+                new DOMRpcImplementationNotAvailableException("Unsupported RPC entry."));
+        }
+
+        private static FluentFuture<DOMRpcResult> invokeRoutedRpc(final RoutedDOMRpcRoutingTableEntry entry,
+            final NormalizedNode<?, ?> input) {
+            final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input,
+                entry.getRpcId().getContextReference());
+
+            // Routing key is present, attempt to deliver as a routed RPC
+            if (maybeKey.isPresent()) {
+                final NormalizedNode<?, ?> key = maybeKey.get();
+                final Object value = key.getValue();
+                if (value instanceof YangInstanceIdentifier) {
+                    final YangInstanceIdentifier iid = (YangInstanceIdentifier) value;
+
+                    // Find a DOMRpcImplementation for a specific iid
+                    final List<DOMRpcImplementation> specificImpls = entry.getImplementations(iid);
+                    if (specificImpls != null) {
+                        return specificImpls.get(0)
+                            .invokeRpc(DOMRpcIdentifier.create(entry.getSchemaPath(), iid), input);
+                    }
+
+                    LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
+
+                    // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
+                    // implementation this way
+                    final List<DOMRpcImplementation> mayBeRemoteImpls =
+                        entry.getImplementations(YangInstanceIdentifier.EMPTY);
+
+                    if (mayBeRemoteImpls != null) {
+                        return mayBeRemoteImpls.get(0)
+                            .invokeRpc(DOMRpcIdentifier.create(entry.getSchemaPath(), iid), input);
+                    }
+
+                } else {
+                    LOG.warn("Ignoring wrong context value {}", value);
+                }
+            }
+
+            final List<DOMRpcImplementation> impls = entry.getImplementations(null);
+            if (impls != null) {
+                return impls.get(0).invokeRpc(entry.getRpcId(), input);
+            }
+
+            return FluentFutures.immediateFailedFluentFuture(
+                new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available",
+                    entry.getSchemaPath()));
+        }
+
+        private static FluentFuture<DOMRpcResult> invokeGlobalRpc(final GlobalDOMRpcRoutingTableEntry entry,
+                final NormalizedNode<?, ?> input) {
+            return entry.getImplementations(YangInstanceIdentifier.EMPTY).get(0).invokeRpc(entry.getRpcId(), input);
+        }
+    }
 }
index cfe4111de27326ecb573fa14c76bd6a956ef2b68..a2d27014e5cc4310fa97a85d81f1dfbdd25e569e 100644 (file)
@@ -7,42 +7,30 @@
  */
 package org.opendaylight.mdsal.dom.broker;
 
-import com.google.common.base.Preconditions;
-import com.google.common.util.concurrent.FluentFuture;
 import java.util.List;
 import java.util.Map;
 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
-import org.opendaylight.mdsal.dom.api.DOMRpcResult;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 
 final class GlobalDOMRpcRoutingTableEntry extends AbstractDOMRpcRoutingTableEntry {
-    private final DOMRpcIdentifier rpcId;
 
     private GlobalDOMRpcRoutingTableEntry(final DOMRpcIdentifier rpcId, final Map<YangInstanceIdentifier,
             List<DOMRpcImplementation>> impls) {
-        super(rpcId.getType(), impls);
-        this.rpcId = Preconditions.checkNotNull(rpcId);
+        super(rpcId, impls);
     }
 
     // We do not need the RpcDefinition, but this makes sure we do not
     // forward something we don't know to be an RPC.
     GlobalDOMRpcRoutingTableEntry(final RpcDefinition def, final Map<YangInstanceIdentifier,
             List<DOMRpcImplementation>> impls) {
-        super(def.getPath(), impls);
-        this.rpcId = DOMRpcIdentifier.create(def.getPath());
-    }
-
-    @Override
-    protected FluentFuture<DOMRpcResult> invokeRpc(final NormalizedNode<?, ?> input) {
-        return getImplementations(YangInstanceIdentifier.EMPTY).get(0).invokeRpc(rpcId, input);
+        super(DOMRpcIdentifier.create(def.getPath()), impls);
     }
 
     @Override
     protected GlobalDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier,
             List<DOMRpcImplementation>> impls) {
-        return new GlobalDOMRpcRoutingTableEntry(rpcId, impls);
+        return new GlobalDOMRpcRoutingTableEntry(getRpcId(), impls);
     }
 }
index 804f36926b0ca42315bbff4cf9bcfd70ab02cdb3..6079b724fb21a2863e271e102ff4bdd4f2e56d30 100644 (file)
@@ -7,87 +7,28 @@
  */
 package org.opendaylight.mdsal.dom.broker;
 
-import com.google.common.base.Preconditions;
-import com.google.common.util.concurrent.FluentFuture;
 import java.util.List;
 import java.util.Map;
-import java.util.Optional;
 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
-import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
-import org.opendaylight.mdsal.dom.api.DOMRpcResult;
-import org.opendaylight.yangtools.util.concurrent.FluentFutures;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 final class RoutedDOMRpcRoutingTableEntry extends AbstractDOMRpcRoutingTableEntry {
-    private static final Logger LOG = LoggerFactory.getLogger(RoutedDOMRpcRoutingTableEntry.class);
-    private final DOMRpcIdentifier globalRpcId;
-    private final YangInstanceIdentifier keyId;
 
-    private RoutedDOMRpcRoutingTableEntry(final DOMRpcIdentifier globalRpcId,
-            final YangInstanceIdentifier keyId,
+    private RoutedDOMRpcRoutingTableEntry(final DOMRpcIdentifier routedRpcId,
             final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
-        super(globalRpcId.getType(), impls);
-        this.keyId = Preconditions.checkNotNull(keyId);
-        this.globalRpcId = Preconditions.checkNotNull(globalRpcId);
+        super(routedRpcId, impls);
     }
 
     RoutedDOMRpcRoutingTableEntry(final RpcDefinition def, final YangInstanceIdentifier keyId,
             final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
-        super(def.getPath(), impls);
-        this.keyId = Preconditions.checkNotNull(keyId);
-        this.globalRpcId = DOMRpcIdentifier.create(def.getPath());
-    }
-
-    @Override
-    protected FluentFuture<DOMRpcResult> invokeRpc(final NormalizedNode<?, ?> input) {
-        final Optional<NormalizedNode<?, ?>> maybeKey = NormalizedNodes.findNode(input, keyId);
-
-        // Routing key is present, attempt to deliver as a routed RPC
-        if (maybeKey.isPresent()) {
-            final NormalizedNode<?, ?> key = maybeKey.get();
-            final Object value = key.getValue();
-            if (value instanceof YangInstanceIdentifier) {
-                final YangInstanceIdentifier iid = (YangInstanceIdentifier) value;
-
-                // Find a DOMRpcImplementation for a specific iid
-                final List<DOMRpcImplementation> specificImpls = getImplementations(iid);
-                if (specificImpls != null) {
-                    return specificImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
-                }
-
-                LOG.debug("No implementation for context {} found will now look for wildcard id", iid);
-
-                // Find a DOMRpcImplementation for a wild card. Usually remote-rpc-connector would register an
-                // implementation this way
-                final List<DOMRpcImplementation> mayBeRemoteImpls = getImplementations(YangInstanceIdentifier.EMPTY);
-
-                if (mayBeRemoteImpls != null) {
-                    return mayBeRemoteImpls.get(0).invokeRpc(DOMRpcIdentifier.create(getSchemaPath(), iid), input);
-                }
-
-            } else {
-                LOG.warn("Ignoring wrong context value {}", value);
-            }
-        }
-
-        final List<DOMRpcImplementation> impls = getImplementations(null);
-        if (impls != null) {
-            return impls.get(0).invokeRpc(globalRpcId, input);
-        }
-
-        return FluentFutures.immediateFailedFluentFuture(
-            new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", getSchemaPath()));
+        super(DOMRpcIdentifier.create(def.getPath(), keyId), impls);
     }
 
     @Override
     protected RoutedDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier,
             List<DOMRpcImplementation>> impls) {
-        return new RoutedDOMRpcRoutingTableEntry(globalRpcId, keyId, impls);
+        return new RoutedDOMRpcRoutingTableEntry(getRpcId(), impls);
     }
 }
index d4a085a3e3ce1a933e337b93d1430e56dcb68e12..e830f7a91e739bcc1eb254ff35e1336a681d4919 100644 (file)
@@ -10,12 +10,12 @@ package org.opendaylight.mdsal.dom.broker;
 import com.google.common.util.concurrent.FluentFuture;
 import java.util.List;
 import java.util.Map;
+import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 final class UnknownDOMRpcRoutingTableEntry extends AbstractDOMRpcRoutingTableEntry {
@@ -23,16 +23,11 @@ final class UnknownDOMRpcRoutingTableEntry extends AbstractDOMRpcRoutingTableEnt
 
     UnknownDOMRpcRoutingTableEntry(final SchemaPath schemaPath, final Map<YangInstanceIdentifier,
             List<DOMRpcImplementation>> impls) {
-        super(schemaPath, impls);
+        super(DOMRpcIdentifier.create(schemaPath), impls);
         unknownRpc = FluentFutures.immediateFailedFluentFuture(
             new DOMRpcImplementationNotAvailableException("SchemaPath %s is not resolved to an RPC", schemaPath));
     }
 
-    @Override
-    protected FluentFuture<DOMRpcResult> invokeRpc(final NormalizedNode<?, ?> input) {
-        return unknownRpc;
-    }
-
     @Override
     protected UnknownDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier,
             List<DOMRpcImplementation>> impls) {
index 4f1ecfc691663205b7742b21ba588c5a2b877404..83e881e91fea7fb2ff257091577b061935719800 100644 (file)
@@ -23,6 +23,7 @@ import java.util.concurrent.TimeoutException;
 import org.junit.Test;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
+import org.opendaylight.mdsal.dom.broker.DOMRpcRouter.RpcInvocation;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
@@ -49,8 +50,8 @@ public class GlobalDOMRpcRoutingTableEntryTest extends TestUtils {
                 rpcImplementation));
 
         try {
-            globalDOMRpcRoutingTableEntry.newInstance(rpcImplementations)
-                    .invokeRpc(TEST_CONTAINER).get(5, TimeUnit.SECONDS);
+            RpcInvocation.invoke(globalDOMRpcRoutingTableEntry.newInstance(rpcImplementations),
+                    TEST_CONTAINER).get(5, TimeUnit.SECONDS);
             fail("Expected DOMRpcImplementationNotAvailableException");
         } catch (ExecutionException e) {
             assertTrue(e.getCause() instanceof DOMRpcImplementationNotAvailableException);
index 190d821b784c4a0d3d0ca2016b02fc09efbfca4a..a67908dda1ae2abcc5567c2d3c7d52d7a04dbdb2 100644 (file)
@@ -18,6 +18,7 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeoutException;
 import org.junit.Test;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
+import org.opendaylight.mdsal.dom.broker.DOMRpcRouter.RpcInvocation;
 import org.opendaylight.mdsal.dom.broker.util.TestModel;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
@@ -35,7 +36,7 @@ public class RoutedDOMRpcRoutingTableEntryTest extends TestUtils {
         assertNotNull(routedDOMRpcRoutingTableEntry.newInstance(new HashMap<>()));
 
         try {
-            routedDOMRpcRoutingTableEntry.invokeRpc(TEST_CHILD).get();
+            RpcInvocation.invoke(routedDOMRpcRoutingTableEntry, TEST_CHILD).get();
             fail("Expected DOMRpcImplementationNotAvailableException");
         } catch (ExecutionException e) {
             assertTrue(e.getCause() instanceof DOMRpcImplementationNotAvailableException);
index 50d81bfdb7d6993541e1f1b43648af2c23bb586a..319841a4cf0d37ea0efb37c41efa6ec2539a22ad 100644 (file)
@@ -17,6 +17,7 @@ import java.util.List;
 import java.util.Map;
 import org.junit.Test;
 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
+import org.opendaylight.mdsal.dom.broker.DOMRpcRouter.RpcInvocation;
 import org.opendaylight.mdsal.dom.broker.util.TestModel;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
@@ -37,7 +38,7 @@ public class UnknownDOMRpcRoutingTableEntryTest extends TestUtils {
 
         assertNotNull(test);
         assertNotNull(test.newInstance(emptyImpls));
-        assertNotNull(test.invokeRpc(TEST_CONTAINER));
+        assertNotNull(RpcInvocation.invoke(test, TEST_CONTAINER));
         assertNotNull(test.getImplementations());
         assertEquals(test.getImplementations(YangInstanceIdentifier.EMPTY), TEST_LIST);
         assertTrue(test.containsContext(YangInstanceIdentifier.EMPTY));