BUG 7798 - Implement agent RPCs for singleton RPC registration testing 84/52184/4
authorTomas Cere <tcere@cisco.com>
Wed, 22 Feb 2017 18:06:37 +0000 (19:06 +0100)
committerTom Pantelis <tpanteli@brocade.com>
Fri, 3 Mar 2017 12:14:01 +0000 (12:14 +0000)
This implements get-singleton-constant and it's lifecycle control rpc's
used for testing.

Change-Id: If4412070aee143fdf3b05479c484c44b90a486d4
Signed-off-by: Tomas Cere <tcere@cisco.com>
opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/MdsalLowLevelTestProvider.java
opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/SingletonGetConstantService.java [new file with mode: 0644]

index f67e465..acf5397 100644 (file)
@@ -14,6 +14,7 @@ import java.util.Map;
 import java.util.concurrent.Future;
 import org.opendaylight.controller.clustering.it.provider.impl.GetConstantService;
 import org.opendaylight.controller.clustering.it.provider.impl.RoutedGetConstantService;
+import org.opendaylight.controller.clustering.it.provider.impl.SingletonGetConstantService;
 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodecFactory;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
@@ -23,6 +24,7 @@ import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
 import org.opendaylight.controller.sal.core.api.model.SchemaService;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.AddShardReplicaInput;
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.BecomeModuleLeaderInput;
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.BecomePrefixLeaderInput;
@@ -69,6 +71,7 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
             new HashMap<>();
 
     private DOMRpcImplementationRegistration<GetConstantService> globalGetConstantRegistration = null;
+    private ClusterSingletonServiceRegistration getSingletonConstantRegistration;
 
     public MdsalLowLevelTestProvider(final RpcProviderRegistry rpcRegistry,
                                      final DOMRpcProviderService domRpcService,
@@ -86,7 +89,28 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
 
     @Override
     public Future<RpcResult<Void>> unregisterSingletonConstant() {
-        return null;
+        LOG.debug("unregister-singleton-constant");
+
+        if (getSingletonConstantRegistration == null) {
+            LOG.debug("No get-singleton-constant registration present.");
+            final RpcError rpcError = RpcResultBuilder
+                    .newError(ErrorType.APPLICATION, "missing-registration", "No get-singleton-constant rpc registration present.");
+            final RpcResult<Void> result = RpcResultBuilder.<Void>failed().withRpcError(rpcError).build();
+            return Futures.immediateFuture(result);
+        }
+
+        try {
+            getSingletonConstantRegistration.close();
+            getSingletonConstantRegistration = null;
+
+            return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
+        } catch (final Exception e) {
+            LOG.debug("There was a problem closing the singleton constant service", e);
+            final RpcError rpcError = RpcResultBuilder
+                    .newError(ErrorType.APPLICATION, "error-closing", "There was a problem closing get-singleton-constant");
+            final RpcResult<Void> result = RpcResultBuilder.<Void>failed().withRpcError(rpcError).build();
+            return Futures.immediateFuture(result);
+        }
     }
 
     @Override
@@ -149,8 +173,20 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
     }
 
     @Override
-    public Future<RpcResult<Void>> registerSingletonConstant(RegisterSingletonConstantInput input) {
-        return null;
+    public Future<RpcResult<Void>> registerSingletonConstant(final RegisterSingletonConstantInput input) {
+
+        LOG.debug("Received register-singleton-constant rpc, input: {}", input);
+
+        if (input.getConstant() == null) {
+            final RpcError error = RpcResultBuilder.newError(
+                    ErrorType.RPC, "Invalid input.", "Constant value is null");
+            return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
+        }
+
+        getSingletonConstantRegistration =
+                SingletonGetConstantService.registerNew(singletonService, domRpcService, input.getConstant());
+
+        return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
     }
 
     @Override
diff --git a/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/SingletonGetConstantService.java b/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/SingletonGetConstantService.java
new file mode 100644 (file)
index 0000000..084f4d5
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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;
+
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
+import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
+import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SingletonGetConstantService implements DOMRpcImplementation, ClusterSingletonService {
+
+    private static final Logger LOG = LoggerFactory.getLogger(SingletonGetConstantService.class);
+
+    private static final QName OUTPUT =
+            QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15", "output");
+    private static final QName CONSTANT =
+            QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15", "constant");
+    private static final QName CONTEXT =
+            QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15", "context");
+    private static final QName GET_SINGLETON_CONSTANT =
+            QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15",
+                    "get-singleton-constant");
+
+    private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
+            ServiceGroupIdentifier.create("get-singleton-constant-service");
+
+    private final DOMRpcProviderService rpcProviderService;
+    private final String constant;
+    private DOMRpcImplementationRegistration<SingletonGetConstantService> rpcRegistration;
+
+    private SingletonGetConstantService(final DOMRpcProviderService rpcProviderService,
+                                        final String constant) {
+
+
+        this.rpcProviderService = rpcProviderService;
+        this.constant = constant;
+    }
+
+    public static ClusterSingletonServiceRegistration registerNew(final ClusterSingletonServiceProvider singletonService,
+                                                                  final DOMRpcProviderService rpcProviderService,
+                                                                  final String constant) {
+        LOG.debug("Registering get-singleton-constant into ClusterSingletonService, value {}", constant);
+
+        return singletonService
+                .registerClusterSingletonService(new SingletonGetConstantService(rpcProviderService, constant));
+    }
+
+    @Nonnull
+    @Override
+    public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull DOMRpcIdentifier rpc, @Nullable NormalizedNode<?, ?> input) {
+        LOG.debug("get-singleton-constant invoked, current value: {}", constant);
+
+        final LeafNode<Object> value = ImmutableLeafNodeBuilder.create()
+                .withNodeIdentifier(new NodeIdentifier(CONSTANT))
+                .withValue(constant)
+                .build();
+
+        final ContainerNode result = ImmutableContainerNodeBuilder.create()
+                .withNodeIdentifier(new NodeIdentifier(OUTPUT))
+                .withChild(value)
+                .build();
+
+        return Futures.immediateCheckedFuture(new DefaultDOMRpcResult(result));
+    }
+
+    @Override
+    public void instantiateServiceInstance() {
+        LOG.debug("Gained ownership of get-singleton-constant, registering service into rpcService");
+        final DOMRpcIdentifier id = DOMRpcIdentifier.create(SchemaPath.create(true, GET_SINGLETON_CONSTANT));
+
+        rpcRegistration = rpcProviderService.registerRpcImplementation(this, id);
+    }
+
+    @Override
+    public ListenableFuture<Void> closeServiceInstance() {
+        LOG.debug("Closing get-singleton-constant instance");
+        rpcRegistration.close();
+        return Futures.immediateFuture(null);
+    }
+
+    @Override
+    public ServiceGroupIdentifier getIdentifier() {
+        return SERVICE_GROUP_IDENTIFIER;
+    }
+}

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.