Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 1 | package net.onrc.onos.registry.controller; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 2 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 3 | import java.io.IOException; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 4 | import java.io.UnsupportedEncodingException; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 5 | import java.net.UnknownHostException; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 6 | import java.util.ArrayList; |
| 7 | import java.util.Collection; |
| 8 | import java.util.HashMap; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 9 | import java.util.List; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 10 | import java.util.Map; |
| 11 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 12 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 13 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 14 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 15 | import net.floodlightcontroller.core.module.IFloodlightService; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 16 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 17 | import org.apache.zookeeper.CreateMode; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 18 | import org.apache.zookeeper.WatchedEvent; |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 19 | import org.apache.zookeeper.Watcher.Event.KeeperState; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 20 | import org.openflow.util.HexString; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 23 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 24 | import com.netflix.curator.RetryPolicy; |
| 25 | import com.netflix.curator.framework.CuratorFramework; |
| 26 | import com.netflix.curator.framework.CuratorFrameworkFactory; |
| 27 | import com.netflix.curator.framework.api.CuratorWatcher; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 28 | import com.netflix.curator.framework.recipes.cache.ChildData; |
| 29 | import com.netflix.curator.framework.recipes.cache.PathChildrenCache; |
| 30 | import com.netflix.curator.framework.recipes.cache.PathChildrenCache.StartMode; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 31 | import com.netflix.curator.framework.recipes.leader.LeaderLatch; |
| 32 | import com.netflix.curator.framework.recipes.leader.Participant; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 33 | import com.netflix.curator.retry.RetryOneTime; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 34 | |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 35 | public class RegistryManager implements IFloodlightModule, IControllerRegistryService { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 36 | |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 37 | protected static Logger log = LoggerFactory.getLogger(RegistryManager.class); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 38 | protected String mastershipId = null; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 39 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 40 | //TODO read this from configuration |
| 41 | protected String connectionString = "localhost:2181"; |
| 42 | private final String namespace = "onos"; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 43 | private final String switchLatchesPath = "/switches"; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 44 | |
| 45 | protected CuratorFramework client; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 46 | |
| 47 | private final String controllerPath = "/controllers"; |
| 48 | protected PathChildrenCache controllerCache; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 49 | |
| 50 | protected Map<String, LeaderLatch> switchLatches; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 51 | protected Map<String, ControlChangeCallback> switchCallbacks; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 52 | |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 53 | protected boolean moduleEnabled = false; |
| 54 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 55 | protected class ParamaterizedCuratorWatcher implements CuratorWatcher { |
| 56 | private String dpid; |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 57 | private boolean isLeader = false; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 58 | private String latchPath; |
| 59 | |
| 60 | public ParamaterizedCuratorWatcher(String dpid, String latchPath){ |
| 61 | this.dpid = dpid; |
| 62 | this.latchPath = latchPath; |
| 63 | } |
| 64 | |
| 65 | @Override |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 66 | public synchronized void process(WatchedEvent event) throws Exception { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 67 | log.debug("Watch Event: {}", event); |
| 68 | |
| 69 | LeaderLatch latch = switchLatches.get(dpid); |
| 70 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 71 | if (event.getState() == KeeperState.Disconnected){ |
| 72 | if (isLeader) { |
| 73 | log.debug("Disconnected while leader - lost leadership for {}", dpid); |
| 74 | |
| 75 | isLeader = false; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 76 | switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), false); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 77 | } |
| 78 | return; |
| 79 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 80 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 81 | try { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 82 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 83 | Participant leader = latch.getLeader(); |
| 84 | |
| 85 | if (leader.getId().equals(mastershipId) && !isLeader){ |
| 86 | log.debug("Became leader for {}", dpid); |
| 87 | |
| 88 | isLeader = true; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 89 | switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), true); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 90 | } |
| 91 | else if (!leader.getId().equals(mastershipId) && isLeader){ |
| 92 | log.debug("Lost leadership for {}", dpid); |
| 93 | |
| 94 | isLeader = false; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 95 | switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), false); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 96 | } |
| 97 | } catch (Exception e){ |
| 98 | if (isLeader){ |
| 99 | log.debug("Exception checking leadership status. Assume leadship lost for {}", |
| 100 | dpid); |
| 101 | |
| 102 | isLeader = false; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 103 | switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), false); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 104 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | client.getChildren().usingWatcher(this).inBackground().forPath(latchPath); |
| 108 | //client.getChildren().usingWatcher(this).forPath(latchPath); |
| 109 | } |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 110 | } |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 111 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 112 | |
| 113 | @Override |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 114 | public void requestControl(long dpid, ControlChangeCallback cb) throws Exception { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 115 | |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 116 | if (!moduleEnabled) return; |
| 117 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 118 | if (mastershipId == null){ |
| 119 | throw new RuntimeException("Must set mastershipId before calling aquireMastership"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | String dpidStr = HexString.toHexString(dpid); |
| 123 | String latchPath = switchLatchesPath + "/" + dpidStr; |
| 124 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 125 | if (switchLatches.get(dpidStr) != null){ |
| 126 | throw new RuntimeException("Leader election for switch " + dpidStr + |
| 127 | "is already running"); |
| 128 | } |
| 129 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 130 | LeaderLatch latch = new LeaderLatch(client, latchPath, mastershipId); |
| 131 | switchLatches.put(dpidStr, latch); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 132 | switchCallbacks.put(dpidStr, cb); |
| 133 | |
| 134 | try { |
| 135 | //client.getChildren().usingWatcher(watcher).inBackground().forPath(singleLatchPath); |
| 136 | client.getChildren().usingWatcher( |
| 137 | new ParamaterizedCuratorWatcher(dpidStr, latchPath)) |
| 138 | .inBackground().forPath(latchPath); |
| 139 | latch.start(); |
| 140 | } catch (Exception e) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 141 | log.warn("Error starting leader latch: {}", e.getMessage()); |
| 142 | throw e; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } |
| 146 | |
| 147 | @Override |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 148 | public void releaseControl(long dpid) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 149 | if (!moduleEnabled) return; |
| 150 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 151 | String dpidStr = HexString.toHexString(dpid); |
| 152 | |
| 153 | LeaderLatch latch = switchLatches.get(dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 154 | if (latch == null) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 155 | log.debug("Trying to release mastership for switch we are not contesting"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | |
| 159 | try { |
| 160 | latch.close(); |
| 161 | } catch (IOException e) { |
Jonathan Hart | 1be4626 | 2013-02-20 16:43:51 -0800 | [diff] [blame] | 162 | //I think it's OK not to do anything here. Either the node got deleted correctly, |
| 163 | //or the connection went down and the node got deleted. |
| 164 | } finally { |
| 165 | switchLatches.remove(dpidStr); |
| 166 | switchCallbacks.remove(dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | @Override |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 171 | public boolean hasControl(long dpid) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 172 | if (!moduleEnabled) return false; |
| 173 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 174 | LeaderLatch latch = switchLatches.get(HexString.toHexString(dpid)); |
| 175 | |
| 176 | if (latch == null) { |
| 177 | log.warn("No leader latch for dpid {}", HexString.toHexString(dpid)); |
| 178 | return false; |
| 179 | } |
| 180 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 181 | try { |
| 182 | return latch.getLeader().getId().equals(mastershipId); |
| 183 | } catch (Exception e) { |
| 184 | //TODO swallow exception? |
| 185 | return false; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void setMastershipId(String id) { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 191 | mastershipId = id; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public String getMastershipId() { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 196 | return mastershipId; |
| 197 | } |
| 198 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 199 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 200 | public Collection<String> getAllControllers() throws RegistryException { |
| 201 | if (!moduleEnabled) return null; |
| 202 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 203 | log.debug("Getting all controllers"); |
Jonathan Hart | 1be4626 | 2013-02-20 16:43:51 -0800 | [diff] [blame] | 204 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 205 | List<String> controllers = new ArrayList<String>(); |
| 206 | for (ChildData data : controllerCache.getCurrentData()){ |
| 207 | |
| 208 | String d = null; |
| 209 | try { |
| 210 | d = new String(data.getData(), "UTF-8"); |
| 211 | } catch (UnsupportedEncodingException e) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 212 | throw new RegistryException("Error encoding string", e); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | controllers.add(d); |
| 216 | } |
| 217 | return controllers; |
| 218 | } |
| 219 | |
| 220 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 221 | public void registerController(String id) throws RegistryException { |
| 222 | if (!moduleEnabled) return; |
| 223 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 224 | byte bytes[] = null; |
| 225 | try { |
| 226 | bytes = id.getBytes("UTF-8"); |
| 227 | } catch (UnsupportedEncodingException e1) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 228 | throw new RegistryException("Error encoding string", e1); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | String path = controllerPath + "/" + id; |
| 232 | |
| 233 | log.info("Registering controller with id {}", id); |
| 234 | |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 235 | //Create ephemeral node in controller registry |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 236 | try { |
| 237 | client.create().withProtection().withMode(CreateMode.EPHEMERAL) |
| 238 | .forPath(path, bytes); |
| 239 | } catch (Exception e) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 240 | throw new RegistryException("Error contacting the Zookeeper service", e); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 245 | public String getControllerForSwitch(long dpid) throws RegistryException { |
| 246 | if (!moduleEnabled) return null; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 247 | // TODO Work out how we should store this controller/switch data. |
| 248 | // The leader latch might be a index to the /controllers collections |
| 249 | // which holds more info on the controller (how to talk to it for example). |
| 250 | |
| 251 | |
| 252 | String strDpid = HexString.toHexString(dpid); |
| 253 | LeaderLatch latch = switchLatches.get(strDpid); |
| 254 | |
| 255 | if (latch == null){ |
| 256 | log.warn("Tried to get controller for non-existent switch"); |
| 257 | return null; |
| 258 | } |
| 259 | |
| 260 | Participant leader = null; |
| 261 | try { |
| 262 | leader = latch.getLeader(); |
| 263 | } catch (Exception e) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 264 | throw new RegistryException("Error contacting the Zookeeper service", e); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | return leader.getId(); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public Collection<Long> getSwitchesControlledByController(String controllerId) { |
| 272 | // TODO Auto-generated method stub |
| 273 | return null; |
| 274 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 275 | |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 276 | |
| 277 | @Override |
| 278 | public Collection<Map<String, String>> getAllSwitches() { |
| 279 | // TODO Auto-generated method stub |
| 280 | return null; |
| 281 | } |
| 282 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 283 | /* |
| 284 | * IFloodlightModule |
| 285 | */ |
| 286 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 287 | @Override |
| 288 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 289 | Collection<Class<? extends IFloodlightService>> l = |
| 290 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 291 | l.add(IControllerRegistryService.class); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 292 | return l; |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 297 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = |
| 298 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 299 | m.put(IControllerRegistryService.class, this); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 300 | return m; |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 305 | // no module dependencies |
| 306 | return null; |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public void init (FloodlightModuleContext context) throws FloodlightModuleException { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 311 | |
| 312 | //Read config to see if we should try and connect to zookeeper |
| 313 | Map<String, String> configOptions = context.getConfigParams(this); |
| 314 | String enableZookeeper = configOptions.get("enableZookeeper"); |
| 315 | if (enableZookeeper != null) { |
| 316 | log.info("Enabling Mastership module - requires Zookeeper connection"); |
| 317 | moduleEnabled = true; |
| 318 | } |
| 319 | else { |
| 320 | log.info("Mastership module is disabled"); |
| 321 | return; |
| 322 | } |
| 323 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 324 | try { |
| 325 | String localHostname = java.net.InetAddress.getLocalHost().getHostName(); |
| 326 | mastershipId = localHostname; |
| 327 | log.debug("Setting mastership id to {}", mastershipId); |
| 328 | } catch (UnknownHostException e) { |
| 329 | // TODO Handle this exception |
| 330 | e.printStackTrace(); |
| 331 | } |
| 332 | |
| 333 | switchLatches = new HashMap<String, LeaderLatch>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 334 | switchCallbacks = new HashMap<String, ControlChangeCallback>(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 335 | |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 336 | //RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); |
| 337 | RetryPolicy retryPolicy = new RetryOneTime(0); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 338 | client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy); |
| 339 | |
| 340 | client.start(); |
| 341 | |
| 342 | client = client.usingNamespace(namespace); |
| 343 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 344 | controllerCache = new PathChildrenCache(client, controllerPath, true); |
| 345 | |
| 346 | try { |
| 347 | controllerCache.start(StartMode.BUILD_INITIAL_CACHE); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 348 | } catch (Exception e) { |
| 349 | // TODO Auto-generated catch block |
| 350 | e.printStackTrace(); |
| 351 | } |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public void startUp (FloodlightModuleContext context) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 356 | // Nothing to be done on startup |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 357 | } |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame^] | 358 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 359 | } |