blob: 268f6f6291c9bc55a13b23af05685d82a4e0a0e6 [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 HIGUCHI1a22cc02014-11-26 13:38:44 -080047import org.onlab.onos.store.service.DatabaseException;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080048import org.onlab.onos.store.service.DatabaseService;
Madan Jampanif5d263b2014-11-13 10:04:40 -080049import org.onlab.onos.store.service.Lock;
50import org.onlab.onos.store.service.LockService;
Madan Jampani12390c12014-11-12 00:35:56 -080051import org.onlab.onos.store.service.VersionedValue;
tom0755a362014-09-24 11:54:43 -070052import org.slf4j.Logger;
53
tom0755a362014-09-24 11:54:43 -070054/**
55 * Playground app component.
56 */
57@Component(immediate = true)
58public class FooComponent {
59
60 private final Logger log = getLogger(getClass());
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected ClusterService clusterService;
64
tom0768a022014-09-24 16:16:16 -070065 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected DeviceService deviceService;
67
tom4e969042014-10-07 00:47:30 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected IntentService intentService;
70
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080071 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected MastershipService mastershipService;
73
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080074 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
75 protected DatabaseAdminService dbAdminService;
76
77 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
78 protected DatabaseService dbService;
79
Madan Jampanif5d263b2014-11-13 10:04:40 -080080 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
81 protected LockService lockService;
82
tom0768a022014-09-24 16:16:16 -070083 private final ClusterEventListener clusterListener = new InnerClusterListener();
84 private final DeviceListener deviceListener = new InnerDeviceListener();
tom4e969042014-10-07 00:47:30 -070085 private final IntentListener intentListener = new InnerIntentListener();
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080086 private final MastershipListener mastershipListener = new InnerMastershipListener();
tom0755a362014-09-24 11:54:43 -070087
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080088 private ScheduledExecutorService executor;
89
tom0755a362014-09-24 11:54:43 -070090 @Activate
91 public void activate() {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080092 executor = newScheduledThreadPool(4, namedThreads("foo-executor-%d"));
93
tom0755a362014-09-24 11:54:43 -070094 clusterService.addListener(clusterListener);
tom0768a022014-09-24 16:16:16 -070095 deviceService.addListener(deviceListener);
tom4e969042014-10-07 00:47:30 -070096 intentService.addListener(intentListener);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080097 mastershipService.addListener(mastershipListener);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080098
99 if (dbService == null || dbAdminService == null) {
100 log.info("Couldn't find DB service");
101 } else {
102 log.info("Found DB service");
Madan Jampanif5d263b2014-11-13 10:04:40 -0800103 //longIncrementor();
104 //lockUnlock();
105 //executor.scheduleAtFixedRate(new LongIncrementor(), 1, 10, TimeUnit.SECONDS);
106 //executor.scheduleAtFixedRate(new LongIncrementor(), 1, 10, TimeUnit.SECONDS);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800107 }
tom0755a362014-09-24 11:54:43 -0700108 log.info("Started");
109 }
110
111 @Deactivate
112 public void deactivate() {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800113 executor.shutdown();
tom0755a362014-09-24 11:54:43 -0700114 clusterService.removeListener(clusterListener);
tom0768a022014-09-24 16:16:16 -0700115 deviceService.removeListener(deviceListener);
tom4e969042014-10-07 00:47:30 -0700116 intentService.removeListener(intentListener);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800117 mastershipService.removeListener(mastershipListener);
tom0755a362014-09-24 11:54:43 -0700118 log.info("Stopped");
119 }
120
121 private class InnerClusterListener implements ClusterEventListener {
122 @Override
123 public void event(ClusterEvent event) {
124 log.info("WOOOOT! {}", event);
125 }
126 }
tom0768a022014-09-24 16:16:16 -0700127
128 private class InnerDeviceListener implements DeviceListener {
129 @Override
130 public void event(DeviceEvent event) {
131 log.info("YEEEEHAAAAW! {}", event);
132 }
133 }
tom4e969042014-10-07 00:47:30 -0700134
135 private class InnerIntentListener implements IntentListener {
136 @Override
137 public void event(IntentEvent event) {
138 String message;
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800139 if (event.type() == IntentEvent.Type.INSTALL_REQ) {
tom4e969042014-10-07 00:47:30 -0700140 message = "WOW! It looks like someone has some intentions: {}";
141 } else if (event.type() == IntentEvent.Type.INSTALLED) {
142 message = "AWESOME! So far things are going great: {}";
143 } else if (event.type() == IntentEvent.Type.WITHDRAWN) {
144 message = "HMMM! Ambitions are fading apparently: {}";
145 } else {
146 message = "CRAP!!! Things are not turning out as intended: {}";
147 }
148 log.info(message, event.subject());
149 }
150 }
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800151
152 private class InnerMastershipListener implements MastershipListener {
153 @Override
154 public void event(MastershipEvent event) {
155 final NodeId myId = clusterService.getLocalNode().id();
156 if (myId.equals(event.roleInfo().master())) {
157 log.info("I have control/I wish you luck {}", event);
158 } else {
159 log.info("you have control {}", event);
160 }
161 }
162 }
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800163
Madan Jampani71582ed2014-11-18 10:06:01 -0800164 private void lockUnlock() throws InterruptedException {
Madan Jampanif5d263b2014-11-13 10:04:40 -0800165 try {
166 final String locksTable = "onos-locks";
167
168 if (!dbAdminService.listTables().contains(locksTable)) {
169 dbAdminService.createTable(locksTable, 10000);
170 }
171 Lock lock = lockService.create("foo-bar");
172 log.info("Requesting lock");
173 lock.lock(10000);
174 //try {
175 //Thread.sleep(5000);
176 //} catch (InterruptedException e) {
177 // TODO Auto-generated catch block
178 //e.printStackTrace();
179 //}
180 log.info("Acquired Lock");
181 log.info("Do I have the lock: {} ", lock.isLocked());
182 //lock.unlock();
183 log.info("Do I have the lock: {} ", lock.isLocked());
184 } finally {
185 log.info("Done");
186 }
187 }
188
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800189 private void longIncrementor() {
190 try {
191 final String someTable = "admin";
192 final String someKey = "long";
193
Yuta HIGUCHI7a0ed162014-11-12 10:06:38 -0800194 if (!dbAdminService.listTables().contains(someTable)) {
195 dbAdminService.createTable(someTable);
196 }
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800197
Madan Jampani12390c12014-11-12 00:35:56 -0800198 VersionedValue vv = dbService.get(someTable, someKey);
199 if (vv == null) {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800200 ByteBuffer zero = ByteBuffer.allocate(Long.BYTES).putLong(0);
Madan Jampani12390c12014-11-12 00:35:56 -0800201 if (dbService.putIfAbsent(someTable, someKey, zero.array())) {
Madan Jampani650840b2014-11-12 01:10:57 -0800202 log.info("Wrote initial value");
203 vv = dbService.get(someTable, someKey);
Madan Jampani12390c12014-11-12 00:35:56 -0800204 } else {
Madan Jampani650840b2014-11-12 01:10:57 -0800205 log.info("Concurrent write detected.");
206 // concurrent write detected, read and fall through
207 vv = dbService.get(someTable, someKey);
208 if (vv == null) {
209 log.error("Shouldn't reach here");
210 }
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800211 }
212 }
Yuta HIGUCHI1a22cc02014-11-26 13:38:44 -0800213 int retry = 1;
Ray Milkeyc5cd0d92014-11-14 14:05:36 -0800214
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800215 do {
Ray Milkeyc5cd0d92014-11-14 14:05:36 -0800216 if (vv == null) {
217 log.error("Shouldn't reach here - value is null");
218 break;
219 }
Madan Jampani12390c12014-11-12 00:35:56 -0800220 ByteBuffer prev = ByteBuffer.wrap(vv.value());
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800221 long next = prev.getLong() + 1;
222 byte[] newValue = ByteBuffer.allocate(Long.BYTES).putLong(next).array();
Madan Jampani12390c12014-11-12 00:35:56 -0800223 if (dbService.putIfVersionMatches(someTable, someKey, newValue, vv.version())) {
224 log.info("Write success. New value: {}", next);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800225 break;
226 } else {
Yuta HIGUCHI1a22cc02014-11-26 13:38:44 -0800227 log.info("Write failed retrying.....{}", retry);
Madan Jampani12390c12014-11-12 00:35:56 -0800228 vv = dbService.get(someTable, someKey);
229 if (vv == null) {
Yuta HIGUCHI1fea0b62014-11-07 00:38:35 -0800230 log.error("Shouldn't reach here");
231 }
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800232 }
Yuta HIGUCHI1a22cc02014-11-26 13:38:44 -0800233 } while (retry++ < 5);
234 } catch (DatabaseException e) {
235 log.debug("DB Exception thrown {}", e.getMessage());
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800236 } catch (Exception e) {
237 log.error("Exception thrown", e);
238 }
239 }
240
241 private final class LongIncrementor implements Runnable {
242
243 @Override
244 public void run() {
245 longIncrementor();
246 }
247
248 }
tom0755a362014-09-24 11:54:43 -0700249}
250
251