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; |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 11 | import java.util.concurrent.ConcurrentHashMap; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 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 | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 19 | import org.apache.zookeeper.CreateMode; |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame^] | 20 | import org.apache.zookeeper.WatchedEvent; |
| 21 | import org.apache.zookeeper.Watcher; |
| 22 | import org.apache.zookeeper.data.Stat; |
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 | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 27 | import com.google.common.base.Charsets; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 28 | import com.netflix.curator.RetryPolicy; |
| 29 | import com.netflix.curator.framework.CuratorFramework; |
| 30 | import com.netflix.curator.framework.CuratorFrameworkFactory; |
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; |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 37 | import com.netflix.curator.framework.recipes.leader.LeaderLatchEvent; |
| 38 | import com.netflix.curator.framework.recipes.leader.LeaderLatchListener; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 39 | import com.netflix.curator.framework.recipes.leader.Participant; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 40 | import com.netflix.curator.retry.ExponentialBackoffRetry; |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame^] | 41 | import com.netflix.curator.utils.ZKPaths; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 42 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 43 | /** |
| 44 | * A registry service that uses Zookeeper. All data is stored in Zookeeper, |
| 45 | * so this can be used as a global registry in a multi-node ONOS cluster. |
| 46 | * @author jono |
| 47 | * |
| 48 | */ |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 49 | public class ZookeeperRegistry implements IFloodlightModule, IControllerRegistryService { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 50 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 51 | protected static Logger log = LoggerFactory.getLogger(ZookeeperRegistry.class); |
| 52 | protected String controllerId = null; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 53 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 54 | protected IRestApiService restApi; |
| 55 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 56 | //This is the default, it's overwritten by the connectionString configuration parameter |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 57 | protected String connectionString = "localhost:2181"; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 58 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 59 | private final String namespace = "onos"; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 60 | private final String switchLatchesPath = "/switches"; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 61 | private final String controllerPath = "/controllers"; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 62 | |
| 63 | protected CuratorFramework client; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 64 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 65 | protected PathChildrenCache controllerCache; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 66 | protected PathChildrenCache switchCache; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 67 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 68 | protected ConcurrentHashMap<String, SwitchLeadershipData> switches; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 69 | protected Map<String, PathChildrenCache> switchPathCaches; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 70 | |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 71 | //Zookeeper performance-related configuration |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame^] | 72 | protected static final int sessionTimeout = 5000; |
| 73 | protected static final int connectionTimeout = 7000; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 74 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 75 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 76 | protected class SwitchLeaderListener implements LeaderLatchListener{ |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 77 | String dpid; |
| 78 | LeaderLatch latch; |
| 79 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 80 | public SwitchLeaderListener(String dpid, LeaderLatch latch){ |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 81 | this.dpid = dpid; |
| 82 | this.latch = latch; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void leaderLatchEvent(CuratorFramework arg0, |
| 87 | LeaderLatchEvent arg1) { |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 88 | log.debug("Leadership changed for {}, now {}", |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 89 | dpid, latch.hasLeadership()); |
| 90 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 91 | //Check that the leadership request is still active - the client |
| 92 | //may have since released the request or even begun another request |
| 93 | //(this is why we use == to check the object instance is the same) |
| 94 | SwitchLeadershipData swData = switches.get(dpid); |
| 95 | if (swData != null && swData.getLatch() == latch){ |
| 96 | swData.getCallback().controlChanged( |
| 97 | HexString.toLong(dpid), latch.hasLeadership()); |
| 98 | } |
| 99 | else { |
| 100 | log.debug("Latch for {} has changed", dpid); |
| 101 | } |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 105 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 106 | /** |
| 107 | * Listens for changes to the switch znodes in Zookeeper. This maintains |
| 108 | * the second level of PathChildrenCaches that hold the controllers |
| 109 | * contending for each switch - there's one for each switch. |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 110 | */ |
| 111 | PathChildrenCacheListener switchPathCacheListener = new PathChildrenCacheListener() { |
| 112 | @Override |
| 113 | public void childEvent(CuratorFramework client, |
| 114 | PathChildrenCacheEvent event) throws Exception { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 115 | log.debug("Root switch path cache got {} event", event.getType()); |
| 116 | |
| 117 | String strSwitch = null; |
| 118 | if (event.getData() != null){ |
| 119 | log.debug("Event path {}", event.getData().getPath()); |
| 120 | String[] splitted = event.getData().getPath().split("/"); |
| 121 | strSwitch = splitted[splitted.length - 1]; |
| 122 | log.debug("Switch name is {}", strSwitch); |
| 123 | } |
| 124 | |
| 125 | switch (event.getType()){ |
| 126 | case CHILD_ADDED: |
| 127 | case CHILD_UPDATED: |
| 128 | //Check we have a PathChildrenCache for this child, add one if not |
| 129 | if (switchPathCaches.get(strSwitch) == null){ |
| 130 | PathChildrenCache pc = new PathChildrenCache(client, |
| 131 | event.getData().getPath(), true); |
| 132 | pc.start(StartMode.NORMAL); |
| 133 | switchPathCaches.put(strSwitch, pc); |
| 134 | } |
| 135 | break; |
| 136 | case CHILD_REMOVED: |
| 137 | //Remove our PathChildrenCache for this child |
| 138 | PathChildrenCache pc = switchPathCaches.remove(strSwitch); |
| 139 | pc.close(); |
| 140 | break; |
| 141 | default: |
| 142 | //All other events are connection status events. We need to do anything |
| 143 | //as the path cache handles these on its own. |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | } |
| 148 | }; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 149 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 150 | |
| 151 | @Override |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 152 | public void requestControl(long dpid, ControlChangeCallback cb) throws RegistryException { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 153 | log.info("Requesting control for {}", HexString.toHexString(dpid)); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 154 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 155 | if (controllerId == null){ |
| 156 | throw new RuntimeException("Must register a controller before calling requestControl"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | String dpidStr = HexString.toHexString(dpid); |
| 160 | String latchPath = switchLatchesPath + "/" + dpidStr; |
| 161 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 162 | if (switches.get(dpidStr) != null){ |
Jonathan Hart | 3c0eccd | 2013-03-12 22:32:50 -0700 | [diff] [blame] | 163 | log.debug("Already contesting {}, returning", HexString.toHexString(dpid)); |
| 164 | return; |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 167 | LeaderLatch latch = new LeaderLatch(client, latchPath, controllerId); |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 168 | latch.addListener(new SwitchLeaderListener(dpidStr, latch)); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 169 | |
Jonathan Hart | 44e56fc | 2013-03-14 16:53:59 -0700 | [diff] [blame] | 170 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 171 | SwitchLeadershipData swData = new SwitchLeadershipData(latch, cb); |
| 172 | SwitchLeadershipData oldData = switches.putIfAbsent(dpidStr, swData); |
| 173 | |
| 174 | if (oldData != null){ |
| 175 | //There was already data for that key in the map |
| 176 | //i.e. someone else got here first so we can't succeed |
| 177 | log.debug("Already requested control for {}", dpidStr); |
| 178 | throw new RegistryException("Already requested control for " + dpidStr); |
| 179 | } |
| 180 | |
| 181 | //Now that we know we were able to add our latch to the collection, |
Jonathan Hart | 44e56fc | 2013-03-14 16:53:59 -0700 | [diff] [blame] | 182 | //we can start the leader election in Zookeeper. However I don't know |
| 183 | //how to handle if the start fails - the latch is already in our |
| 184 | //switches list. |
| 185 | //TODO seems like there's a Curator bug when latch.start is called when |
| 186 | //there's no Zookeeper connection which causes two znodes to be put in |
| 187 | //Zookeeper at the latch path when we reconnect to Zookeeper. |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 188 | try { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 189 | latch.start(); |
| 190 | } catch (Exception e) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 191 | log.warn("Error starting leader latch: {}", e.getMessage()); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 192 | throw new RegistryException("Error starting leader latch for " + dpidStr, e); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | } |
| 196 | |
| 197 | @Override |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 198 | public void releaseControl(long dpid) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 199 | log.info("Releasing control for {}", HexString.toHexString(dpid)); |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 200 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 201 | String dpidStr = HexString.toHexString(dpid); |
| 202 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 203 | SwitchLeadershipData swData = switches.remove(dpidStr); |
| 204 | |
| 205 | if (swData == null) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 206 | log.debug("Trying to release control of a switch we are not contesting"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 207 | return; |
| 208 | } |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 209 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 210 | LeaderLatch latch = swData.getLatch(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 211 | |
| 212 | try { |
| 213 | latch.close(); |
| 214 | } catch (IOException e) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 215 | //I think it's OK not to do anything here. Either the node got |
| 216 | //deleted correctly, or the connection went down and the node got deleted. |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | @Override |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 221 | public boolean hasControl(long dpid) { |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 222 | String dpidStr = HexString.toHexString(dpid); |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 223 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 224 | SwitchLeadershipData swData = switches.get(dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 225 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 226 | if (swData == null) { |
| 227 | log.warn("No leader latch for dpid {}", dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 231 | return swData.getLatch().hasLeadership(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 235 | public String getControllerId() { |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 236 | return controllerId; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 239 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 240 | public Collection<String> getAllControllers() throws RegistryException { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 241 | log.debug("Getting all controllers"); |
Jonathan Hart | 1be4626 | 2013-02-20 16:43:51 -0800 | [diff] [blame] | 242 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 243 | List<String> controllers = new ArrayList<String>(); |
| 244 | for (ChildData data : controllerCache.getCurrentData()){ |
| 245 | |
| 246 | String d = null; |
| 247 | try { |
| 248 | d = new String(data.getData(), "UTF-8"); |
| 249 | } catch (UnsupportedEncodingException e) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 250 | throw new RegistryException("Error encoding string", e); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | controllers.add(d); |
| 254 | } |
| 255 | return controllers; |
| 256 | } |
| 257 | |
| 258 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 259 | public void registerController(String id) throws RegistryException { |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 260 | if (controllerId != null) { |
| 261 | throw new RegistryException( |
| 262 | "Controller already registered with id " + controllerId); |
| 263 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 264 | |
| 265 | controllerId = id; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 266 | |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 267 | byte bytes[] = id.getBytes(Charsets.UTF_8); |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame^] | 268 | String path = ZKPaths.makePath(controllerPath, controllerId); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 269 | |
| 270 | log.info("Registering controller with id {}", id); |
| 271 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 272 | try { |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame^] | 273 | //We need to set a watch to recreate the node in the controller |
| 274 | //registry if it gets deleted - e.g. on Zookeeper connection loss. |
| 275 | Watcher watcher = new Watcher(){ |
| 276 | @Override |
| 277 | public void process(WatchedEvent event) { |
| 278 | log.debug("got any watch event {} ", event); |
| 279 | |
| 280 | String path = ZKPaths.makePath(controllerPath, controllerId); |
| 281 | byte bytes[] = controllerId.getBytes(Charsets.UTF_8); |
| 282 | |
| 283 | try { |
| 284 | if (event.getType() == Event.EventType.NodeDeleted){ |
| 285 | log.debug("got a node deleted event"); |
| 286 | |
| 287 | |
| 288 | client.create().withMode(CreateMode.EPHEMERAL) |
| 289 | .forPath(path, bytes); |
| 290 | } |
| 291 | } catch (Exception e) { |
| 292 | log.warn("Error recreating controller node for {}: {}", |
| 293 | controllerId, e.getMessage()); |
| 294 | } finally { |
| 295 | try { |
| 296 | client.checkExists().usingWatcher(this).forPath(path); |
| 297 | } catch (Exception e2){ |
| 298 | log.warn("Error resetting watch for {}: {}", |
| 299 | controllerId, e2.getMessage()); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | }; |
| 304 | |
| 305 | //Create ephemeral node in controller registry |
| 306 | //TODO Use protection |
| 307 | client.create().withMode(CreateMode.EPHEMERAL) |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 308 | .forPath(path, bytes); |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame^] | 309 | client.checkExists().usingWatcher(watcher).forPath(path); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 310 | } catch (Exception e) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 311 | throw new RegistryException("Error contacting the Zookeeper service", e); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 316 | public String getControllerForSwitch(long dpid) throws RegistryException { |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 317 | // TODO work out synchronization |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 318 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 319 | String dpidStr = HexString.toHexString(dpid); |
Jonathan Hart | 44e56fc | 2013-03-14 16:53:59 -0700 | [diff] [blame] | 320 | |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame^] | 321 | SwitchLeadershipData swData = switches.get(dpidStr); |
| 322 | //LeaderLatch latch = (switches.get(dpidStr) != null)?switches.get(dpidStr).getLatch():null; |
Pankaj Berde | 017960a | 2013-03-14 20:32:26 -0700 | [diff] [blame] | 323 | |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame^] | 324 | if (swData == null){ |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 325 | log.warn("Tried to get controller for non-existent switch"); |
| 326 | return null; |
| 327 | } |
| 328 | |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame^] | 329 | LeaderLatch latch = swData.getLatch(); |
| 330 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 331 | Participant leader = null; |
| 332 | try { |
| 333 | leader = latch.getLeader(); |
| 334 | } catch (Exception e) { |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 335 | throw new RegistryException("Error contacting the Zookeeper service", e); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | return leader.getId(); |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public Collection<Long> getSwitchesControlledByController(String controllerId) { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 343 | //TODO remove this if not needed |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 344 | throw new RuntimeException("Not yet implemented"); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 345 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 346 | |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 347 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 348 | //TODO what should happen when there's no ZK connection? Currently we just return |
| 349 | //the cache but this may lead to false impressions - i.e. we don't actually know |
| 350 | //what's in ZK so we shouldn't say we do |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 351 | @Override |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 352 | public Map<String, List<ControllerRegistryEntry>> getAllSwitches() { |
| 353 | Map<String, List<ControllerRegistryEntry>> data = |
| 354 | new HashMap<String, List<ControllerRegistryEntry>>(); |
| 355 | |
| 356 | for (Map.Entry<String, PathChildrenCache> entry : switchPathCaches.entrySet()){ |
| 357 | List<ControllerRegistryEntry> contendingControllers = |
| 358 | new ArrayList<ControllerRegistryEntry>(); |
| 359 | |
| 360 | if (entry.getValue().getCurrentData().size() < 1){ |
| 361 | log.info("Switch entry with no leader elections: {}", entry.getKey()); |
| 362 | continue; |
| 363 | } |
| 364 | |
| 365 | for (ChildData d : entry.getValue().getCurrentData()) { |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 366 | |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 367 | String controllerId = new String(d.getData(), Charsets.UTF_8); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 368 | |
| 369 | String[] splitted = d.getPath().split("-"); |
| 370 | int sequenceNumber = Integer.parseInt(splitted[splitted.length - 1]); |
| 371 | |
| 372 | contendingControllers.add(new ControllerRegistryEntry(controllerId, sequenceNumber)); |
| 373 | } |
| 374 | |
| 375 | Collections.sort(contendingControllers); |
| 376 | data.put(entry.getKey(), contendingControllers); |
| 377 | } |
| 378 | return data; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 381 | /* |
| 382 | * IFloodlightModule |
| 383 | */ |
| 384 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 385 | @Override |
| 386 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 387 | Collection<Class<? extends IFloodlightService>> l = |
| 388 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 389 | l.add(IControllerRegistryService.class); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 390 | return l; |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 395 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = |
| 396 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 397 | m.put(IControllerRegistryService.class, this); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 398 | return m; |
| 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 403 | Collection<Class<? extends IFloodlightService>> l = |
| 404 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 405 | l.add(IRestApiService.class); |
| 406 | return l; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 409 | //TODO currently blocks startup when it can't get a Zookeeper connection. |
| 410 | //Do we support starting up with no Zookeeper connection? |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 411 | @Override |
| 412 | public void init (FloodlightModuleContext context) throws FloodlightModuleException { |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 413 | log.info("Initialising the Zookeeper Registry - Zookeeper connection required"); |
| 414 | |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 415 | //Read the Zookeeper connection string from the config |
| 416 | Map<String, String> configParams = context.getConfigParams(this); |
| 417 | String connectionString = configParams.get("connectionString"); |
| 418 | if (connectionString != null){ |
| 419 | this.connectionString = connectionString; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 420 | } |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 421 | log.info("Setting Zookeeper connection string to {}", this.connectionString); |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 422 | |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 423 | restApi = context.getServiceImpl(IRestApiService.class); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 424 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 425 | switches = new ConcurrentHashMap<String, SwitchLeadershipData>(); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 426 | switchPathCaches = new HashMap<String, PathChildrenCache>(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 427 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 428 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 429 | client = CuratorFrameworkFactory.newClient(this.connectionString, |
Jonathan Hart | cc957a0 | 2013-02-26 10:39:04 -0800 | [diff] [blame] | 430 | sessionTimeout, connectionTimeout, retryPolicy); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 431 | |
| 432 | client.start(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 433 | client = client.usingNamespace(namespace); |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 434 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 435 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 436 | controllerCache = new PathChildrenCache(client, controllerPath, true); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 437 | switchCache = new PathChildrenCache(client, switchLatchesPath, true); |
| 438 | switchCache.getListenable().addListener(switchPathCacheListener); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 439 | |
| 440 | try { |
| 441 | controllerCache.start(StartMode.BUILD_INITIAL_CACHE); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 442 | |
| 443 | //Don't prime the cache, we want a notification for each child node in the path |
| 444 | switchCache.start(StartMode.NORMAL); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 445 | } catch (Exception e) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 446 | throw new FloodlightModuleException("Error initialising ZookeeperRegistry: " |
| 447 | + e.getMessage()); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 448 | } |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | @Override |
| 452 | public void startUp (FloodlightModuleContext context) { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 453 | restApi.addRestletRoutable(new RegistryWebRoutable()); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 454 | } |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 455 | } |