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=55b56741afe3dd5e325e4e6337e2eb68fe914ec1;hb=e94ab3dd58a01902310d512179b6702837240e87;hp=c1c43f767601b07b7c42cea3cc09027807abc154;hpb=258d8039ac144aeee2efa7943228c0fc6cdaf651;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 c1c43f7676..55b56741af 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 @@ -11,16 +11,20 @@ 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.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.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.Future; +import javax.annotation.PreDestroy; +import javax.inject.Inject; +import javax.inject.Singleton; 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.mdsal.binding.api.NotificationService; +import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry; 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; @@ -31,32 +35,53 @@ import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120 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.concepts.Registration; +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.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class KitchenServiceImpl extends AbstractMXBean +@Singleton +@Component(service = KitchenService.class, immediate = true) +public final 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 ExecutorService executor = Executors.newCachedThreadPool(); private final ToasterService toaster; - - private final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); + private final Registration reg; private volatile boolean toasterOutOfBread; - public KitchenServiceImpl(final ToasterService toaster) { + @Inject + @Activate + public KitchenServiceImpl(@Reference final RpcConsumerRegistry rpcRegistry, + @Reference final NotificationService notifService) { super("KitchenService", "toaster-consumer", null); - this.toaster = toaster; + toaster = rpcRegistry.getRpcService(ToasterService.class); + reg = notifService.registerNotificationListener(this); + register(); + } + + @PreDestroy + @Deactivate + public void close() { + unregister(); + reg.close(); } @Override - public Future> makeBreakfast(final EggsType eggsType, final Class toastType, + 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. @@ -85,28 +110,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(final EggsType eggsType) { - return executor.submit(() -> RpcResultBuilder.success().build()); + return Futures.submit(() -> RpcResultBuilder.success().build(), executor); } - private ListenableFuture> makeToast(final Class toastType, - final 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(EMPTY_MAKE_OUTPUT) - .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); @@ -116,17 +139,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; @@ -146,7 +168,7 @@ public class KitchenServiceImpl extends AbstractMXBean */ @Override public void onToasterRestocked(final ToasterRestocked notification) { - LOG.info("ToasterRestocked notification - amountOfBread: " + notification.getAmountOfBread()); + LOG.info("ToasterRestocked notification - amountOfBread: {}", notification.getAmountOfBread()); toasterOutOfBread = false; } }