blob: 8706e854531169ce75ff7c7230a5fa70ce8bbb64 [file] [log] [blame]
Jonathan Hartd82f20d2013-02-21 18:04:24 -08001package net.onrc.onos.registry.controller;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -08002
Jonathan Hartbd181b62013-02-17 16:05:38 -08003import java.io.IOException;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -08004import java.util.ArrayList;
5import java.util.Collection;
Jonathan Hart3d7730a2013-02-22 11:51:17 -08006import java.util.Collections;
Jonathan Hart599c6b32013-03-24 22:42:02 -07007import java.util.Comparator;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -08008import java.util.HashMap;
Jonathan Hartedd6a442013-02-20 15:22:06 -08009import java.util.List;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080010import java.util.Map;
Jonathan Hart89187372013-03-14 16:41:09 -070011import java.util.concurrent.ConcurrentHashMap;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080012
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080013import net.floodlightcontroller.core.module.FloodlightModuleContext;
14import net.floodlightcontroller.core.module.FloodlightModuleException;
15import net.floodlightcontroller.core.module.IFloodlightModule;
16import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hart3d7730a2013-02-22 11:51:17 -080017import net.floodlightcontroller.restserver.IRestApiService;
Naoki Shiotab32edf52013-12-12 14:09:36 -080018import net.onrc.onos.registry.controller.web.RegistryWebRoutable;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080019
Jonathan Hartbd181b62013-02-17 16:05:38 -080020import org.openflow.util.HexString;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080023
Jonathan Hartd10008d2013-02-23 17:04:08 -080024import com.google.common.base.Charsets;
Jonathan Hartbd181b62013-02-17 16:05:38 -080025import com.netflix.curator.RetryPolicy;
26import com.netflix.curator.framework.CuratorFramework;
27import com.netflix.curator.framework.CuratorFrameworkFactory;
Jonathan Hart1530ccc2013-04-03 19:36:02 -070028import com.netflix.curator.framework.recipes.atomic.AtomicValue;
29import com.netflix.curator.framework.recipes.atomic.DistributedAtomicLong;
Jonathan Hartedd6a442013-02-20 15:22:06 -080030import com.netflix.curator.framework.recipes.cache.ChildData;
31import com.netflix.curator.framework.recipes.cache.PathChildrenCache;
32import com.netflix.curator.framework.recipes.cache.PathChildrenCache.StartMode;
Jonathan Hart3d7730a2013-02-22 11:51:17 -080033import com.netflix.curator.framework.recipes.cache.PathChildrenCacheEvent;
34import com.netflix.curator.framework.recipes.cache.PathChildrenCacheListener;
Jonathan Hartbd181b62013-02-17 16:05:38 -080035import com.netflix.curator.framework.recipes.leader.LeaderLatch;
Jonathan Hart0de09492013-03-13 14:37:21 -070036import com.netflix.curator.framework.recipes.leader.LeaderLatchEvent;
37import com.netflix.curator.framework.recipes.leader.LeaderLatchListener;
Jonathan Hart3d7730a2013-02-22 11:51:17 -080038import com.netflix.curator.retry.ExponentialBackoffRetry;
Jonathan Hart1530ccc2013-04-03 19:36:02 -070039import com.netflix.curator.retry.RetryOneTime;
Jonathan Hart71c0ffc2013-03-24 15:58:42 -070040import com.netflix.curator.x.discovery.ServiceCache;
41import com.netflix.curator.x.discovery.ServiceDiscovery;
42import com.netflix.curator.x.discovery.ServiceDiscoveryBuilder;
43import com.netflix.curator.x.discovery.ServiceInstance;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080044import java.util.concurrent.ArrayBlockingQueue;
45import java.util.concurrent.BlockingQueue;
Jonathan Hartbd181b62013-02-17 16:05:38 -080046
Jonathan Hart7bf62172013-02-28 13:17:18 -080047/**
48 * A registry service that uses Zookeeper. All data is stored in Zookeeper,
49 * so this can be used as a global registry in a multi-node ONOS cluster.
50 * @author jono
51 *
52 */
Jonathan Hartbd766972013-02-22 15:13:03 -080053public class ZookeeperRegistry implements IFloodlightModule, IControllerRegistryService {
Jonathan Hartc6eee9e2013-02-18 14:58:27 -080054
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070055 protected final static Logger log = LoggerFactory.getLogger(ZookeeperRegistry.class);
Jonathan Hartbd766972013-02-22 15:13:03 -080056 protected String controllerId = null;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080057
Jonathan Hart3d7730a2013-02-22 11:51:17 -080058 protected IRestApiService restApi;
59
Jonathan Hart7bf62172013-02-28 13:17:18 -080060 //This is the default, it's overwritten by the connectionString configuration parameter
Jonathan Hartbd181b62013-02-17 16:05:38 -080061 protected String connectionString = "localhost:2181";
Jonathan Hart3d7730a2013-02-22 11:51:17 -080062
Jonathan Hartbd181b62013-02-17 16:05:38 -080063 private final String namespace = "onos";
Jonathan Hartedd6a442013-02-20 15:22:06 -080064 private final String switchLatchesPath = "/switches";
Jonathan Hart71c0ffc2013-03-24 15:58:42 -070065
66 private final String SERVICES_PATH = "/"; //i.e. the root of our namespace
67 private final String CONTROLLER_SERVICE_NAME = "controllers";
Jonathan Hartbd181b62013-02-17 16:05:38 -080068
69 protected CuratorFramework client;
Jonathan Hartedd6a442013-02-20 15:22:06 -080070
Jonathan Hart3d7730a2013-02-22 11:51:17 -080071 protected PathChildrenCache switchCache;
Jonathan Hartbd181b62013-02-17 16:05:38 -080072
Jonathan Hart89187372013-03-14 16:41:09 -070073 protected ConcurrentHashMap<String, SwitchLeadershipData> switches;
Jonathan Hart3d7730a2013-02-22 11:51:17 -080074 protected Map<String, PathChildrenCache> switchPathCaches;
Jonathan Hartbd181b62013-02-17 16:05:38 -080075
Jonathan Hart1530ccc2013-04-03 19:36:02 -070076 private final String ID_COUNTER_PATH = "/flowidcounter";
77 private final Long ID_BLOCK_SIZE = 0x100000000L;
78 protected DistributedAtomicLong distributedIdCounter;
79
Jonathan Hart97801ac2013-02-26 14:29:16 -080080 //Zookeeper performance-related configuration
Jonathan Hart0b3eee42013-03-16 18:20:04 -070081 protected static final int sessionTimeout = 5000;
82 protected static final int connectionTimeout = 7000;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080083 private volatile IdBlock idBlock = null;
Jonathan Hart57080fb2013-02-21 10:55:46 -080084
Jonathan Hartbd181b62013-02-17 16:05:38 -080085
Jonathan Hart89187372013-03-14 16:41:09 -070086 protected class SwitchLeaderListener implements LeaderLatchListener{
Jonathan Hart0de09492013-03-13 14:37:21 -070087 String dpid;
88 LeaderLatch latch;
89
Jonathan Hart89187372013-03-14 16:41:09 -070090 public SwitchLeaderListener(String dpid, LeaderLatch latch){
Jonathan Hart0de09492013-03-13 14:37:21 -070091 this.dpid = dpid;
92 this.latch = latch;
93 }
94
95 @Override
96 public void leaderLatchEvent(CuratorFramework arg0,
97 LeaderLatchEvent arg1) {
Jonathan Hart89187372013-03-14 16:41:09 -070098 log.debug("Leadership changed for {}, now {}",
Jonathan Hart0de09492013-03-13 14:37:21 -070099 dpid, latch.hasLeadership());
100
Jonathan Hart89187372013-03-14 16:41:09 -0700101 //Check that the leadership request is still active - the client
102 //may have since released the request or even begun another request
103 //(this is why we use == to check the object instance is the same)
104 SwitchLeadershipData swData = switches.get(dpid);
Naoki Shiota2999e3d2014-01-03 17:22:59 -0800105 if (swData == null) {
Naoki Shiota1a5ca912014-01-03 17:02:31 -0800106 log.debug("Leadership data {} not found", dpid);
Naoki Shiota2999e3d2014-01-03 17:22:59 -0800107 return;
Naoki Shiota1a5ca912014-01-03 17:02:31 -0800108 }
109
110 if (swData.getLatch() == latch){
Jonathan Hart89187372013-03-14 16:41:09 -0700111 swData.getCallback().controlChanged(
112 HexString.toLong(dpid), latch.hasLeadership());
113 }
114 else {
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700115 log.debug("Latch for {} has changed: old latch {} - new latch {}",
116 new Object[]{dpid, latch, swData.getLatch()});
Jonathan Hart89187372013-03-14 16:41:09 -0700117 }
Jonathan Hart0de09492013-03-13 14:37:21 -0700118 }
119 }
120
Naoki Shiotad00accf2013-06-25 14:40:37 -0700121 protected class SwitchPathCacheListener implements PathChildrenCacheListener {
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800122 @Override
123 public void childEvent(CuratorFramework client,
124 PathChildrenCacheEvent event) throws Exception {
Jonathan Hartcbb4b952013-03-18 16:15:18 -0700125 //log.debug("Root switch path cache got {} event", event.getType());
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800126
127 String strSwitch = null;
128 if (event.getData() != null){
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800129 String[] splitted = event.getData().getPath().split("/");
130 strSwitch = splitted[splitted.length - 1];
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800131 }
132
133 switch (event.getType()){
134 case CHILD_ADDED:
135 case CHILD_UPDATED:
136 //Check we have a PathChildrenCache for this child, add one if not
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700137 synchronized (switchPathCaches){
138 if (switchPathCaches.get(strSwitch) == null){
139 PathChildrenCache pc = new PathChildrenCache(client,
140 event.getData().getPath(), true);
141 pc.start(StartMode.NORMAL);
142 switchPathCaches.put(strSwitch, pc);
143 }
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800144 }
145 break;
146 case CHILD_REMOVED:
147 //Remove our PathChildrenCache for this child
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700148 PathChildrenCache pc = null;
149 synchronized(switchPathCaches){
150 pc = switchPathCaches.remove(strSwitch);
151 }
152 if (pc != null){
153 pc.close();
154 }
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800155 break;
156 default:
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700157 //All other events are connection status events. We don't need to
158 //do anything as the path cache handles these on its own.
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800159 break;
160 }
161
162 }
163 };
Naoki Shiotad00accf2013-06-25 14:40:37 -0700164 /**
165 * Listens for changes to the switch znodes in Zookeeper. This maintains
166 * the second level of PathChildrenCaches that hold the controllers
167 * contending for each switch - there's one for each switch.
168 */
169 PathChildrenCacheListener switchPathCacheListener = new SwitchPathCacheListener();
Jonathan Hart71c0ffc2013-03-24 15:58:42 -0700170 protected ServiceDiscovery<ControllerService> serviceDiscovery;
171 protected ServiceCache<ControllerService> serviceCache;
Jonathan Hartedd6a442013-02-20 15:22:06 -0800172
Jonathan Hartbd181b62013-02-17 16:05:38 -0800173
174 @Override
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800175 public void requestControl(long dpid, ControlChangeCallback cb) throws RegistryException {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800176 log.info("Requesting control for {}", HexString.toHexString(dpid));
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800177
Jonathan Hartbd766972013-02-22 15:13:03 -0800178 if (controllerId == null){
179 throw new RuntimeException("Must register a controller before calling requestControl");
Jonathan Hartbd181b62013-02-17 16:05:38 -0800180 }
181
182 String dpidStr = HexString.toHexString(dpid);
183 String latchPath = switchLatchesPath + "/" + dpidStr;
184
Jonathan Hart89187372013-03-14 16:41:09 -0700185 if (switches.get(dpidStr) != null){
Jonathan Hart3c0eccd2013-03-12 22:32:50 -0700186 log.debug("Already contesting {}, returning", HexString.toHexString(dpid));
Pankaj Berdeda7187b2013-03-18 15:24:59 -0700187 throw new RegistryException("Already contesting control for " + dpidStr);
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800188 }
189
Jonathan Hartbd766972013-02-22 15:13:03 -0800190 LeaderLatch latch = new LeaderLatch(client, latchPath, controllerId);
Jonathan Hart89187372013-03-14 16:41:09 -0700191 latch.addListener(new SwitchLeaderListener(dpidStr, latch));
Jonathan Hartbd181b62013-02-17 16:05:38 -0800192
Jonathan Hart44e56fc2013-03-14 16:53:59 -0700193
Jonathan Hart89187372013-03-14 16:41:09 -0700194 SwitchLeadershipData swData = new SwitchLeadershipData(latch, cb);
195 SwitchLeadershipData oldData = switches.putIfAbsent(dpidStr, swData);
196
197 if (oldData != null){
198 //There was already data for that key in the map
199 //i.e. someone else got here first so we can't succeed
200 log.debug("Already requested control for {}", dpidStr);
201 throw new RegistryException("Already requested control for " + dpidStr);
202 }
203
204 //Now that we know we were able to add our latch to the collection,
Jonathan Hart44e56fc2013-03-14 16:53:59 -0700205 //we can start the leader election in Zookeeper. However I don't know
206 //how to handle if the start fails - the latch is already in our
207 //switches list.
208 //TODO seems like there's a Curator bug when latch.start is called when
209 //there's no Zookeeper connection which causes two znodes to be put in
210 //Zookeeper at the latch path when we reconnect to Zookeeper.
Jonathan Hartbd181b62013-02-17 16:05:38 -0800211 try {
Jonathan Hartbd181b62013-02-17 16:05:38 -0800212 latch.start();
213 } catch (Exception e) {
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800214 log.warn("Error starting leader latch: {}", e.getMessage());
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800215 throw new RegistryException("Error starting leader latch for " + dpidStr, e);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800216 }
217
218 }
219
220 @Override
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800221 public void releaseControl(long dpid) {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800222 log.info("Releasing control for {}", HexString.toHexString(dpid));
Jonathan Hart57080fb2013-02-21 10:55:46 -0800223
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800224 String dpidStr = HexString.toHexString(dpid);
225
Jonathan Hart89187372013-03-14 16:41:09 -0700226 SwitchLeadershipData swData = switches.remove(dpidStr);
227
228 if (swData == null) {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800229 log.debug("Trying to release control of a switch we are not contesting");
Jonathan Hartbd181b62013-02-17 16:05:38 -0800230 return;
231 }
Jonathan Hart89187372013-03-14 16:41:09 -0700232
Jonathan Hart89187372013-03-14 16:41:09 -0700233 LeaderLatch latch = swData.getLatch();
Jonathan Hartbd181b62013-02-17 16:05:38 -0800234
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700235 latch.removeAllListeners();
236
Jonathan Hartbd181b62013-02-17 16:05:38 -0800237 try {
238 latch.close();
239 } catch (IOException e) {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800240 //I think it's OK not to do anything here. Either the node got
241 //deleted correctly, or the connection went down and the node got deleted.
Umesh Krishnaswamy0ef75ee2013-03-25 17:50:27 -0700242 log.debug("releaseControl: caught IOException {}", dpidStr);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800243 }
244 }
245
246 @Override
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800247 public boolean hasControl(long dpid) {
Jonathan Hart89187372013-03-14 16:41:09 -0700248 String dpidStr = HexString.toHexString(dpid);
Jonathan Hart57080fb2013-02-21 10:55:46 -0800249
Jonathan Hart89187372013-03-14 16:41:09 -0700250 SwitchLeadershipData swData = switches.get(dpidStr);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800251
Jonathan Hart89187372013-03-14 16:41:09 -0700252 if (swData == null) {
253 log.warn("No leader latch for dpid {}", dpidStr);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800254 return false;
255 }
256
Jonathan Hart89187372013-03-14 16:41:09 -0700257 return swData.getLatch().hasLeadership();
Jonathan Hartbd181b62013-02-17 16:05:38 -0800258 }
259
260 @Override
Jonathan Hart7bf62172013-02-28 13:17:18 -0800261 public String getControllerId() {
Jonathan Hartbd766972013-02-22 15:13:03 -0800262 return controllerId;
Jonathan Hartbd181b62013-02-17 16:05:38 -0800263 }
264
Jonathan Hartedd6a442013-02-20 15:22:06 -0800265 @Override
Jonathan Hart57080fb2013-02-21 10:55:46 -0800266 public Collection<String> getAllControllers() throws RegistryException {
Jonathan Hartedd6a442013-02-20 15:22:06 -0800267 log.debug("Getting all controllers");
Jonathan Hart1be46262013-02-20 16:43:51 -0800268
Jonathan Hartedd6a442013-02-20 15:22:06 -0800269 List<String> controllers = new ArrayList<String>();
Jonathan Hart71c0ffc2013-03-24 15:58:42 -0700270 for (ServiceInstance<ControllerService> instance : serviceCache.getInstances()){
271 String id = instance.getPayload().getControllerId();
272 if (!controllers.contains(id)){
273 controllers.add(id);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800274 }
Jonathan Hartedd6a442013-02-20 15:22:06 -0800275 }
Jonathan Hart71c0ffc2013-03-24 15:58:42 -0700276
Jonathan Hartedd6a442013-02-20 15:22:06 -0800277 return controllers;
278 }
279
280 @Override
Jonathan Hart57080fb2013-02-21 10:55:46 -0800281 public void registerController(String id) throws RegistryException {
Jonathan Hartd10008d2013-02-23 17:04:08 -0800282 if (controllerId != null) {
283 throw new RegistryException(
284 "Controller already registered with id " + controllerId);
285 }
Jonathan Hartbd766972013-02-22 15:13:03 -0800286
287 controllerId = id;
Jonathan Hart57080fb2013-02-21 10:55:46 -0800288
Jonathan Hartedd6a442013-02-20 15:22:06 -0800289 try {
Jonathan Hart71c0ffc2013-03-24 15:58:42 -0700290 ServiceInstance<ControllerService> thisInstance = ServiceInstance.<ControllerService>builder()
291 .name(CONTROLLER_SERVICE_NAME)
292 .payload(new ControllerService(controllerId))
293 //.port((int)(65535 * Math.random())) // in a real application, you'd use a common port
294 //.uriSpec(uriSpec)
295 .build();
Jonathan Hart0b3eee42013-03-16 18:20:04 -0700296
Jonathan Hart71c0ffc2013-03-24 15:58:42 -0700297 serviceDiscovery.registerService(thisInstance);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800298 } catch (Exception e) {
Jonathan Hart71c0ffc2013-03-24 15:58:42 -0700299 // TODO Auto-generated catch block
300 e.printStackTrace();
Jonathan Hartedd6a442013-02-20 15:22:06 -0800301 }
Jonathan Hart71c0ffc2013-03-24 15:58:42 -0700302
Jonathan Hartedd6a442013-02-20 15:22:06 -0800303 }
304
305 @Override
Jonathan Hart57080fb2013-02-21 10:55:46 -0800306 public String getControllerForSwitch(long dpid) throws RegistryException {
Jonathan Hart89187372013-03-14 16:41:09 -0700307 String dpidStr = HexString.toHexString(dpid);
Pankaj Berde017960a2013-03-14 20:32:26 -0700308
Jonathan Hart599c6b32013-03-24 22:42:02 -0700309 PathChildrenCache switchCache = switchPathCaches.get(dpidStr);
310
311 if (switchCache == null){
Jonathan Hartedd6a442013-02-20 15:22:06 -0800312 log.warn("Tried to get controller for non-existent switch");
313 return null;
314 }
315
Jonathan Hartf4e80842013-03-26 23:55:02 -0700316 try {
317 //We've seen issues with these caches get stuck out of date, so we'll have to
318 //force them to refresh before each read. This slows down the method as it
319 //blocks on a Zookeeper query, however at the moment only the cleanup thread
320 //uses this and that isn't particularly time-sensitive.
321 switchCache.rebuild();
322 } catch (Exception e) {
323 // TODO Auto-generated catch block
324 e.printStackTrace();
325 }
326
Jonathan Hart599c6b32013-03-24 22:42:02 -0700327 List<ChildData> sortedData = new ArrayList<ChildData>(switchCache.getCurrentData());
Jonathan Hart0b3eee42013-03-16 18:20:04 -0700328
Jonathan Hart599c6b32013-03-24 22:42:02 -0700329 Collections.sort(
330 sortedData,
331 new Comparator<ChildData>(){
332 private String getSequenceNumber(String path){
333 return path.substring(path.lastIndexOf('-') + 1);
334 }
335 @Override
336 public int compare(ChildData lhs, ChildData rhs) {
337 return getSequenceNumber(lhs.getPath()).
338 compareTo(getSequenceNumber(rhs.getPath()));
339 }
340 }
341 );
Jonathan Hartedd6a442013-02-20 15:22:06 -0800342
Jonathan Hart56b296e2013-03-25 13:30:10 -0700343 if (sortedData.size() == 0){
344 return null;
345 }
346
Jonathan Hart599c6b32013-03-24 22:42:02 -0700347 return new String(sortedData.get(0).getData(), Charsets.UTF_8);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800348 }
349
350 @Override
351 public Collection<Long> getSwitchesControlledByController(String controllerId) {
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800352 //TODO remove this if not needed
Jonathan Hartbd766972013-02-22 15:13:03 -0800353 throw new RuntimeException("Not yet implemented");
Jonathan Hartedd6a442013-02-20 15:22:06 -0800354 }
Jonathan Hartbd181b62013-02-17 16:05:38 -0800355
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800356
Jonathan Hart89187372013-03-14 16:41:09 -0700357 //TODO what should happen when there's no ZK connection? Currently we just return
358 //the cache but this may lead to false impressions - i.e. we don't actually know
359 //what's in ZK so we shouldn't say we do
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800360 @Override
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800361 public Map<String, List<ControllerRegistryEntry>> getAllSwitches() {
362 Map<String, List<ControllerRegistryEntry>> data =
363 new HashMap<String, List<ControllerRegistryEntry>>();
364
365 for (Map.Entry<String, PathChildrenCache> entry : switchPathCaches.entrySet()){
366 List<ControllerRegistryEntry> contendingControllers =
367 new ArrayList<ControllerRegistryEntry>();
368
369 if (entry.getValue().getCurrentData().size() < 1){
Jonathan Hartcbb4b952013-03-18 16:15:18 -0700370 //TODO prevent even having the PathChildrenCache in this case
371 //log.info("Switch entry with no leader elections: {}", entry.getKey());
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800372 continue;
373 }
374
375 for (ChildData d : entry.getValue().getCurrentData()) {
Jonathan Hart97801ac2013-02-26 14:29:16 -0800376
Jonathan Hartd10008d2013-02-23 17:04:08 -0800377 String controllerId = new String(d.getData(), Charsets.UTF_8);
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800378
379 String[] splitted = d.getPath().split("-");
380 int sequenceNumber = Integer.parseInt(splitted[splitted.length - 1]);
381
382 contendingControllers.add(new ControllerRegistryEntry(controllerId, sequenceNumber));
383 }
384
385 Collections.sort(contendingControllers);
386 data.put(entry.getKey(), contendingControllers);
387 }
388 return data;
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800389 }
390
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800391 public IdBlock allocateUniqueIdBlock(long range) {
392 try {
393 AtomicValue<Long> result = null;
394 do {
395 result = distributedIdCounter.add(range);
396 } while (result == null || !result.succeeded());
397
398 return new IdBlock(result.preValue(), result.postValue() - 1, range);
399 } catch (Exception e) {
400 log.error("Error allocating ID block");
401 }
402 return null;
403 }
404
Naoki Shiotaa3b2dfa2013-06-27 13:52:24 -0700405 /**
406 * Returns a block of IDs which are unique and unused.
407 * Range of IDs is fixed size and is assigned incrementally as this method called.
408 * Since the range of IDs is managed by Zookeeper in distributed way, this method may block when
409 * requests come up simultaneously.
410 */
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800411 @Override
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700412 public IdBlock allocateUniqueIdBlock(){
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800413 return allocateUniqueIdBlock(ID_BLOCK_SIZE);
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700414 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800415
Jonathan Hartbd181b62013-02-17 16:05:38 -0800416 /*
417 * IFloodlightModule
418 */
419
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800420 @Override
421 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartedd6a442013-02-20 15:22:06 -0800422 Collection<Class<? extends IFloodlightService>> l =
423 new ArrayList<Class<? extends IFloodlightService>>();
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800424 l.add(IControllerRegistryService.class);
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800425 return l;
426 }
427
428 @Override
429 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
430 Map<Class<? extends IFloodlightService>, IFloodlightService> m =
431 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800432 m.put(IControllerRegistryService.class, this);
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800433 return m;
434 }
435
436 @Override
437 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800438 Collection<Class<? extends IFloodlightService>> l =
439 new ArrayList<Class<? extends IFloodlightService>>();
440 l.add(IRestApiService.class);
441 return l;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800442 }
443
Jonathan Hart89187372013-03-14 16:41:09 -0700444 //TODO currently blocks startup when it can't get a Zookeeper connection.
445 //Do we support starting up with no Zookeeper connection?
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800446 @Override
447 public void init (FloodlightModuleContext context) throws FloodlightModuleException {
Jonathan Hartbd766972013-02-22 15:13:03 -0800448 log.info("Initialising the Zookeeper Registry - Zookeeper connection required");
449
Jonathan Hart97801ac2013-02-26 14:29:16 -0800450 //Read the Zookeeper connection string from the config
451 Map<String, String> configParams = context.getConfigParams(this);
452 String connectionString = configParams.get("connectionString");
453 if (connectionString != null){
454 this.connectionString = connectionString;
Jonathan Hart57080fb2013-02-21 10:55:46 -0800455 }
Jonathan Hart97801ac2013-02-26 14:29:16 -0800456 log.info("Setting Zookeeper connection string to {}", this.connectionString);
Jonathan Hart57080fb2013-02-21 10:55:46 -0800457
Jonathan Hart97801ac2013-02-26 14:29:16 -0800458 restApi = context.getServiceImpl(IRestApiService.class);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800459
Jonathan Hart89187372013-03-14 16:41:09 -0700460 switches = new ConcurrentHashMap<String, SwitchLeadershipData>();
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700461 //switchPathCaches = new HashMap<String, PathChildrenCache>();
462 switchPathCaches = new ConcurrentHashMap<String, PathChildrenCache>();
Jonathan Hartbd181b62013-02-17 16:05:38 -0800463
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800464 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
Jonathan Hart97801ac2013-02-26 14:29:16 -0800465 client = CuratorFrameworkFactory.newClient(this.connectionString,
Jonathan Hartcc957a02013-02-26 10:39:04 -0800466 sessionTimeout, connectionTimeout, retryPolicy);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800467
468 client.start();
Jonathan Hartbd181b62013-02-17 16:05:38 -0800469 client = client.usingNamespace(namespace);
Jonathan Hart97801ac2013-02-26 14:29:16 -0800470
Jonathan Hart1530ccc2013-04-03 19:36:02 -0700471 distributedIdCounter = new DistributedAtomicLong(
472 client,
473 ID_COUNTER_PATH,
474 new RetryOneTime(100));
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800475
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800476 switchCache = new PathChildrenCache(client, switchLatchesPath, true);
477 switchCache.getListenable().addListener(switchPathCacheListener);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800478
Jonathan Hart71c0ffc2013-03-24 15:58:42 -0700479 //Build the service discovery object
480 serviceDiscovery = ServiceDiscoveryBuilder.builder(ControllerService.class)
481 .client(client).basePath(SERVICES_PATH).build();
482
483 //We read the list of services very frequently (GUI periodically queries them)
484 //so we'll cache them to cut down on Zookeeper queries.
485 serviceCache = serviceDiscovery.serviceCacheBuilder()
486 .name(CONTROLLER_SERVICE_NAME).build();
487
488
Jonathan Hartedd6a442013-02-20 15:22:06 -0800489 try {
Jonathan Hart71c0ffc2013-03-24 15:58:42 -0700490 serviceDiscovery.start();
491 serviceCache.start();
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800492
493 //Don't prime the cache, we want a notification for each child node in the path
494 switchCache.start(StartMode.NORMAL);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800495 } catch (Exception e) {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800496 throw new FloodlightModuleException("Error initialising ZookeeperRegistry: "
497 + e.getMessage());
Jonathan Hartedd6a442013-02-20 15:22:06 -0800498 }
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800499 }
500
501 @Override
502 public void startUp (FloodlightModuleContext context) {
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800503 restApi.addRestletRoutable(new RegistryWebRoutable());
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800504 }
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800505}