blob: db8514d57f76d210948ad7a08aee27bcf9a7b6c9 [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 Jampani12390c12014-11-12 00:35:56 -080024import java.util.concurrent.TimeUnit;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080025
tom0755a362014-09-24 11:54:43 -070026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.onlab.onos.cluster.ClusterEvent;
32import org.onlab.onos.cluster.ClusterEventListener;
33import org.onlab.onos.cluster.ClusterService;
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080034import org.onlab.onos.cluster.NodeId;
35import org.onlab.onos.mastership.MastershipEvent;
36import org.onlab.onos.mastership.MastershipListener;
37import org.onlab.onos.mastership.MastershipService;
tom0768a022014-09-24 16:16:16 -070038import org.onlab.onos.net.device.DeviceEvent;
39import org.onlab.onos.net.device.DeviceListener;
40import org.onlab.onos.net.device.DeviceService;
tom4e969042014-10-07 00:47:30 -070041import org.onlab.onos.net.intent.IntentEvent;
42import org.onlab.onos.net.intent.IntentListener;
43import org.onlab.onos.net.intent.IntentService;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080044import org.onlab.onos.store.service.DatabaseAdminService;
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080045import org.onlab.onos.store.service.DatabaseService;
Madan Jampani12390c12014-11-12 00:35:56 -080046import org.onlab.onos.store.service.VersionedValue;
tom0755a362014-09-24 11:54:43 -070047import org.slf4j.Logger;
48
tom0755a362014-09-24 11:54:43 -070049/**
50 * Playground app component.
51 */
52@Component(immediate = true)
53public class FooComponent {
54
55 private final Logger log = getLogger(getClass());
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected ClusterService clusterService;
59
tom0768a022014-09-24 16:16:16 -070060 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected DeviceService deviceService;
62
tom4e969042014-10-07 00:47:30 -070063 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected IntentService intentService;
65
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080066 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected MastershipService mastershipService;
68
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080069 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
70 protected DatabaseAdminService dbAdminService;
71
72 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY)
73 protected DatabaseService dbService;
74
tom0768a022014-09-24 16:16:16 -070075 private final ClusterEventListener clusterListener = new InnerClusterListener();
76 private final DeviceListener deviceListener = new InnerDeviceListener();
tom4e969042014-10-07 00:47:30 -070077 private final IntentListener intentListener = new InnerIntentListener();
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080078 private final MastershipListener mastershipListener = new InnerMastershipListener();
tom0755a362014-09-24 11:54:43 -070079
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080080 private ScheduledExecutorService executor;
81
tom0755a362014-09-24 11:54:43 -070082 @Activate
83 public void activate() {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080084 executor = newScheduledThreadPool(4, namedThreads("foo-executor-%d"));
85
tom0755a362014-09-24 11:54:43 -070086 clusterService.addListener(clusterListener);
tom0768a022014-09-24 16:16:16 -070087 deviceService.addListener(deviceListener);
tom4e969042014-10-07 00:47:30 -070088 intentService.addListener(intentListener);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -080089 mastershipService.addListener(mastershipListener);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080090
91 if (dbService == null || dbAdminService == null) {
92 log.info("Couldn't find DB service");
93 } else {
94 log.info("Found DB service");
Madan Jampani12390c12014-11-12 00:35:56 -080095 longIncrementor();
96 executor.scheduleAtFixedRate(new LongIncrementor(), 1, 10, TimeUnit.SECONDS);
97 executor.scheduleAtFixedRate(new LongIncrementor(), 1, 10, TimeUnit.SECONDS);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -080098 }
tom0755a362014-09-24 11:54:43 -070099 log.info("Started");
100 }
101
102 @Deactivate
103 public void deactivate() {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800104 executor.shutdown();
tom0755a362014-09-24 11:54:43 -0700105 clusterService.removeListener(clusterListener);
tom0768a022014-09-24 16:16:16 -0700106 deviceService.removeListener(deviceListener);
tom4e969042014-10-07 00:47:30 -0700107 intentService.removeListener(intentListener);
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800108 mastershipService.removeListener(mastershipListener);
tom0755a362014-09-24 11:54:43 -0700109 log.info("Stopped");
110 }
111
112 private class InnerClusterListener implements ClusterEventListener {
113 @Override
114 public void event(ClusterEvent event) {
115 log.info("WOOOOT! {}", event);
116 }
117 }
tom0768a022014-09-24 16:16:16 -0700118
119 private class InnerDeviceListener implements DeviceListener {
120 @Override
121 public void event(DeviceEvent event) {
122 log.info("YEEEEHAAAAW! {}", event);
123 }
124 }
tom4e969042014-10-07 00:47:30 -0700125
126 private class InnerIntentListener implements IntentListener {
127 @Override
128 public void event(IntentEvent event) {
129 String message;
130 if (event.type() == IntentEvent.Type.SUBMITTED) {
131 message = "WOW! It looks like someone has some intentions: {}";
132 } else if (event.type() == IntentEvent.Type.INSTALLED) {
133 message = "AWESOME! So far things are going great: {}";
134 } else if (event.type() == IntentEvent.Type.WITHDRAWN) {
135 message = "HMMM! Ambitions are fading apparently: {}";
136 } else {
137 message = "CRAP!!! Things are not turning out as intended: {}";
138 }
139 log.info(message, event.subject());
140 }
141 }
Yuta HIGUCHI0c6e1842014-11-05 22:34:23 -0800142
143 private class InnerMastershipListener implements MastershipListener {
144 @Override
145 public void event(MastershipEvent event) {
146 final NodeId myId = clusterService.getLocalNode().id();
147 if (myId.equals(event.roleInfo().master())) {
148 log.info("I have control/I wish you luck {}", event);
149 } else {
150 log.info("you have control {}", event);
151 }
152 }
153 }
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800154
155 private void longIncrementor() {
156 try {
157 final String someTable = "admin";
158 final String someKey = "long";
159
160 dbAdminService.createTable(someTable);
161
Madan Jampani12390c12014-11-12 00:35:56 -0800162 VersionedValue vv = dbService.get(someTable, someKey);
163 if (vv == null) {
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800164 ByteBuffer zero = ByteBuffer.allocate(Long.BYTES).putLong(0);
Madan Jampani12390c12014-11-12 00:35:56 -0800165 if (dbService.putIfAbsent(someTable, someKey, zero.array())) {
166 log.info("Wrote initial value");
167 vv = dbService.get(someTable, someKey);
168 } else {
169 log.info("Concurrent write detected.");
170 // concurrent write detected, read and fall through
171 vv = dbService.get(someTable, someKey);
172 if (vv == null) {
173 log.error("Shouldn't reach here");
174 }
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800175 }
176 }
177 int retry = 5;
178 do {
Madan Jampani12390c12014-11-12 00:35:56 -0800179 ByteBuffer prev = ByteBuffer.wrap(vv.value());
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800180 long next = prev.getLong() + 1;
181 byte[] newValue = ByteBuffer.allocate(Long.BYTES).putLong(next).array();
Madan Jampani12390c12014-11-12 00:35:56 -0800182 if (dbService.putIfVersionMatches(someTable, someKey, newValue, vv.version())) {
183 log.info("Write success. New value: {}", next);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800184 break;
185 } else {
Yuta HIGUCHI1fea0b62014-11-07 00:38:35 -0800186 log.info("Write failed trying to write {}", next);
Madan Jampani12390c12014-11-12 00:35:56 -0800187 vv = dbService.get(someTable, someKey);
188 if (vv == null) {
Yuta HIGUCHI1fea0b62014-11-07 00:38:35 -0800189 log.error("Shouldn't reach here");
190 }
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800191 }
Yuta HIGUCHIc6b8f612014-11-06 19:04:13 -0800192 } while (retry-- > 0);
Yuta HIGUCHI361664e2014-11-06 17:28:47 -0800193 } catch (Exception e) {
194 log.error("Exception thrown", e);
195 }
196 }
197
198 private final class LongIncrementor implements Runnable {
199
200 @Override
201 public void run() {
202 longIncrementor();
203 }
204
205 }
tom0755a362014-09-24 11:54:43 -0700206}
207
208