Updater toaster to use datastore
[controller.git] / opendaylight / md-sal / samples / toaster-provider / src / main / java / org / opendaylight / controller / sample / toaster / provider / OpendaylightToaster.java
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());
         }