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