Updater toaster to use datastore 23/5823/1
authorRobert Varga <rovarga@cisco.com>
Mon, 31 Mar 2014 16:06:20 +0000 (18:06 +0200)
committerRobert Varga <rovarga@cisco.com>
Mon, 31 Mar 2014 16:06:20 +0000 (18:06 +0200)
This patch adds the ability to expose the toaster status into MD-SAL, as
expected by the module. It further tweaks a thinko: dark toasts take
longer to make!

Change-Id: I3054ec52f53272c3fb08494517c6c86524403949
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/samples/toaster-provider/src/main/java/org/opendaylight/controller/config/yang/config/toaster_provider/impl/ToasterProviderModule.java
opendaylight/md-sal/samples/toaster-provider/src/main/java/org/opendaylight/controller/sample/toaster/provider/OpendaylightToaster.java
opendaylight/md-sal/samples/toaster-provider/src/main/yang/toaster-provider-impl.yang

index 8abff2c..d9bb36e 100644 (file)
@@ -14,7 +14,6 @@ import org.opendaylight.controller.sample.toaster.provider.OpendaylightToaster;
 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster;
 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterData;
 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -47,6 +46,7 @@ public final class ToasterProviderModule extends org.opendaylight.controller.con
 
         // Register to md-sal
         opendaylightToaster.setNotificationProvider(getNotificationServiceDependency());
+        opendaylightToaster.setDataProvider(getDataBrokerDependency());
         final BindingAwareBroker.RpcRegistration<ToasterService> rpcRegistration = getRpcRegistryDependency()
                 .addRpcImplementation(ToasterService.class, opendaylightToaster);
 
@@ -62,6 +62,7 @@ public final class ToasterProviderModule extends org.opendaylight.controller.con
             public void close() throws Exception {
                 rpcRegistration.close();
                 runtimeReg.close();
+                opendaylightToaster.close();
                 log.info("Toaster provider (instance {}) torn down.", this);
             }
 
index e1d6980..2dab924 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.controller.sample.toaster.provider;
 
 import java.util.Collections;
 import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
@@ -16,6 +17,8 @@ import java.util.concurrent.atomic.AtomicLong;
 
 import org.opendaylight.controller.config.yang.config.toaster_provider.impl.ToasterProviderRuntimeMXBean;
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
+import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
 import org.opendaylight.controller.sal.common.util.Rpcs;
 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.DisplayString;
 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput;
@@ -26,6 +29,7 @@ import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120
 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterBuilder;
 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterData;
 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
@@ -33,37 +37,37 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.util.concurrent.Futures;
 
-public class OpendaylightToaster implements ToasterData, ToasterService, ToasterProviderRuntimeMXBean {
+public class OpendaylightToaster implements ToasterData, ToasterService, ToasterProviderRuntimeMXBean, AutoCloseable {
 
     private static final Logger log = LoggerFactory.getLogger(OpendaylightToaster.class);
+    private static final InstanceIdentifier<Toaster>  toasterIID = InstanceIdentifier.builder(Toaster.class).build();
 
     private static final DisplayString toasterManufacturer = new DisplayString("Opendaylight");
     private static final DisplayString toasterModelNumber = new DisplayString("Model 1 - Binding Aware");
-    private final ToasterStatus toasterStatus;
 
     private NotificationProviderService notificationProvider;
+    private DataBrokerService dataProvider;
     private final ExecutorService executor;
 
     private Future<RpcResult<Void>> currentTask;
 
     public OpendaylightToaster() {
-        toasterStatus = ToasterStatus.Down;
         executor = Executors.newFixedThreadPool(1);
     }
 
     @Override
-    public Toaster getToaster() {
+    public synchronized Toaster getToaster() {
         ToasterBuilder tb = new ToasterBuilder();
         tb //
         .setToasterManufacturer(toasterManufacturer) //
-                .setToasterModelNumber(toasterModelNumber) //
-                .setToasterStatus(toasterStatus);
+        .setToasterModelNumber(toasterModelNumber) //
+        .setToasterStatus(currentTask == null ? ToasterStatus.Up : ToasterStatus.Down);
 
         return tb.build();
     }
 
     @Override
-    public Future<RpcResult<Void>> cancelToast() {
+    public synchronized Future<RpcResult<Void>> cancelToast() {
         if (currentTask != null) {
             cancelToastImpl();
         }
@@ -71,14 +75,14 @@ public class OpendaylightToaster implements ToasterData, ToasterService, Toaster
     }
 
     @Override
-    public Future<RpcResult<Void>> makeToast(MakeToastInput input) {
-        // TODO Auto-generated method stub
-        log.trace("makeToast - Received input for toast");
+    public synchronized Future<RpcResult<Void>> makeToast(MakeToastInput input) {
+        log.debug("makeToast - Received input for toast");
         logToastInput(input);
         if (currentTask != null) {
             return inProgressError();
         }
         currentTask = executor.submit(new MakeToastTask(input));
+        updateStatus();
         return currentTask;
     }
 
@@ -98,6 +102,11 @@ public class OpendaylightToaster implements ToasterData, ToasterService, Toaster
         this.notificationProvider = salService;
     }
 
+    public void setDataProvider(DataBrokerService salDataProvider) {
+        this.dataProvider = salDataProvider;
+        updateStatus();
+    }
+
     private void logToastInput(MakeToastInput input) {
         String toastType = input.getToasterToastType().getName();
         String toastDoneness = input.getToasterDoneness().toString();
@@ -111,6 +120,31 @@ public class OpendaylightToaster implements ToasterData, ToasterService, Toaster
         return toastsMade.get();
     }
 
+    private void updateStatus() {
+        if (dataProvider != null) {
+            final DataModificationTransaction t = dataProvider.beginTransaction();
+            t.removeOperationalData(toasterIID);
+            t.putOperationalData(toasterIID, getToaster());
+
+            try {
+                t.commit().get();
+            } catch (InterruptedException | ExecutionException e) {
+                log.warn("Failed to update toaster status, operational otherwise", e);
+            }
+        } else {
+            log.trace("No data provider configured, not updating status");
+        }
+    }
+
+    @Override
+    public void close() throws ExecutionException, InterruptedException {
+        if (dataProvider != null) {
+            final DataModificationTransaction t = dataProvider.beginTransaction();
+            t.removeOperationalData(toasterIID);
+            t.commit().get();
+        }
+    }
+
     private class MakeToastTask implements Callable<RpcResult<Void>> {
 
         final MakeToastInput toastRequest;
@@ -120,17 +154,18 @@ public class OpendaylightToaster implements ToasterData, ToasterService, Toaster
         }
 
         @Override
-        public RpcResult<Void> call() throws Exception {
-            Thread.sleep(1000);
+        public RpcResult<Void> call() throws InterruptedException {
+            Thread.sleep(1000 * toastRequest.getToasterDoneness());
 
             ToastDoneBuilder notifyBuilder = new ToastDoneBuilder();
             notifyBuilder.setToastStatus(ToastStatus.Done);
             notificationProvider.publish(notifyBuilder.build());
-            log.trace("Toast Done");
+            log.debug("Toast Done");
             logToastInput(toastRequest);
-            currentTask = null;
 
+            currentTask = null;
             toastsMade.incrementAndGet();
+            updateStatus();
 
             return Rpcs.<Void> getRpcResult(true, null, Collections.<RpcError> emptySet());
         }
index 0be8874..17b0c8d 100644 (file)
@@ -47,6 +47,14 @@ module toaster-provider-impl {
                 }
             }
 
+            container data-broker {
+                uses config:service-ref {
+                    refine type {
+                        mandatory false;
+                        config:required-identity mdsal:binding-data-broker;
+                    }
+                }
+            }
         }
     }
 
@@ -60,4 +68,4 @@ module toaster-provider-impl {
 
         }
     }
-}
\ No newline at end of file
+}