Bump versions to 4.0.0-SNAPSHOT
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / PrefixShardHandler.java
index 8f711b3337c9fbdfc8087aa138df3d5a2712d007..58666ecd0a9769b09bdd22b3bbab177c4089d9b4 100644 (file)
@@ -5,31 +5,28 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.controller.clustering.it.provider.impl;
 
 package org.opendaylight.controller.clustering.it.provider.impl;
 
-import static org.opendaylight.controller.clustering.it.provider.impl.ProduceTransactionsHandler.ID;
-import static org.opendaylight.controller.clustering.it.provider.impl.ProduceTransactionsHandler.ID_INT;
-import static org.opendaylight.controller.clustering.it.provider.impl.ProduceTransactionsHandler.ID_INTS;
-import static org.opendaylight.controller.clustering.it.provider.impl.ProduceTransactionsHandler.ITEM;
+import static org.opendaylight.controller.clustering.it.provider.impl.AbstractTransactionHandler.ID;
+import static org.opendaylight.controller.clustering.it.provider.impl.AbstractTransactionHandler.ID_INT;
+import static org.opendaylight.controller.clustering.it.provider.impl.AbstractTransactionHandler.ID_INTS;
+import static org.opendaylight.controller.clustering.it.provider.impl.AbstractTransactionHandler.ITEM;
 
 
-import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
 import com.google.common.util.concurrent.SettableFuture;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.CompletionStage;
 import java.util.stream.Collectors;
 import com.google.common.util.concurrent.SettableFuture;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.CompletionStage;
 import java.util.stream.Collectors;
-import javax.annotation.Nullable;
 import org.opendaylight.controller.cluster.access.concepts.MemberName;
 import org.opendaylight.controller.cluster.sharding.DistributedShardFactory;
 import org.opendaylight.controller.cluster.access.concepts.MemberName;
 import org.opendaylight.controller.cluster.sharding.DistributedShardFactory;
-import org.opendaylight.controller.cluster.sharding.DistributedShardFactory.DistributedShardRegistration;
+import org.opendaylight.controller.cluster.sharding.DistributedShardRegistration;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
-import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
@@ -38,8 +35,11 @@ import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.CreatePrefixShardInput;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.CreatePrefixShardInput;
-import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.ProduceTransactionsOutput;
+import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.CreatePrefixShardOutput;
+import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.CreatePrefixShardOutputBuilder;
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RemovePrefixShardInput;
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RemovePrefixShardInput;
+import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RemovePrefixShardOutput;
+import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.RemovePrefixShardOutputBuilder;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
@@ -53,6 +53,7 @@ import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableCo
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@Deprecated(forRemoval = true)
 public class PrefixShardHandler {
 
     private static final Logger LOG = LoggerFactory.getLogger(PrefixShardHandler.class);
 public class PrefixShardHandler {
 
     private static final Logger LOG = LoggerFactory.getLogger(PrefixShardHandler.class);
@@ -75,15 +76,16 @@ public class PrefixShardHandler {
         this.serializer = serializer;
     }
 
         this.serializer = serializer;
     }
 
-    public ListenableFuture<RpcResult<Void>> onCreatePrefixShard(final CreatePrefixShardInput input) {
+    public ListenableFuture<RpcResult<CreatePrefixShardOutput>> onCreatePrefixShard(
+            final CreatePrefixShardInput input) {
 
 
-        final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
+        final SettableFuture<RpcResult<CreatePrefixShardOutput>> future = SettableFuture.create();
 
         final CompletionStage<DistributedShardRegistration> completionStage;
         final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
 
         try {
 
         final CompletionStage<DistributedShardRegistration> completionStage;
         final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
 
         try {
-             completionStage = shardFactory.createDistributedShard(
+            completionStage = shardFactory.createDistributedShard(
                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, identifier),
                     input.getReplicas().stream().map(MemberName::forName).collect(Collectors.toList()));
 
                     new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, identifier),
                     input.getReplicas().stream().map(MemberName::forName).collect(Collectors.toList()));
 
@@ -91,31 +93,30 @@ public class PrefixShardHandler {
                 LOG.debug("Shard[{}] created successfully.", identifier);
                 registrations.put(identifier, registration);
 
                 LOG.debug("Shard[{}] created successfully.", identifier);
                 registrations.put(identifier, registration);
 
-                final CheckedFuture<Void, TransactionCommitFailedException> ensureFuture = ensureListExists();
-                Futures.addCallback(ensureFuture, new FutureCallback<Void>() {
+                final ListenableFuture<?> ensureFuture = ensureListExists();
+                Futures.addCallback(ensureFuture, new FutureCallback<Object>() {
                     @Override
                     @Override
-                    public void onSuccess(@Nullable Void result) {
+                    public void onSuccess(final Object result) {
                         LOG.debug("Initial list write successful.");
                         LOG.debug("Initial list write successful.");
-                        future.set(RpcResultBuilder.<Void>success().build());
+                        future.set(RpcResultBuilder.success(new CreatePrefixShardOutputBuilder().build()).build());
                     }
 
                     @Override
                     }
 
                     @Override
-                    public void onFailure(Throwable throwable) {
+                    public void onFailure(final Throwable throwable) {
                         LOG.warn("Shard[{}] creation failed:", identifier, throwable);
 
                         LOG.warn("Shard[{}] creation failed:", identifier, throwable);
 
-                        final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
-                                "Shard creation failed", "cluster-test-app", "", throwable);
-                        future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
+                        final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION,
+                                "create-shard-failed", "Shard creation failed", "cluster-test-app", "", throwable);
+                        future.set(RpcResultBuilder.<CreatePrefixShardOutput>failed().withRpcError(error).build());
                     }
                     }
-                });
-
+                }, MoreExecutors.directExecutor());
             });
             completionStage.exceptionally(throwable -> {
                 LOG.warn("Shard[{}] creation failed:", identifier, throwable);
 
                 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
                         "Shard creation failed", "cluster-test-app", "", throwable);
             });
             completionStage.exceptionally(throwable -> {
                 LOG.warn("Shard[{}] creation failed:", identifier, throwable);
 
                 final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
                         "Shard creation failed", "cluster-test-app", "", throwable);
-                future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
+                future.set(RpcResultBuilder.<CreatePrefixShardOutput>failed().withRpcError(error).build());
                 return null;
             });
         } catch (final DOMDataTreeShardingConflictException e) {
                 return null;
             });
         } catch (final DOMDataTreeShardingConflictException e) {
@@ -123,13 +124,14 @@ public class PrefixShardHandler {
 
             final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
                     "Sharding conflict", "cluster-test-app", "", e);
 
             final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "create-shard-failed",
                     "Sharding conflict", "cluster-test-app", "", e);
-            future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
+            future.set(RpcResultBuilder.<CreatePrefixShardOutput>failed().withRpcError(error).build());
         }
 
         return future;
     }
 
         }
 
         return future;
     }
 
-    public ListenableFuture<RpcResult<Void>> onRemovePrefixShard(final RemovePrefixShardInput input) {
+    public ListenableFuture<RpcResult<RemovePrefixShardOutput>> onRemovePrefixShard(
+            final RemovePrefixShardInput input) {
 
         final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
         final DistributedShardRegistration registration = registrations.get(identifier);
 
         final YangInstanceIdentifier identifier = serializer.toYangInstanceIdentifier(input.getPrefix());
         final DistributedShardRegistration registration = registrations.get(identifier);
@@ -137,26 +139,27 @@ public class PrefixShardHandler {
         if (registration == null) {
             final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "registration-missing",
                     "No shard registered at this prefix.");
         if (registration == null) {
             final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "registration-missing",
                     "No shard registered at this prefix.");
-            return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
+            return Futures.immediateFuture(RpcResultBuilder.<RemovePrefixShardOutput>failed().withRpcError(error)
+                .build());
         }
 
         }
 
-        final SettableFuture<RpcResult<Void>> future = SettableFuture.create();
+        final SettableFuture<RpcResult<RemovePrefixShardOutput>> future = SettableFuture.create();
 
         final CompletionStage<Void> close = registration.close();
 
         final CompletionStage<Void> close = registration.close();
-        close.thenRun(() -> future.set(RpcResultBuilder.<Void>success().build()));
+        close.thenRun(() -> future.set(RpcResultBuilder.success(new RemovePrefixShardOutputBuilder().build()).build()));
         close.exceptionally(throwable -> {
             LOG.warn("Shard[{}] removal failed:", identifier, throwable);
 
             final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "remove-shard-failed",
                     "Shard removal failed", "cluster-test-app", "", throwable);
         close.exceptionally(throwable -> {
             LOG.warn("Shard[{}] removal failed:", identifier, throwable);
 
             final RpcError error = RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "remove-shard-failed",
                     "Shard removal failed", "cluster-test-app", "", throwable);
-            future.set(RpcResultBuilder.<Void>failed().withRpcError(error).build());
+            future.set(RpcResultBuilder.<RemovePrefixShardOutput>failed().withRpcError(error).build());
             return null;
         });
 
         return future;
     }
 
             return null;
         });
 
         return future;
     }
 
-    private CheckedFuture<Void, TransactionCommitFailedException> ensureListExists() {
+    private ListenableFuture<?> ensureListExists() {
 
         final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = ImmutableNodes.mapNodeBuilder(ID_INT);
 
 
         final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = ImmutableNodes.mapNodeBuilder(ID_INT);
 
@@ -175,21 +178,21 @@ public class PrefixShardHandler {
                 .build();
 
         final DOMDataTreeProducer producer = domDataTreeService.createProducer(Collections.singleton(
                 .build();
 
         final DOMDataTreeProducer producer = domDataTreeService.createProducer(Collections.singleton(
-                new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY)));
+                new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty())));
 
         final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
 
         final DOMDataTreeWriteCursor cursor =
                 tx.createCursor(new DOMDataTreeIdentifier(
 
         final DOMDataTreeCursorAwareTransaction tx = producer.createTransaction(false);
 
         final DOMDataTreeWriteCursor cursor =
                 tx.createCursor(new DOMDataTreeIdentifier(
-                        LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY));
+                        LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty()));
 
         cursor.merge(containerNode.getIdentifier(), containerNode);
         cursor.close();
 
 
         cursor.merge(containerNode.getIdentifier(), containerNode);
         cursor.close();
 
-        final CheckedFuture<Void, TransactionCommitFailedException> future = tx.submit();
-        Futures.addCallback(future, new FutureCallback<Void>() {
+        final ListenableFuture<?> future = tx.commit();
+        Futures.addCallback(future, new FutureCallback<Object>() {
             @Override
             @Override
-            public void onSuccess(@Nullable Void result) {
+            public void onSuccess(final Object result) {
                 try {
                     LOG.debug("Closing producer for initial list.");
                     producer.close();
                 try {
                     LOG.debug("Closing producer for initial list.");
                     producer.close();
@@ -199,10 +202,10 @@ public class PrefixShardHandler {
             }
 
             @Override
             }
 
             @Override
-            public void onFailure(Throwable throwable) {
+            public void onFailure(final Throwable throwable) {
                 //NOOP handled by the caller of this method.
             }
                 //NOOP handled by the caller of this method.
             }
-        });
+        }, MoreExecutors.directExecutor());
         return future;
     }
 }
         return future;
     }
 }