X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsamples%2Ftoaster-consumer%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsample%2Fkitchen%2Fimpl%2FKitchenServiceImpl.java;h=0583368c20f97e7601dbbf0c0b37f720116636b2;hp=911a8c87d7166edc38c7faa2597678c8f55c611a;hb=310e1cf3dca0c2e94994a09c0faf7483d738e576;hpb=04a788d2df5303c60cdbcff02254291f411566bd 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 911a8c87d7..0583368c20 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 @@ -1,10 +1,30 @@ +/* + * Copyright (c) 2014, 2015 Brocade Communications Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * 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.AsyncFunction; +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 org.opendaylight.controller.config.yang.config.kitchen_service.impl.KitchenServiceRuntimeMXBean; +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.ToastType; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterListener; @@ -12,64 +32,112 @@ 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.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.slf4j.Logger; import org.slf4j.LoggerFactory; -public class KitchenServiceImpl implements KitchenService, KitchenServiceRuntimeMXBean, ToasterListener { +public class KitchenServiceImpl extends AbstractMXBean + implements KitchenService, KitchenServiceRuntimeMXBean, ToasterListener { - private static final Logger log = LoggerFactory.getLogger( KitchenServiceImpl.class ); + private static final Logger LOG = LoggerFactory.getLogger(KitchenServiceImpl.class); private final ToasterService toaster; + private final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); + private volatile boolean toasterOutOfBread; public KitchenServiceImpl(ToasterService toaster) { + super("KitchenService", "toaster-consumer", null); this.toaster = toaster; } @Override - public boolean makeBreakfast( EggsType eggs, Class toast, int toastDoneness ) { + 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. + + ListenableFuture> makeToastFuture = JdkFutureAdapters + .listenInPoolThread(makeToast(toastType, toastDoneness), executor); + + ListenableFuture> makeEggsFuture = makeEggs(eggsType); + + // Combine the 2 ListenableFutures into 1 containing a list RpcResults. + + ListenableFuture>> combinedFutures = Futures + .allAsList(ImmutableList.of(makeToastFuture, makeEggsFuture)); - if( toasterOutOfBread ) - { - log.info( "We're out of toast but we can make eggs" ); - return true; + // Then transform the RpcResults into 1. + + return Futures.transform(combinedFutures, + (AsyncFunction>, RpcResult>) results -> { + boolean atLeastOneSucceeded = false; + Builder errorList = ImmutableList.builder(); + for (RpcResult result : results) { + if (result.isSuccessful()) { + atLeastOneSucceeded = true; + } + + if (result.getErrors() != null) { + errorList.addAll(result.getErrors()); + } + } + + return Futures.immediateFuture(RpcResultBuilder.status(atLeastOneSucceeded) + .withRpcErrors(errorList.build()).build()); + }); + } + + private ListenableFuture> makeEggs(EggsType eggsType) { + return executor.submit(() -> RpcResultBuilder.success().build()); + } + + private Future> makeToast(Class toastType, 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()); } // Access the ToasterService to make the toast. - // We don't actually make the eggs for this example - sorry. - MakeToastInputBuilder toastInput = new MakeToastInputBuilder(); - toastInput.setToasterDoneness( (long) toastDoneness); - toastInput.setToasterToastType( toast ); - try { - RpcResult result = toaster.makeToast( toastInput.build() ).get(); + MakeToastInput toastInput = new MakeToastInputBuilder().setToasterDoneness((long) toastDoneness) + .setToasterToastType(toastType).build(); - if( result.isSuccessful() ) { - log.info( "makeToast succeeded" ); + return toaster.makeToast(toastInput); + } + + @Override + 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(); + if (result.isSuccessful()) { + LOG.info("makeBreakfast succeeded"); } else { - log.warn( "makeToast failed: " + result.getErrors() ); + LOG.warn("makeBreakfast failed: " + result.getErrors()); } return result.isSuccessful(); - } catch( InterruptedException | ExecutionException e ) { - log.warn( "Error occurred during toast creation" ); + + } catch (InterruptedException | ExecutionException e) { + LOG.warn("An error occurred while maing breakfast: " + e); } - return false; - } - @Override - public Boolean makeScrambledWithWheat() { - return makeBreakfast( EggsType.SCRAMBLED, WheatBread.class, 2 ); + return Boolean.FALSE; } /** * Implemented from the ToasterListener interface. */ @Override - public void onToasterOutOfBread( ToasterOutOfBread notification ) { - log.info( "ToasterOutOfBread notification" ); + public void onToasterOutOfBread(ToasterOutOfBread notification) { + LOG.info("ToasterOutOfBread notification"); toasterOutOfBread = true; } @@ -77,8 +145,8 @@ public class KitchenServiceImpl implements KitchenService, KitchenServiceRuntime * Implemented from the ToasterListener interface. */ @Override - public void onToasterRestocked( ToasterRestocked notification ) { - log.info( "ToasterRestocked notification - amountOfBread: " + notification.getAmountOfBread() ); + public void onToasterRestocked(ToasterRestocked notification) { + LOG.info("ToasterRestocked notification - amountOfBread: " + notification.getAmountOfBread()); toasterOutOfBread = false; } }