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