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