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; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 6 | import java.util.Collections; |
Jonathan Hart | 599c6b3 | 2013-03-24 22:42:02 -0700 | [diff] [blame] | 7 | import java.util.Comparator; |
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; |
Naoki Shiota | b32edf5 | 2013-12-12 14:09:36 -0800 | [diff] [blame] | 18 | import net.onrc.onos.registry.controller.web.RegistryWebRoutable; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 19 | |
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 | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 28 | import com.netflix.curator.framework.recipes.atomic.AtomicValue; |
| 29 | import com.netflix.curator.framework.recipes.atomic.DistributedAtomicLong; |
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; |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 36 | import com.netflix.curator.framework.recipes.leader.LeaderLatchEvent; |
| 37 | import com.netflix.curator.framework.recipes.leader.LeaderLatchListener; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 38 | import com.netflix.curator.retry.ExponentialBackoffRetry; |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 39 | import com.netflix.curator.retry.RetryOneTime; |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 40 | import com.netflix.curator.x.discovery.ServiceCache; |
| 41 | import com.netflix.curator.x.discovery.ServiceDiscovery; |
| 42 | import com.netflix.curator.x.discovery.ServiceDiscoveryBuilder; |
| 43 | import com.netflix.curator.x.discovery.ServiceInstance; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 44 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 45 | /** |
| 46 | * A registry service that uses Zookeeper. All data is stored in Zookeeper, |
| 47 | * so this can be used as a global registry in a multi-node ONOS cluster. |
| 48 | * @author jono |
| 49 | * |
| 50 | */ |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 51 | public class ZookeeperRegistry implements IFloodlightModule, IControllerRegistryService { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 52 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 53 | protected final static Logger log = LoggerFactory.getLogger(ZookeeperRegistry.class); |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 54 | protected String controllerId = null; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 55 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 56 | protected IRestApiService restApi; |
| 57 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 58 | //This is the default, it's overwritten by the connectionString configuration parameter |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 59 | protected String connectionString = "localhost:2181"; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 60 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 61 | private final String namespace = "onos"; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 62 | private final String switchLatchesPath = "/switches"; |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 63 | |
| 64 | private final String SERVICES_PATH = "/"; //i.e. the root of our namespace |
| 65 | private final String CONTROLLER_SERVICE_NAME = "controllers"; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 66 | |
| 67 | protected CuratorFramework client; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 68 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 69 | protected PathChildrenCache switchCache; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 70 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 71 | protected ConcurrentHashMap<String, SwitchLeadershipData> switches; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 72 | protected Map<String, PathChildrenCache> switchPathCaches; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 73 | |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 74 | private final String ID_COUNTER_PATH = "/flowidcounter"; |
| 75 | private final Long ID_BLOCK_SIZE = 0x100000000L; |
| 76 | protected DistributedAtomicLong distributedIdCounter; |
| 77 | |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 78 | //Zookeeper performance-related configuration |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame] | 79 | protected static final int sessionTimeout = 5000; |
| 80 | protected static final int connectionTimeout = 7000; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 81 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 82 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 83 | protected class SwitchLeaderListener implements LeaderLatchListener{ |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 84 | String dpid; |
| 85 | LeaderLatch latch; |
| 86 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 87 | public SwitchLeaderListener(String dpid, LeaderLatch latch){ |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 88 | this.dpid = dpid; |
| 89 | this.latch = latch; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void leaderLatchEvent(CuratorFramework arg0, |
| 94 | LeaderLatchEvent arg1) { |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 95 | log.debug("Leadership changed for {}, now {}", |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 96 | dpid, latch.hasLeadership()); |
| 97 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 98 | //Check that the leadership request is still active - the client |
| 99 | //may have since released the request or even begun another request |
| 100 | //(this is why we use == to check the object instance is the same) |
| 101 | SwitchLeadershipData swData = switches.get(dpid); |
| 102 | if (swData != null && swData.getLatch() == latch){ |
| 103 | swData.getCallback().controlChanged( |
| 104 | HexString.toLong(dpid), latch.hasLeadership()); |
| 105 | } |
| 106 | else { |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 107 | log.debug("Latch for {} has changed: old latch {} - new latch {}", |
| 108 | new Object[]{dpid, latch, swData.getLatch()}); |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 109 | } |
Jonathan Hart | 0de0949 | 2013-03-13 14:37:21 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 113 | protected class SwitchPathCacheListener implements PathChildrenCacheListener { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 114 | @Override |
| 115 | public void childEvent(CuratorFramework client, |
| 116 | PathChildrenCacheEvent event) throws Exception { |
Jonathan Hart | cbb4b95 | 2013-03-18 16:15:18 -0700 | [diff] [blame] | 117 | //log.debug("Root switch path cache got {} event", event.getType()); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 118 | |
| 119 | String strSwitch = null; |
| 120 | if (event.getData() != null){ |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 121 | String[] splitted = event.getData().getPath().split("/"); |
| 122 | strSwitch = splitted[splitted.length - 1]; |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 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 |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 129 | synchronized (switchPathCaches){ |
| 130 | if (switchPathCaches.get(strSwitch) == null){ |
| 131 | PathChildrenCache pc = new PathChildrenCache(client, |
| 132 | event.getData().getPath(), true); |
| 133 | pc.start(StartMode.NORMAL); |
| 134 | switchPathCaches.put(strSwitch, pc); |
| 135 | } |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 136 | } |
| 137 | break; |
| 138 | case CHILD_REMOVED: |
| 139 | //Remove our PathChildrenCache for this child |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 140 | PathChildrenCache pc = null; |
| 141 | synchronized(switchPathCaches){ |
| 142 | pc = switchPathCaches.remove(strSwitch); |
| 143 | } |
| 144 | if (pc != null){ |
| 145 | pc.close(); |
| 146 | } |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 147 | break; |
| 148 | default: |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 149 | //All other events are connection status events. We don't need to |
| 150 | //do anything as the path cache handles these on its own. |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | |
| 154 | } |
| 155 | }; |
Naoki Shiota | d00accf | 2013-06-25 14:40:37 -0700 | [diff] [blame] | 156 | /** |
| 157 | * Listens for changes to the switch znodes in Zookeeper. This maintains |
| 158 | * the second level of PathChildrenCaches that hold the controllers |
| 159 | * contending for each switch - there's one for each switch. |
| 160 | */ |
| 161 | PathChildrenCacheListener switchPathCacheListener = new SwitchPathCacheListener(); |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 162 | protected ServiceDiscovery<ControllerService> serviceDiscovery; |
| 163 | protected ServiceCache<ControllerService> serviceCache; |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 164 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 165 | |
| 166 | @Override |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 167 | public void requestControl(long dpid, ControlChangeCallback cb) throws RegistryException { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 168 | log.info("Requesting control for {}", HexString.toHexString(dpid)); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 169 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 170 | if (controllerId == null){ |
| 171 | throw new RuntimeException("Must register a controller before calling requestControl"); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | String dpidStr = HexString.toHexString(dpid); |
| 175 | String latchPath = switchLatchesPath + "/" + dpidStr; |
| 176 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 177 | if (switches.get(dpidStr) != null){ |
Jonathan Hart | 3c0eccd | 2013-03-12 22:32:50 -0700 | [diff] [blame] | 178 | log.debug("Already contesting {}, returning", HexString.toHexString(dpid)); |
Pankaj Berde | da7187b | 2013-03-18 15:24:59 -0700 | [diff] [blame] | 179 | throw new RegistryException("Already contesting control for " + dpidStr); |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 182 | LeaderLatch latch = new LeaderLatch(client, latchPath, controllerId); |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 183 | latch.addListener(new SwitchLeaderListener(dpidStr, latch)); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 184 | |
Jonathan Hart | 44e56fc | 2013-03-14 16:53:59 -0700 | [diff] [blame] | 185 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 186 | SwitchLeadershipData swData = new SwitchLeadershipData(latch, cb); |
| 187 | SwitchLeadershipData oldData = switches.putIfAbsent(dpidStr, swData); |
| 188 | |
| 189 | if (oldData != null){ |
| 190 | //There was already data for that key in the map |
| 191 | //i.e. someone else got here first so we can't succeed |
| 192 | log.debug("Already requested control for {}", dpidStr); |
| 193 | throw new RegistryException("Already requested control for " + dpidStr); |
| 194 | } |
| 195 | |
| 196 | //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] | 197 | //we can start the leader election in Zookeeper. However I don't know |
| 198 | //how to handle if the start fails - the latch is already in our |
| 199 | //switches list. |
| 200 | //TODO seems like there's a Curator bug when latch.start is called when |
| 201 | //there's no Zookeeper connection which causes two znodes to be put in |
| 202 | //Zookeeper at the latch path when we reconnect to Zookeeper. |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 203 | try { |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 204 | latch.start(); |
| 205 | } catch (Exception e) { |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 206 | log.warn("Error starting leader latch: {}", e.getMessage()); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 207 | throw new RegistryException("Error starting leader latch for " + dpidStr, e); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | } |
| 211 | |
| 212 | @Override |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 213 | public void releaseControl(long dpid) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 214 | log.info("Releasing control for {}", HexString.toHexString(dpid)); |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 215 | |
Jonathan Hart | c6eee9e | 2013-02-18 14:58:27 -0800 | [diff] [blame] | 216 | String dpidStr = HexString.toHexString(dpid); |
| 217 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 218 | SwitchLeadershipData swData = switches.remove(dpidStr); |
| 219 | |
| 220 | if (swData == null) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 221 | 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] | 222 | return; |
| 223 | } |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 224 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 225 | LeaderLatch latch = swData.getLatch(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 226 | |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 227 | latch.removeAllListeners(); |
| 228 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 229 | try { |
| 230 | latch.close(); |
| 231 | } catch (IOException e) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 232 | //I think it's OK not to do anything here. Either the node got |
| 233 | //deleted correctly, or the connection went down and the node got deleted. |
Umesh Krishnaswamy | 0ef75ee | 2013-03-25 17:50:27 -0700 | [diff] [blame] | 234 | log.debug("releaseControl: caught IOException {}", 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 | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 240 | String dpidStr = HexString.toHexString(dpid); |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 241 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 242 | SwitchLeadershipData swData = switches.get(dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 243 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 244 | if (swData == null) { |
| 245 | log.warn("No leader latch for dpid {}", dpidStr); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 249 | return swData.getLatch().hasLeadership(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | @Override |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 253 | public String getControllerId() { |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 254 | return controllerId; |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 257 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 258 | public Collection<String> getAllControllers() throws RegistryException { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 259 | log.debug("Getting all controllers"); |
Jonathan Hart | 1be4626 | 2013-02-20 16:43:51 -0800 | [diff] [blame] | 260 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 261 | List<String> controllers = new ArrayList<String>(); |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 262 | for (ServiceInstance<ControllerService> instance : serviceCache.getInstances()){ |
| 263 | String id = instance.getPayload().getControllerId(); |
| 264 | if (!controllers.contains(id)){ |
| 265 | controllers.add(id); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 266 | } |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 267 | } |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 268 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 269 | return controllers; |
| 270 | } |
| 271 | |
| 272 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 273 | public void registerController(String id) throws RegistryException { |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 274 | if (controllerId != null) { |
| 275 | throw new RegistryException( |
| 276 | "Controller already registered with id " + controllerId); |
| 277 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 278 | |
| 279 | controllerId = id; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 280 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 281 | try { |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 282 | ServiceInstance<ControllerService> thisInstance = ServiceInstance.<ControllerService>builder() |
| 283 | .name(CONTROLLER_SERVICE_NAME) |
| 284 | .payload(new ControllerService(controllerId)) |
| 285 | //.port((int)(65535 * Math.random())) // in a real application, you'd use a common port |
| 286 | //.uriSpec(uriSpec) |
| 287 | .build(); |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame] | 288 | |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 289 | serviceDiscovery.registerService(thisInstance); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 290 | } catch (Exception e) { |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 291 | // TODO Auto-generated catch block |
| 292 | e.printStackTrace(); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 293 | } |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 294 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Override |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 298 | public String getControllerForSwitch(long dpid) throws RegistryException { |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 299 | String dpidStr = HexString.toHexString(dpid); |
Pankaj Berde | 017960a | 2013-03-14 20:32:26 -0700 | [diff] [blame] | 300 | |
Jonathan Hart | 599c6b3 | 2013-03-24 22:42:02 -0700 | [diff] [blame] | 301 | PathChildrenCache switchCache = switchPathCaches.get(dpidStr); |
| 302 | |
| 303 | if (switchCache == null){ |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 304 | log.warn("Tried to get controller for non-existent switch"); |
| 305 | return null; |
| 306 | } |
| 307 | |
Jonathan Hart | f4e8084 | 2013-03-26 23:55:02 -0700 | [diff] [blame] | 308 | try { |
| 309 | //We've seen issues with these caches get stuck out of date, so we'll have to |
| 310 | //force them to refresh before each read. This slows down the method as it |
| 311 | //blocks on a Zookeeper query, however at the moment only the cleanup thread |
| 312 | //uses this and that isn't particularly time-sensitive. |
| 313 | switchCache.rebuild(); |
| 314 | } catch (Exception e) { |
| 315 | // TODO Auto-generated catch block |
| 316 | e.printStackTrace(); |
| 317 | } |
| 318 | |
Jonathan Hart | 599c6b3 | 2013-03-24 22:42:02 -0700 | [diff] [blame] | 319 | List<ChildData> sortedData = new ArrayList<ChildData>(switchCache.getCurrentData()); |
Jonathan Hart | 0b3eee4 | 2013-03-16 18:20:04 -0700 | [diff] [blame] | 320 | |
Jonathan Hart | 599c6b3 | 2013-03-24 22:42:02 -0700 | [diff] [blame] | 321 | Collections.sort( |
| 322 | sortedData, |
| 323 | new Comparator<ChildData>(){ |
| 324 | private String getSequenceNumber(String path){ |
| 325 | return path.substring(path.lastIndexOf('-') + 1); |
| 326 | } |
| 327 | @Override |
| 328 | public int compare(ChildData lhs, ChildData rhs) { |
| 329 | return getSequenceNumber(lhs.getPath()). |
| 330 | compareTo(getSequenceNumber(rhs.getPath())); |
| 331 | } |
| 332 | } |
| 333 | ); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 334 | |
Jonathan Hart | 56b296e | 2013-03-25 13:30:10 -0700 | [diff] [blame] | 335 | if (sortedData.size() == 0){ |
| 336 | return null; |
| 337 | } |
| 338 | |
Jonathan Hart | 599c6b3 | 2013-03-24 22:42:02 -0700 | [diff] [blame] | 339 | return new String(sortedData.get(0).getData(), Charsets.UTF_8); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public Collection<Long> getSwitchesControlledByController(String controllerId) { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 344 | //TODO remove this if not needed |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 345 | throw new RuntimeException("Not yet implemented"); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 346 | } |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 347 | |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 348 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 349 | //TODO what should happen when there's no ZK connection? Currently we just return |
| 350 | //the cache but this may lead to false impressions - i.e. we don't actually know |
| 351 | //what's in ZK so we shouldn't say we do |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 352 | @Override |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 353 | public Map<String, List<ControllerRegistryEntry>> getAllSwitches() { |
| 354 | Map<String, List<ControllerRegistryEntry>> data = |
| 355 | new HashMap<String, List<ControllerRegistryEntry>>(); |
| 356 | |
| 357 | for (Map.Entry<String, PathChildrenCache> entry : switchPathCaches.entrySet()){ |
| 358 | List<ControllerRegistryEntry> contendingControllers = |
| 359 | new ArrayList<ControllerRegistryEntry>(); |
| 360 | |
| 361 | if (entry.getValue().getCurrentData().size() < 1){ |
Jonathan Hart | cbb4b95 | 2013-03-18 16:15:18 -0700 | [diff] [blame] | 362 | //TODO prevent even having the PathChildrenCache in this case |
| 363 | //log.info("Switch entry with no leader elections: {}", entry.getKey()); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 364 | continue; |
| 365 | } |
| 366 | |
| 367 | for (ChildData d : entry.getValue().getCurrentData()) { |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 368 | |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 369 | String controllerId = new String(d.getData(), Charsets.UTF_8); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 370 | |
| 371 | String[] splitted = d.getPath().split("-"); |
| 372 | int sequenceNumber = Integer.parseInt(splitted[splitted.length - 1]); |
| 373 | |
| 374 | contendingControllers.add(new ControllerRegistryEntry(controllerId, sequenceNumber)); |
| 375 | } |
| 376 | |
| 377 | Collections.sort(contendingControllers); |
| 378 | data.put(entry.getKey(), contendingControllers); |
| 379 | } |
| 380 | return data; |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Naoki Shiota | a3b2dfa | 2013-06-27 13:52:24 -0700 | [diff] [blame] | 383 | /** |
| 384 | * Returns a block of IDs which are unique and unused. |
| 385 | * Range of IDs is fixed size and is assigned incrementally as this method called. |
| 386 | * Since the range of IDs is managed by Zookeeper in distributed way, this method may block when |
| 387 | * requests come up simultaneously. |
| 388 | */ |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 389 | public IdBlock allocateUniqueIdBlock(){ |
| 390 | try { |
| 391 | AtomicValue<Long> result = null; |
| 392 | do { |
| 393 | result = distributedIdCounter.add(ID_BLOCK_SIZE); |
| 394 | } while (result == null || !result.succeeded()); |
| 395 | |
| 396 | return new IdBlock(result.preValue(), result.postValue() - 1, ID_BLOCK_SIZE); |
| 397 | } catch (Exception e) { |
| 398 | log.error("Error allocating ID block"); |
| 399 | } |
| 400 | |
| 401 | return null; |
| 402 | } |
| 403 | |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 404 | /* |
| 405 | * IFloodlightModule |
| 406 | */ |
| 407 | |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 408 | @Override |
| 409 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 410 | Collection<Class<? extends IFloodlightService>> l = |
| 411 | new ArrayList<Class<? extends IFloodlightService>>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 412 | l.add(IControllerRegistryService.class); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 413 | return l; |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 418 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = |
| 419 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
Jonathan Hart | d82f20d | 2013-02-21 18:04:24 -0800 | [diff] [blame] | 420 | m.put(IControllerRegistryService.class, this); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 421 | return m; |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 426 | Collection<Class<? extends IFloodlightService>> l = |
| 427 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 428 | l.add(IRestApiService.class); |
| 429 | return l; |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 432 | //TODO currently blocks startup when it can't get a Zookeeper connection. |
| 433 | //Do we support starting up with no Zookeeper connection? |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 434 | @Override |
| 435 | public void init (FloodlightModuleContext context) throws FloodlightModuleException { |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 436 | log.info("Initialising the Zookeeper Registry - Zookeeper connection required"); |
| 437 | |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 438 | //Read the Zookeeper connection string from the config |
| 439 | Map<String, String> configParams = context.getConfigParams(this); |
| 440 | String connectionString = configParams.get("connectionString"); |
| 441 | if (connectionString != null){ |
| 442 | this.connectionString = connectionString; |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 443 | } |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 444 | log.info("Setting Zookeeper connection string to {}", this.connectionString); |
Jonathan Hart | 57080fb | 2013-02-21 10:55:46 -0800 | [diff] [blame] | 445 | |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 446 | restApi = context.getServiceImpl(IRestApiService.class); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 447 | |
Jonathan Hart | 8918737 | 2013-03-14 16:41:09 -0700 | [diff] [blame] | 448 | switches = new ConcurrentHashMap<String, SwitchLeadershipData>(); |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 449 | //switchPathCaches = new HashMap<String, PathChildrenCache>(); |
| 450 | switchPathCaches = new ConcurrentHashMap<String, PathChildrenCache>(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 451 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 452 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 453 | client = CuratorFrameworkFactory.newClient(this.connectionString, |
Jonathan Hart | cc957a0 | 2013-02-26 10:39:04 -0800 | [diff] [blame] | 454 | sessionTimeout, connectionTimeout, retryPolicy); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 455 | |
| 456 | client.start(); |
Jonathan Hart | bd181b6 | 2013-02-17 16:05:38 -0800 | [diff] [blame] | 457 | client = client.usingNamespace(namespace); |
Jonathan Hart | 97801ac | 2013-02-26 14:29:16 -0800 | [diff] [blame] | 458 | |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 459 | distributedIdCounter = new DistributedAtomicLong( |
| 460 | client, |
| 461 | ID_COUNTER_PATH, |
| 462 | new RetryOneTime(100)); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 463 | |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 464 | switchCache = new PathChildrenCache(client, switchLatchesPath, true); |
| 465 | switchCache.getListenable().addListener(switchPathCacheListener); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 466 | |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 467 | //Build the service discovery object |
| 468 | serviceDiscovery = ServiceDiscoveryBuilder.builder(ControllerService.class) |
| 469 | .client(client).basePath(SERVICES_PATH).build(); |
| 470 | |
| 471 | //We read the list of services very frequently (GUI periodically queries them) |
| 472 | //so we'll cache them to cut down on Zookeeper queries. |
| 473 | serviceCache = serviceDiscovery.serviceCacheBuilder() |
| 474 | .name(CONTROLLER_SERVICE_NAME).build(); |
| 475 | |
| 476 | |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 477 | try { |
Jonathan Hart | 71c0ffc | 2013-03-24 15:58:42 -0700 | [diff] [blame] | 478 | serviceDiscovery.start(); |
| 479 | serviceCache.start(); |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 480 | |
| 481 | //Don't prime the cache, we want a notification for each child node in the path |
| 482 | switchCache.start(StartMode.NORMAL); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 483 | } catch (Exception e) { |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 484 | throw new FloodlightModuleException("Error initialising ZookeeperRegistry: " |
| 485 | + e.getMessage()); |
Jonathan Hart | edd6a44 | 2013-02-20 15:22:06 -0800 | [diff] [blame] | 486 | } |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | @Override |
| 490 | public void startUp (FloodlightModuleContext context) { |
Jonathan Hart | 3d7730a | 2013-02-22 11:51:17 -0800 | [diff] [blame] | 491 | restApi.addRestletRoutable(new RegistryWebRoutable()); |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 492 | } |
Umesh Krishnaswamy | b56bb29 | 2013-02-12 20:28:27 -0800 | [diff] [blame] | 493 | } |