blob: acb6e523063e3cd01a043fdc90531aa070fd8d86 [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;
Jonathan Hartedd6a442013-02-20 15:22:06 -08004import java.io.UnsupportedEncodingException;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -08005import java.util.ArrayList;
6import java.util.Collection;
Jonathan Hart3d7730a2013-02-22 11:51:17 -08007import java.util.Collections;
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;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080018
Jonathan Hartedd6a442013-02-20 15:22:06 -080019import org.apache.zookeeper.CreateMode;
Jonathan Hart0b3eee42013-03-16 18:20:04 -070020import org.apache.zookeeper.WatchedEvent;
21import org.apache.zookeeper.Watcher;
Jonathan Hartbd181b62013-02-17 16:05:38 -080022import org.openflow.util.HexString;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080025
Jonathan Hartd10008d2013-02-23 17:04:08 -080026import com.google.common.base.Charsets;
Jonathan Hartbd181b62013-02-17 16:05:38 -080027import com.netflix.curator.RetryPolicy;
28import com.netflix.curator.framework.CuratorFramework;
29import com.netflix.curator.framework.CuratorFrameworkFactory;
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 Hartbd181b62013-02-17 16:05:38 -080038import com.netflix.curator.framework.recipes.leader.Participant;
Jonathan Hart3d7730a2013-02-22 11:51:17 -080039import com.netflix.curator.retry.ExponentialBackoffRetry;
Jonathan Hart0b3eee42013-03-16 18:20:04 -070040import com.netflix.curator.utils.ZKPaths;
Jonathan Hartbd181b62013-02-17 16:05:38 -080041
Jonathan Hart7bf62172013-02-28 13:17:18 -080042/**
43 * A registry service that uses Zookeeper. All data is stored in Zookeeper,
44 * so this can be used as a global registry in a multi-node ONOS cluster.
45 * @author jono
46 *
47 */
Jonathan Hartbd766972013-02-22 15:13:03 -080048public class ZookeeperRegistry implements IFloodlightModule, IControllerRegistryService {
Jonathan Hartc6eee9e2013-02-18 14:58:27 -080049
Jonathan Hartbd766972013-02-22 15:13:03 -080050 protected static Logger log = LoggerFactory.getLogger(ZookeeperRegistry.class);
51 protected String controllerId = null;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080052
Jonathan Hart3d7730a2013-02-22 11:51:17 -080053 protected IRestApiService restApi;
54
Jonathan Hart7bf62172013-02-28 13:17:18 -080055 //This is the default, it's overwritten by the connectionString configuration parameter
Jonathan Hartbd181b62013-02-17 16:05:38 -080056 protected String connectionString = "localhost:2181";
Jonathan Hart3d7730a2013-02-22 11:51:17 -080057
Jonathan Hartbd181b62013-02-17 16:05:38 -080058 private final String namespace = "onos";
Jonathan Hartedd6a442013-02-20 15:22:06 -080059 private final String switchLatchesPath = "/switches";
Jonathan Hart3d7730a2013-02-22 11:51:17 -080060 private final String controllerPath = "/controllers";
Jonathan Hartbd181b62013-02-17 16:05:38 -080061
62 protected CuratorFramework client;
Jonathan Hartedd6a442013-02-20 15:22:06 -080063
Jonathan Hartedd6a442013-02-20 15:22:06 -080064 protected PathChildrenCache controllerCache;
Jonathan Hart3d7730a2013-02-22 11:51:17 -080065 protected PathChildrenCache switchCache;
Jonathan Hartbd181b62013-02-17 16:05:38 -080066
Jonathan Hart89187372013-03-14 16:41:09 -070067 protected ConcurrentHashMap<String, SwitchLeadershipData> switches;
Jonathan Hart3d7730a2013-02-22 11:51:17 -080068 protected Map<String, PathChildrenCache> switchPathCaches;
Jonathan Hartbd181b62013-02-17 16:05:38 -080069
Jonathan Hart97801ac2013-02-26 14:29:16 -080070 //Zookeeper performance-related configuration
Jonathan Hart0b3eee42013-03-16 18:20:04 -070071 protected static final int sessionTimeout = 5000;
72 protected static final int connectionTimeout = 7000;
Jonathan Hart57080fb2013-02-21 10:55:46 -080073
Jonathan Hartbd181b62013-02-17 16:05:38 -080074
Jonathan Hart89187372013-03-14 16:41:09 -070075 protected class SwitchLeaderListener implements LeaderLatchListener{
Jonathan Hart0de09492013-03-13 14:37:21 -070076 String dpid;
77 LeaderLatch latch;
78
Jonathan Hart89187372013-03-14 16:41:09 -070079 public SwitchLeaderListener(String dpid, LeaderLatch latch){
Jonathan Hart0de09492013-03-13 14:37:21 -070080 this.dpid = dpid;
81 this.latch = latch;
82 }
83
84 @Override
85 public void leaderLatchEvent(CuratorFramework arg0,
86 LeaderLatchEvent arg1) {
Jonathan Hart89187372013-03-14 16:41:09 -070087 log.debug("Leadership changed for {}, now {}",
Jonathan Hart0de09492013-03-13 14:37:21 -070088 dpid, latch.hasLeadership());
89
Jonathan Hart89187372013-03-14 16:41:09 -070090 //Check that the leadership request is still active - the client
91 //may have since released the request or even begun another request
92 //(this is why we use == to check the object instance is the same)
93 SwitchLeadershipData swData = switches.get(dpid);
94 if (swData != null && swData.getLatch() == latch){
95 swData.getCallback().controlChanged(
96 HexString.toLong(dpid), latch.hasLeadership());
97 }
98 else {
Jonathan Hart4baf3be2013-03-21 18:26:13 -070099 log.debug("Latch for {} has changed: old latch {} - new latch {}",
100 new Object[]{dpid, latch, swData.getLatch()});
Jonathan Hart89187372013-03-14 16:41:09 -0700101 }
Jonathan Hart0de09492013-03-13 14:37:21 -0700102 }
103 }
104
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800105
Jonathan Hart7bf62172013-02-28 13:17:18 -0800106 /**
107 * Listens for changes to the switch znodes in Zookeeper. This maintains
108 * the second level of PathChildrenCaches that hold the controllers
109 * contending for each switch - there's one for each switch.
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800110 */
111 PathChildrenCacheListener switchPathCacheListener = new PathChildrenCacheListener() {
112 @Override
113 public void childEvent(CuratorFramework client,
114 PathChildrenCacheEvent event) throws Exception {
Jonathan Hartcbb4b952013-03-18 16:15:18 -0700115 //log.debug("Root switch path cache got {} event", event.getType());
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800116
117 String strSwitch = null;
118 if (event.getData() != null){
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800119 String[] splitted = event.getData().getPath().split("/");
120 strSwitch = splitted[splitted.length - 1];
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800121 }
122
123 switch (event.getType()){
124 case CHILD_ADDED:
125 case CHILD_UPDATED:
126 //Check we have a PathChildrenCache for this child, add one if not
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700127 synchronized (switchPathCaches){
128 if (switchPathCaches.get(strSwitch) == null){
129 PathChildrenCache pc = new PathChildrenCache(client,
130 event.getData().getPath(), true);
131 pc.start(StartMode.NORMAL);
132 switchPathCaches.put(strSwitch, pc);
133 }
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800134 }
135 break;
136 case CHILD_REMOVED:
137 //Remove our PathChildrenCache for this child
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700138 PathChildrenCache pc = null;
139 synchronized(switchPathCaches){
140 pc = switchPathCaches.remove(strSwitch);
141 }
142 if (pc != null){
143 pc.close();
144 }
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800145 break;
146 default:
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700147 //All other events are connection status events. We don't need to
148 //do anything as the path cache handles these on its own.
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800149 break;
150 }
151
152 }
153 };
Jonathan Hartedd6a442013-02-20 15:22:06 -0800154
Jonathan Hartbd181b62013-02-17 16:05:38 -0800155
156 @Override
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800157 public void requestControl(long dpid, ControlChangeCallback cb) throws RegistryException {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800158 log.info("Requesting control for {}", HexString.toHexString(dpid));
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800159
Jonathan Hartbd766972013-02-22 15:13:03 -0800160 if (controllerId == null){
161 throw new RuntimeException("Must register a controller before calling requestControl");
Jonathan Hartbd181b62013-02-17 16:05:38 -0800162 }
163
164 String dpidStr = HexString.toHexString(dpid);
165 String latchPath = switchLatchesPath + "/" + dpidStr;
166
Jonathan Hart89187372013-03-14 16:41:09 -0700167 if (switches.get(dpidStr) != null){
Jonathan Hart3c0eccd2013-03-12 22:32:50 -0700168 log.debug("Already contesting {}, returning", HexString.toHexString(dpid));
Pankaj Berdeda7187b2013-03-18 15:24:59 -0700169 throw new RegistryException("Already contesting control for " + dpidStr);
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800170 }
171
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700172 log.debug("No latch for switch {}", dpidStr);
173
Jonathan Hartbd766972013-02-22 15:13:03 -0800174 LeaderLatch latch = new LeaderLatch(client, latchPath, controllerId);
Jonathan Hart89187372013-03-14 16:41:09 -0700175 latch.addListener(new SwitchLeaderListener(dpidStr, latch));
Jonathan Hartbd181b62013-02-17 16:05:38 -0800176
Jonathan Hart44e56fc2013-03-14 16:53:59 -0700177
Jonathan Hart89187372013-03-14 16:41:09 -0700178 SwitchLeadershipData swData = new SwitchLeadershipData(latch, cb);
179 SwitchLeadershipData oldData = switches.putIfAbsent(dpidStr, swData);
180
181 if (oldData != null){
182 //There was already data for that key in the map
183 //i.e. someone else got here first so we can't succeed
184 log.debug("Already requested control for {}", dpidStr);
185 throw new RegistryException("Already requested control for " + dpidStr);
186 }
187
188 //Now that we know we were able to add our latch to the collection,
Jonathan Hart44e56fc2013-03-14 16:53:59 -0700189 //we can start the leader election in Zookeeper. However I don't know
190 //how to handle if the start fails - the latch is already in our
191 //switches list.
192 //TODO seems like there's a Curator bug when latch.start is called when
193 //there's no Zookeeper connection which causes two znodes to be put in
194 //Zookeeper at the latch path when we reconnect to Zookeeper.
Jonathan Hartbd181b62013-02-17 16:05:38 -0800195 try {
Jonathan Hartbd181b62013-02-17 16:05:38 -0800196 latch.start();
197 } catch (Exception e) {
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800198 log.warn("Error starting leader latch: {}", e.getMessage());
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800199 throw new RegistryException("Error starting leader latch for " + dpidStr, e);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800200 }
201
202 }
203
204 @Override
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800205 public void releaseControl(long dpid) {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800206 log.info("Releasing control for {}", HexString.toHexString(dpid));
Jonathan Hart57080fb2013-02-21 10:55:46 -0800207
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800208 String dpidStr = HexString.toHexString(dpid);
209
Jonathan Hart89187372013-03-14 16:41:09 -0700210 SwitchLeadershipData swData = switches.remove(dpidStr);
211
212 if (swData == null) {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800213 log.debug("Trying to release control of a switch we are not contesting");
Jonathan Hartbd181b62013-02-17 16:05:38 -0800214 return;
215 }
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700216
217 log.debug("swData for {} was not null: {}", dpidStr, swData);
Jonathan Hart89187372013-03-14 16:41:09 -0700218
Jonathan Hart89187372013-03-14 16:41:09 -0700219 LeaderLatch latch = swData.getLatch();
Jonathan Hartbd181b62013-02-17 16:05:38 -0800220
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700221 latch.removeAllListeners();
222
Jonathan Hartbd181b62013-02-17 16:05:38 -0800223 try {
224 latch.close();
225 } catch (IOException e) {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800226 //I think it's OK not to do anything here. Either the node got
227 //deleted correctly, or the connection went down and the node got deleted.
Jonathan Hartbd181b62013-02-17 16:05:38 -0800228 }
229 }
230
231 @Override
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800232 public boolean hasControl(long dpid) {
Jonathan Hart89187372013-03-14 16:41:09 -0700233 String dpidStr = HexString.toHexString(dpid);
Jonathan Hart57080fb2013-02-21 10:55:46 -0800234
Jonathan Hart89187372013-03-14 16:41:09 -0700235 SwitchLeadershipData swData = switches.get(dpidStr);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800236
Jonathan Hart89187372013-03-14 16:41:09 -0700237 if (swData == null) {
238 log.warn("No leader latch for dpid {}", dpidStr);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800239 return false;
240 }
241
Jonathan Hart89187372013-03-14 16:41:09 -0700242 return swData.getLatch().hasLeadership();
Jonathan Hartbd181b62013-02-17 16:05:38 -0800243 }
244
245 @Override
Jonathan Hart7bf62172013-02-28 13:17:18 -0800246 public String getControllerId() {
Jonathan Hartbd766972013-02-22 15:13:03 -0800247 return controllerId;
Jonathan Hartbd181b62013-02-17 16:05:38 -0800248 }
249
Jonathan Hartedd6a442013-02-20 15:22:06 -0800250 @Override
Jonathan Hart57080fb2013-02-21 10:55:46 -0800251 public Collection<String> getAllControllers() throws RegistryException {
Jonathan Hartedd6a442013-02-20 15:22:06 -0800252 log.debug("Getting all controllers");
Jonathan Hart1be46262013-02-20 16:43:51 -0800253
Jonathan Hartedd6a442013-02-20 15:22:06 -0800254 List<String> controllers = new ArrayList<String>();
255 for (ChildData data : controllerCache.getCurrentData()){
256
257 String d = null;
258 try {
259 d = new String(data.getData(), "UTF-8");
260 } catch (UnsupportedEncodingException e) {
Jonathan Hart57080fb2013-02-21 10:55:46 -0800261 throw new RegistryException("Error encoding string", e);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800262 }
263
264 controllers.add(d);
265 }
266 return controllers;
267 }
268
269 @Override
Jonathan Hart57080fb2013-02-21 10:55:46 -0800270 public void registerController(String id) throws RegistryException {
Jonathan Hartd10008d2013-02-23 17:04:08 -0800271 if (controllerId != null) {
272 throw new RegistryException(
273 "Controller already registered with id " + controllerId);
274 }
Jonathan Hartbd766972013-02-22 15:13:03 -0800275
276 controllerId = id;
Jonathan Hart57080fb2013-02-21 10:55:46 -0800277
Jonathan Hartd10008d2013-02-23 17:04:08 -0800278 byte bytes[] = id.getBytes(Charsets.UTF_8);
Jonathan Hart0b3eee42013-03-16 18:20:04 -0700279 String path = ZKPaths.makePath(controllerPath, controllerId);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800280
281 log.info("Registering controller with id {}", id);
282
Jonathan Hartedd6a442013-02-20 15:22:06 -0800283 try {
Jonathan Hart0b3eee42013-03-16 18:20:04 -0700284 //We need to set a watch to recreate the node in the controller
285 //registry if it gets deleted - e.g. on Zookeeper connection loss.
286 Watcher watcher = new Watcher(){
287 @Override
288 public void process(WatchedEvent event) {
289 log.debug("got any watch event {} ", event);
290
291 String path = ZKPaths.makePath(controllerPath, controllerId);
292 byte bytes[] = controllerId.getBytes(Charsets.UTF_8);
293
294 try {
295 if (event.getType() == Event.EventType.NodeDeleted){
296 log.debug("got a node deleted event");
297
298
299 client.create().withMode(CreateMode.EPHEMERAL)
300 .forPath(path, bytes);
301 }
302 } catch (Exception e) {
303 log.warn("Error recreating controller node for {}: {}",
304 controllerId, e.getMessage());
305 } finally {
306 try {
307 client.checkExists().usingWatcher(this).forPath(path);
308 } catch (Exception e2){
309 log.warn("Error resetting watch for {}: {}",
310 controllerId, e2.getMessage());
311 }
312 }
313 }
314 };
315
316 //Create ephemeral node in controller registry
317 //TODO Use protection
318 client.create().withMode(CreateMode.EPHEMERAL)
Jonathan Hartedd6a442013-02-20 15:22:06 -0800319 .forPath(path, bytes);
Jonathan Hart0b3eee42013-03-16 18:20:04 -0700320 client.checkExists().usingWatcher(watcher).forPath(path);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800321 } catch (Exception e) {
Jonathan Hart57080fb2013-02-21 10:55:46 -0800322 throw new RegistryException("Error contacting the Zookeeper service", e);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800323 }
324 }
325
326 @Override
Jonathan Hart57080fb2013-02-21 10:55:46 -0800327 public String getControllerForSwitch(long dpid) throws RegistryException {
Jonathan Hart89187372013-03-14 16:41:09 -0700328 // TODO work out synchronization
Jonathan Hartedd6a442013-02-20 15:22:06 -0800329
Jonathan Hart89187372013-03-14 16:41:09 -0700330 String dpidStr = HexString.toHexString(dpid);
Jonathan Hart44e56fc2013-03-14 16:53:59 -0700331
Jonathan Hart0b3eee42013-03-16 18:20:04 -0700332 SwitchLeadershipData swData = switches.get(dpidStr);
333 //LeaderLatch latch = (switches.get(dpidStr) != null)?switches.get(dpidStr).getLatch():null;
Pankaj Berde017960a2013-03-14 20:32:26 -0700334
Jonathan Hart0b3eee42013-03-16 18:20:04 -0700335 if (swData == null){
Jonathan Hartedd6a442013-02-20 15:22:06 -0800336 log.warn("Tried to get controller for non-existent switch");
337 return null;
338 }
339
Jonathan Hart0b3eee42013-03-16 18:20:04 -0700340 LeaderLatch latch = swData.getLatch();
341
Jonathan Hartedd6a442013-02-20 15:22:06 -0800342 Participant leader = null;
343 try {
344 leader = latch.getLeader();
345 } catch (Exception e) {
Jonathan Hart57080fb2013-02-21 10:55:46 -0800346 throw new RegistryException("Error contacting the Zookeeper service", e);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800347 }
348
349 return leader.getId();
350 }
351
352 @Override
353 public Collection<Long> getSwitchesControlledByController(String controllerId) {
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800354 //TODO remove this if not needed
Jonathan Hartbd766972013-02-22 15:13:03 -0800355 throw new RuntimeException("Not yet implemented");
Jonathan Hartedd6a442013-02-20 15:22:06 -0800356 }
Jonathan Hartbd181b62013-02-17 16:05:38 -0800357
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800358
Jonathan Hart89187372013-03-14 16:41:09 -0700359 //TODO what should happen when there's no ZK connection? Currently we just return
360 //the cache but this may lead to false impressions - i.e. we don't actually know
361 //what's in ZK so we shouldn't say we do
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800362 @Override
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800363 public Map<String, List<ControllerRegistryEntry>> getAllSwitches() {
364 Map<String, List<ControllerRegistryEntry>> data =
365 new HashMap<String, List<ControllerRegistryEntry>>();
366
367 for (Map.Entry<String, PathChildrenCache> entry : switchPathCaches.entrySet()){
368 List<ControllerRegistryEntry> contendingControllers =
369 new ArrayList<ControllerRegistryEntry>();
370
371 if (entry.getValue().getCurrentData().size() < 1){
Jonathan Hartcbb4b952013-03-18 16:15:18 -0700372 //TODO prevent even having the PathChildrenCache in this case
373 //log.info("Switch entry with no leader elections: {}", entry.getKey());
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800374 continue;
375 }
376
377 for (ChildData d : entry.getValue().getCurrentData()) {
Jonathan Hart97801ac2013-02-26 14:29:16 -0800378
Jonathan Hartd10008d2013-02-23 17:04:08 -0800379 String controllerId = new String(d.getData(), Charsets.UTF_8);
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800380
381 String[] splitted = d.getPath().split("-");
382 int sequenceNumber = Integer.parseInt(splitted[splitted.length - 1]);
383
384 contendingControllers.add(new ControllerRegistryEntry(controllerId, sequenceNumber));
385 }
386
387 Collections.sort(contendingControllers);
388 data.put(entry.getKey(), contendingControllers);
389 }
390 return data;
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800391 }
392
Jonathan Hartbd181b62013-02-17 16:05:38 -0800393 /*
394 * IFloodlightModule
395 */
396
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800397 @Override
398 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartedd6a442013-02-20 15:22:06 -0800399 Collection<Class<? extends IFloodlightService>> l =
400 new ArrayList<Class<? extends IFloodlightService>>();
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800401 l.add(IControllerRegistryService.class);
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800402 return l;
403 }
404
405 @Override
406 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
407 Map<Class<? extends IFloodlightService>, IFloodlightService> m =
408 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800409 m.put(IControllerRegistryService.class, this);
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800410 return m;
411 }
412
413 @Override
414 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800415 Collection<Class<? extends IFloodlightService>> l =
416 new ArrayList<Class<? extends IFloodlightService>>();
417 l.add(IRestApiService.class);
418 return l;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800419 }
420
Jonathan Hart89187372013-03-14 16:41:09 -0700421 //TODO currently blocks startup when it can't get a Zookeeper connection.
422 //Do we support starting up with no Zookeeper connection?
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800423 @Override
424 public void init (FloodlightModuleContext context) throws FloodlightModuleException {
Jonathan Hartbd766972013-02-22 15:13:03 -0800425 log.info("Initialising the Zookeeper Registry - Zookeeper connection required");
426
Jonathan Hart97801ac2013-02-26 14:29:16 -0800427 //Read the Zookeeper connection string from the config
428 Map<String, String> configParams = context.getConfigParams(this);
429 String connectionString = configParams.get("connectionString");
430 if (connectionString != null){
431 this.connectionString = connectionString;
Jonathan Hart57080fb2013-02-21 10:55:46 -0800432 }
Jonathan Hart97801ac2013-02-26 14:29:16 -0800433 log.info("Setting Zookeeper connection string to {}", this.connectionString);
Jonathan Hart57080fb2013-02-21 10:55:46 -0800434
Jonathan Hart97801ac2013-02-26 14:29:16 -0800435 restApi = context.getServiceImpl(IRestApiService.class);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800436
Jonathan Hart89187372013-03-14 16:41:09 -0700437 switches = new ConcurrentHashMap<String, SwitchLeadershipData>();
Jonathan Hart4baf3be2013-03-21 18:26:13 -0700438 //switchPathCaches = new HashMap<String, PathChildrenCache>();
439 switchPathCaches = new ConcurrentHashMap<String, PathChildrenCache>();
Jonathan Hartbd181b62013-02-17 16:05:38 -0800440
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800441 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
Jonathan Hart97801ac2013-02-26 14:29:16 -0800442 client = CuratorFrameworkFactory.newClient(this.connectionString,
Jonathan Hartcc957a02013-02-26 10:39:04 -0800443 sessionTimeout, connectionTimeout, retryPolicy);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800444
445 client.start();
Jonathan Hartbd181b62013-02-17 16:05:38 -0800446 client = client.usingNamespace(namespace);
Jonathan Hart97801ac2013-02-26 14:29:16 -0800447
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800448
Jonathan Hartedd6a442013-02-20 15:22:06 -0800449 controllerCache = new PathChildrenCache(client, controllerPath, true);
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800450 switchCache = new PathChildrenCache(client, switchLatchesPath, true);
451 switchCache.getListenable().addListener(switchPathCacheListener);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800452
453 try {
454 controllerCache.start(StartMode.BUILD_INITIAL_CACHE);
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800455
456 //Don't prime the cache, we want a notification for each child node in the path
457 switchCache.start(StartMode.NORMAL);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800458 } catch (Exception e) {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800459 throw new FloodlightModuleException("Error initialising ZookeeperRegistry: "
460 + e.getMessage());
Jonathan Hartedd6a442013-02-20 15:22:06 -0800461 }
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800462 }
463
464 @Override
465 public void startUp (FloodlightModuleContext context) {
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800466 restApi.addRestletRoutable(new RegistryWebRoutable());
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800467 }
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800468}