Search in sources :

Example 1 with MakeToastInput

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

the class OpenDaylightToasterTest method testSomething.

@Test
// ignored because it is not a test right now. Illustrative purposes only.
@Ignore
public void testSomething() throws Exception {
    MakeToastInput toastInput = new MakeToastInputBuilder().setToasterDoneness(1L).setToasterToastType(WheatBread.class).build();
    // NOTE: In a real test we would want to override the Thread.sleep() to
    // prevent our junit test
    // for sleeping for a second...
    Future<RpcResult<Void>> makeToast = toaster.makeToast(toastInput);
    RpcResult<Void> rpcResult = makeToast.get();
    assertNotNull(rpcResult);
    assertTrue(rpcResult.isSuccessful());
// etc
}
Also used : MakeToastInput(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) MakeToastInputBuilder(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInputBuilder) WheatBread(org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WheatBread) Ignore(org.junit.Ignore) AbstractConcurrentDataBrokerTest(org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest) Test(org.junit.Test)

Example 2 with MakeToastInput

use of org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput 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

Optional (com.google.common.base.Optional)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)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 MakeToastInput (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput)1 MakeToastInputBuilder (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInputBuilder)1 ToasterStatus (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster.ToasterStatus)1 WheatBread (org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WheatBread)1 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)1