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);
}
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());
}
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());
}
}
}
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());
}
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());
}
}
});
}
Aggregations