Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 16 | package org.onlab.onos.foo; |
| 17 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 18 | import java.nio.ByteBuffer; |
| 19 | import java.util.concurrent.ScheduledExecutorService; |
| 20 | |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onlab.onos.cluster.ClusterEvent; |
| 27 | import org.onlab.onos.cluster.ClusterEventListener; |
| 28 | import org.onlab.onos.cluster.ClusterService; |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 29 | import org.onlab.onos.cluster.NodeId; |
| 30 | import org.onlab.onos.mastership.MastershipEvent; |
| 31 | import org.onlab.onos.mastership.MastershipListener; |
| 32 | import org.onlab.onos.mastership.MastershipService; |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.device.DeviceEvent; |
| 34 | import org.onlab.onos.net.device.DeviceListener; |
| 35 | import org.onlab.onos.net.device.DeviceService; |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.intent.IntentEvent; |
| 37 | import org.onlab.onos.net.intent.IntentListener; |
| 38 | import org.onlab.onos.net.intent.IntentService; |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 39 | import org.onlab.onos.store.service.DatabaseAdminService; |
| 40 | import org.onlab.onos.store.service.DatabaseException; |
| 41 | import org.onlab.onos.store.service.DatabaseService; |
| 42 | import org.onlab.onos.store.service.OptionalResult; |
| 43 | import org.onlab.onos.store.service.PreconditionFailedException; |
| 44 | import org.onlab.onos.store.service.ReadRequest; |
| 45 | import org.onlab.onos.store.service.ReadResult; |
| 46 | import org.onlab.onos.store.service.WriteRequest; |
| 47 | import org.onlab.onos.store.service.WriteResult; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 48 | import org.slf4j.Logger; |
| 49 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 50 | import static org.onlab.util.Tools.namedThreads; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 51 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 52 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Playground app component. |
| 56 | */ |
| 57 | @Component(immediate = true) |
| 58 | public class FooComponent { |
| 59 | |
| 60 | private final Logger log = getLogger(getClass()); |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected ClusterService clusterService; |
| 64 | |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected DeviceService deviceService; |
| 67 | |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected IntentService intentService; |
| 70 | |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected MastershipService mastershipService; |
| 73 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 74 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY) |
| 75 | protected DatabaseAdminService dbAdminService; |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY) |
| 78 | protected DatabaseService dbService; |
| 79 | |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 80 | private final ClusterEventListener clusterListener = new InnerClusterListener(); |
| 81 | private final DeviceListener deviceListener = new InnerDeviceListener(); |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 82 | private final IntentListener intentListener = new InnerIntentListener(); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 83 | private final MastershipListener mastershipListener = new InnerMastershipListener(); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 84 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 85 | private ScheduledExecutorService executor; |
| 86 | |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 87 | @Activate |
| 88 | public void activate() { |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 89 | executor = newScheduledThreadPool(4, namedThreads("foo-executor-%d")); |
| 90 | |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 91 | clusterService.addListener(clusterListener); |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 92 | deviceService.addListener(deviceListener); |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 93 | intentService.addListener(intentListener); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 94 | mastershipService.addListener(mastershipListener); |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 95 | |
| 96 | if (dbService == null || dbAdminService == null) { |
| 97 | log.info("Couldn't find DB service"); |
| 98 | } else { |
| 99 | log.info("Found DB service"); |
| 100 | // longIncrementor(); |
| 101 | // executor.scheduleAtFixedRate(new LongIncrementor(), 1, 10, TimeUnit.SECONDS); |
| 102 | // executor.scheduleAtFixedRate(new LongIncrementor(), 1, 10, TimeUnit.SECONDS); |
| 103 | } |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 104 | log.info("Started"); |
| 105 | } |
| 106 | |
| 107 | @Deactivate |
| 108 | public void deactivate() { |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 109 | executor.shutdown(); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 110 | clusterService.removeListener(clusterListener); |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 111 | deviceService.removeListener(deviceListener); |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 112 | intentService.removeListener(intentListener); |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 113 | mastershipService.removeListener(mastershipListener); |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 114 | log.info("Stopped"); |
| 115 | } |
| 116 | |
| 117 | private class InnerClusterListener implements ClusterEventListener { |
| 118 | @Override |
| 119 | public void event(ClusterEvent event) { |
| 120 | log.info("WOOOOT! {}", event); |
| 121 | } |
| 122 | } |
tom | 0768a02 | 2014-09-24 16:16:16 -0700 | [diff] [blame] | 123 | |
| 124 | private class InnerDeviceListener implements DeviceListener { |
| 125 | @Override |
| 126 | public void event(DeviceEvent event) { |
| 127 | log.info("YEEEEHAAAAW! {}", event); |
| 128 | } |
| 129 | } |
tom | 4e96904 | 2014-10-07 00:47:30 -0700 | [diff] [blame] | 130 | |
| 131 | private class InnerIntentListener implements IntentListener { |
| 132 | @Override |
| 133 | public void event(IntentEvent event) { |
| 134 | String message; |
| 135 | if (event.type() == IntentEvent.Type.SUBMITTED) { |
| 136 | message = "WOW! It looks like someone has some intentions: {}"; |
| 137 | } else if (event.type() == IntentEvent.Type.INSTALLED) { |
| 138 | message = "AWESOME! So far things are going great: {}"; |
| 139 | } else if (event.type() == IntentEvent.Type.WITHDRAWN) { |
| 140 | message = "HMMM! Ambitions are fading apparently: {}"; |
| 141 | } else { |
| 142 | message = "CRAP!!! Things are not turning out as intended: {}"; |
| 143 | } |
| 144 | log.info(message, event.subject()); |
| 145 | } |
| 146 | } |
Yuta HIGUCHI | 0c6e184 | 2014-11-05 22:34:23 -0800 | [diff] [blame] | 147 | |
| 148 | private class InnerMastershipListener implements MastershipListener { |
| 149 | @Override |
| 150 | public void event(MastershipEvent event) { |
| 151 | final NodeId myId = clusterService.getLocalNode().id(); |
| 152 | if (myId.equals(event.roleInfo().master())) { |
| 153 | log.info("I have control/I wish you luck {}", event); |
| 154 | } else { |
| 155 | log.info("you have control {}", event); |
| 156 | } |
| 157 | } |
| 158 | } |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 159 | |
| 160 | private void longIncrementor() { |
| 161 | try { |
| 162 | final String someTable = "admin"; |
| 163 | final String someKey = "long"; |
| 164 | |
| 165 | dbAdminService.createTable(someTable); |
| 166 | |
| 167 | ReadResult read = dbService.read(ReadRequest.get(someTable, someKey)); |
| 168 | if (!read.valueExists()) { |
| 169 | ByteBuffer zero = ByteBuffer.allocate(Long.BYTES).putLong(0); |
| 170 | try { |
| 171 | dbService.write(WriteRequest |
| 172 | .putIfAbsent(someTable, |
| 173 | someKey, |
| 174 | zero.array())); |
| 175 | log.info("Wrote initial value"); |
| 176 | read = dbService.read(ReadRequest.get(someTable, someKey)); |
| 177 | } catch (PreconditionFailedException e) { |
| 178 | log.info("Concurrent write detected.", e); |
| 179 | |
| 180 | // concurrent write detected, read and fall through |
| 181 | read = dbService.read(ReadRequest.get(someTable, someKey)); |
| 182 | if (!read.valueExists()) { |
| 183 | log.error("Shouldn't reach here"); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | int retry = 5; |
| 188 | do { |
| 189 | ByteBuffer prev = ByteBuffer.wrap(read.value().value()); |
| 190 | long next = prev.getLong() + 1; |
| 191 | byte[] newValue = ByteBuffer.allocate(Long.BYTES).putLong(next).array(); |
| 192 | OptionalResult<WriteResult, DatabaseException> result |
| 193 | = dbService.writeNothrow(WriteRequest |
| 194 | .putIfVersionMatches(someTable, |
| 195 | someKey, |
| 196 | newValue, |
| 197 | read.value().version())); |
| 198 | if (result.hasValidResult()) { |
| 199 | log.info("Write success {} -> {}", result.get().previousValue(), next); |
| 200 | break; |
| 201 | } else { |
| 202 | log.info("Write failed trying to write{}", next); |
| 203 | } |
| 204 | } while(retry-- > 0); |
| 205 | } catch (Exception e) { |
| 206 | log.error("Exception thrown", e); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | private final class LongIncrementor implements Runnable { |
| 211 | |
| 212 | @Override |
| 213 | public void run() { |
| 214 | longIncrementor(); |
| 215 | } |
| 216 | |
| 217 | } |
tom | 0755a36 | 2014-09-24 11:54:43 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |