X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fyang-prototype%2Fsal%2Fsamples%2Ftoaster-consumer%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsample%2Ftoaster%2Fprovider%2Fimpl%2FToastConsumerImpl.java;fp=opendaylight%2Fsal%2Fyang-prototype%2Fsal%2Fsamples%2Ftoaster-consumer%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsample%2Ftoaster%2Fprovider%2Fimpl%2FToastConsumerImpl.java;h=71ef68d565581866380f8e9cfe173a235dfc6d19;hb=35620f5ccf1ed4d1e4ac78a97fff3c1fede8ad95;hp=0000000000000000000000000000000000000000;hpb=42183ad5bfefff7d6de9df467cdaa600a450af29;p=controller.git diff --git a/opendaylight/sal/yang-prototype/sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/toaster/provider/impl/ToastConsumerImpl.java b/opendaylight/sal/yang-prototype/sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/toaster/provider/impl/ToastConsumerImpl.java new file mode 100644 index 0000000000..71ef68d565 --- /dev/null +++ b/opendaylight/sal/yang-prototype/sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/toaster/provider/impl/ToastConsumerImpl.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.sample.toaster.provider.impl; + +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.concurrent.ExecutionException; + +import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext; +import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; +import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer; +import org.opendaylight.controller.sal.binding.api.NotificationListener; +import org.opendaylight.controller.sal.binding.api.NotificationService; +import org.opendaylight.controller.sample.toaster.provider.api.ToastConsumer; +import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev20091120.MakeToastInputBuilder; +import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev20091120.ToastDone; +import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev20091120.ToastType; +import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev20091120.ToasterService; +import org.opendaylight.yangtools.yang.common.RpcResult; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ToastConsumerImpl implements BundleActivator, BindingAwareConsumer, ToastConsumer, + NotificationListener { + + private static final Logger log = LoggerFactory.getLogger(ToastConsumerImpl.class); + + private ToasterService toaster; + + private ConsumerContext session; + + @Override + public boolean createToast(Class type, int doneness) { + MakeToastInputBuilder toastInput = new MakeToastInputBuilder(); + toastInput.setToasterDoneness((long) doneness); + toastInput.setToasterToastType(type); + + try { + RpcResult result = getToastService().makeToast(toastInput.build()).get(); + + if (result.isSuccessful()) { + log.info("Toast was successfuly finished"); + } else { + log.info("Toast was not successfuly finished"); + } + return result.isSuccessful(); + } catch (InterruptedException | ExecutionException e) { + log.info("Error occured during toast creation"); + } + return false; + + } + + @Override + public void onSessionInitialized(ConsumerContext session) { + this.session = session; + NotificationService notificationService = session.getSALService(NotificationService.class); + notificationService.addNotificationListener(ToastDone.class, this); + + } + + @Override + public void start(BundleContext context) throws Exception { + ServiceReference brokerRef = context.getServiceReference(BindingAwareBroker.class); + BindingAwareBroker broker = context.getService(brokerRef); + broker.registerConsumer(this, context); + Dictionary properties = new Hashtable<>(); + context.registerService(ToastConsumer.class, this, properties); + } + + @Override + public void stop(BundleContext context) throws Exception { + // TODO Auto-generated method stub + + } + + @Override + public void onNotification(ToastDone notification) { + log.info("ToastDone Notification Received: {} ",notification.getToastStatus()); + + } + + private ToasterService getToastService() { + if (toaster == null) { + toaster = session.getRpcService(ToasterService.class); + } + return toaster; + } + +}