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