Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 1 | package net.onrc.onos.registry.controller; |
| 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; |
| 8 | |
| 9 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 10 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 11 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 12 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 13 | import net.floodlightcontroller.restserver.IRestApiService; |
| 14 | |
| 15 | import org.openflow.util.HexString; |
| 16 | import org.slf4j.Logger; |
| 17 | import org.slf4j.LoggerFactory; |
| 18 | |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 19 | /** |
| 20 | * Implementation of a registry that doesn't rely on any external registry |
| 21 | * service. This is designed to be used only in single-node setups (e.g. for |
| 22 | * development). All registry data is stored in local memory. |
| 23 | * @author jono |
| 24 | * |
| 25 | */ |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 26 | public class StandaloneRegistry implements IFloodlightModule, |
| 27 | IControllerRegistryService { |
| 28 | protected static Logger log = LoggerFactory.getLogger(StandaloneRegistry.class); |
| 29 | |
| 30 | protected IRestApiService restApi; |
| 31 | |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 32 | protected String controllerId = null; |
| 33 | protected Map<String, ControlChangeCallback> switchCallbacks; |
| 34 | |
| 35 | |
| 36 | @Override |
| 37 | public void requestControl(long dpid, ControlChangeCallback cb) |
| 38 | throws RegistryException { |
| 39 | if (controllerId == null) { |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 40 | throw new RuntimeException( |
| 41 | "Must register a controller before calling requestControl"); |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | switchCallbacks.put(HexString.toHexString(dpid), cb); |
| 45 | |
| 46 | log.debug("Control granted for {}", HexString.toHexString(dpid)); |
| 47 | |
| 48 | //Immediately grant request for control |
| 49 | if (cb != null) { |
| 50 | cb.controlChanged(dpid, true); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void releaseControl(long dpid) { |
| 56 | ControlChangeCallback cb = switchCallbacks.remove(HexString.toHexString(dpid)); |
| 57 | |
| 58 | log.debug("Control released for {}", HexString.toHexString(dpid)); |
| 59 | |
| 60 | if (cb != null){ |
| 61 | cb.controlChanged(dpid, false); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public boolean hasControl(long dpid) { |
| 67 | return switchCallbacks.containsKey(HexString.toHexString(dpid)); |
| 68 | } |
| 69 | |
| 70 | @Override |
Jonathan Hart | 7bf6217 | 2013-02-28 13:17:18 -0800 | [diff] [blame] | 71 | public String getControllerId() { |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 72 | return controllerId; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void registerController(String controllerId) |
| 77 | throws RegistryException { |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 78 | if (this.controllerId != null) { |
| 79 | throw new RegistryException( |
| 80 | "Controller already registered with id " + this.controllerId); |
| 81 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 82 | this.controllerId = controllerId; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Collection<String> getAllControllers() throws RegistryException { |
| 87 | List<String> l = new ArrayList<String>(); |
| 88 | l.add(controllerId); |
| 89 | return l; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public String getControllerForSwitch(long dpid) throws RegistryException { |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 94 | return (switchCallbacks.get(HexString.toHexString(dpid)) != null)? controllerId: null; |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Map<String, List<ControllerRegistryEntry>> getAllSwitches() { |
| 99 | Map<String, List<ControllerRegistryEntry>> switches = |
| 100 | new HashMap<String, List<ControllerRegistryEntry>>(); |
| 101 | |
| 102 | for (String strSwitch : switchCallbacks.keySet()){ |
| 103 | log.debug("Swtich _{}", strSwitch); |
| 104 | List<ControllerRegistryEntry> list = new ArrayList<ControllerRegistryEntry>(); |
| 105 | list.add(new ControllerRegistryEntry(controllerId, 0)); |
| 106 | |
| 107 | switches.put(strSwitch, list); |
| 108 | } |
| 109 | |
| 110 | return switches; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Collection<Long> getSwitchesControlledByController( |
| 115 | String controllerId) { |
| 116 | throw new RuntimeException("Not yet implemented"); |
| 117 | } |
Jonathan Hart | 1530ccc | 2013-04-03 19:36:02 -0700 | [diff] [blame] | 118 | |
| 119 | @Override |
| 120 | public IdBlock allocateUniqueIdBlock(){ |
| 121 | //XXX Not exactly unique... |
| 122 | return new IdBlock(0L, 0x10000000L, 0x10000000L); |
| 123 | } |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 124 | |
| 125 | @Override |
| 126 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 127 | Collection<Class<? extends IFloodlightService>> l = |
| 128 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 129 | l.add(IControllerRegistryService.class); |
| 130 | return l; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 135 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = |
| 136 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
| 137 | m.put(IControllerRegistryService.class, this); |
| 138 | return m; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 143 | Collection<Class<? extends IFloodlightService>> l = |
| 144 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 145 | l.add(IRestApiService.class); |
| 146 | return l; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void init(FloodlightModuleContext context) |
| 151 | throws FloodlightModuleException { |
| 152 | restApi = context.getServiceImpl(IRestApiService.class); |
| 153 | |
| 154 | switchCallbacks = new HashMap<String, ControlChangeCallback>(); |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void startUp(FloodlightModuleContext context) { |
| 159 | restApi.addRestletRoutable(new RegistryWebRoutable()); |
| 160 | } |
| 161 | |
| 162 | } |