1899afb7078b52ca1b936a9669899c475499d5aa
[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
9 package org.opendaylight.controller.sample.kitchen.impl;
10
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableList.Builder;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.JdkFutureAdapters;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.ListeningExecutorService;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.Future;
22 import org.opendaylight.controller.md.sal.common.util.jmx.AbstractMXBean;
23 import org.opendaylight.controller.sample.kitchen.api.EggsType;
24 import org.opendaylight.controller.sample.kitchen.api.KitchenService;
25 import org.opendaylight.controller.sample.kitchen.api.KitchenServiceRuntimeMXBean;
26 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput;
27 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInputBuilder;
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.RpcError;
35 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class KitchenServiceImpl extends AbstractMXBean
42         implements KitchenService, KitchenServiceRuntimeMXBean, ToasterListener {
43
44     private static final Logger LOG = LoggerFactory.getLogger(KitchenServiceImpl.class);
45
46     private final ToasterService toaster;
47
48     private final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
49
50     private volatile boolean toasterOutOfBread;
51
52     public KitchenServiceImpl(ToasterService toaster) {
53         super("KitchenService", "toaster-consumer", null);
54         this.toaster = toaster;
55     }
56
57     @Override
58     public Future<RpcResult<Void>> makeBreakfast(EggsType eggsType, Class<? extends ToastType> toastType,
59             int toastDoneness) {
60         // Call makeToast and use JdkFutureAdapters to convert the Future to a ListenableFuture, The
61         // OpendaylightToaster impl already returns a ListenableFuture so the conversion is actually a no-op.
62
63         ListenableFuture<RpcResult<Void>> makeToastFuture = JdkFutureAdapters
64                 .listenInPoolThread(makeToast(toastType, toastDoneness), executor);
65
66         ListenableFuture<RpcResult<Void>> makeEggsFuture = makeEggs(eggsType);
67
68         // Combine the 2 ListenableFutures into 1 containing a list RpcResults.
69
70         ListenableFuture<List<RpcResult<Void>>> combinedFutures = Futures
71                 .allAsList(ImmutableList.of(makeToastFuture, makeEggsFuture));
72
73         // Then transform the RpcResults into 1.
74
75         return Futures.transformAsync(combinedFutures, results -> {
76             boolean atLeastOneSucceeded = false;
77             Builder<RpcError> errorList = ImmutableList.builder();
78             for (RpcResult<Void> result : results) {
79                 if (result.isSuccessful()) {
80                     atLeastOneSucceeded = true;
81                 }
82
83                 if (result.getErrors() != null) {
84                     errorList.addAll(result.getErrors());
85                 }
86             }
87
88             return Futures.immediateFuture(RpcResultBuilder.<Void>status(atLeastOneSucceeded)
89                     .withRpcErrors(errorList.build()).build());
90         });
91     }
92
93     private ListenableFuture<RpcResult<Void>> makeEggs(EggsType eggsType) {
94         return executor.submit(() -> RpcResultBuilder.<Void>success().build());
95     }
96
97     private Future<RpcResult<Void>> makeToast(Class<? extends ToastType> toastType, int toastDoneness) {
98
99         if (toasterOutOfBread) {
100             LOG.info("We're out of toast but we can make eggs");
101             return Futures.immediateFuture(RpcResultBuilder.<Void>success().withWarning(ErrorType.APPLICATION,
102                     "partial-operation", "Toaster is out of bread but we can make you eggs").build());
103         }
104
105         // Access the ToasterService to make the toast.
106
107         MakeToastInput toastInput = new MakeToastInputBuilder().setToasterDoneness((long) toastDoneness)
108                 .setToasterToastType(toastType).build();
109
110         return toaster.makeToast(toastInput);
111     }
112
113     @Override
114     public Boolean makeScrambledWithWheat() {
115         try {
116             // This call has to block since we must return a result to the JMX client.
117             RpcResult<Void> result = makeBreakfast(EggsType.SCRAMBLED, WheatBread.class, 2).get();
118             if (result.isSuccessful()) {
119                 LOG.info("makeBreakfast succeeded");
120             } else {
121                 LOG.warn("makeBreakfast failed: " + result.getErrors());
122             }
123
124             return result.isSuccessful();
125
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(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(ToasterRestocked notification) {
147         LOG.info("ToasterRestocked notification - amountOfBread: " + notification.getAmountOfBread());
148         toasterOutOfBread = false;
149     }
150 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.