1e5c10c0c1f8bb4c5a29b400889b22ec18fb12c7
[controller.git] / opendaylight / md-sal / samples / toaster-consumer / src / main / java / org / opendaylight / controller / sample / kitchen / impl / KitchenServiceImpl.java
1 /*
2  * Copyright (c) 2014, 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sample.kitchen.impl;
9
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableList.Builder;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.Future;
20 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
21 import org.opendaylight.controller.sample.kitchen.api.EggsType;
22 import org.opendaylight.controller.sample.kitchen.api.KitchenService;
23 import org.opendaylight.controller.sample.kitchen.api.KitchenServiceRuntimeMXBean;
24 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput;
25 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInputBuilder;
26 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastOutput;
27 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastOutputBuilder;
28 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToastType;
29 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterListener;
30 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterOutOfBread;
31 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterRestocked;
32 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService;
33 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WheatBread;
34 import org.opendaylight.yangtools.yang.common.ErrorTag;
35 import org.opendaylight.yangtools.yang.common.ErrorType;
36 import org.opendaylight.yangtools.yang.common.RpcError;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 import org.opendaylight.yangtools.yang.common.Uint32;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class KitchenServiceImpl extends AbstractMXBean
44         implements KitchenService, KitchenServiceRuntimeMXBean, ToasterListener {
45
46     private static final Logger LOG = LoggerFactory.getLogger(KitchenServiceImpl.class);
47     private static final MakeToastOutput EMPTY_MAKE_OUTPUT = new MakeToastOutputBuilder().build();
48
49     private final ToasterService toaster;
50
51     private final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
52
53     private volatile boolean toasterOutOfBread;
54
55     public KitchenServiceImpl(final ToasterService toaster) {
56         super("KitchenService", "toaster-consumer", null);
57         this.toaster = toaster;
58     }
59
60     @Override
61     public Future<RpcResult<Void>> makeBreakfast(final EggsType eggsType, final Class<? extends ToastType> toastType,
62             final int toastDoneness) {
63         // Call makeToast, The OpendaylightToaster impl already returns a ListenableFuture so the conversion is
64         // actually a no-op.
65
66         ListenableFuture<RpcResult<MakeToastOutput>> makeToastFuture = makeToast(toastType, toastDoneness);
67
68         ListenableFuture<RpcResult<Void>> makeEggsFuture = makeEggs(eggsType);
69
70         // Combine the 2 ListenableFutures into 1 containing a list RpcResults.
71
72         ListenableFuture<List<RpcResult<? extends Object>>> combinedFutures = Futures
73                 .allAsList(ImmutableList.of(makeToastFuture, makeEggsFuture));
74
75         // Then transform the RpcResults into 1.
76
77         return Futures.transformAsync(combinedFutures, results -> {
78             boolean atLeastOneSucceeded = false;
79             Builder<RpcError> errorList = ImmutableList.builder();
80             for (RpcResult<? extends Object> result : results) {
81                 if (result.isSuccessful()) {
82                     atLeastOneSucceeded = true;
83                 }
84
85                 if (result.getErrors() != null) {
86                     errorList.addAll(result.getErrors());
87                 }
88             }
89
90             return RpcResultBuilder.<Void>status(atLeastOneSucceeded).withRpcErrors(errorList.build()).buildFuture();
91         }, MoreExecutors.directExecutor());
92     }
93
94     private ListenableFuture<RpcResult<Void>> makeEggs(final EggsType eggsType) {
95         return executor.submit(() -> RpcResultBuilder.<Void>success().build());
96     }
97
98     private ListenableFuture<RpcResult<MakeToastOutput>> makeToast(final Class<? extends ToastType> toastType,
99             final int toastDoneness) {
100
101         if (toasterOutOfBread) {
102             LOG.info("We're out of toast but we can make eggs");
103             return RpcResultBuilder.success(EMPTY_MAKE_OUTPUT)
104                 .withWarning(ErrorType.APPLICATION, ErrorTag.PARTIAL_OPERATION,
105                     "Toaster is out of bread but we can make you eggs")
106                 .buildFuture();
107         }
108
109         // Access the ToasterService to make the toast.
110
111         MakeToastInput toastInput = new MakeToastInputBuilder().setToasterDoneness(Uint32.valueOf(toastDoneness))
112                 .setToasterToastType(toastType).build();
113
114         return toaster.makeToast(toastInput);
115     }
116
117     @Override
118     public Boolean makeScrambledWithWheat() {
119         try {
120             // This call has to block since we must return a result to the JMX client.
121             RpcResult<Void> result = makeBreakfast(EggsType.SCRAMBLED, WheatBread.class, 2).get();
122             if (result.isSuccessful()) {
123                 LOG.info("makeBreakfast succeeded");
124             } else {
125                 LOG.warn("makeBreakfast failed: {}", result.getErrors());
126             }
127
128             return result.isSuccessful();
129         } catch (InterruptedException | ExecutionException e) {
130             LOG.warn("An error occurred while maing breakfast", e);
131         }
132
133         return Boolean.FALSE;
134     }
135
136     /**
137      * Implemented from the ToasterListener interface.
138      */
139     @Override
140     public void onToasterOutOfBread(final ToasterOutOfBread notification) {
141         LOG.info("ToasterOutOfBread notification");
142         toasterOutOfBread = true;
143     }
144
145     /**
146      * Implemented from the ToasterListener interface.
147      */
148     @Override
149     public void onToasterRestocked(final ToasterRestocked notification) {
150         LOG.info("ToasterRestocked notification - amountOfBread: {}", notification.getAmountOfBread());
151         toasterOutOfBread = false;
152     }
153 }