Bug 7800 - Implement agent RPCs for Yang notification testing
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / MdsalLowLevelTestProvider.java
index bdc2d2f22468821604e4610bd42f92dc8ced4b3e..7ada4bea61090c1aa31c384de9a66c92e34c17c9 100644 (file)
@@ -9,22 +9,24 @@
 package org.opendaylight.controller.clustering.it.provider;
 
 import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.SettableFuture;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.Future;
 import org.opendaylight.controller.clustering.it.provider.impl.FlappingSingletonService;
 import org.opendaylight.controller.clustering.it.provider.impl.GetConstantService;
+import org.opendaylight.controller.clustering.it.provider.impl.PublishNotificationsTask;
 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.clustering.it.provider.impl.YnlListener;
+import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
+import org.opendaylight.controller.md.sal.binding.api.NotificationService;
 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.binding.generator.impl.GeneratedClassLoadingStrategy;
 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;
@@ -54,7 +56,6 @@ 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;
 
@@ -65,6 +66,8 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
     private final RpcProviderRegistry rpcRegistry;
     private final BindingAwareBroker.RpcRegistration<OdlMdsalLowlevelControlService> registration;
     private final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer;
+    private final NotificationPublishService notificationPublishService;
+    private final NotificationService notificationService;
     private final SchemaService schemaService;
     private final ClusterSingletonServiceProvider singletonService;
     private final DOMRpcProviderService domRpcService;
@@ -72,6 +75,8 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
     private Map<InstanceIdentifier<?>, DOMRpcImplementationRegistration<RoutedGetConstantService>> routedRegistrations =
             new HashMap<>();
 
+    private Map<String, ListenerRegistration<YnlListener>> ynlRegistrations = new HashMap<>();
+
     private DOMRpcImplementationRegistration<GetConstantService> globalGetConstantRegistration = null;
     private ClusterSingletonServiceRegistration getSingletonConstantRegistration;
     private FlappingSingletonService flappingSingletonService;
@@ -80,12 +85,16 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
                                      final DOMRpcProviderService domRpcService,
                                      final ClusterSingletonServiceProvider singletonService,
                                      final SchemaService schemaService,
-                                     final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
+                                     final BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer,
+                                     final NotificationPublishService notificationPublishService,
+                                     final NotificationService notificationService) {
         this.rpcRegistry = rpcRegistry;
         this.domRpcService = domRpcService;
         this.singletonService = singletonService;
         this.schemaService = schemaService;
         this.bindingNormalizedNodeSerializer = bindingNormalizedNodeSerializer;
+        this.notificationPublishService = notificationPublishService;
+        this.notificationService = notificationService;
 
         registration = rpcRegistry.addRpcImplementation(OdlMdsalLowlevelControlService.class, this);
     }
@@ -117,8 +126,16 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
     }
 
     @Override
-    public Future<RpcResult<Void>> publishNotifications(PublishNotificationsInput input) {
-        return null;
+    public Future<RpcResult<Void>> publishNotifications(final PublishNotificationsInput input) {
+        LOG.debug("publish-notifications, input: {}", input);
+
+        final PublishNotificationsTask task = new PublishNotificationsTask(notificationPublishService, input.getId(),
+                input.getSeconds(), input.getNotificationsPerSecond());
+
+        final SettableFuture<RpcResult<Void>> settableFuture = SettableFuture.create();
+        task.start(settableFuture);
+
+        return settableFuture;
     }
 
     @Override
@@ -147,8 +164,20 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
     }
 
     @Override
-    public Future<RpcResult<Void>> subscribeYnl(SubscribeYnlInput input) {
-        return null;
+    public Future<RpcResult<Void>> subscribeYnl(final SubscribeYnlInput input) {
+
+        LOG.debug("subscribe-ynl, input: {}", input);
+
+        if (ynlRegistrations.containsKey(input.getId())) {
+            final RpcError error = RpcResultBuilder.newError(ErrorType.RPC, "Registration present.",
+                    "There is already ynl listener registered for this id: " + input.getId());
+            return Futures.immediateFuture(RpcResultBuilder.<Void>failed().withRpcError(error).build());
+        }
+
+        ynlRegistrations.put(input.getId(),
+                notificationService.registerNotificationListener(new YnlListener(input.getId())));
+
+        return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
     }
 
     @Override
@@ -300,8 +329,23 @@ public class MdsalLowLevelTestProvider implements OdlMdsalLowlevelControlService
     }
 
     @Override
-    public Future<RpcResult<UnsubscribeYnlOutput>> unsubscribeYnl(UnsubscribeYnlInput input) {
-        return null;
+    public Future<RpcResult<UnsubscribeYnlOutput>> unsubscribeYnl(final UnsubscribeYnlInput input) {
+        LOG.debug("Received unsubscribe-ynl, input: {}", input);
+
+        if (!ynlRegistrations.containsKey(input.getId())) {
+            final RpcError rpcError = RpcResultBuilder
+                    .newError(ErrorType.APPLICATION, "missing-registration", "No ynl listener with this id registered.");
+            final RpcResult<UnsubscribeYnlOutput> result =
+                    RpcResultBuilder.<UnsubscribeYnlOutput>failed().withRpcError(rpcError).build();
+            return Futures.immediateFuture(result);
+        }
+
+        final ListenerRegistration<YnlListener> registration = ynlRegistrations.remove(input.getId());
+        final UnsubscribeYnlOutput output = registration.getInstance().getOutput();
+
+        registration.close();
+
+        return Futures.immediateFuture(RpcResultBuilder.<UnsubscribeYnlOutput>success().withResult(output).build());
     }
 
     @Override