2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sample.toaster.provider.impl;
10 import java.util.Dictionary;
11 import java.util.Hashtable;
12 import java.util.concurrent.ExecutionException;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
17 import org.opendaylight.controller.sal.binding.api.NotificationListener;
18 import org.opendaylight.controller.sal.binding.api.NotificationService;
19 import org.opendaylight.controller.sample.toaster.provider.api.ToastConsumer;
20 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev20091120.MakeToastInputBuilder;
21 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev20091120.ToastDone;
22 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev20091120.ToastType;
23 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev20091120.ToasterService;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.osgi.framework.BundleActivator;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.ServiceReference;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class ToastConsumerImpl implements BundleActivator, BindingAwareConsumer, ToastConsumer,
32 NotificationListener<ToastDone> {
34 private static final Logger log = LoggerFactory.getLogger(ToastConsumerImpl.class);
36 private ToasterService toaster;
38 private ConsumerContext session;
41 public boolean createToast(Class<? extends ToastType> type, int doneness) {
42 MakeToastInputBuilder toastInput = new MakeToastInputBuilder();
43 toastInput.setToasterDoneness((long) doneness);
44 toastInput.setToasterToastType(type);
47 RpcResult<Void> result = getToastService().makeToast(toastInput.build()).get();
49 if (result.isSuccessful()) {
50 log.info("Toast was successfuly finished");
52 log.info("Toast was not successfuly finished");
54 return result.isSuccessful();
55 } catch (InterruptedException | ExecutionException e) {
56 log.info("Error occured during toast creation");
63 public void onSessionInitialized(ConsumerContext session) {
64 this.session = session;
65 NotificationService notificationService = session.getSALService(NotificationService.class);
66 notificationService.addNotificationListener(ToastDone.class, this);
71 public void start(BundleContext context) throws Exception {
72 ServiceReference<BindingAwareBroker> brokerRef = context.getServiceReference(BindingAwareBroker.class);
73 BindingAwareBroker broker = context.getService(brokerRef);
74 broker.registerConsumer(this, context);
75 Dictionary<String, String> properties = new Hashtable<>();
76 context.registerService(ToastConsumer.class, this, properties);
80 public void stop(BundleContext context) throws Exception {
81 // TODO Auto-generated method stub
86 public void onNotification(ToastDone notification) {
87 log.info("ToastDone Notification Received: {} ",notification.getToastStatus());
91 private ToasterService getToastService() {
92 if (toaster == null) {
93 toaster = session.getRpcService(ToasterService.class);