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