Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.onoslistener; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Map; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 6 | import java.util.concurrent.ScheduledExecutorService; |
| 7 | import java.util.concurrent.TimeUnit; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 8 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 9 | import org.openflow.util.HexString; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 10 | import org.slf4j.Logger; |
| 11 | import org.slf4j.LoggerFactory; |
| 12 | |
| 13 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 14 | import net.floodlightcontroller.core.INetMapStorage.DM_OPERATION; |
| 15 | import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 16 | import net.floodlightcontroller.core.ISwitchStorage.SwitchState; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 17 | import net.floodlightcontroller.core.IOFSwitch; |
| 18 | import net.floodlightcontroller.core.IOFSwitchListener; |
| 19 | import net.floodlightcontroller.core.ISwitchStorage; |
| 20 | import net.floodlightcontroller.core.internal.SwitchStorageImpl; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 21 | import net.floodlightcontroller.core.internal.TopoSwitchServiceImpl; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 22 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 23 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 24 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 25 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 26 | import net.floodlightcontroller.core.util.SingletonTask; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 27 | import net.floodlightcontroller.devicemanager.IDevice; |
| 28 | import net.floodlightcontroller.devicemanager.IDeviceListener; |
| 29 | import net.floodlightcontroller.devicemanager.IDeviceService; |
| 30 | import net.floodlightcontroller.devicemanager.IDeviceStorage; |
| 31 | import net.floodlightcontroller.devicemanager.internal.DeviceStorageImpl; |
| 32 | import net.floodlightcontroller.linkdiscovery.ILinkDiscoveryListener; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 33 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
| 34 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
| 35 | import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback; |
| 36 | import net.onrc.onos.registry.controller.RegistryException; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 37 | |
| 38 | public class OnosPublisher implements IDeviceListener, IOFSwitchListener, |
| 39 | ILinkDiscoveryListener, IFloodlightModule { |
| 40 | |
| 41 | protected IDeviceStorage devStore; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 42 | protected ISwitchStorage swStore; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 43 | protected static Logger log; |
| 44 | protected IDeviceService deviceService; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 45 | protected IControllerRegistryService registryService; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 46 | |
| 47 | protected static final String DBConfigFile = "dbconf"; |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 48 | protected static final String CleanupEnabled = "EnableCleanup"; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 49 | protected IThreadPoolService threadPool; |
| 50 | |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 51 | protected final int CLEANUP_TASK_INTERVAL = 10; // 10 sec |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 52 | protected SingletonTask cleanupTask; |
| 53 | |
| 54 | /** |
| 55 | * Cleanup and synch switch state from registry |
| 56 | */ |
| 57 | protected class SwitchCleanup implements ControlChangeCallback, Runnable { |
| 58 | @Override |
| 59 | public void run() { |
| 60 | try { |
| 61 | log.debug("Running cleanup thread"); |
| 62 | switchCleanup(); |
| 63 | } |
| 64 | catch (Exception e) { |
| 65 | log.error("Error in cleanup thread", e); |
| 66 | } finally { |
| 67 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 68 | TimeUnit.SECONDS); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void controlChanged(long dpid, boolean hasControl) { |
| 74 | // TODO Auto-generated method stub |
| 75 | |
| 76 | if (hasControl) { |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 77 | log.debug("got control to set inactive sw {}", HexString.toHexString(dpid)); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 78 | swStore.update(HexString.toHexString(dpid),SwitchState.INACTIVE, DM_OPERATION.UPDATE); |
| 79 | registryService.releaseControl(dpid); |
| 80 | } |
| 81 | } |
| 82 | } |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 83 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 84 | protected void switchCleanup() { |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 85 | TopoSwitchServiceImpl impl = new TopoSwitchServiceImpl(); |
| 86 | Iterable<ISwitchObject> switches = impl.getActiveSwitches(); |
Jonathan Hart | f02a093 | 2013-03-18 18:30:48 -0700 | [diff] [blame] | 87 | |
| 88 | log.debug("Checking for inactive switches"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 89 | // For each switch check if a controller exists in controller registry |
| 90 | for (ISwitchObject sw: switches) { |
Jonathan Hart | f02a093 | 2013-03-18 18:30:48 -0700 | [diff] [blame] | 91 | //log.debug("checking if switch is inactive: {}", sw.getDPID()); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 92 | try { |
| 93 | long dpid = HexString.toLong(sw.getDPID()); |
| 94 | String controller = registryService.getControllerForSwitch(dpid); |
| 95 | if (controller == null) { |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 96 | log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid)); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 97 | registryService.requestControl(dpid, new SwitchCleanup()); |
| 98 | } else { |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 99 | log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 100 | } |
| 101 | } catch (NumberFormatException e) { |
| 102 | // TODO Auto-generated catch block |
| 103 | e.printStackTrace(); |
| 104 | } catch (RegistryException e) { |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 105 | log.debug("Caught RegistryException trying to requestControl in cleanup thread"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 106 | e.printStackTrace(); |
| 107 | } |
| 108 | } |
| 109 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 110 | |
| 111 | @Override |
| 112 | public void linkDiscoveryUpdate(LDUpdate update) { |
| 113 | // TODO Auto-generated method stub |
| 114 | |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void addedSwitch(IOFSwitch sw) { |
| 119 | // TODO Auto-generated method stub |
| 120 | |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void removedSwitch(IOFSwitch sw) { |
| 125 | // TODO Auto-generated method stub |
| 126 | |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void switchPortChanged(Long switchId) { |
| 131 | // TODO Auto-generated method stub |
| 132 | |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public String getName() { |
| 137 | return "OnosPublisher"; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void deviceAdded(IDevice device) { |
| 142 | // TODO Auto-generated method stub |
| 143 | log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString()); |
| 144 | devStore.addDevice(device); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void deviceRemoved(IDevice device) { |
| 149 | // TODO Auto-generated method stub |
| 150 | |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void deviceMoved(IDevice device) { |
| 155 | // TODO Auto-generated method stub |
| 156 | devStore.changeDeviceAttachments(device); |
| 157 | |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void deviceIPV4AddrChanged(IDevice device) { |
| 162 | // TODO Auto-generated method stub |
| 163 | devStore.changeDeviceIPv4Address(device); |
| 164 | |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void deviceVlanChanged(IDevice device) { |
| 169 | // TODO Auto-generated method stub |
| 170 | } |
| 171 | |
| 172 | |
| 173 | @Override |
| 174 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 175 | // TODO Auto-generated method stub |
| 176 | return null; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
| 181 | // TODO Auto-generated method stub |
| 182 | return null; |
| 183 | } |
| 184 | |
| 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(IDeviceService.class); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 191 | l.add(IThreadPoolService.class); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 192 | return l; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void init(FloodlightModuleContext context) |
| 197 | throws FloodlightModuleException { |
| 198 | // TODO Auto-generated method stub |
| 199 | Map<String, String> configMap = context.getConfigParams(this); |
| 200 | String conf = configMap.get(DBConfigFile); |
| 201 | |
| 202 | log = LoggerFactory.getLogger(OnosPublisher.class); |
| 203 | deviceService = context.getServiceImpl(IDeviceService.class); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 204 | threadPool = context.getServiceImpl(IThreadPoolService.class); |
| 205 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 206 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 207 | devStore = new DeviceStorageImpl(); |
| 208 | devStore.init(conf); |
| 209 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 210 | swStore = new SwitchStorageImpl(); |
| 211 | swStore.init(conf); |
| 212 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 213 | log.debug("Initializing OnosPublisher module with {}", conf); |
| 214 | |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void startUp(FloodlightModuleContext context) { |
| 219 | // TODO Auto-generated method stub |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 220 | Map<String, String> configMap = context.getConfigParams(this); |
| 221 | String cleanupNeeded = configMap.get(CleanupEnabled); |
| 222 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 223 | deviceService.addListener(this); |
| 224 | // Setup the Cleanup task. |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 225 | if (cleanupNeeded == null || !cleanupNeeded.equals("False")) { |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 226 | ScheduledExecutorService ses = threadPool.getScheduledExecutor(); |
| 227 | cleanupTask = new SingletonTask(ses, new SwitchCleanup()); |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 228 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS); |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 229 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | } |