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