Bump versions 9.0.4-SNAPSHOT
[controller.git] / opendaylight / md-sal / samples / toaster-consumer / src / main / java / org / opendaylight / controller / sample / kitchen / impl / KitchenServiceImpl.java
index 50ae8fd04fcce09c66a9eb7dd512af433df058d7..b67de11367874fd0e3aeb13645fd602fc7ac7a7c 100644 (file)
+/*
+ * 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 java.util.Arrays;
-import java.util.Collections;
+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.MoreExecutors;
 import java.util.List;
-import java.util.concurrent.Callable;
+import java.util.Set;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import org.opendaylight.controller.config.yang.config.kitchen_service.impl.KitchenServiceRuntimeMXBean;
-import org.opendaylight.controller.sal.common.util.RpcErrors;
-import org.opendaylight.controller.sal.common.util.Rpcs;
+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.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput;
+import org.opendaylight.controller.sample.kitchen.api.KitchenServiceRuntimeMXBean;
+import org.opendaylight.mdsal.binding.api.NotificationService;
+import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
+import org.opendaylight.mdsal.binding.api.RpcService;
+import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToast;
 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.RpcError.ErrorSeverity;
+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.RpcResult;
-import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
+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;
 
-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;
-
-public class KitchenServiceImpl implements KitchenService, KitchenServiceRuntimeMXBean, ToasterListener {
-
-    private static final Logger log = LoggerFactory.getLogger( KitchenServiceImpl.class );
+@Singleton
+@Component(service = KitchenService.class, immediate = true)
+public final class KitchenServiceImpl extends AbstractMXBean implements KitchenService, KitchenServiceRuntimeMXBean {
+    private static final Logger LOG = LoggerFactory.getLogger(KitchenServiceImpl.class);
+    private static final MakeToastOutput EMPTY_MAKE_OUTPUT = new MakeToastOutputBuilder().build();
 
-    private final ToasterService toaster;
-
-    private final ListeningExecutorService executor =
-                                   MoreExecutors.listeningDecorator( Executors.newCachedThreadPool() );
+    private final ExecutorService executor = Executors.newCachedThreadPool();
+    private final MakeToast makeToast;
+    private final Registration reg;
 
     private volatile boolean toasterOutOfBread;
 
-    public KitchenServiceImpl(ToasterService toaster) {
-        this.toaster = toaster;
+    @Inject
+    @Activate
+    public KitchenServiceImpl(@Reference final RpcService rpcService,
+            @Reference final NotificationService notifService) {
+        super("KitchenService", "toaster-consumer", null);
+        makeToast = rpcService.getRpc(MakeToast.class);
+        reg = notifService.registerCompositeListener(new CompositeListener(Set.of(
+            new CompositeListener.Component<>(ToasterOutOfBread.class, notification -> {
+                LOG.info("ToasterOutOfBread notification");
+                toasterOutOfBread = true;
+            }),
+            new CompositeListener.Component<>(ToasterRestocked.class, notification -> {
+                LOG.info("ToasterRestocked notification - amountOfBread: {}", notification.getAmountOfBread());
+                toasterOutOfBread = false;
+            }))));
+        register();
     }
 
-    @Override
-    public Future<RpcResult<Void>> makeBreakfast( EggsType eggsType, Class<? extends ToastType> toastType,
-                                                  int toastDoneness ) {
+    @PreDestroy
+    @Deactivate
+    public void close() {
+        unregister();
+        reg.close();
+    }
 
-        // Call makeToast and use JdkFutureAdapters to convert the Future to a ListenableFuture,
-        // The OpendaylightToaster impl already returns a ListenableFuture so the conversion is
+    @Override
+    public ListenableFuture<RpcResult<Void>> 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<RpcResult<Void>> makeToastFuture = JdkFutureAdapters.listenInPoolThread(
-                makeToast( toastType, toastDoneness ), executor );
+        ListenableFuture<RpcResult<MakeToastOutput>> makeToastFuture = makeToast(toastType, toastDoneness);
 
-        ListenableFuture<RpcResult<Void>> makeEggsFuture = makeEggs( eggsType );
+        ListenableFuture<RpcResult<Void>> makeEggsFuture = makeEggs(eggsType);
 
-        // Combine the 2 ListenableFutures into 1 containing a list of RpcResults.
+        // Combine the 2 ListenableFutures into 1 containing a list RpcResults.
 
-        ListenableFuture<List<RpcResult<Void>>> combinedFutures =
-                Futures.allAsList( ImmutableList.of( makeToastFuture, makeEggsFuture ) );
+        ListenableFuture<List<RpcResult<? extends Object>>> combinedFutures = Futures
+                .allAsList(ImmutableList.of(makeToastFuture, makeEggsFuture));
 
         // Then transform the RpcResults into 1.
 
-        return Futures.transform( combinedFutures,
-            new AsyncFunction<List<RpcResult<Void>>,RpcResult<Void>>() {
-                @Override
-                public ListenableFuture<RpcResult<Void>> apply( List<RpcResult<Void>> results )
-                                                                                 throws Exception {
-                    boolean atLeastOneSucceeded = false;
-                    Builder<RpcError> errorList = ImmutableList.builder();
-                    for( RpcResult<Void> result: results ) {
-                        if( result.isSuccessful() ) {
-                            atLeastOneSucceeded = true;
-                        }
-
-                        if( result.getErrors() != null ) {
-                            errorList.addAll( result.getErrors() );
-                        }
-                    }
-
-                    return Futures.immediateFuture(
-                              Rpcs.<Void> getRpcResult( atLeastOneSucceeded, errorList.build() ) );
+        return Futures.transformAsync(combinedFutures, results -> {
+            boolean atLeastOneSucceeded = false;
+            Builder<RpcError> errorList = ImmutableList.builder();
+            for (RpcResult<? extends Object> result : results) {
+                if (result.isSuccessful()) {
+                    atLeastOneSucceeded = true;
                 }
-        } );
-    }
-
-    private ListenableFuture<RpcResult<Void>> makeEggs( EggsType eggsType ) {
 
-        return executor.submit( new Callable<RpcResult<Void>>() {
+                if (result.getErrors() != null) {
+                    errorList.addAll(result.getErrors());
+                }
+            }
 
-            @Override
-            public RpcResult<Void> call() throws Exception {
+            return RpcResultBuilder.<Void>status(atLeastOneSucceeded).withRpcErrors(errorList.build()).buildFuture();
+        }, MoreExecutors.directExecutor());
+    }
 
-                // We don't actually do anything here - just return a successful result.
-                return Rpcs.<Void> getRpcResult( true, Collections.<RpcError>emptyList() );
-            }
-        } );
+    private ListenableFuture<RpcResult<Void>> makeEggs(final EggsType eggsType) {
+        return Futures.submit(() -> RpcResultBuilder.<Void>success().build(), executor);
     }
 
-    private Future<RpcResult<Void>> makeToast( Class<? extends ToastType> toastType,
-                                               int toastDoneness ) {
-
-        if( toasterOutOfBread )
-        {
-            log.info( "We're out of toast but we can make eggs" );
-            return Futures.immediateFuture( Rpcs.<Void> getRpcResult( true,
-                       Arrays.asList( RpcErrors.getRpcError( "", "partial-operation", null,
-                                          ErrorSeverity.WARNING,
-                                          "Toaster is out of bread but we can make you eggs",
-                                          ErrorType.APPLICATION, null ) ) ) );
+    private ListenableFuture<RpcResult<MakeToastOutput>> makeToast(final ToastType toastType, final int toastDoneness) {
+        if (toasterOutOfBread) {
+            LOG.info("We're out of toast but we can make eggs");
+            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 )
-            .setToasterToastType( toastType )
-            .build();
-
-        return toaster.makeToast( toastInput );
+        return makeToast.invoke(new MakeToastInputBuilder()
+            .setToasterDoneness(Uint32.valueOf(toastDoneness))
+            .setToasterToastType(toastType)
+            .build());
     }
 
     @Override
     public Boolean makeScrambledWithWheat() {
         try {
             // This call has to block since we must return a result to the JMX client.
-            RpcResult<Void> result = makeBreakfast( EggsType.SCRAMBLED, WheatBread.class, 2 ).get();
-            if( result.isSuccessful() ) {
-                log.info( "makeBreakfast succeeded" );
+            RpcResult<Void> 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 );
+        } catch (InterruptedException | ExecutionException e) {
+            LOG.warn("An error occurred while maing breakfast", e);
         }
 
         return Boolean.FALSE;
     }
-
-    /**
-     * Implemented from the ToasterListener interface.
-     */
-    @Override
-    public void onToasterOutOfBread( ToasterOutOfBread notification ) {
-        log.info( "ToasterOutOfBread notification" );
-        toasterOutOfBread = true;
-    }
-
-    /**
-     * Implemented from the ToasterListener interface.
-     */
-    @Override
-    public void onToasterRestocked( ToasterRestocked notification ) {
-        log.info( "ToasterRestocked notification - amountOfBread: " + notification.getAmountOfBread() );
-        toasterOutOfBread = false;
-    }
 }