Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.registry; |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
Jonathan Hart | 1dbcce6 | 2014-06-04 15:21:45 -0700 | [diff] [blame] | 5 | import java.util.Collections; |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 6 | import java.util.HashMap; |
| 7 | import java.util.List; |
| 8 | import java.util.Map; |
Pavlin Radoslavov | 52163ed | 2014-03-19 11:39:34 -0700 | [diff] [blame] | 9 | import java.util.concurrent.atomic.AtomicLong; |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 10 | |
Pavlin Radoslavov | c35229e | 2014-02-06 16:19:37 -0800 | [diff] [blame] | 11 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 12 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 13 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 14 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 15 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 16 | import net.floodlightcontroller.restserver.IRestApiService; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.registry.web.RegistryWebRoutable; |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 18 | |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 19 | import org.apache.commons.lang.NotImplementedException; |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 20 | import org.openflow.util.HexString; |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 24 | /** |
| 25 | * Implementation of a registry that doesn't rely on any external registry |
| 26 | * service. This is designed to be used only in single-node setups (e.g. for |
| 27 | * development). All registry data is stored in local memory. |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 28 | */ |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 29 | public class StandaloneRegistry implements IFloodlightModule, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | IControllerRegistryService { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 31 | private static final Logger log = LoggerFactory.getLogger(StandaloneRegistry.class); |
Pavlin Radoslavov | 52163ed | 2014-03-19 11:39:34 -0700 | [diff] [blame] | 32 | |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 33 | private IRestApiService restApi; |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 34 | |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 35 | private String registeredControllerId; |
| 36 | private Map<String, ControlChangeCallback> switchCallbacks; |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 37 | |
Jonathan Hart | 1dbcce6 | 2014-06-04 15:21:45 -0700 | [diff] [blame] | 38 | private long blockTop; |
| 39 | private static final long BLOCK_SIZE = 0x1000000L; |
| 40 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 41 | // |
| 42 | // Unique ID generation state |
| 43 | // |
| 44 | private static AtomicLong nextUniqueId = new AtomicLong(0); |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 45 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | @Override |
| 47 | public void requestControl(long dpid, ControlChangeCallback cb) |
| 48 | throws RegistryException { |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 49 | if (registeredControllerId == null) { |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 50 | throw new IllegalStateException( |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 51 | "Must register a controller before calling requestControl"); |
| 52 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 53 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 54 | switchCallbacks.put(HexString.toHexString(dpid), cb); |
Pavlin Radoslavov | f1377ce | 2014-02-05 17:37:24 -0800 | [diff] [blame] | 55 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | log.debug("Control granted for {}", HexString.toHexString(dpid)); |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 57 | |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 58 | // Immediately grant request for control |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 59 | if (cb != null) { |
| 60 | cb.controlChanged(dpid, true); |
| 61 | } |
| 62 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 63 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | @Override |
| 65 | public void releaseControl(long dpid) { |
| 66 | ControlChangeCallback cb = switchCallbacks.remove(HexString.toHexString(dpid)); |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 67 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 68 | log.debug("Control released for {}", HexString.toHexString(dpid)); |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 69 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 70 | if (cb != null) { |
| 71 | cb.controlChanged(dpid, false); |
| 72 | } |
| 73 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 74 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | @Override |
| 76 | public boolean hasControl(long dpid) { |
| 77 | return switchCallbacks.containsKey(HexString.toHexString(dpid)); |
| 78 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 79 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 80 | @Override |
| 81 | public boolean isClusterLeader() { |
| 82 | return true; |
| 83 | } |
Pavlin Radoslavov | 52163ed | 2014-03-19 11:39:34 -0700 | [diff] [blame] | 84 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | @Override |
| 86 | public String getControllerId() { |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 87 | return registeredControllerId; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 89 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 90 | @Override |
| 91 | public void registerController(String controllerId) |
| 92 | throws RegistryException { |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 93 | if (registeredControllerId != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | throw new RegistryException( |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 95 | "Controller already registered with id " + registeredControllerId); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 96 | } |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 97 | registeredControllerId = controllerId; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 99 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | @Override |
| 101 | public Collection<String> getAllControllers() throws RegistryException { |
Jonathan Hart | 1dbcce6 | 2014-06-04 15:21:45 -0700 | [diff] [blame] | 102 | //List<String> l = new ArrayList<String>(); |
| 103 | //l.add(registeredControllerId); |
| 104 | //return l; |
| 105 | return Collections.singletonList(registeredControllerId); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public String getControllerForSwitch(long dpid) throws RegistryException { |
Jonathan Hart | 1dbcce6 | 2014-06-04 15:21:45 -0700 | [diff] [blame] | 110 | return (switchCallbacks.get(HexString.toHexString(dpid)) == null) |
| 111 | ? null : registeredControllerId; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public Map<String, List<ControllerRegistryEntry>> getAllSwitches() { |
| 116 | Map<String, List<ControllerRegistryEntry>> switches = |
| 117 | new HashMap<String, List<ControllerRegistryEntry>>(); |
| 118 | |
| 119 | for (String strSwitch : switchCallbacks.keySet()) { |
| 120 | log.debug("Switch _{}", strSwitch); |
Jonathan Hart | 1dbcce6 | 2014-06-04 15:21:45 -0700 | [diff] [blame] | 121 | List<ControllerRegistryEntry> list = |
| 122 | new ArrayList<ControllerRegistryEntry>(); |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 123 | list.add(new ControllerRegistryEntry(registeredControllerId, 0)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 124 | |
| 125 | switches.put(strSwitch, list); |
| 126 | } |
| 127 | |
| 128 | return switches; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public Collection<Long> getSwitchesControlledByController( |
| 133 | String controllerId) { |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 134 | throw new NotImplementedException("Not yet implemented"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | /** |
| 138 | * Returns a block of IDs which are unique and unused. |
Jonathan Hart | 1dbcce6 | 2014-06-04 15:21:45 -0700 | [diff] [blame] | 139 | * Range of IDs is fixed size and is assigned incrementally as this method |
| 140 | * called. |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 141 | * |
| 142 | * @return an IdBlock containing a set of unique IDs |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 143 | */ |
| 144 | @Override |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 145 | public IdBlock allocateUniqueIdBlock() { |
| 146 | synchronized (this) { |
| 147 | long blockHead = blockTop; |
| 148 | long blockTail = blockTop + BLOCK_SIZE; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 149 | |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 150 | IdBlock block = new IdBlock(blockHead, blockTail - 1, BLOCK_SIZE); |
| 151 | blockTop = blockTail; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 152 | |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 153 | return block; |
| 154 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get a globally unique ID. |
| 159 | * |
| 160 | * @return a globally unique ID. |
| 161 | */ |
| 162 | @Override |
| 163 | public long getNextUniqueId() { |
| 164 | return nextUniqueId.incrementAndGet(); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 169 | Collection<Class<? extends IFloodlightService>> l = |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 170 | new ArrayList<Class<? extends IFloodlightService>>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 171 | l.add(IControllerRegistryService.class); |
| 172 | return l; |
| 173 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 174 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 175 | @Override |
| 176 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 177 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = |
| 178 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
| 179 | m.put(IControllerRegistryService.class, this); |
| 180 | return m; |
| 181 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 182 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 185 | Collection<Class<? extends IFloodlightService>> l = |
| 186 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 187 | l.add(IFloodlightProviderService.class); |
| 188 | l.add(IRestApiService.class); |
| 189 | return l; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void init(FloodlightModuleContext context) |
| 194 | throws FloodlightModuleException { |
| 195 | restApi = context.getServiceImpl(IRestApiService.class); |
| 196 | |
| 197 | switchCallbacks = new HashMap<String, ControlChangeCallback>(); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void startUp(FloodlightModuleContext context) { |
| 202 | restApi.addRestletRoutable(new RegistryWebRoutable()); |
| 203 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 204 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 205 | @Override |
| 206 | public IdBlock allocateUniqueIdBlock(long range) { |
Jonathan Hart | 12a26aa | 2014-06-04 14:33:09 -0700 | [diff] [blame] | 207 | throw new UnsupportedOperationException("Not supported yet"); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 210 | } |