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