From: Robert Varga Date: Mon, 28 Oct 2019 17:07:38 +0000 (+0100) Subject: Turn ActionableResource into an abstract class X-Git-Tag: release/aluminium~19 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=1a76721e11f313a1cb414709e9ef007160d878bc;p=genius.git Turn ActionableResource into an abstract class This prevents outside instantiation of the interface, so that we can hide the details of dispatch from outside world, making it an implementation detail. JIRA: GENIUS-281 Change-Id: I5fa6208515501bc7f8f915544da69ac47cd3a8cc Signed-off-by: Robert Varga --- diff --git a/interfacemanager/interfacemanager-impl/src/main/java/org/opendaylight/genius/interfacemanager/renderer/ovs/utilities/BatchingUtils.java b/interfacemanager/interfacemanager-impl/src/main/java/org/opendaylight/genius/interfacemanager/renderer/ovs/utilities/BatchingUtils.java index 7c30d43ab..168a60091 100644 --- a/interfacemanager/interfacemanager-impl/src/main/java/org/opendaylight/genius/interfacemanager/renderer/ovs/utilities/BatchingUtils.java +++ b/interfacemanager/interfacemanager-impl/src/main/java/org/opendaylight/genius/interfacemanager/renderer/ovs/utilities/BatchingUtils.java @@ -37,9 +37,9 @@ public class BatchingUtils implements AutoCloseable { private static final int DEFAULT_BATCH_SIZE = 1000; private static final int DEFAULT_BATCH_INTERVAL = 500; - private final BlockingQueue topologyConfigShardBufferQ = new LinkedBlockingQueue<>(); - private final BlockingQueue defaultConfigShardBufferQ = new LinkedBlockingQueue<>(); - private final BlockingQueue defaultOperationalShardBufferQ = new LinkedBlockingQueue<>(); + private final BlockingQueue> topologyConfigShardBufferQ = new LinkedBlockingQueue<>(); + private final BlockingQueue> defaultConfigShardBufferQ = new LinkedBlockingQueue<>(); + private final BlockingQueue> defaultOperationalShardBufferQ = new LinkedBlockingQueue<>(); private final DataBroker dataBroker; private final ResourceBatchingManager resourceBatchingManager = ResourceBatchingManager.getInstance(); @@ -78,7 +78,7 @@ public class BatchingUtils implements AutoCloseable { getQueue(entityType).add(ActionableResources.create(path, data)); } - public BlockingQueue getQueue(EntityType entityType) { + public BlockingQueue> getQueue(EntityType entityType) { switch (entityType) { case DEFAULT_CONFIG: return defaultConfigShardBufferQ; diff --git a/itm/itm-impl/src/main/java/org/opendaylight/genius/itm/impl/ITMBatchingUtils.java b/itm/itm-impl/src/main/java/org/opendaylight/genius/itm/impl/ITMBatchingUtils.java index fb8069609..af9ae3bcc 100644 --- a/itm/itm-impl/src/main/java/org/opendaylight/genius/itm/impl/ITMBatchingUtils.java +++ b/itm/itm-impl/src/main/java/org/opendaylight/genius/itm/impl/ITMBatchingUtils.java @@ -26,10 +26,12 @@ public final class ITMBatchingUtils { private static final Logger LOG = LoggerFactory.getLogger(ITMBatchingUtils.class); - private static final BlockingQueue DEFAULT_OPERATIONAL_SHARD_BUFFER_Q - = new LinkedBlockingQueue<>(); - private static final BlockingQueue DEFAULT_CONFIG_SHARD_BUFFER_Q = new LinkedBlockingQueue<>(); - private static final BlockingQueue TOPOLOGY_CONFIG_SHARD_BUFFER_Q = new LinkedBlockingQueue<>(); + private static final BlockingQueue> DEFAULT_OPERATIONAL_SHARD_BUFFER_Q = + new LinkedBlockingQueue<>(); + private static final BlockingQueue> DEFAULT_CONFIG_SHARD_BUFFER_Q = + new LinkedBlockingQueue<>(); + private static final BlockingQueue> TOPOLOGY_CONFIG_SHARD_BUFFER_Q = + new LinkedBlockingQueue<>(); private static DataBroker dataBroker; @@ -84,7 +86,7 @@ public final class ITMBatchingUtils { } @NonNull - public static BlockingQueue getQueue(EntityType entityType) { + public static BlockingQueue> getQueue(EntityType entityType) { switch (entityType) { case DEFAULT_OPERATIONAL: return DEFAULT_OPERATIONAL_SHARD_BUFFER_Q; diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ActionableResource.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ActionableResource.java index 965df4a6b..fb962d038 100644 --- a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ActionableResource.java +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ActionableResource.java @@ -7,25 +7,42 @@ */ package org.opendaylight.genius.utils.batching; +import static java.util.Objects.requireNonNull; + import com.google.common.util.concurrent.ListenableFuture; +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -public interface ActionableResource { +public abstract class ActionableResource { - short CREATE = 1; - short UPDATE = 2; - short DELETE = 3; - short READ = 4; + static final short CREATE = 1; + static final short UPDATE = 2; + static final short DELETE = 3; + static final short READ = 4; // MDSAL-534 Merge,Put with no create_missing_parents flag - short UPDATECONTAINER = 5; + static final short UPDATECONTAINER = 5; + + private final InstanceIdentifier path; + private final short action; + + // Hidden to prevent subclassing outside of this package + ActionableResource(final InstanceIdentifier path, final short action) { + this.path = requireNonNull(path); + this.action = action; + } - InstanceIdentifier getInstanceIdentifier(); + final short getAction() { + return action; + } - Object getInstance(); + final @NonNull InstanceIdentifier getInstanceIdentifier() { + return path; + } - Object getOldInstance(); + abstract Object getInstance(); - short getAction(); + abstract Object getOldInstance(); - ListenableFuture getResultFuture(); + abstract ListenableFuture getResultFuture(); } diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ActionableResourceImpl.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ActionableResourceImpl.java index 9bb7cbe26..d709e3881 100644 --- a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ActionableResourceImpl.java +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ActionableResourceImpl.java @@ -12,60 +12,47 @@ import static java.util.Objects.requireNonNull; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.SettableFuture; import org.opendaylight.yangtools.concepts.Identifier; +import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -class ActionableResourceImpl implements ActionableResource { +class ActionableResourceImpl extends ActionableResource { + private final SettableFuture future = SettableFuture.create(); private final Object instance; private final Object oldInstance; private final Object key; - private final InstanceIdentifier identifier; - private final short action; - private final SettableFuture future = SettableFuture.create(); - ActionableResourceImpl(InstanceIdentifier identifier, short action, Object updatedData, Object oldData) { + ActionableResourceImpl(InstanceIdentifier path, short action, Object updatedData, Object oldData) { + super(path, action); this.key = null; - this.action = action; - this.identifier = requireNonNull(identifier); this.instance = updatedData; this.oldInstance = oldData; } - ActionableResourceImpl(Identifier key, InstanceIdentifier identifier, short action, Object updatedData, + ActionableResourceImpl(Identifier key, InstanceIdentifier path, short action, Object updatedData, Object oldData) { + super(path, action); this.key = requireNonNull(key); - this.action = action; - this.identifier = requireNonNull(identifier); this.instance = updatedData; this.oldInstance = oldData; } @Override - public Object getInstance() { + final Object getInstance() { return this.instance; } @Override - public Object getOldInstance() { + final Object getOldInstance() { return this.oldInstance; } @Override - public InstanceIdentifier getInstanceIdentifier() { - return this.identifier; - } - - @Override - public short getAction() { - return action; - } - - @Override - public ListenableFuture getResultFuture() { + final ListenableFuture getResultFuture() { return future; } @Override - public String toString() { - return key != null ? key.toString() : identifier.toString(); + public final String toString() { + return key != null ? key.toString() : getInstanceIdentifier().toString(); } } diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ActionableResources.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ActionableResources.java index bd6fa7988..23d5308a8 100644 --- a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ActionableResources.java +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ActionableResources.java @@ -22,39 +22,39 @@ public final class ActionableResources { } - public static @NonNull ActionableResource create(final InstanceIdentifier path, + public static @NonNull ActionableResource create(final InstanceIdentifier path, final T data) { - return new ActionableResourceImpl(path, ActionableResource.CREATE, requireNonNull(data), null); + return new ActionableResourceImpl<>(path, ActionableResource.CREATE, requireNonNull(data), null); } - public static @NonNull ActionableResource create(final Identifier identifier, + public static @NonNull ActionableResource create(final Identifier identifier, final InstanceIdentifier path, final T data) { - return new ActionableResourceImpl(identifier, path, ActionableResource.CREATE, requireNonNull(data), null); + return new ActionableResourceImpl<>(identifier, path, ActionableResource.CREATE, requireNonNull(data), null); } - public static @NonNull ActionableResource update(final InstanceIdentifier path, + public static @NonNull ActionableResource update(final InstanceIdentifier path, final T newData) { - return new ActionableResourceImpl(path, ActionableResource.UPDATE, requireNonNull(newData), null); + return new ActionableResourceImpl<>(path, ActionableResource.UPDATE, requireNonNull(newData), null); } - public static @NonNull ActionableResource update(final Identifier identifier, + public static @NonNull ActionableResource update(final Identifier identifier, final InstanceIdentifier path, final T newData, final T oldData) { - return new ActionableResourceImpl(identifier, path, ActionableResource.UPDATE, requireNonNull(newData), + return new ActionableResourceImpl<>(identifier, path, ActionableResource.UPDATE, requireNonNull(newData), oldData); } - public static @NonNull ActionableResource delete(final InstanceIdentifier path) { - return new ActionableResourceImpl(path, ActionableResource.DELETE, null, null); + public static @NonNull ActionableResource delete(final InstanceIdentifier path) { + return new ActionableResourceImpl<>(path, ActionableResource.DELETE, null, null); } - public static @NonNull ActionableResource delete(final Identifier identifier, + public static @NonNull ActionableResource delete(final Identifier identifier, final InstanceIdentifier path, final T data) { - return new ActionableResourceImpl(identifier, path, ActionableResource.DELETE, data, null); + return new ActionableResourceImpl<>(identifier, path, ActionableResource.DELETE, data, null); } - public static @NonNull ActionableResource updateContainer(final + public static @NonNull ActionableResource updateContainer(final InstanceIdentifier path, final T newData) { - return new ActionableResourceImpl(path, ActionableResource.UPDATECONTAINER, requireNonNull(newData), + return new ActionableResourceImpl<>(path, ActionableResource.UPDATECONTAINER, requireNonNull(newData), null); } } diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ResourceBatchingManager.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ResourceBatchingManager.java index b0969ebab..962ffed1f 100644 --- a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ResourceBatchingManager.java +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/batching/ResourceBatchingManager.java @@ -68,7 +68,7 @@ public class ResourceBatchingManager implements AutoCloseable { CONFIG_INVENTORY(LogicalDatastoreType.CONFIGURATION), OPERATIONAL_INVENTORY(LogicalDatastoreType.OPERATIONAL); - BlockingQueue queue = new LinkedBlockingQueue<>(); + BlockingQueue> queue = new LinkedBlockingQueue<>(); LogicalDatastoreType datastoreType; ShardResource(LogicalDatastoreType datastoreType) { @@ -79,12 +79,12 @@ public class ResourceBatchingManager implements AutoCloseable { return datastoreType; } - BlockingQueue getQueue() { + BlockingQueue> getQueue() { return queue; } } - private final ConcurrentHashMap, ResourceHandler>> + private final ConcurrentHashMap>, ResourceHandler>> resourceHandlerMapper = new ConcurrentHashMap<>(); private final ConcurrentHashMap @@ -108,8 +108,8 @@ public class ResourceBatchingManager implements AutoCloseable { resourceBatchingThreadMapper.values().forEach(ScheduledExecutorService::shutdown); } - public void registerBatchableResource( - String resourceType, final BlockingQueue resQueue, final ResourceHandler resHandler) { + public void registerBatchableResource(final String resourceType, + final BlockingQueue> resQueue, final ResourceHandler resHandler) { Preconditions.checkNotNull(resQueue, "ResourceQueue to use for batching cannot not be null."); Preconditions.checkNotNull(resHandler, "ResourceHandler cannot not be null."); @@ -160,7 +160,7 @@ public class ResourceBatchingManager implements AutoCloseable { */ public FluentFuture> read( String resourceType, InstanceIdentifier identifier) throws InterruptedException, ExecutionException { - BlockingQueue queue = getQueue(resourceType); + BlockingQueue> queue = getQueue(resourceType); if (queue != null) { if (pendingModificationByResourceType.get(resourceType).contains(identifier)) { SettableFuture> readFuture = SettableFuture.create(); @@ -180,10 +180,10 @@ public class ResourceBatchingManager implements AutoCloseable { public ListenableFuture merge(ShardResource shardResource, InstanceIdentifier identifier, DataObject updatedData) { - BlockingQueue queue = shardResource.getQueue(); + BlockingQueue> queue = shardResource.getQueue(); if (queue != null) { beforeModification(shardResource.name(), identifier); - ActionableResource actResource = new ActionableResourceImpl( + ActionableResource actResource = new ActionableResourceImpl<>( identifier, ActionableResource.UPDATE, updatedData, null/*oldData*/); queue.add(actResource); return actResource.getResultFuture(); @@ -194,20 +194,20 @@ public class ResourceBatchingManager implements AutoCloseable { } public void merge(String resourceType, InstanceIdentifier identifier, DataObject updatedData) { - BlockingQueue queue = getQueue(resourceType); + BlockingQueue> queue = getQueue(resourceType); if (queue != null) { beforeModification(resourceType, identifier); - ActionableResource actResource = new ActionableResourceImpl( + ActionableResource actResource = new ActionableResourceImpl<>( identifier, ActionableResource.UPDATE, updatedData, null/*oldData*/); queue.add(actResource); } } public ListenableFuture delete(ShardResource shardResource, InstanceIdentifier identifier) { - BlockingQueue queue = shardResource.getQueue(); + BlockingQueue> queue = shardResource.getQueue(); if (queue != null) { beforeModification(shardResource.name(), identifier); - ActionableResource actResource = new ActionableResourceImpl( + ActionableResource actResource = new ActionableResourceImpl<>( identifier, ActionableResource.DELETE, null, null/*oldData*/); queue.add(actResource); return actResource.getResultFuture(); @@ -218,10 +218,10 @@ public class ResourceBatchingManager implements AutoCloseable { } public void delete(String resourceType, InstanceIdentifier identifier) { - BlockingQueue queue = getQueue(resourceType); + BlockingQueue> queue = getQueue(resourceType); if (queue != null) { beforeModification(resourceType, identifier); - ActionableResource actResource = new ActionableResourceImpl( + ActionableResource actResource = new ActionableResourceImpl<>( identifier, ActionableResource.DELETE, null, null/*oldData*/); queue.add(actResource); } @@ -229,10 +229,10 @@ public class ResourceBatchingManager implements AutoCloseable { public ListenableFuture put(ShardResource shardResource, InstanceIdentifier identifier, DataObject updatedData) { - BlockingQueue queue = shardResource.getQueue(); + BlockingQueue> queue = shardResource.getQueue(); if (queue != null) { beforeModification(shardResource.name(), identifier); - ActionableResource actResource = new ActionableResourceImpl( + ActionableResource actResource = new ActionableResourceImpl<>( identifier, ActionableResource.CREATE, updatedData, null/*oldData*/); queue.add(actResource); return actResource.getResultFuture(); @@ -243,16 +243,16 @@ public class ResourceBatchingManager implements AutoCloseable { } public void put(String resourceType, InstanceIdentifier identifier, DataObject updatedData) { - BlockingQueue queue = getQueue(resourceType); + BlockingQueue> queue = getQueue(resourceType); if (queue != null) { beforeModification(resourceType, identifier); - ActionableResource actResource = new ActionableResourceImpl( + ActionableResource actResource = new ActionableResourceImpl<>( identifier, ActionableResource.CREATE, updatedData, null/*oldData*/); queue.add(actResource); } } - private BlockingQueue getQueue(String resourceType) { + private BlockingQueue> getQueue(String resourceType) { if (resourceHandlerMapper.containsKey(resourceType)) { return resourceHandlerMapper.get(resourceType).getLeft(); } @@ -277,16 +277,16 @@ public class ResourceBatchingManager implements AutoCloseable { @Override public void run() { - List resList = new ArrayList<>(); + List> resList = new ArrayList<>(); try { - Pair, ResourceHandler> resMapper = + Pair>, ResourceHandler> resMapper = resourceHandlerMapper.get(resourceType); if (resMapper == null) { LOG.error("Unable to find resourceMapper for batching the ResourceType {}", resourceType); return; } - BlockingQueue resQueue = resMapper.getLeft(); + BlockingQueue> resQueue = resMapper.getLeft(); ResourceHandler resHandler = resMapper.getRight(); resList.add(resQueue.take()); resQueue.drainTo(resList); @@ -323,9 +323,9 @@ public class ResourceBatchingManager implements AutoCloseable { private class MdsalDsTask { String resourceType; - List actResourceList; + List> actResourceList; - MdsalDsTask(String resourceType, List actResourceList) { + MdsalDsTask(String resourceType, List> actResourceList) { this.resourceType = resourceType; this.actResourceList = actResourceList; } @@ -333,7 +333,7 @@ public class ResourceBatchingManager implements AutoCloseable { @SuppressWarnings("unchecked") public void process() { LOG.trace("Picked up 3 size {} of resourceType {}", actResourceList.size(), resourceType); - Pair, ResourceHandler> resMapper = + Pair>, ResourceHandler> resMapper = resourceHandlerMapper.get(resourceType); if (resMapper == null) { LOG.error("Unable to find resourceMapper for batching the ResourceType {}", resourceType); @@ -345,7 +345,7 @@ public class ResourceBatchingManager implements AutoCloseable { ReadWriteTransaction tx = broker.newReadWriteTransaction(); List transactionObjects = new ArrayList<>(); Map> txMap = new HashMap<>(); - for (ActionableResource actResource : actResourceList) { + for (ActionableResource actResource : actResourceList) { int startSize = transactionObjects.size(); switch (actResource.getAction()) { case ActionableResource.CREATE: @@ -464,7 +464,7 @@ public class ResourceBatchingManager implements AutoCloseable { } } - private static class ActionableReadResource extends ActionableResourceImpl { + private static class ActionableReadResource extends ActionableResourceImpl { private final SettableFuture> readFuture; ActionableReadResource(InstanceIdentifier identifier, SettableFuture> readFuture) { diff --git a/mdsalutil/mdsalutil-impl/src/main/java/org/opendaylight/genius/mdsalutil/internal/FlowBatchingUtils.java b/mdsalutil/mdsalutil-impl/src/main/java/org/opendaylight/genius/mdsalutil/internal/FlowBatchingUtils.java index 467658e12..e09a4b77d 100644 --- a/mdsalutil/mdsalutil-impl/src/main/java/org/opendaylight/genius/mdsalutil/internal/FlowBatchingUtils.java +++ b/mdsalutil/mdsalutil-impl/src/main/java/org/opendaylight/genius/mdsalutil/internal/FlowBatchingUtils.java @@ -17,7 +17,7 @@ import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; class FlowBatchingUtils { - private final BlockingQueue inventoryConfigShardBufferQ = new LinkedBlockingQueue<>(); + private final BlockingQueue> inventoryConfigShardBufferQ = new LinkedBlockingQueue<>(); public void registerWithBatchManager(ResourceHandler resourceHandler) { ResourceBatchingManager resBatchingManager = ResourceBatchingManager.getInstance();