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; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 8 | import java.util.Collections; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 9 | import java.util.HashMap; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 10 | import java.util.List; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 11 | import java.util.Map; |
| 12 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 13 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 14 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 15 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 16 | import net.floodlightcontroller.core.module.IFloodlightService; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 17 | import net.floodlightcontroller.restserver.IRestApiService; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 18 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 19 | import org.apache.commons.lang.NotImplementedException; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 20 | import org.apache.zookeeper.CreateMode; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 21 | import org.apache.zookeeper.WatchedEvent; |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 22 | import org.apache.zookeeper.Watcher.Event.KeeperState; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 23 | import org.openflow.util.HexString; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 26 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 27 | import com.netflix.curator.RetryPolicy; |
| 28 | import com.netflix.curator.framework.CuratorFramework; |
| 29 | import com.netflix.curator.framework.CuratorFrameworkFactory; |
| 30 | import com.netflix.curator.framework.api.CuratorWatcher; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 31 | import com.netflix.curator.framework.recipes.cache.ChildData; |
| 32 | import com.netflix.curator.framework.recipes.cache.PathChildrenCache; |
| 33 | import com.netflix.curator.framework.recipes.cache.PathChildrenCache.StartMode; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 34 | import com.netflix.curator.framework.recipes.cache.PathChildrenCacheEvent; |
| 35 | import com.netflix.curator.framework.recipes.cache.PathChildrenCacheListener; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 36 | import com.netflix.curator.framework.recipes.leader.LeaderLatch; |
| 37 | import com.netflix.curator.framework.recipes.leader.Participant; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 38 | import com.netflix.curator.retry.ExponentialBackoffRetry; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 39 | |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 40 | public class RegistryManager implements IFloodlightModule, IControllerRegistryService { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 41 | |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 42 | protected static Logger log = LoggerFactory.getLogger(RegistryManager.class); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 43 | protected String mastershipId = null; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 44 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 45 | protected IRestApiService restApi; |
| 46 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 47 | //TODO read this from configuration |
| 48 | protected String connectionString = "localhost:2181"; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 49 | |
| 50 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 51 | private final String namespace = "onos"; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 52 | private final String switchLatchesPath = "/switches"; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 53 | private final String controllerPath = "/controllers"; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 54 | |
| 55 | protected CuratorFramework client; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 56 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 57 | protected PathChildrenCache controllerCache; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 58 | protected PathChildrenCache switchCache; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 59 | |
| 60 | protected Map<String, LeaderLatch> switchLatches; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 61 | protected Map<String, ControlChangeCallback> switchCallbacks; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 62 | protected Map<String, PathChildrenCache> switchPathCaches; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 63 | |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 64 | protected boolean moduleEnabled = false; |
| 65 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 66 | protected class ParamaterizedCuratorWatcher implements CuratorWatcher { |
| 67 | private String dpid; |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 68 | private boolean isLeader = false; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 69 | private String latchPath; |
| 70 | |
| 71 | public ParamaterizedCuratorWatcher(String dpid, String latchPath){ |
| 72 | this.dpid = dpid; |
| 73 | this.latchPath = latchPath; |
| 74 | } |
| 75 | |
| 76 | @Override |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 77 | public synchronized void process(WatchedEvent event) throws Exception { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 78 | log.debug("Watch Event: {}", event); |
| 79 | |
| 80 | LeaderLatch latch = switchLatches.get(dpid); |
| 81 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 82 | if (event.getState() == KeeperState.Disconnected){ |
| 83 | if (isLeader) { |
| 84 | log.debug("Disconnected while leader - lost leadership for {}", dpid); |
| 85 | |
| 86 | isLeader = false; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 87 | ControlChangeCallback cb = switchCallbacks.get(dpid); |
| 88 | if (cb != null) { |
| 89 | //Allow callback to be null if the requester doesn't want a callback |
| 90 | cb.controlChanged(HexString.toLong(dpid), false); |
| 91 | } |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 92 | } |
| 93 | return; |
| 94 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 95 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 96 | try { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 97 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 98 | Participant leader = latch.getLeader(); |
| 99 | |
| 100 | if (leader.getId().equals(mastershipId) && !isLeader){ |
| 101 | log.debug("Became leader for {}", dpid); |
| 102 | |
| 103 | isLeader = true; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 104 | switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), true); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 105 | } |
| 106 | else if (!leader.getId().equals(mastershipId) && isLeader){ |
| 107 | log.debug("Lost leadership for {}", dpid); |
| 108 | |
| 109 | isLeader = false; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 110 | switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), false); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 111 | } |
| 112 | } catch (Exception e){ |
| 113 | if (isLeader){ |
| 114 | log.debug("Exception checking leadership status. Assume leadship lost for {}", |
| 115 | dpid); |
| 116 | |
| 117 | isLeader = false; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 118 | switchCallbacks.get(dpid).controlChanged(HexString.toLong(dpid), false); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 119 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | client.getChildren().usingWatcher(this).inBackground().forPath(latchPath); |
| 123 | //client.getChildren().usingWatcher(this).forPath(latchPath); |
| 124 | } |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 125 | } |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 126 | |
| 127 | |
| 128 | /* |
| 129 | * Listens for changes to the switch znodes in Zookeeper. This maintains the second level of |
| 130 | * PathChildrenCaches that hold the controllers contending for each switch - there's one for |
| 131 | * each switch. |
| 132 | */ |
| 133 | PathChildrenCacheListener switchPathCacheListener = new PathChildrenCacheListener() { |
| 134 | @Override |
| 135 | public void childEvent(CuratorFramework client, |
| 136 | PathChildrenCacheEvent event) throws Exception { |
| 137 | // TODO Auto-generated method stub |
| 138 | log.debug("Root switch path cache got {} event", event.getType()); |
| 139 | |
| 140 | String strSwitch = null; |
| 141 | if (event.getData() != null){ |
| 142 | log.debug("Event path {}", event.getData().getPath()); |
| 143 | String[] splitted = event.getData().getPath().split("/"); |
| 144 | strSwitch = splitted[splitted.length - 1]; |
| 145 | log.debug("Switch name is {}", strSwitch); |
| 146 | } |
| 147 | |
| 148 | switch (event.getType()){ |
| 149 | case CHILD_ADDED: |
| 150 | case CHILD_UPDATED: |
| 151 | //Check we have a PathChildrenCache for this child, add one if not |
| 152 | if (switchPathCaches.get(strSwitch) == null){ |
| 153 | PathChildrenCache pc = new PathChildrenCache(client, |
| 154 | event.getData().getPath(), true); |
| 155 | pc.start(StartMode.NORMAL); |
| 156 | switchPathCaches.put(strSwitch, pc); |
| 157 | } |
| 158 | break; |
| 159 | case CHILD_REMOVED: |
| 160 | //Remove our PathChildrenCache for this child |
| 161 | PathChildrenCache pc = switchPathCaches.remove(strSwitch); |
| 162 | pc.close(); |
| 163 | break; |
| 164 | default: |
| 165 | //All other events are connection status events. We need to do anything |
| 166 | //as the path cache handles these on its own. |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | } |
| 171 | }; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 172 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 173 | |
| 174 | @Override |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 175 | public void requestControl(long dpid, ControlChangeCallback cb) throws RegistryException { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 176 | |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 177 | if (!moduleEnabled) return; |
| 178 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 179 | if (mastershipId == null){ |
| 180 | throw new RuntimeException("Must set mastershipId before calling aquireMastership"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | String dpidStr = HexString.toHexString(dpid); |
| 184 | String latchPath = switchLatchesPath + "/" + dpidStr; |
| 185 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 186 | if (switchLatches.get(dpidStr) != null){ |
| 187 | throw new RuntimeException("Leader election for switch " + dpidStr + |
| 188 | "is already running"); |
| 189 | } |
| 190 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 191 | LeaderLatch latch = new LeaderLatch(client, latchPath, mastershipId); |
| 192 | switchLatches.put(dpidStr, latch); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 193 | switchCallbacks.put(dpidStr, cb); |
| 194 | |
| 195 | try { |
| 196 | //client.getChildren().usingWatcher(watcher).inBackground().forPath(singleLatchPath); |
| 197 | client.getChildren().usingWatcher( |
| 198 | new ParamaterizedCuratorWatcher(dpidStr, latchPath)) |
| 199 | .inBackground().forPath(latchPath); |
| 200 | latch.start(); |
| 201 | } catch (Exception e) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 202 | log.warn("Error starting leader latch: {}", e.getMessage()); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 203 | throw new RegistryException("Error starting leader latch for " + dpidStr, e); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | } |
| 207 | |
| 208 | @Override |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 209 | public void releaseControl(long dpid) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 210 | if (!moduleEnabled) return; |
| 211 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 212 | String dpidStr = HexString.toHexString(dpid); |
| 213 | |
| 214 | LeaderLatch latch = switchLatches.get(dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 215 | if (latch == null) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 216 | log.debug("Trying to release mastership for switch we are not contesting"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | |
| 220 | try { |
| 221 | latch.close(); |
| 222 | } catch (IOException e) { |
Jonathan Hart | 1be4626 | 2013-02-20 16:43:51 -0800 | [diff] [blame] | 223 | //I think it's OK not to do anything here. Either the node got deleted correctly, |
| 224 | //or the connection went down and the node got deleted. |
| 225 | } finally { |
| 226 | switchLatches.remove(dpidStr); |
| 227 | switchCallbacks.remove(dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | @Override |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 232 | public boolean hasControl(long dpid) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 233 | if (!moduleEnabled) return false; |
| 234 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 235 | LeaderLatch latch = switchLatches.get(HexString.toHexString(dpid)); |
| 236 | |
| 237 | if (latch == null) { |
| 238 | log.warn("No leader latch for dpid {}", HexString.toHexString(dpid)); |
| 239 | return false; |
| 240 | } |
| 241 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 242 | try { |
| 243 | return latch.getLeader().getId().equals(mastershipId); |
| 244 | } catch (Exception e) { |
| 245 | //TODO swallow exception? |
| 246 | return false; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public void setMastershipId(String id) { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 252 | mastershipId = id; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public String getMastershipId() { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 257 | return mastershipId; |
| 258 | } |
| 259 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 260 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 261 | public Collection<String> getAllControllers() throws RegistryException { |
| 262 | if (!moduleEnabled) return null; |
| 263 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 264 | log.debug("Getting all controllers"); |
Jonathan Hart | 1be4626 | 2013-02-20 16:43:51 -0800 | [diff] [blame] | 265 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 266 | List<String> controllers = new ArrayList<String>(); |
| 267 | for (ChildData data : controllerCache.getCurrentData()){ |
| 268 | |
| 269 | String d = null; |
| 270 | try { |
| 271 | d = new String(data.getData(), "UTF-8"); |
| 272 | } catch (UnsupportedEncodingException e) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 273 | throw new RegistryException("Error encoding string", e); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | controllers.add(d); |
| 277 | } |
| 278 | return controllers; |
| 279 | } |
| 280 | |
| 281 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 282 | public void registerController(String id) throws RegistryException { |
| 283 | if (!moduleEnabled) return; |
| 284 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 285 | byte bytes[] = null; |
| 286 | try { |
| 287 | bytes = id.getBytes("UTF-8"); |
| 288 | } catch (UnsupportedEncodingException e1) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 289 | throw new RegistryException("Error encoding string", e1); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | String path = controllerPath + "/" + id; |
| 293 | |
| 294 | log.info("Registering controller with id {}", id); |
| 295 | |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 296 | //Create ephemeral node in controller registry |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 297 | try { |
| 298 | client.create().withProtection().withMode(CreateMode.EPHEMERAL) |
| 299 | .forPath(path, bytes); |
| 300 | } catch (Exception e) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 301 | throw new RegistryException("Error contacting the Zookeeper service", e); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 306 | public String getControllerForSwitch(long dpid) throws RegistryException { |
| 307 | if (!moduleEnabled) return null; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 308 | // TODO Work out how we should store this controller/switch data. |
| 309 | // The leader latch might be a index to the /controllers collections |
| 310 | // which holds more info on the controller (how to talk to it for example). |
| 311 | |
| 312 | |
| 313 | String strDpid = HexString.toHexString(dpid); |
| 314 | LeaderLatch latch = switchLatches.get(strDpid); |
| 315 | |
| 316 | if (latch == null){ |
| 317 | log.warn("Tried to get controller for non-existent switch"); |
| 318 | return null; |
| 319 | } |
| 320 | |
| 321 | Participant leader = null; |
| 322 | try { |
| 323 | leader = latch.getLeader(); |
| 324 | } catch (Exception e) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 325 | throw new RegistryException("Error contacting the Zookeeper service", e); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | return leader.getId(); |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public Collection<Long> getSwitchesControlledByController(String controllerId) { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 333 | //TODO remove this if not needed |
| 334 | throw new NotImplementedException(); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 335 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 336 | |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 337 | |
| 338 | @Override |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 339 | public Map<String, List<ControllerRegistryEntry>> getAllSwitches() { |
| 340 | Map<String, List<ControllerRegistryEntry>> data = |
| 341 | new HashMap<String, List<ControllerRegistryEntry>>(); |
| 342 | |
| 343 | for (Map.Entry<String, PathChildrenCache> entry : switchPathCaches.entrySet()){ |
| 344 | List<ControllerRegistryEntry> contendingControllers = |
| 345 | new ArrayList<ControllerRegistryEntry>(); |
| 346 | |
| 347 | if (entry.getValue().getCurrentData().size() < 1){ |
| 348 | log.info("Switch entry with no leader elections: {}", entry.getKey()); |
| 349 | continue; |
| 350 | } |
| 351 | |
| 352 | for (ChildData d : entry.getValue().getCurrentData()) { |
| 353 | /* |
| 354 | if (d.getPath().length() < 1){ |
| 355 | log.info("Switch entry with no leader elections: {}", d.getPath()); |
| 356 | continue; |
| 357 | } |
| 358 | */ |
| 359 | |
| 360 | String controllerId = null; |
| 361 | try { |
| 362 | controllerId = new String(d.getData(), "UTF-8"); |
| 363 | } catch (UnsupportedEncodingException e) { |
| 364 | log.warn("Encoding exception: {}", e.getMessage()); |
| 365 | } |
| 366 | |
| 367 | String[] splitted = d.getPath().split("-"); |
| 368 | int sequenceNumber = Integer.parseInt(splitted[splitted.length - 1]); |
| 369 | |
| 370 | contendingControllers.add(new ControllerRegistryEntry(controllerId, sequenceNumber)); |
| 371 | } |
| 372 | |
| 373 | Collections.sort(contendingControllers); |
| 374 | data.put(entry.getKey(), contendingControllers); |
| 375 | } |
| 376 | return data; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 379 | /* |
| 380 | * IFloodlightModule |
| 381 | */ |
| 382 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 383 | @Override |
| 384 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 385 | Collection<Class<? extends IFloodlightService>> l = |
| 386 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 387 | l.add(IControllerRegistryService.class); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 388 | return l; |
| 389 | } |
| 390 | |
| 391 | @Override |
| 392 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 393 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = |
| 394 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 395 | m.put(IControllerRegistryService.class, this); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 396 | return m; |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 401 | Collection<Class<? extends IFloodlightService>> l = |
| 402 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 403 | l.add(IRestApiService.class); |
| 404 | return l; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public void init (FloodlightModuleContext context) throws FloodlightModuleException { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 409 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 410 | restApi = context.getServiceImpl(IRestApiService.class); |
| 411 | |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 412 | //Read config to see if we should try and connect to zookeeper |
| 413 | Map<String, String> configOptions = context.getConfigParams(this); |
| 414 | String enableZookeeper = configOptions.get("enableZookeeper"); |
| 415 | if (enableZookeeper != null) { |
| 416 | log.info("Enabling Mastership module - requires Zookeeper connection"); |
| 417 | moduleEnabled = true; |
| 418 | } |
| 419 | else { |
| 420 | log.info("Mastership module is disabled"); |
| 421 | return; |
| 422 | } |
| 423 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 424 | try { |
| 425 | String localHostname = java.net.InetAddress.getLocalHost().getHostName(); |
| 426 | mastershipId = localHostname; |
| 427 | log.debug("Setting mastership id to {}", mastershipId); |
| 428 | } catch (UnknownHostException e) { |
| 429 | // TODO Handle this exception |
| 430 | e.printStackTrace(); |
| 431 | } |
| 432 | |
| 433 | switchLatches = new HashMap<String, LeaderLatch>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 434 | switchCallbacks = new HashMap<String, ControlChangeCallback>(); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 435 | switchPathCaches = new HashMap<String, PathChildrenCache>(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 436 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 437 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); |
| 438 | //RetryPolicy retryPolicy = new RetryOneTime(0); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 439 | client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy); |
| 440 | |
| 441 | client.start(); |
| 442 | |
| 443 | client = client.usingNamespace(namespace); |
| 444 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 445 | //Put some data in for testing |
| 446 | try { |
| 447 | registerController(mastershipId); |
| 448 | requestControl(2L, null); |
| 449 | } catch (RegistryException e1) { |
| 450 | // TODO Auto-generated catch block |
| 451 | e1.printStackTrace(); |
| 452 | } |
| 453 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 454 | controllerCache = new PathChildrenCache(client, controllerPath, true); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 455 | switchCache = new PathChildrenCache(client, switchLatchesPath, true); |
| 456 | switchCache.getListenable().addListener(switchPathCacheListener); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 457 | |
| 458 | try { |
| 459 | controllerCache.start(StartMode.BUILD_INITIAL_CACHE); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 460 | |
| 461 | //Don't prime the cache, we want a notification for each child node in the path |
| 462 | switchCache.start(StartMode.NORMAL); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 463 | } catch (Exception e) { |
| 464 | // TODO Auto-generated catch block |
| 465 | e.printStackTrace(); |
| 466 | } |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public void startUp (FloodlightModuleContext context) { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame^] | 471 | restApi.addRestletRoutable(new RegistryWebRoutable()); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 472 | } |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 473 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 474 | } |