blob: 51335febf38cbce60e85688b6635809a4ac95a4e [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;
11
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080012import net.floodlightcontroller.core.module.FloodlightModuleContext;
13import net.floodlightcontroller.core.module.FloodlightModuleException;
14import net.floodlightcontroller.core.module.IFloodlightModule;
15import net.floodlightcontroller.core.module.IFloodlightService;
Jonathan Hart3d7730a2013-02-22 11:51:17 -080016import net.floodlightcontroller.restserver.IRestApiService;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080017
Jonathan Hartedd6a442013-02-20 15:22:06 -080018import org.apache.zookeeper.CreateMode;
Jonathan Hartbd181b62013-02-17 16:05:38 -080019import org.apache.zookeeper.WatchedEvent;
Jonathan Hartc6eee9e2013-02-18 14:58:27 -080020import org.apache.zookeeper.Watcher.Event.KeeperState;
Jonathan Hartbd181b62013-02-17 16:05:38 -080021import org.openflow.util.HexString;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080024
Jonathan Hartd10008d2013-02-23 17:04:08 -080025import com.google.common.base.Charsets;
Jonathan Hartbd181b62013-02-17 16:05:38 -080026import com.netflix.curator.RetryPolicy;
27import com.netflix.curator.framework.CuratorFramework;
28import com.netflix.curator.framework.CuratorFrameworkFactory;
29import com.netflix.curator.framework.api.CuratorWatcher;
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;
36import com.netflix.curator.framework.recipes.leader.Participant;
Jonathan Hart3d7730a2013-02-22 11:51:17 -080037import com.netflix.curator.retry.ExponentialBackoffRetry;
Jonathan Hartbd181b62013-02-17 16:05:38 -080038
Jonathan Hart7bf62172013-02-28 13:17:18 -080039/**
40 * A registry service that uses Zookeeper. All data is stored in Zookeeper,
41 * so this can be used as a global registry in a multi-node ONOS cluster.
42 * @author jono
43 *
44 */
Jonathan Hartbd766972013-02-22 15:13:03 -080045public class ZookeeperRegistry implements IFloodlightModule, IControllerRegistryService {
Jonathan Hartc6eee9e2013-02-18 14:58:27 -080046
Jonathan Hartbd766972013-02-22 15:13:03 -080047 protected static Logger log = LoggerFactory.getLogger(ZookeeperRegistry.class);
48 protected String controllerId = null;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -080049
Jonathan Hart3d7730a2013-02-22 11:51:17 -080050 protected IRestApiService restApi;
51
Jonathan Hart7bf62172013-02-28 13:17:18 -080052 //This is the default, it's overwritten by the connectionString configuration parameter
Jonathan Hartbd181b62013-02-17 16:05:38 -080053 protected String connectionString = "localhost:2181";
Jonathan Hart3d7730a2013-02-22 11:51:17 -080054
Jonathan Hartbd181b62013-02-17 16:05:38 -080055 private final String namespace = "onos";
Jonathan Hartedd6a442013-02-20 15:22:06 -080056 private final String switchLatchesPath = "/switches";
Jonathan Hart3d7730a2013-02-22 11:51:17 -080057 private final String controllerPath = "/controllers";
Jonathan Hartbd181b62013-02-17 16:05:38 -080058
59 protected CuratorFramework client;
Jonathan Hartedd6a442013-02-20 15:22:06 -080060
Jonathan Hartedd6a442013-02-20 15:22:06 -080061 protected PathChildrenCache controllerCache;
Jonathan Hart3d7730a2013-02-22 11:51:17 -080062 protected PathChildrenCache switchCache;
Jonathan Hartbd181b62013-02-17 16:05:38 -080063
64 protected Map<String, LeaderLatch> switchLatches;
Jonathan Hartd82f20d2013-02-21 18:04:24 -080065 protected Map<String, ControlChangeCallback> switchCallbacks;
Jonathan Hart3d7730a2013-02-22 11:51:17 -080066 protected Map<String, PathChildrenCache> switchPathCaches;
Jonathan Hartbd181b62013-02-17 16:05:38 -080067
Jonathan Hart97801ac2013-02-26 14:29:16 -080068 //Zookeeper performance-related configuration
Jonathan Hartcc957a02013-02-26 10:39:04 -080069 protected static final int sessionTimeout = 2000;
70 protected static final int connectionTimeout = 4000;
Jonathan Hart57080fb2013-02-21 10:55:46 -080071
Jonathan Hart7bf62172013-02-28 13:17:18 -080072 /**
73 * Watches for changes in switch leadership election. The Curator
74 * LeaderLatch doesn't notify us when leadership changes so we set a watch
75 * on the election znodes to get leadership change events. The process
76 * method will be called whenever the switches children change in
77 * Zookeeper. We then have to work out whether to send a control-changed
78 * event to our clients and reset the watch.
79 *
80 * TODO I think it's possible to miss events that happen while we're
81 * processing the watch and before we've set a new watch. Need to think
82 * of a safer way to implement leader change notifications.
83 *
84 */
Jonathan Hartbd181b62013-02-17 16:05:38 -080085 protected class ParamaterizedCuratorWatcher implements CuratorWatcher {
86 private String dpid;
Jonathan Hartc6eee9e2013-02-18 14:58:27 -080087 private boolean isLeader = false;
Jonathan Hartbd181b62013-02-17 16:05:38 -080088 private String latchPath;
89
90 public ParamaterizedCuratorWatcher(String dpid, String latchPath){
91 this.dpid = dpid;
92 this.latchPath = latchPath;
93 }
94
95 @Override
Jonathan Hartedd6a442013-02-20 15:22:06 -080096 public synchronized void process(WatchedEvent event) throws Exception {
Jonathan Hartbd181b62013-02-17 16:05:38 -080097 log.debug("Watch Event: {}", event);
98
Jonathan Hartbd181b62013-02-17 16:05:38 -080099
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800100 if (event.getState() == KeeperState.Disconnected){
101 if (isLeader) {
102 log.debug("Disconnected while leader - lost leadership for {}", dpid);
103
104 isLeader = false;
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800105 ControlChangeCallback cb = switchCallbacks.get(dpid);
106 if (cb != null) {
107 //Allow callback to be null if the requester doesn't want a callback
108 cb.controlChanged(HexString.toLong(dpid), false);
109 }
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800110 }
111 return;
Jonathan Hart7bf62172013-02-28 13:17:18 -0800112 //TODO Watcher is never reset once we reconnect to Zookeeper
113 }
114
115 LeaderLatch latch = switchLatches.get(dpid);
116 if (latch == null){
117 log.debug("In watcher process, looks like control was released for {}",
118 dpid);
119 return;
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800120 }
Jonathan Hartbd181b62013-02-17 16:05:38 -0800121
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800122 try {
Jonathan Hartedd6a442013-02-20 15:22:06 -0800123
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800124 Participant leader = latch.getLeader();
125
Jonathan Hartbd766972013-02-22 15:13:03 -0800126 if (leader.getId().equals(controllerId) && !isLeader){
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800127 log.debug("Became leader for {}", dpid);
128
129 isLeader = true;
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800130 switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), true);
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800131 }
Jonathan Hartbd766972013-02-22 15:13:03 -0800132 else if (!leader.getId().equals(controllerId) && isLeader){
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800133 log.debug("Lost leadership for {}", dpid);
134
135 isLeader = false;
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800136 switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), false);
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800137 }
138 } catch (Exception e){
139 if (isLeader){
Jonathan Hart7bf62172013-02-28 13:17:18 -0800140 log.debug("Exception checking leadership status. Assume leadership lost for {}",
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800141 dpid);
142
143 isLeader = false;
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800144 switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), false);
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800145 }
Jonathan Hart7bf62172013-02-28 13:17:18 -0800146 } finally {
147 client.getChildren().usingWatcher(this).inBackground().forPath(latchPath);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800148 }
Jonathan Hartbd181b62013-02-17 16:05:38 -0800149 //client.getChildren().usingWatcher(this).forPath(latchPath);
150 }
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800151 }
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800152
153
Jonathan Hart7bf62172013-02-28 13:17:18 -0800154 /**
155 * Listens for changes to the switch znodes in Zookeeper. This maintains
156 * the second level of PathChildrenCaches that hold the controllers
157 * contending for each switch - there's one for each switch.
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800158 */
159 PathChildrenCacheListener switchPathCacheListener = new PathChildrenCacheListener() {
160 @Override
161 public void childEvent(CuratorFramework client,
162 PathChildrenCacheEvent event) throws Exception {
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800163 log.debug("Root switch path cache got {} event", event.getType());
164
165 String strSwitch = null;
166 if (event.getData() != null){
167 log.debug("Event path {}", event.getData().getPath());
168 String[] splitted = event.getData().getPath().split("/");
169 strSwitch = splitted[splitted.length - 1];
170 log.debug("Switch name is {}", strSwitch);
171 }
172
173 switch (event.getType()){
174 case CHILD_ADDED:
175 case CHILD_UPDATED:
176 //Check we have a PathChildrenCache for this child, add one if not
177 if (switchPathCaches.get(strSwitch) == null){
178 PathChildrenCache pc = new PathChildrenCache(client,
179 event.getData().getPath(), true);
180 pc.start(StartMode.NORMAL);
181 switchPathCaches.put(strSwitch, pc);
182 }
183 break;
184 case CHILD_REMOVED:
185 //Remove our PathChildrenCache for this child
186 PathChildrenCache pc = switchPathCaches.remove(strSwitch);
187 pc.close();
188 break;
189 default:
190 //All other events are connection status events. We need to do anything
191 //as the path cache handles these on its own.
192 break;
193 }
194
195 }
196 };
Jonathan Hartedd6a442013-02-20 15:22:06 -0800197
Jonathan Hartbd181b62013-02-17 16:05:38 -0800198
199 @Override
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800200 public void requestControl(long dpid, ControlChangeCallback cb) throws RegistryException {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800201 log.info("Requesting control for {}", HexString.toHexString(dpid));
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800202
Jonathan Hartbd766972013-02-22 15:13:03 -0800203 if (controllerId == null){
204 throw new RuntimeException("Must register a controller before calling requestControl");
Jonathan Hartbd181b62013-02-17 16:05:38 -0800205 }
206
207 String dpidStr = HexString.toHexString(dpid);
208 String latchPath = switchLatchesPath + "/" + dpidStr;
209
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800210 if (switchLatches.get(dpidStr) != null){
211 throw new RuntimeException("Leader election for switch " + dpidStr +
212 "is already running");
213 }
214
Jonathan Hartbd766972013-02-22 15:13:03 -0800215 LeaderLatch latch = new LeaderLatch(client, latchPath, controllerId);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800216 switchLatches.put(dpidStr, latch);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800217 switchCallbacks.put(dpidStr, cb);
218
219 try {
220 //client.getChildren().usingWatcher(watcher).inBackground().forPath(singleLatchPath);
221 client.getChildren().usingWatcher(
222 new ParamaterizedCuratorWatcher(dpidStr, latchPath))
223 .inBackground().forPath(latchPath);
224 latch.start();
225 } catch (Exception e) {
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800226 log.warn("Error starting leader latch: {}", e.getMessage());
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800227 throw new RegistryException("Error starting leader latch for " + dpidStr, e);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800228 }
229
230 }
231
232 @Override
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800233 public void releaseControl(long dpid) {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800234 log.info("Releasing control for {}", HexString.toHexString(dpid));
Jonathan Hart57080fb2013-02-21 10:55:46 -0800235
Jonathan Hartc6eee9e2013-02-18 14:58:27 -0800236 String dpidStr = HexString.toHexString(dpid);
237
238 LeaderLatch latch = switchLatches.get(dpidStr);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800239 if (latch == null) {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800240 log.debug("Trying to release control of a switch we are not contesting");
Jonathan Hartbd181b62013-02-17 16:05:38 -0800241 return;
242 }
243
244 try {
245 latch.close();
246 } catch (IOException e) {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800247 //I think it's OK not to do anything here. Either the node got
248 //deleted correctly, or the connection went down and the node got deleted.
Jonathan Hart1be46262013-02-20 16:43:51 -0800249 } finally {
250 switchLatches.remove(dpidStr);
251 switchCallbacks.remove(dpidStr);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800252 }
253 }
254
255 @Override
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800256 public boolean hasControl(long dpid) {
Jonathan Hart57080fb2013-02-21 10:55:46 -0800257
Jonathan Hartbd181b62013-02-17 16:05:38 -0800258 LeaderLatch latch = switchLatches.get(HexString.toHexString(dpid));
259
260 if (latch == null) {
261 log.warn("No leader latch for dpid {}", HexString.toHexString(dpid));
262 return false;
263 }
264
Jonathan Hartbd181b62013-02-17 16:05:38 -0800265 try {
Jonathan Hartbd766972013-02-22 15:13:03 -0800266 return latch.getLeader().getId().equals(controllerId);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800267 } catch (Exception e) {
268 //TODO swallow exception?
269 return false;
270 }
271 }
272
273 @Override
Jonathan Hart7bf62172013-02-28 13:17:18 -0800274 public String getControllerId() {
Jonathan Hartbd766972013-02-22 15:13:03 -0800275 return controllerId;
Jonathan Hartbd181b62013-02-17 16:05:38 -0800276 }
277
Jonathan Hartedd6a442013-02-20 15:22:06 -0800278 @Override
Jonathan Hart57080fb2013-02-21 10:55:46 -0800279 public Collection<String> getAllControllers() throws RegistryException {
Jonathan Hartedd6a442013-02-20 15:22:06 -0800280 log.debug("Getting all controllers");
Jonathan Hart1be46262013-02-20 16:43:51 -0800281
Jonathan Hartedd6a442013-02-20 15:22:06 -0800282 List<String> controllers = new ArrayList<String>();
283 for (ChildData data : controllerCache.getCurrentData()){
284
285 String d = null;
286 try {
287 d = new String(data.getData(), "UTF-8");
288 } catch (UnsupportedEncodingException e) {
Jonathan Hart57080fb2013-02-21 10:55:46 -0800289 throw new RegistryException("Error encoding string", e);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800290 }
291
292 controllers.add(d);
293 }
294 return controllers;
295 }
296
297 @Override
Jonathan Hart57080fb2013-02-21 10:55:46 -0800298 public void registerController(String id) throws RegistryException {
Jonathan Hartd10008d2013-02-23 17:04:08 -0800299 if (controllerId != null) {
300 throw new RegistryException(
301 "Controller already registered with id " + controllerId);
302 }
Jonathan Hartbd766972013-02-22 15:13:03 -0800303
304 controllerId = id;
Jonathan Hart57080fb2013-02-21 10:55:46 -0800305
Jonathan Hartd10008d2013-02-23 17:04:08 -0800306 byte bytes[] = id.getBytes(Charsets.UTF_8);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800307
308 String path = controllerPath + "/" + id;
309
310 log.info("Registering controller with id {}", id);
311
Jonathan Hart57080fb2013-02-21 10:55:46 -0800312 //Create ephemeral node in controller registry
Jonathan Hartedd6a442013-02-20 15:22:06 -0800313 try {
314 client.create().withProtection().withMode(CreateMode.EPHEMERAL)
315 .forPath(path, bytes);
316 } catch (Exception e) {
Jonathan Hart57080fb2013-02-21 10:55:46 -0800317 throw new RegistryException("Error contacting the Zookeeper service", e);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800318 }
319 }
320
321 @Override
Jonathan Hart57080fb2013-02-21 10:55:46 -0800322 public String getControllerForSwitch(long dpid) throws RegistryException {
Jonathan Hartedd6a442013-02-20 15:22:06 -0800323 // TODO Work out how we should store this controller/switch data.
324 // The leader latch might be a index to the /controllers collections
325 // which holds more info on the controller (how to talk to it for example).
326
Jonathan Hartedd6a442013-02-20 15:22:06 -0800327 String strDpid = HexString.toHexString(dpid);
328 LeaderLatch latch = switchLatches.get(strDpid);
329
330 if (latch == null){
331 log.warn("Tried to get controller for non-existent switch");
332 return null;
333 }
334
335 Participant leader = null;
336 try {
337 leader = latch.getLeader();
338 } catch (Exception e) {
Jonathan Hart57080fb2013-02-21 10:55:46 -0800339 throw new RegistryException("Error contacting the Zookeeper service", e);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800340 }
341
342 return leader.getId();
343 }
344
345 @Override
346 public Collection<Long> getSwitchesControlledByController(String controllerId) {
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800347 //TODO remove this if not needed
Jonathan Hartbd766972013-02-22 15:13:03 -0800348 throw new RuntimeException("Not yet implemented");
Jonathan Hartedd6a442013-02-20 15:22:06 -0800349 }
Jonathan Hartbd181b62013-02-17 16:05:38 -0800350
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800351
352 @Override
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800353 public Map<String, List<ControllerRegistryEntry>> getAllSwitches() {
354 Map<String, List<ControllerRegistryEntry>> data =
355 new HashMap<String, List<ControllerRegistryEntry>>();
356
357 for (Map.Entry<String, PathChildrenCache> entry : switchPathCaches.entrySet()){
358 List<ControllerRegistryEntry> contendingControllers =
359 new ArrayList<ControllerRegistryEntry>();
360
361 if (entry.getValue().getCurrentData().size() < 1){
362 log.info("Switch entry with no leader elections: {}", entry.getKey());
363 continue;
364 }
365
366 for (ChildData d : entry.getValue().getCurrentData()) {
Jonathan Hart97801ac2013-02-26 14:29:16 -0800367
Jonathan Hartd10008d2013-02-23 17:04:08 -0800368 String controllerId = new String(d.getData(), Charsets.UTF_8);
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800369
370 String[] splitted = d.getPath().split("-");
371 int sequenceNumber = Integer.parseInt(splitted[splitted.length - 1]);
372
373 contendingControllers.add(new ControllerRegistryEntry(controllerId, sequenceNumber));
374 }
375
376 Collections.sort(contendingControllers);
377 data.put(entry.getKey(), contendingControllers);
378 }
379 return data;
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800380 }
381
Jonathan Hartbd181b62013-02-17 16:05:38 -0800382 /*
383 * IFloodlightModule
384 */
385
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800386 @Override
387 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Jonathan Hartedd6a442013-02-20 15:22:06 -0800388 Collection<Class<? extends IFloodlightService>> l =
389 new ArrayList<Class<? extends IFloodlightService>>();
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800390 l.add(IControllerRegistryService.class);
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800391 return l;
392 }
393
394 @Override
395 public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
396 Map<Class<? extends IFloodlightService>, IFloodlightService> m =
397 new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800398 m.put(IControllerRegistryService.class, this);
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800399 return m;
400 }
401
402 @Override
403 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800404 Collection<Class<? extends IFloodlightService>> l =
405 new ArrayList<Class<? extends IFloodlightService>>();
406 l.add(IRestApiService.class);
407 return l;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800408 }
409
410 @Override
411 public void init (FloodlightModuleContext context) throws FloodlightModuleException {
Jonathan Hartbd766972013-02-22 15:13:03 -0800412 log.info("Initialising the Zookeeper Registry - Zookeeper connection required");
413
Jonathan Hart97801ac2013-02-26 14:29:16 -0800414 //Read the Zookeeper connection string from the config
415 Map<String, String> configParams = context.getConfigParams(this);
416 String connectionString = configParams.get("connectionString");
417 if (connectionString != null){
418 this.connectionString = connectionString;
Jonathan Hart57080fb2013-02-21 10:55:46 -0800419 }
Jonathan Hart97801ac2013-02-26 14:29:16 -0800420 log.info("Setting Zookeeper connection string to {}", this.connectionString);
Jonathan Hart57080fb2013-02-21 10:55:46 -0800421
Jonathan Hart97801ac2013-02-26 14:29:16 -0800422 restApi = context.getServiceImpl(IRestApiService.class);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800423
424 switchLatches = new HashMap<String, LeaderLatch>();
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800425 switchCallbacks = new HashMap<String, ControlChangeCallback>();
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800426 switchPathCaches = new HashMap<String, PathChildrenCache>();
Jonathan Hartbd181b62013-02-17 16:05:38 -0800427
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800428 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
Jonathan Hart97801ac2013-02-26 14:29:16 -0800429 client = CuratorFrameworkFactory.newClient(this.connectionString,
Jonathan Hartcc957a02013-02-26 10:39:04 -0800430 sessionTimeout, connectionTimeout, retryPolicy);
Jonathan Hartbd181b62013-02-17 16:05:38 -0800431
432 client.start();
433
434 client = client.usingNamespace(namespace);
Jonathan Hart97801ac2013-02-26 14:29:16 -0800435
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800436
Jonathan Hartedd6a442013-02-20 15:22:06 -0800437 controllerCache = new PathChildrenCache(client, controllerPath, true);
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800438 switchCache = new PathChildrenCache(client, switchLatchesPath, true);
439 switchCache.getListenable().addListener(switchPathCacheListener);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800440
441 try {
442 controllerCache.start(StartMode.BUILD_INITIAL_CACHE);
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800443
444 //Don't prime the cache, we want a notification for each child node in the path
445 switchCache.start(StartMode.NORMAL);
Jonathan Hartedd6a442013-02-20 15:22:06 -0800446 } catch (Exception e) {
Jonathan Hart7bf62172013-02-28 13:17:18 -0800447 throw new FloodlightModuleException("Error initialising ZookeeperRegistry: "
448 + e.getMessage());
Jonathan Hartedd6a442013-02-20 15:22:06 -0800449 }
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800450 }
451
452 @Override
453 public void startUp (FloodlightModuleContext context) {
Jonathan Hart3d7730a2013-02-22 11:51:17 -0800454 restApi.addRestletRoutable(new RegistryWebRoutable());
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800455 }
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800456}