Deprecated controller fine-grained sharding APIs
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / AbstractDOMRpcRoutingTableEntry.java
index 09c4f4fe6d53afd4eb554a45c70100fa0fb3acb5..b346c4e09343cf173ded273a106c874db421d68d 100644 (file)
@@ -10,12 +10,15 @@ package org.opendaylight.controller.md.sal.dom.broker.impl;
 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.CheckedFuture;
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
@@ -27,16 +30,17 @@ abstract class AbstractDOMRpcRoutingTableEntry {
     private final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls;
     private final SchemaPath schemaPath;
 
-    protected AbstractDOMRpcRoutingTableEntry(final SchemaPath schemaPath, final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
+    AbstractDOMRpcRoutingTableEntry(final SchemaPath schemaPath,
+        final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
         this.schemaPath = Preconditions.checkNotNull(schemaPath);
         this.impls = Preconditions.checkNotNull(impls);
     }
 
-    protected final SchemaPath getSchemaPath() {
+    final SchemaPath getSchemaPath() {
         return schemaPath;
     }
 
-    protected final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
+    final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
         return impls.get(context);
     }
 
@@ -44,35 +48,52 @@ abstract class AbstractDOMRpcRoutingTableEntry {
         return impls;
     }
 
-    public boolean containsContext(final YangInstanceIdentifier contextReference) {
+    final boolean containsContext(final YangInstanceIdentifier contextReference) {
         return impls.containsKey(contextReference);
     }
 
-    final Set<YangInstanceIdentifier> registeredIdentifiers() {
-        return impls.keySet();
+    final Set<YangInstanceIdentifier> registeredIdentifiers(final DOMRpcAvailabilityListener l) {
+        return Maps.filterValues(impls, list -> list.stream().anyMatch(l::acceptsImplementation)).keySet();
     }
 
-    final AbstractDOMRpcRoutingTableEntry add(final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> newRpcs) {
+    /**
+     *
+     * @param implementation
+     * @param newRpcs List of new RPCs, must be mutable
+     * @return
+     */
+    final AbstractDOMRpcRoutingTableEntry add(final DOMRpcImplementation implementation,
+            final List<YangInstanceIdentifier> newRpcs) {
         final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
-        for (Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
+        for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
             if (newRpcs.remove(ve.getKey())) {
-                final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
+                final List<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
                 i.addAll(ve.getValue());
                 i.add(implementation);
+
+                // New implementation is at the end, this will move it to be the last among implementations
+                // with equal cost -- relying on sort() being stable.
+                i.sort(Comparator.comparingLong(DOMRpcImplementation::invocationCost));
                 vb.put(ve.getKey(), i);
             } else {
                 vb.put(ve);
             }
         }
+        for(final YangInstanceIdentifier ii : newRpcs) {
+            final List<DOMRpcImplementation> impl = new ArrayList<>(1);
+            impl.add(implementation);
+            vb.put(ii, impl);
+        }
 
         return newInstance(vb.build());
     }
 
-    final AbstractDOMRpcRoutingTableEntry remove(final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> removed) {
+    final AbstractDOMRpcRoutingTableEntry remove(final DOMRpcImplementation implementation,
+            final List<YangInstanceIdentifier> removed) {
         final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
-        for (Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
+        for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
             if (removed.remove(ve.getKey())) {
-                final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
+                final List<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
                 i.remove(implementation);
                 // We could trimToSize(), but that may perform another copy just to get rid
                 // of a single element. That is probably not worth the trouble.