Search in sources :

Example 1 with Toaster

use of org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster in project controller by opendaylight.

the class ToasterTest method testToaster.

@Test
public void testToaster() throws Exception {
    MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
    ObjectName providerOn = new ObjectName("org.opendaylight.controller:name=OpendaylightToaster,type=toaster-provider");
    long toastsMade = (long) platformMBeanServer.getAttribute(providerOn, "ToastsMade");
    assertEquals(0, toastsMade);
    boolean success = true;
    // Make toasts using OSGi service
    success &= kitchenService.makeBreakfast(EggsType.SCRAMBLED, HashBrown.class, 4).get().isSuccessful();
    success &= kitchenService.makeBreakfast(EggsType.POACHED, WhiteBread.class, 8).get().isSuccessful();
    assertTrue("Not all breakfasts succeeded", success);
    // Verify toasts made count on provider via JMX/config-subsystem
    toastsMade = (long) platformMBeanServer.getAttribute(providerOn, "ToastsMade");
    assertEquals(2, toastsMade);
}
Also used : HashBrown(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.HashBrown) WhiteBread(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WhiteBread) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 2 with Toaster

use of org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster in project controller by opendaylight.

the class OpendaylightToaster method restockToaster.

/**
 * RestConf RPC call implemented from the ToasterService interface.
 * Restocks the bread for the toaster, resets the toastsMade counter to 0, and sends a
 * ToasterRestocked notification.
 */
@Override
public Future<RpcResult<java.lang.Void>> restockToaster(final RestockToasterInput input) {
    LOG.info("restockToaster: " + input);
    amountOfBreadInStock.set(input.getAmountOfBreadToStock());
    if (amountOfBreadInStock.get() > 0) {
        ToasterRestocked reStockedNotification = new ToasterRestockedBuilder().setAmountOfBread(input.getAmountOfBreadToStock()).build();
        notificationProvider.offerNotification(reStockedNotification);
    }
    return Futures.immediateFuture(RpcResultBuilder.<Void>success().build());
}
Also used : ToasterRestocked(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterRestocked) ToasterRestockedBuilder(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterRestockedBuilder)

Example 3 with Toaster

use of org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster in project controller by opendaylight.

the class OpendaylightToaster method onDataTreeChanged.

/**
 * Implemented from the DataTreeChangeListener interface.
 */
@Override
public void onDataTreeChanged(Collection<DataTreeModification<Toaster>> changes) {
    for (DataTreeModification<Toaster> change : changes) {
        DataObjectModification<Toaster> rootNode = change.getRootNode();
        if (rootNode.getModificationType() == WRITE) {
            Toaster oldToaster = rootNode.getDataBefore();
            Toaster newToaster = rootNode.getDataAfter();
            LOG.info("onDataTreeChanged - Toaster config with path {} was added or replaced: " + "old Toaster: {}, new Toaster: {}", change.getRootPath().getRootIdentifier(), oldToaster, newToaster);
            Long darkness = newToaster.getDarknessFactor();
            if (darkness != null) {
                darknessFactor.set(darkness);
            }
        } else if (rootNode.getModificationType() == DELETE) {
            LOG.info("onDataTreeChanged - Toaster config with path {} was deleted: old Toaster: {}", change.getRootPath().getRootIdentifier(), rootNode.getDataBefore());
        }
    }
}
Also used : Toaster(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 4 with Toaster

use of org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster in project controller by opendaylight.

the class OpenDaylightToasterTest method testToasterInitOnStartUp.

@Test
public void testToasterInitOnStartUp() throws Exception {
    DataBroker broker = getDataBroker();
    ReadOnlyTransaction readTx = broker.newReadOnlyTransaction();
    Optional<Toaster> optional = readTx.read(LogicalDatastoreType.OPERATIONAL, TOASTER_IID).get();
    assertNotNull(optional);
    assertTrue("Operational toaster not present", optional.isPresent());
    Toaster toasterData = optional.get();
    assertEquals(Toaster.ToasterStatus.Up, toasterData.getToasterStatus());
    assertEquals(new DisplayString("Opendaylight"), toasterData.getToasterManufacturer());
    assertEquals(new DisplayString("Model 1 - Binding Aware"), toasterData.getToasterModelNumber());
    Optional<Toaster> configToaster = readTx.read(LogicalDatastoreType.CONFIGURATION, TOASTER_IID).get();
    assertFalse("Didn't expect config data for toaster.", configToaster.isPresent());
}
Also used : Toaster(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster) DisplayString(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.DisplayString) ReadOnlyTransaction(org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction) DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) AbstractConcurrentDataBrokerTest(org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest) Test(org.junit.Test)

Example 5 with Toaster

use of org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster in project controller by opendaylight.

the class OpendaylightToaster method checkStatusAndMakeToast.

private void checkStatusAndMakeToast(final MakeToastInput input, final SettableFuture<RpcResult<Void>> futureResult, final int tries) {
    // Read the ToasterStatus and, if currently Up, try to write the status to Down.
    // If that succeeds, then we essentially have an exclusive lock and can proceed
    // to make toast.
    final ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
    ListenableFuture<Optional<Toaster>> readFuture = tx.read(OPERATIONAL, TOASTER_IID);
    final ListenableFuture<Void> commitFuture = Futures.transformAsync(readFuture, toasterData -> {
        ToasterStatus toasterStatus = ToasterStatus.Up;
        if (toasterData.isPresent()) {
            toasterStatus = toasterData.get().getToasterStatus();
        }
        LOG.debug("Read toaster status: {}", toasterStatus);
        if (toasterStatus == ToasterStatus.Up) {
            if (outOfBread()) {
                LOG.debug("Toaster is out of bread");
                return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("", makeToasterOutOfBreadError()));
            }
            LOG.debug("Setting Toaster status to Down");
            // We're not currently making toast - try to update the status to Down
            // to indicate we're going to make toast. This acts as a lock to prevent
            // concurrent toasting.
            tx.put(OPERATIONAL, TOASTER_IID, buildToaster(ToasterStatus.Down));
            return tx.submit();
        }
        LOG.debug("Oops - already making toast!");
        // TransactionStatus in the RpcResult as an error condition.
        return Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("", makeToasterInUseError()));
    });
    Futures.addCallback(commitFuture, new FutureCallback<Void>() {

        @Override
        public void onSuccess(final Void result) {
            // OK to make toast
            currentMakeToastTask.set(executor.submit(new MakeToastTask(input, futureResult)));
        }

        @Override
        public void onFailure(final Throwable ex) {
            if (ex instanceof OptimisticLockFailedException) {
                if (tries - 1 > 0) {
                    LOG.debug("Got OptimisticLockFailedException - trying again");
                    checkStatusAndMakeToast(input, futureResult, tries - 1);
                } else {
                    futureResult.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, ex.getMessage()).build());
                }
            } else if (ex instanceof TransactionCommitFailedException) {
                LOG.debug("Failed to commit Toaster status", ex);
                // Probably already making toast.
                futureResult.set(RpcResultBuilder.<Void>failed().withRpcErrors(((TransactionCommitFailedException) ex).getErrorList()).build());
            } else {
                LOG.debug("Unexpected error committing Toaster status", ex);
                futureResult.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, "Unexpected error committing Toaster status", ex).build());
            }
        }
    });
}
Also used : TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) Optional(com.google.common.base.Optional) ReadWriteTransaction(org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction) ToasterStatus(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster.ToasterStatus) OptimisticLockFailedException(org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException)

Aggregations

Test (org.junit.Test)2 Toaster (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster)2 Optional (com.google.common.base.Optional)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 DataBroker (org.opendaylight.controller.md.sal.binding.api.DataBroker)1 ReadOnlyTransaction (org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction)1 ReadWriteTransaction (org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction)1 AbstractConcurrentDataBrokerTest (org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest)1 OptimisticLockFailedException (org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException)1 TransactionCommitFailedException (org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException)1 DisplayString (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.DisplayString)1 HashBrown (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.HashBrown)1 ToasterStatus (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster.ToasterStatus)1 ToasterRestocked (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterRestocked)1 ToasterRestockedBuilder (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterRestockedBuilder)1 WhiteBread (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WhiteBread)1