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.Hashtable;
11 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareConsumer;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
16 import org.opendaylight.controller.sal.binding.api.NotificationListener;
17 import org.opendaylight.controller.sal.binding.api.NotificationService;
18 import org.opendaylight.controller.sample.toaster.provider.api.ToastConsumer;
19 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInputBuilder;
20 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToastDone;
21 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToastType;
22 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.osgi.framework.BundleActivator;
25 import org.osgi.framework.BundleContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class ToastConsumerImpl extends AbstractBindingAwareConsumer implements BundleActivator, BindingAwareConsumer, ToastConsumer,
30 NotificationListener<ToastDone> {
32 private static final Logger log = LoggerFactory.getLogger(ToastConsumerImpl.class);
34 private ToasterService toaster;
36 private ConsumerContext session;
39 public boolean createToast(Class<? extends ToastType> type, int doneness) {
40 MakeToastInputBuilder toastInput = new MakeToastInputBuilder();
41 toastInput.setToasterDoneness((long) doneness);
42 toastInput.setToasterToastType(type);
45 RpcResult<Void> result = getToastService().makeToast(toastInput.build()).get();
47 if (result.isSuccessful()) {
48 log.trace("Toast was successfuly finished");
50 log.warn("Toast was not successfuly finished");
52 return result.isSuccessful();
53 } catch (InterruptedException | ExecutionException e) {
54 log.warn("Error occured during toast creation");
62 protected void startImpl(BundleContext context) {
63 context.registerService(ToastConsumer.class, this, new Hashtable<String,String>());
67 public void onSessionInitialized(ConsumerContext session) {
68 this.session = session;
69 NotificationService notificationService = session.getSALService(NotificationService.class);
70 notificationService.registerNotificationListener(ToastDone.class, this);
74 public void onNotification(ToastDone notification) {
75 log.trace("ToastDone Notification Received: {} ",notification.getToastStatus());
79 private ToasterService getToastService() {
80 if (toaster == null) {
81 toaster = session.getRpcService(ToasterService.class);