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 | } |
| 118 | |
| 119 | @Override |
| 120 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 121 | Collection<Class<? extends IFloodlightService>> l = |
| 122 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 123 | l.add(IControllerRegistryService.class); |
| 124 | return l; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 129 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = |
| 130 | new HashMap<Class<? extends IFloodlightService>, IFloodlightService>(); |
| 131 | m.put(IControllerRegistryService.class, this); |
| 132 | return m; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 137 | Collection<Class<? extends IFloodlightService>> l = |
| 138 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 139 | l.add(IRestApiService.class); |
| 140 | return l; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void init(FloodlightModuleContext context) |
| 145 | throws FloodlightModuleException { |
| 146 | restApi = context.getServiceImpl(IRestApiService.class); |
| 147 | |
| 148 | switchCallbacks = new HashMap<String, ControlChangeCallback>(); |
Jonathan Hart | bd76697 | 2013-02-22 15:13:03 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void startUp(FloodlightModuleContext context) { |
| 153 | restApi.addRestletRoutable(new RegistryWebRoutable()); |
| 154 | } |
| 155 | |
| 156 | } |