blob: 7bb24dfb98c190995f9037de4ba08119dcc91989 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
tom0755a362014-09-24 11:54:43 -070016package org.onlab.onos.foo;
17
Madan Jampani12390c12014-11-12 00:35:56 -080018import static java.util.concurrent.Executors.newScheduledThreadPool;
19import static org.onlab.util.Tools.namedThreads;
20import static org.slf4j.LoggerFactory.getLogger;
21
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080022import java.nio.ByteBuffer;
23import java.util.concurrent.ScheduledExecutorService;
Madan Jampanif5d263b2014-11-13 10:04:40 -080024//import java.util.concurrent.TimeUnit;
25
26
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080027
tom0755a362014-09-24 11:54:43 -070028import org.apache.felix.scr.annotations.Activate;
29import org.apache.felix.scr.annotations.Component;
30import org.apache.felix.scr.annotations.Deactivate;
31import org.apache.felix.scr.annotations.Reference;
32import org.apache.felix.scr.annotations.ReferenceCardinality;
33import org.onlab.onos.cluster.ClusterEvent;
34import org.onlab.onos.cluster.ClusterEventListener;
35import org.onlab.onos.cluster.ClusterService;
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080036import org.onlab.onos.cluster.NodeId;
37import org.onlab.onos.mastership.MastershipEvent;
38import org.onlab.onos.mastership.MastershipListener;
39import org.onlab.onos.mastership.MastershipService;
tom0768a022014-09-24 16:16:16 -070040import org.onlab.onos.net.device.DeviceEvent;
41import org.onlab.onos.net.device.DeviceListener;
42import org.onlab.onos.net.device.DeviceService;
tom4e969042014-10-07 00:47:30 -070043import org.onlab.onos.net.intent.IntentEvent;
44import org.onlab.onos.net.intent.IntentListener;
45import org.onlab.onos.net.intent.IntentService;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080046import org.onlab.onos.store.service.DatabaseAdminService;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080047import org.onlab.onos.store.service.DatabaseService;
Madan Jampanif5d263b2014-11-13 10:04:40 -080048import org.onlab.onos.store.service.Lock;
49import org.onlab.onos.store.service.LockService;
Madan Jampani12390c12014-11-12 00:35:56 -080050import org.onlab.onos.store.service.VersionedValue;
tom0755a362014-09-24 11:54:43 -070051import org.slf4j.Logger;
52
tom0755a362014-09-24 11:54:43 -070053/**
54 * Playground app component.
55 */
56@Component(immediate = true)
57public class FooComponent {
58
59 private final Logger log = getLogger(getClass());
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected ClusterService clusterService;
63
tom0768a022014-09-24 16:16:16 -070064 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected DeviceService deviceService;
66
tom4e969042014-10-07 00:47:30 -070067 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected IntentService intentService;
69
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080070 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected MastershipService mastershipService;
72
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080073 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
74 protected DatabaseAdminService dbAdminService;
75
76 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
77 protected DatabaseService dbService;
78
Madan Jampanif5d263b2014-11-13 10:04:40 -080079 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
80 protected LockService lockService;
81
tom0768a022014-09-24 16:16:16 -070082 private final ClusterEventListener clusterListener = new InnerClusterListener();
83 private final DeviceListener deviceListener = new InnerDeviceListener();
tom4e969042014-10-07 00:47:30 -070084 private final IntentListener intentListener = new InnerIntentListener();
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080085 private final MastershipListener mastershipListener = new InnerMastershipListener();
tom0755a362014-09-24 11:54:43 -070086
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080087 private ScheduledExecutorService executor;
88
tom0755a362014-09-24 11:54:43 -070089 @Activate
90 public void activate() {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080091 executor = newScheduledThreadPool(4, namedThreads("foo-executor-%d"));
92
tom0755a362014-09-24 11:54:43 -070093 clusterService.addListener(clusterListener);
tom0768a022014-09-24 16:16:16 -070094 deviceService.addListener(deviceListener);
tom4e969042014-10-07 00:47:30 -070095 intentService.addListener(intentListener);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080096 mastershipService.addListener(mastershipListener);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080097
98 if (dbService == null || dbAdminService == null) {
99 log.info("Couldn't find DB service");
100 } else {
101 log.info("Found DB service");
Madan Jampanif5d263b2014-11-13 10:04:40 -0800102 //longIncrementor();
103 //lockUnlock();
104 //executor.scheduleAtFixedRate(new LongIncrementor(), 1, 10, TimeUnit.SECONDS);
105 //executor.scheduleAtFixedRate(new LongIncrementor(), 1, 10, TimeUnit.SECONDS);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800106 }
tom0755a362014-09-24 11:54:43 -0700107 log.info("Started");
108 }
109
110 @Deactivate
111 public void deactivate() {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800112 executor.shutdown();
tom0755a362014-09-24 11:54:43 -0700113 clusterService.removeListener(clusterListener);
tom0768a022014-09-24 16:16:16 -0700114 deviceService.removeListener(deviceListener);
tom4e969042014-10-07 00:47:30 -0700115 intentService.removeListener(intentListener);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800116 mastershipService.removeListener(mastershipListener);
tom0755a362014-09-24 11:54:43 -0700117 log.info("Stopped");
118 }
119
120 private class InnerClusterListener implements ClusterEventListener {
121 @Override
122 public void event(ClusterEvent event) {
123 log.info("WOOOOT! {}", event);
124 }
125 }
tom0768a022014-09-24 16:16:16 -0700126
127 private class InnerDeviceListener implements DeviceListener {
128 @Override
129 public void event(DeviceEvent event) {
130 log.info("YEEEEHAAAAW! {}", event);
131 }
132 }
tom4e969042014-10-07 00:47:30 -0700133
134 private class InnerIntentListener implements IntentListener {
135 @Override
136 public void event(IntentEvent event) {
137 String message;
138 if (event.type() == IntentEvent.Type.SUBMITTED) {
139 message = "WOW! It looks like someone has some intentions: {}";
140 } else if (event.type() == IntentEvent.Type.INSTALLED) {
141 message = "AWESOME! So far things are going great: {}";
142 } else if (event.type() == IntentEvent.Type.WITHDRAWN) {
143 message = "HMMM! Ambitions are fading apparently: {}";
144 } else {
145 message = "CRAP!!! Things are not turning out as intended: {}";
146 }
147 log.info(message, event.subject());
148 }
149 }
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800150
151 private class InnerMastershipListener implements MastershipListener {
152 @Override
153 public void event(MastershipEvent event) {
154 final NodeId myId = clusterService.getLocalNode().id();
155 if (myId.equals(event.roleInfo().master())) {
156 log.info("I have control/I wish you luck {}", event);
157 } else {
158 log.info("you have control {}", event);
159 }
160 }
161 }
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800162
Madan Jampani71582ed2014-11-18 10:06:01 -0800163 private void lockUnlock() throws InterruptedException {
Madan Jampanif5d263b2014-11-13 10:04:40 -0800164 try {
165 final String locksTable = "onos-locks";
166
167 if (!dbAdminService.listTables().contains(locksTable)) {
168 dbAdminService.createTable(locksTable, 10000);
169 }
170 Lock lock = lockService.create("foo-bar");
171 log.info("Requesting lock");
172 lock.lock(10000);
173 //try {
174 //Thread.sleep(5000);
175 //} catch (InterruptedException e) {
176 // TODO Auto-generated catch block
177 //e.printStackTrace();
178 //}
179 log.info("Acquired Lock");
180 log.info("Do I have the lock: {} ", lock.isLocked());
181 //lock.unlock();
182 log.info("Do I have the lock: {} ", lock.isLocked());
183 } finally {
184 log.info("Done");
185 }
186 }
187
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800188 private void longIncrementor() {
189 try {
190 final String someTable = "admin";
191 final String someKey = "long";
192
Yuta HIGUCHI7a0ed162014-11-12 10:06:38 -0800193 if (!dbAdminService.listTables().contains(someTable)) {
194 dbAdminService.createTable(someTable);
195 }
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800196
Madan Jampani12390c12014-11-12 00:35:56 -0800197 VersionedValue vv = dbService.get(someTable, someKey);
198 if (vv == null) {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800199 ByteBuffer zero = ByteBuffer.allocate(Long.BYTES).putLong(0);
Madan Jampani12390c12014-11-12 00:35:56 -0800200 if (dbService.putIfAbsent(someTable, someKey, zero.array())) {
Madan Jampani650840b2014-11-12 01:10:57 -0800201 log.info("Wrote initial value");
202 vv = dbService.get(someTable, someKey);
Madan Jampani12390c12014-11-12 00:35:56 -0800203 } else {
Madan Jampani650840b2014-11-12 01:10:57 -0800204 log.info("Concurrent write detected.");
205 // concurrent write detected, read and fall through
206 vv = dbService.get(someTable, someKey);
207 if (vv == null) {
208 log.error("Shouldn't reach here");
209 }
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800210 }
211 }
212 int retry = 5;
Ray Milkeyc5cd0d92014-11-14 14:05:36 -0800213
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800214 do {
Ray Milkeyc5cd0d92014-11-14 14:05:36 -0800215 if (vv == null) {
216 log.error("Shouldn't reach here - value is null");
217 break;
218 }
Madan Jampani12390c12014-11-12 00:35:56 -0800219 ByteBuffer prev = ByteBuffer.wrap(vv.value());
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800220 long next = prev.getLong() + 1;
221 byte[] newValue = ByteBuffer.allocate(Long.BYTES).putLong(next).array();
Madan Jampani12390c12014-11-12 00:35:56 -0800222 if (dbService.putIfVersionMatches(someTable, someKey, newValue, vv.version())) {
223 log.info("Write success. New value: {}", next);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800224 break;
225 } else {
Yuta HIGUCHI1fea0b62014-11-07 00:38:35 -0800226 log.info("Write failed trying to write {}", next);
Madan Jampani12390c12014-11-12 00:35:56 -0800227 vv = dbService.get(someTable, someKey);
228 if (vv == null) {
Yuta HIGUCHI1fea0b62014-11-07 00:38:35 -0800229 log.error("Shouldn't reach here");
230 }
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800231 }
Yuta HIGUCHIc6b8f612014-11-06 19:04:13 -0800232 } while (retry-- > 0);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800233 } catch (Exception e) {
234 log.error("Exception thrown", e);
235 }
236 }
237
238 private final class LongIncrementor implements Runnable {
239
240 @Override
241 public void run() {
242 longIncrementor();
243 }
244
245 }
tom0755a362014-09-24 11:54:43 -0700246}
247
248