BUG 7797 - Implement agent RPCs for routed RPC testing 17/52117/6
authorTomas Cere <tcere@cisco.com>
Mon, 20 Feb 2017 13:22:19 +0000 (14:22 +0100)
committerTom Pantelis <tpanteli@brocade.com>
Fri, 3 Mar 2017 12:14:00 +0000 (12:14 +0000)
This implements routed get-constant and it's lifecycle
controlling rpc's.

Change-Id: I8ce04c5e62758a80ab5fc01921ad59c4373aeb3f
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/RoutedGetConstantService.java [new file with mode: 0644]
opendaylight/md-sal/samples/clustering-test-app/provider/src/main/resources/org/opendaylight/blueprint/cluster-test-app.xml

index 81f0220..f67e465 100644 (file)
@@ -9,12 +9,19 @@
 package org.opendaylight.controller.clustering.it.provider;
 
 import com.google.common.util.concurrent.Futures;
+import java.util.HashMap;
+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.md.sal.binding.impl.BindingToNormalizedNodeCodec;
+import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodecFactory;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
 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.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;
@@ -36,10 +43,14 @@ import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.l
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeYnlInput;
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeYnlOutput;
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.WriteTransactionsInput;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -49,17 +60,26 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
 
     private final RpcProviderRegistry rpcRegistry;
     private final BindingAwareBroker.RpcRegistration<OdlMdsalLowlevelControlService> registration;
+    private final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer;
+    private final SchemaService schemaService;
     private final ClusterSingletonServiceProvider singletonService;
     private final DOMRpcProviderService domRpcService;
 
+    private Map<InstanceIdentifier<?>, DOMRpcImplementationRegistration<RoutedGetConstantService>> routedRegistrations =
+            new HashMap<>();
+
     private DOMRpcImplementationRegistration<GetConstantService> globalGetConstantRegistration = null;
 
     public MdsalLowLevelTestProvider(final RpcProviderRegistry rpcRegistry,
                                      final DOMRpcProviderService domRpcService,
-                                     final ClusterSingletonServiceProvider singletonService) {
+                                     final ClusterSingletonServiceProvider singletonService,
+                                     final SchemaService schemaService,
+                                     final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
         this.rpcRegistry = rpcRegistry;
         this.domRpcService = domRpcService;
         this.singletonService = singletonService;
+        this.schemaService = schemaService;
+        this.bindingNormalizedNodeSerializer = bindingNormalizedNodeSerializer;
 
         registration = rpcRegistry.addRpcImplementation(OdlMdsalLowlevelControlService.class, this);
     }
@@ -110,8 +130,22 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
     }
 
     @Override
-    public Future<RpcResult<Void>> unregisterBoundConstant(UnregisterBoundConstantInput input) {
-        return null;
+    public Future<RpcResult<Void>> unregisterBoundConstant(final UnregisterBoundConstantInput input) {
+        LOG.debug("unregister-bound-constant, {}", input);
+
+        final DOMRpcImplementationRegistration<RoutedGetConstantService> registration =
+                routedRegistrations.remove(input.getContext());
+
+        if (registration == null) {
+            LOG.debug("No get-contexted-constant registration for context: {}", input.getContext());
+            final RpcError rpcError = RpcResultBuilder
+                    .newError(ErrorType.APPLICATION, "missing-registration", "No get-constant rpc registration present.");
+            final RpcResult<Void> result = RpcResultBuilder.<Void>failed().withRpcError(rpcError).build();
+            return Futures.immediateFuture(result);
+        }
+
+        registration.close();
+        return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
     }
 
     @Override
@@ -156,8 +190,33 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
     }
 
     @Override
-    public Future<RpcResult<Void>> registerBoundConstant(RegisterBoundConstantInput input) {
-        return null;
+    public Future<RpcResult<Void>> registerBoundConstant(final RegisterBoundConstantInput input) {
+        LOG.debug("register-bound-constant: {}", input);
+
+        if (input.getContext() == null) {
+            final RpcError error = RpcResultBuilder.newError(
+                    ErrorType.RPC, "Invalid input.", "Context value is null");
+            return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
+        }
+
+        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());
+        }
+
+        if (routedRegistrations.containsKey(input.getContext())) {
+            final RpcError error = RpcResultBuilder.newError(ErrorType.RPC, "Registration present.",
+                    "There is already a rpc registered for context: " + input.getContext());
+            return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
+        }
+
+        final DOMRpcImplementationRegistration<RoutedGetConstantService> registration =
+                RoutedGetConstantService.registerNew(bindingNormalizedNodeSerializer, domRpcService,
+                        input.getConstant(), input.getContext());
+
+        routedRegistrations.put(input.getContext(), registration);
+        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/RoutedGetConstantService.java b/opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/RoutedGetConstantService.java
new file mode 100644 (file)
index 0000000..b4bc304
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * 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 javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
+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.binding.dom.codec.api.BindingNormalizedNodeSerializer;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+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 RoutedGetConstantService implements DOMRpcImplementation {
+
+    private static final Logger LOG = LoggerFactory.getLogger(RoutedGetConstantService.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_CONTEXTED_CONSTANT =
+            QName.create("tag:opendaylight.org,2017:controller:yang:lowlevel:target","2017-02-15",
+                    "get-contexted-constant");
+
+    private final String constant;
+
+    private RoutedGetConstantService(final String constant) {
+        this.constant = constant;
+    }
+
+    public static DOMRpcImplementationRegistration<RoutedGetConstantService> registerNew(
+            final BindingNormalizedNodeSerializer codec, final DOMRpcProviderService rpcProviderService,
+            final String constant, final InstanceIdentifier<?> context) {
+
+        LOG.debug("Registering get-contexted-constant on context: {}, with value: {}", context, constant);
+
+        final YangInstanceIdentifier yid = codec.toYangInstanceIdentifier(context);
+        final DOMRpcIdentifier id = DOMRpcIdentifier.create(SchemaPath.create(true, GET_CONTEXTED_CONSTANT), yid);
+
+        return rpcProviderService.registerRpcImplementation(new RoutedGetConstantService(constant), id);
+    }
+
+    @Nonnull
+    @Override
+    public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final DOMRpcIdentifier rpc,
+                                                                  @Nullable final NormalizedNode<?, ?> input) {
+        LOG.debug("get-contexted-constant invoked, current value: {}", constant);
+
+        final LeafNode<Object> value = ImmutableLeafNodeBuilder.create()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONSTANT))
+                .withValue(constant)
+                .build();
+
+        final ContainerNode result = ImmutableContainerNodeBuilder.create()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(OUTPUT))
+                .withChild(value)
+                .build();
+
+        return Futures.immediateCheckedFuture(new DefaultDOMRpcResult(result));
+    }
+}
index 687b1a3..08a302e 100644 (file)
@@ -11,6 +11,8 @@
   <reference id="domRpcProviderService" interface="org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService"/>
   <reference id="clusterSingletonService" interface="org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider"/>
   <reference id="domDataBroker" interface="org.opendaylight.controller.md.sal.dom.api.DOMDataBroker"/>
+  <reference id="schemaService" interface="org.opendaylight.controller.sal.core.api.model.SchemaService"/>
+  <reference id="normalizedNodeSerializer" interface="org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer"/>
 
   <bean id="purchaseCarProvider" class="org.opendaylight.controller.clustering.it.provider.PurchaseCarProvider" >
     <property name="notificationProvider" ref="notificationService"/>
@@ -48,6 +50,8 @@
     <argument ref="bindingRpcRegistry"/>
     <argument ref="domRpcProviderService"/>
     <argument ref="clusterSingletonService"/>
+    <argument ref="schemaService"/>
+    <argument ref="normalizedNodeSerializer"/>
   </bean>
 
 </blueprint>

©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.