Merge "Implement PortNumberBuilder"
[controller.git] / opendaylight / md-sal / samples / toaster-consumer / src / main / java / org / opendaylight / controller / sample / kitchen / impl / KitchenServiceImpl.java
1 package org.opendaylight.controller.sample.kitchen.impl;
2
3 import java.util.concurrent.ExecutionException;
4
5 import org.opendaylight.controller.config.yang.config.kitchen_service.impl.KitchenServiceRuntimeMXBean;
6 import org.opendaylight.controller.sample.kitchen.api.EggsType;
7 import org.opendaylight.controller.sample.kitchen.api.KitchenService;
8 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInputBuilder;
9 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToastType;
10 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterListener;
11 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterOutOfBread;
12 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterRestocked;
13 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService;
14 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WheatBread;
15 import org.opendaylight.yangtools.yang.common.RpcResult;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class KitchenServiceImpl implements KitchenService, KitchenServiceRuntimeMXBean, ToasterListener {
20
21     private static final Logger log = LoggerFactory.getLogger( KitchenServiceImpl.class );
22
23     private final ToasterService toaster;
24
25     private volatile boolean toasterOutOfBread;
26
27     public KitchenServiceImpl(ToasterService toaster) {
28         this.toaster = toaster;
29     }
30
31     @Override
32     public boolean makeBreakfast( EggsType eggs, Class<? extends ToastType> toast, int toastDoneness ) {
33
34         if( toasterOutOfBread )
35         {
36             log.info( "We're out of toast but we can make eggs" );
37             return true;
38         }
39
40         // Access the ToasterService to make the toast.
41         // We don't actually make the eggs for this example - sorry.
42         MakeToastInputBuilder toastInput = new MakeToastInputBuilder();
43         toastInput.setToasterDoneness( (long) toastDoneness);
44         toastInput.setToasterToastType( toast );
45
46         try {
47             RpcResult<Void> result = toaster.makeToast( toastInput.build() ).get();
48
49             if( result.isSuccessful() ) {
50                 log.info( "makeToast succeeded" );
51             } else {
52                 log.warn( "makeToast failed: " + result.getErrors() );
53             }
54
55             return result.isSuccessful();
56         } catch( InterruptedException | ExecutionException e ) {
57             log.warn( "Error occurred during toast creation" );
58         }
59         return false;
60     }
61
62     @Override
63     public Boolean makeScrambledWithWheat() {
64         return makeBreakfast( EggsType.SCRAMBLED, WheatBread.class, 2 );
65     }
66
67     /**
68      * Implemented from the ToasterListener interface.
69      */
70     @Override
71     public void onToasterOutOfBread( ToasterOutOfBread notification ) {
72         log.info( "ToasterOutOfBread notification" );
73         toasterOutOfBread = true;
74     }
75
76     /**
77      * Implemented from the ToasterListener interface.
78      */
79     @Override
80     public void onToasterRestocked( ToasterRestocked notification ) {
81         log.info( "ToasterRestocked notification - amountOfBread: " + notification.getAmountOfBread() );
82         toasterOutOfBread = false;
83     }
84 }

©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.