Merge "Topology Manager to avoid redundant edge updates"
[controller.git] / opendaylight / md-sal / samples / toaster-provider / src / main / java / org / opendaylight / controller / sample / toaster / provider / OpendaylightToaster.java
1 /*
2  * Copyright (c) 2014 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;
9
10 import java.util.Collections;
11 import java.util.concurrent.Callable;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.Executors;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.atomic.AtomicLong;
16
17 import org.opendaylight.controller.config.yang.config.toaster_provider.impl.ToasterProviderRuntimeMXBean;
18 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
19 import org.opendaylight.controller.sal.common.util.Rpcs;
20 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.DisplayString;
21 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput;
22 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToastDone.ToastStatus;
23 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToastDoneBuilder;
24 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster;
25 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.Toaster.ToasterStatus;
26 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterBuilder;
27 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterData;
28 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService;
29 import org.opendaylight.yangtools.yang.common.RpcError;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.common.util.concurrent.Futures;
35
36 public class OpendaylightToaster implements ToasterData, ToasterService, ToasterProviderRuntimeMXBean {
37
38     private static final Logger log = LoggerFactory.getLogger(OpendaylightToaster.class);
39
40     private static final DisplayString toasterManufacturer = new DisplayString("Opendaylight");
41     private static final DisplayString toasterModelNumber = new DisplayString("Model 1 - Binding Aware");
42     private final ToasterStatus toasterStatus;
43
44     private NotificationProviderService notificationProvider;
45     private final ExecutorService executor;
46
47     private Future<RpcResult<Void>> currentTask;
48
49     public OpendaylightToaster() {
50         toasterStatus = ToasterStatus.Down;
51         executor = Executors.newFixedThreadPool(1);
52     }
53
54     @Override
55     public Toaster getToaster() {
56         ToasterBuilder tb = new ToasterBuilder();
57         tb //
58         .setToasterManufacturer(toasterManufacturer) //
59                 .setToasterModelNumber(toasterModelNumber) //
60                 .setToasterStatus(toasterStatus);
61
62         return tb.build();
63     }
64
65     @Override
66     public Future<RpcResult<Void>> cancelToast() {
67         if (currentTask != null) {
68             cancelToastImpl();
69         }
70         return null;
71     }
72
73     @Override
74     public Future<RpcResult<Void>> makeToast(MakeToastInput input) {
75         // TODO Auto-generated method stub
76         log.trace("makeToast - Received input for toast");
77         logToastInput(input);
78         if (currentTask != null) {
79             return inProgressError();
80         }
81         currentTask = executor.submit(new MakeToastTask(input));
82         return currentTask;
83     }
84
85     private Future<RpcResult<Void>> inProgressError() {
86         RpcResult<Void> result = Rpcs.<Void> getRpcResult(false, null, Collections.<RpcError> emptySet());
87         return Futures.immediateFuture(result);
88     }
89
90     private void cancelToastImpl() {
91         currentTask.cancel(true);
92         ToastDoneBuilder toastDone = new ToastDoneBuilder();
93         toastDone.setToastStatus(ToastStatus.Cancelled);
94         notificationProvider.publish(toastDone.build());
95     }
96
97     public void setNotificationProvider(NotificationProviderService salService) {
98         this.notificationProvider = salService;
99     }
100
101     private void logToastInput(MakeToastInput input) {
102         String toastType = input.getToasterToastType().getName();
103         String toastDoneness = input.getToasterDoneness().toString();
104         log.trace("Toast: {} doneness: {}", toastType, toastDoneness);
105     }
106
107     private final AtomicLong toastsMade = new AtomicLong(0);
108
109     @Override
110     public Long getToastsMade() {
111         return toastsMade.get();
112     }
113
114     private class MakeToastTask implements Callable<RpcResult<Void>> {
115
116         final MakeToastInput toastRequest;
117
118         public MakeToastTask(MakeToastInput toast) {
119             toastRequest = toast;
120         }
121
122         @Override
123         public RpcResult<Void> call() throws Exception {
124             Thread.sleep(1000);
125
126             ToastDoneBuilder notifyBuilder = new ToastDoneBuilder();
127             notifyBuilder.setToastStatus(ToastStatus.Done);
128             notificationProvider.publish(notifyBuilder.build());
129             log.trace("Toast Done");
130             logToastInput(toastRequest);
131             currentTask = null;
132
133             toastsMade.incrementAndGet();
134
135             return Rpcs.<Void> getRpcResult(true, null, Collections.<RpcError> emptySet());
136         }
137     }
138 }

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