Merge "Devices dashlet show port total numbers, modal shows collapsible list Replaced...
[controller.git] / opendaylight / sal / yang-prototype / sal / samples / toaster-consumer / src / main / java / org / opendaylight / controller / sample / toaster / provider / impl / ToastConsumerImpl.java
1 /*
2  * Copyright (c) 2013 Cisco 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 package org.opendaylight.controller.sample.toaster.provider.impl;
9
10 import java.util.Dictionary;
11 import java.util.Hashtable;
12 import java.util.concurrent.ExecutionException;
13
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;
30
31 public class ToastConsumerImpl implements BundleActivator, BindingAwareConsumer, ToastConsumer,
32         NotificationListener<ToastDone> {
33
34     private static final Logger log = LoggerFactory.getLogger(ToastConsumerImpl.class);
35
36     private ToasterService toaster;
37
38     private ConsumerContext session;
39
40     @Override
41     public boolean createToast(Class<? extends ToastType> type, int doneness) {
42         MakeToastInputBuilder toastInput = new MakeToastInputBuilder();
43         toastInput.setToasterDoneness((long) doneness);
44         toastInput.setToasterToastType(type);
45
46         try {
47             RpcResult<Void> result = getToastService().makeToast(toastInput.build()).get();
48
49             if (result.isSuccessful()) {
50                 log.info("Toast was successfuly finished");
51             } else {
52                 log.info("Toast was not successfuly finished");
53             }
54             return result.isSuccessful();
55         } catch (InterruptedException | ExecutionException e) {
56             log.info("Error occured during toast creation");
57         }
58         return false;
59
60     }
61
62     @Override
63     public void onSessionInitialized(ConsumerContext session) {
64         this.session = session;
65         NotificationService notificationService = session.getSALService(NotificationService.class);
66         notificationService.addNotificationListener(ToastDone.class, this);
67
68     }
69
70     @Override
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);
77     }
78
79     @Override
80     public void stop(BundleContext context) throws Exception {
81         // TODO Auto-generated method stub
82
83     }
84
85     @Override
86     public void onNotification(ToastDone notification) {
87         log.info("ToastDone Notification Received: {} ",notification.getToastStatus());
88
89     }
90
91     private ToasterService getToastService() {
92         if (toaster == null) {
93             toaster = session.getRpcService(ToasterService.class);
94         }
95         return toaster;
96     }
97
98 }