X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsamples%2Ftoaster-consumer%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsample%2Fkitchen%2Fimpl%2FKitchenServiceImpl.java;h=4f245685aa6318f9a477b2ea576222b655142117;hb=ded31428cae8b8907dcf06aba0dcd2fdd8cbe01b;hp=1899afb7078b52ca1b936a9669899c475499d5aa;hpb=6602310b78b5bf54899456e2efa867d116731267;p=controller.git diff --git a/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/kitchen/impl/KitchenServiceImpl.java b/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/kitchen/impl/KitchenServiceImpl.java index 1899afb707..4f245685aa 100644 --- a/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/kitchen/impl/KitchenServiceImpl.java +++ b/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/kitchen/impl/KitchenServiceImpl.java @@ -5,36 +5,37 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.sample.kitchen.impl; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList.Builder; import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.JdkFutureAdapters; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.MoreExecutors; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; -import java.util.concurrent.Future; import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean; import org.opendaylight.controller.sample.kitchen.api.EggsType; import org.opendaylight.controller.sample.kitchen.api.KitchenService; import org.opendaylight.controller.sample.kitchen.api.KitchenServiceRuntimeMXBean; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInputBuilder; +import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastOutput; +import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastOutputBuilder; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToastType; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterListener; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterOutOfBread; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterRestocked; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WheatBread; +import org.opendaylight.yangtools.yang.common.ErrorTag; +import org.opendaylight.yangtools.yang.common.ErrorType; import org.opendaylight.yangtools.yang.common.RpcError; -import org.opendaylight.yangtools.yang.common.RpcError.ErrorType; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; +import org.opendaylight.yangtools.yang.common.Uint32; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,6 +43,7 @@ public class KitchenServiceImpl extends AbstractMXBean implements KitchenService, KitchenServiceRuntimeMXBean, ToasterListener { private static final Logger LOG = LoggerFactory.getLogger(KitchenServiceImpl.class); + private static final MakeToastOutput EMPTY_MAKE_OUTPUT = new MakeToastOutputBuilder().build(); private final ToasterService toaster; @@ -49,25 +51,24 @@ public class KitchenServiceImpl extends AbstractMXBean private volatile boolean toasterOutOfBread; - public KitchenServiceImpl(ToasterService toaster) { + public KitchenServiceImpl(final ToasterService toaster) { super("KitchenService", "toaster-consumer", null); this.toaster = toaster; } @Override - public Future> makeBreakfast(EggsType eggsType, Class toastType, - int toastDoneness) { - // Call makeToast and use JdkFutureAdapters to convert the Future to a ListenableFuture, The - // OpendaylightToaster impl already returns a ListenableFuture so the conversion is actually a no-op. + public ListenableFuture> makeBreakfast(final EggsType eggsType, final ToastType toastType, + final int toastDoneness) { + // Call makeToast, The OpendaylightToaster impl already returns a ListenableFuture so the conversion is + // actually a no-op. - ListenableFuture> makeToastFuture = JdkFutureAdapters - .listenInPoolThread(makeToast(toastType, toastDoneness), executor); + ListenableFuture> makeToastFuture = makeToast(toastType, toastDoneness); ListenableFuture> makeEggsFuture = makeEggs(eggsType); // Combine the 2 ListenableFutures into 1 containing a list RpcResults. - ListenableFuture>> combinedFutures = Futures + ListenableFuture>> combinedFutures = Futures .allAsList(ImmutableList.of(makeToastFuture, makeEggsFuture)); // Then transform the RpcResults into 1. @@ -75,7 +76,7 @@ public class KitchenServiceImpl extends AbstractMXBean return Futures.transformAsync(combinedFutures, results -> { boolean atLeastOneSucceeded = false; Builder errorList = ImmutableList.builder(); - for (RpcResult result : results) { + for (RpcResult result : results) { if (result.isSuccessful()) { atLeastOneSucceeded = true; } @@ -85,26 +86,26 @@ public class KitchenServiceImpl extends AbstractMXBean } } - return Futures.immediateFuture(RpcResultBuilder.status(atLeastOneSucceeded) - .withRpcErrors(errorList.build()).build()); - }); + return RpcResultBuilder.status(atLeastOneSucceeded).withRpcErrors(errorList.build()).buildFuture(); + }, MoreExecutors.directExecutor()); } - private ListenableFuture> makeEggs(EggsType eggsType) { + private ListenableFuture> makeEggs(final EggsType eggsType) { return executor.submit(() -> RpcResultBuilder.success().build()); } - private Future> makeToast(Class toastType, int toastDoneness) { - + private ListenableFuture> makeToast(final ToastType toastType, final int toastDoneness) { if (toasterOutOfBread) { LOG.info("We're out of toast but we can make eggs"); - return Futures.immediateFuture(RpcResultBuilder.success().withWarning(ErrorType.APPLICATION, - "partial-operation", "Toaster is out of bread but we can make you eggs").build()); + return RpcResultBuilder.success(EMPTY_MAKE_OUTPUT) + .withWarning(ErrorType.APPLICATION, ErrorTag.PARTIAL_OPERATION, + "Toaster is out of bread but we can make you eggs") + .buildFuture(); } // Access the ToasterService to make the toast. - MakeToastInput toastInput = new MakeToastInputBuilder().setToasterDoneness((long) toastDoneness) + MakeToastInput toastInput = new MakeToastInputBuilder().setToasterDoneness(Uint32.valueOf(toastDoneness)) .setToasterToastType(toastType).build(); return toaster.makeToast(toastInput); @@ -114,17 +115,16 @@ public class KitchenServiceImpl extends AbstractMXBean public Boolean makeScrambledWithWheat() { try { // This call has to block since we must return a result to the JMX client. - RpcResult result = makeBreakfast(EggsType.SCRAMBLED, WheatBread.class, 2).get(); + RpcResult result = makeBreakfast(EggsType.SCRAMBLED, WheatBread.VALUE, 2).get(); if (result.isSuccessful()) { LOG.info("makeBreakfast succeeded"); } else { - LOG.warn("makeBreakfast failed: " + result.getErrors()); + LOG.warn("makeBreakfast failed: {}", result.getErrors()); } return result.isSuccessful(); - } catch (InterruptedException | ExecutionException e) { - LOG.warn("An error occurred while maing breakfast: " + e); + LOG.warn("An error occurred while maing breakfast", e); } return Boolean.FALSE; @@ -134,7 +134,7 @@ public class KitchenServiceImpl extends AbstractMXBean * Implemented from the ToasterListener interface. */ @Override - public void onToasterOutOfBread(ToasterOutOfBread notification) { + public void onToasterOutOfBread(final ToasterOutOfBread notification) { LOG.info("ToasterOutOfBread notification"); toasterOutOfBread = true; } @@ -143,8 +143,8 @@ public class KitchenServiceImpl extends AbstractMXBean * Implemented from the ToasterListener interface. */ @Override - public void onToasterRestocked(ToasterRestocked notification) { - LOG.info("ToasterRestocked notification - amountOfBread: " + notification.getAmountOfBread()); + public void onToasterRestocked(final ToasterRestocked notification) { + LOG.info("ToasterRestocked notification - amountOfBread: {}", notification.getAmountOfBread()); toasterOutOfBread = false; } }