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