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