HIGUCHI Yuta | 155cfc5 | 2013-06-14 15:54:16 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.floodlightlistener; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
Pavlin Radoslavov | 2806940 | 2013-10-18 18:43:11 -0700 | [diff] [blame] | 5 | import java.util.HashMap; |
Pavlin Radoslavov | 0fe7002 | 2013-11-02 16:13:12 -0700 | [diff] [blame] | 6 | import java.util.List; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 7 | import java.util.Map; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 8 | import java.util.concurrent.ScheduledExecutorService; |
| 9 | import java.util.concurrent.TimeUnit; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 10 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 11 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 12 | import net.floodlightcontroller.core.IOFSwitch; |
| 13 | import net.floodlightcontroller.core.IOFSwitchListener; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 14 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 15 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 16 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 17 | import net.floodlightcontroller.core.module.IFloodlightService; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 18 | import net.floodlightcontroller.core.util.SingletonTask; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 19 | import net.floodlightcontroller.devicemanager.IDevice; |
| 20 | import net.floodlightcontroller.devicemanager.IDeviceListener; |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 21 | import net.floodlightcontroller.routing.Link; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 22 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 23 | import net.onrc.onos.datagrid.IDatagridService; |
Pankaj Berde | 38646d6 | 2013-06-21 11:34:04 -0700 | [diff] [blame] | 24 | import net.onrc.onos.graph.GraphDBConnection; |
| 25 | import net.onrc.onos.graph.GraphDBOperation; |
| 26 | import net.onrc.onos.graph.IDBConnection; |
| 27 | import net.onrc.onos.graph.LocalTopologyEventListener; |
HIGUCHI Yuta | 2d01158 | 2013-06-15 01:47:11 -0700 | [diff] [blame] | 28 | import net.onrc.onos.ofcontroller.core.IDeviceStorage; |
| 29 | import net.onrc.onos.ofcontroller.core.ILinkStorage; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 30 | import net.onrc.onos.ofcontroller.core.INetMapStorage.DM_OPERATION; |
HIGUCHI Yuta | 2051490 | 2013-06-12 11:24:16 -0700 | [diff] [blame] | 31 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 32 | import net.onrc.onos.ofcontroller.core.IOFSwitchPortListener; |
| 33 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
HIGUCHI Yuta | 2d01158 | 2013-06-15 01:47:11 -0700 | [diff] [blame] | 34 | import net.onrc.onos.ofcontroller.core.internal.DeviceStorageImpl; |
| 35 | import net.onrc.onos.ofcontroller.core.internal.LinkStorageImpl; |
HIGUCHI Yuta | ed49ef7 | 2013-06-12 11:34:10 -0700 | [diff] [blame] | 36 | import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl; |
HIGUCHI Yuta | a56fbde | 2013-06-17 14:26:05 -0700 | [diff] [blame] | 37 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryListener; |
| 38 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService; |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 39 | import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo; |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 40 | import net.onrc.onos.ofcontroller.proxyarp.ArpMessage; |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 41 | import net.onrc.onos.ofcontroller.topology.TopologyElement; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 42 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
| 43 | import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback; |
| 44 | import net.onrc.onos.registry.controller.RegistryException; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 45 | |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 46 | import net.onrc.onos.ofcontroller.flowmanager.PerformanceMonitor; |
| 47 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 48 | import org.openflow.protocol.OFPhysicalPort; |
| 49 | import org.openflow.util.HexString; |
| 50 | import org.slf4j.Logger; |
| 51 | import org.slf4j.LoggerFactory; |
| 52 | |
| 53 | import com.google.common.net.InetAddresses; |
| 54 | |
Pavlin Radoslavov | 2806940 | 2013-10-18 18:43:11 -0700 | [diff] [blame] | 55 | public class NetworkGraphPublisher implements IDeviceListener, |
| 56 | IOFSwitchListener, |
| 57 | IOFSwitchPortListener, |
| 58 | ILinkDiscoveryListener, |
| 59 | IFloodlightModule, |
| 60 | INetworkGraphService { |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 61 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 62 | protected IDeviceStorage devStore; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 63 | protected ISwitchStorage swStore; |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 64 | protected ILinkStorage linkStore; |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 65 | protected final static Logger log = LoggerFactory.getLogger(NetworkGraphPublisher.class); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 66 | //protected IDeviceService deviceService; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 67 | protected IControllerRegistryService registryService; |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 68 | protected GraphDBOperation op; |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 69 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 70 | protected static final String DBConfigFile = "dbconf"; |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 71 | protected static final String CleanupEnabled = "EnableCleanup"; |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 72 | protected IThreadPoolService threadPool; |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 73 | protected IFloodlightProviderService floodlightProvider; |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 74 | |
Pankaj Berde | 6201614 | 2013-04-09 15:35:50 -0700 | [diff] [blame] | 75 | protected final int CLEANUP_TASK_INTERVAL = 60; // 1 min |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 76 | protected SingletonTask cleanupTask; |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 77 | protected ILinkDiscoveryService linkDiscovery; |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 78 | |
| 79 | protected IDatagridService datagridService; |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 80 | |
| 81 | /** |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 82 | * Cleanup and synch switch state from registry |
| 83 | */ |
| 84 | protected class SwitchCleanup implements ControlChangeCallback, Runnable { |
| 85 | @Override |
| 86 | public void run() { |
Yuta HIGUCHI | 61509a4 | 2013-12-17 10:41:04 -0800 | [diff] [blame] | 87 | String old = Thread.currentThread().getName(); |
Yuta HIGUCHI | 1412e8c | 2014-01-03 17:19:01 -0800 | [diff] [blame] | 88 | Thread.currentThread().setName("SwitchCleanup@" + old); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 89 | try { |
| 90 | log.debug("Running cleanup thread"); |
| 91 | switchCleanup(); |
| 92 | } |
| 93 | catch (Exception e) { |
| 94 | log.error("Error in cleanup thread", e); |
| 95 | } finally { |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 96 | op.close(); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 97 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 98 | TimeUnit.SECONDS); |
Yuta HIGUCHI | 61509a4 | 2013-12-17 10:41:04 -0800 | [diff] [blame] | 99 | Thread.currentThread().setName(old); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void controlChanged(long dpid, boolean hasControl) { |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 105 | if (hasControl) { |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 106 | log.debug("got control to set inactive sw {}", HexString.toHexString(dpid)); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 107 | try { |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 108 | // Get the affected ports |
| 109 | List<Short> ports = swStore.getPorts(HexString.toHexString(dpid)); |
| 110 | // Get the affected links |
| 111 | List<Link> links = linkStore.getLinks(HexString.toHexString(dpid)); |
| 112 | // Get the affected reverse links |
| 113 | List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(dpid)); |
| 114 | links.addAll(reverseLinks); |
| 115 | |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 116 | //if (swStore.updateSwitch(HexString.toHexString(dpid), SwitchState.INACTIVE, DM_OPERATION.UPDATE)) { |
| 117 | if (swStore.deactivateSwitch(HexString.toHexString(dpid))) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 118 | registryService.releaseControl(dpid); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 119 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 120 | // TODO publish UPDATE_SWITCH event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 121 | // |
| 122 | // NOTE: Here we explicitly send |
| 123 | // notification to remove the |
| 124 | // switch, because it is inactive |
| 125 | // |
| 126 | TopologyElement topologyElement = |
| 127 | new TopologyElement(dpid); |
| 128 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
| 129 | |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 130 | // Publish: remove the affected ports |
| 131 | for (Short port : ports) { |
| 132 | TopologyElement topologyElementPort = |
| 133 | new TopologyElement(dpid, port); |
| 134 | datagridService.notificationSendTopologyElementRemoved(topologyElementPort); |
| 135 | } |
| 136 | // Publish: remove the affected links |
| 137 | for (Link link : links) { |
| 138 | TopologyElement topologyElementLink = |
| 139 | new TopologyElement(link.getSrc(), |
| 140 | link.getSrcPort(), |
| 141 | link.getDst(), |
| 142 | link.getDstPort()); |
| 143 | datagridService.notificationSendTopologyElementRemoved(topologyElementLink); |
| 144 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 145 | } |
| 146 | } catch (Exception e) { |
| 147 | log.error("Error in SwitchCleanup:controlChanged ", e); |
| 148 | } |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 149 | } |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 152 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 153 | protected void switchCleanup() { |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 154 | op.close(); |
| 155 | Iterable<ISwitchObject> switches = op.getActiveSwitches(); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 156 | |
Jonathan Hart | f02a093 | 2013-03-18 18:30:48 -0700 | [diff] [blame] | 157 | log.debug("Checking for inactive switches"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 158 | // For each switch check if a controller exists in controller registry |
| 159 | for (ISwitchObject sw: switches) { |
Jonathan Hart | f02a093 | 2013-03-18 18:30:48 -0700 | [diff] [blame] | 160 | //log.debug("checking if switch is inactive: {}", sw.getDPID()); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 161 | try { |
| 162 | long dpid = HexString.toLong(sw.getDPID()); |
| 163 | String controller = registryService.getControllerForSwitch(dpid); |
| 164 | if (controller == null) { |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 165 | log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid)); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 166 | registryService.requestControl(dpid, new SwitchCleanup()); |
Umesh Krishnaswamy | 255b988 | 2013-04-02 19:55:43 -0700 | [diff] [blame] | 167 | //} else { |
| 168 | // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 169 | } |
| 170 | } catch (NumberFormatException e) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 171 | log.debug("Caught NumberFormatException trying to requestControl in cleanup thread"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 172 | e.printStackTrace(); |
| 173 | } catch (RegistryException e) { |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 174 | log.debug("Caught RegistryException trying to requestControl in cleanup thread"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 175 | e.printStackTrace(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 176 | } |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 177 | } |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 178 | op.close(); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 179 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 180 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 181 | @Override |
| 182 | public void linkDiscoveryUpdate(LDUpdate update) { |
| 183 | Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort()); |
| 184 | //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 185 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 186 | switch (update.getOperation()) { |
| 187 | case LINK_REMOVED: |
| 188 | log.debug("LinkDiscoveryUpdate(): Removing link {}", lt); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 189 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 190 | if (linkStore.deleteLink(lt)) { |
| 191 | // TODO publish DELETE_LINK event here |
| 192 | TopologyElement topologyElement = |
| 193 | new TopologyElement(update.getSrc(), |
| 194 | update.getSrcPort(), |
| 195 | update.getDst(), |
| 196 | update.getDstPort()); |
| 197 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
| 198 | } |
| 199 | break; |
| 200 | case LINK_UPDATED: |
| 201 | log.debug("LinkDiscoveryUpdate(): Updating link {}", lt); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 202 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 203 | LinkInfo linfo = linkStore.getLinkInfo(lt); |
| 204 | // TODO update "linfo" using portState derived using "update" |
| 205 | if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) { |
| 206 | // TODO publish UPDATE_LINK event here |
| 207 | // |
| 208 | // TODO NOTE: Here we assume that updated |
| 209 | // link is UP. |
| 210 | // |
| 211 | TopologyElement topologyElement = |
| 212 | new TopologyElement(update.getSrc(), |
| 213 | update.getSrcPort(), |
| 214 | update.getDst(), |
| 215 | update.getDstPort()); |
| 216 | datagridService.notificationSendTopologyElementUpdated(topologyElement); |
| 217 | } |
| 218 | break; |
| 219 | case LINK_ADDED: |
| 220 | log.debug("LinkDiscoveryUpdate(): Adding link {}", lt); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 221 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 222 | if (linkStore.addLink(lt)) { |
| 223 | // TODO publish ADD_LINK event here |
| 224 | TopologyElement topologyElement = |
| 225 | new TopologyElement(update.getSrc(), |
| 226 | update.getSrcPort(), |
| 227 | update.getDst(), |
| 228 | update.getDstPort()); |
| 229 | datagridService.notificationSendTopologyElementAdded(topologyElement); |
| 230 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 231 | |
pingping-lin | 0092603 | 2013-12-18 12:13:08 +0800 | [diff] [blame] | 232 | break; |
| 233 | default: |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 238 | |
| 239 | @Override |
| 240 | public void addedSwitch(IOFSwitch sw) { |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 241 | if (registryService.hasControl(sw.getId())) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 242 | if (swStore.addSwitch(sw)) { |
| 243 | // TODO publish ADD_SWITCH event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 244 | TopologyElement topologyElement = |
| 245 | new TopologyElement(sw.getId()); |
Pavlin Radoslavov | 649c97d | 2013-11-04 16:00:23 -0800 | [diff] [blame] | 246 | datagridService.notificationSendTopologyElementAdded(topologyElement); |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 247 | |
| 248 | // Publish: add the ports |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 249 | // TODO: Add only ports that are UP? |
| 250 | for (OFPhysicalPort port : sw.getPorts()) { |
Jonathan Hart | 8a5d097 | 2013-12-04 10:02:44 -0800 | [diff] [blame] | 251 | TopologyElement topologyElementPort = |
| 252 | new TopologyElement(sw.getId(), port.getPortNumber()); |
| 253 | datagridService.notificationSendTopologyElementAdded(topologyElementPort); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 254 | |
Jonathan Hart | 8a5d097 | 2013-12-04 10:02:44 -0800 | [diff] [blame] | 255 | // Allow links to be discovered on this port now that it's |
| 256 | // in the database |
| 257 | linkDiscovery.RemoveFromSuppressLLDPs(sw.getId(), port.getPortNumber()); |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 258 | } |
Pavlin Radoslavov | 649c97d | 2013-11-04 16:00:23 -0800 | [diff] [blame] | 259 | |
Pavlin Radoslavov | 0fe7002 | 2013-11-02 16:13:12 -0700 | [diff] [blame] | 260 | // Add all links that might be connected already |
| 261 | List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId())); |
| 262 | // Add all reverse links as well |
| 263 | List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId())); |
| 264 | links.addAll(reverseLinks); |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 265 | |
| 266 | // Publish: add the links |
Pavlin Radoslavov | 0fe7002 | 2013-11-02 16:13:12 -0700 | [diff] [blame] | 267 | for (Link link : links) { |
| 268 | TopologyElement topologyElementLink = |
| 269 | new TopologyElement(link.getSrc(), |
| 270 | link.getSrcPort(), |
| 271 | link.getDst(), |
| 272 | link.getDstPort()); |
| 273 | datagridService.notificationSendTopologyElementAdded(topologyElementLink); |
| 274 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 275 | } |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 276 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void removedSwitch(IOFSwitch sw) { |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 281 | /* |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 282 | if (registryService.hasControl(sw.getId())) { |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 283 | // Get the affected ports |
| 284 | List<Short> ports = swStore.getPorts(HexString.toHexString(sw.getId())); |
| 285 | // Get the affected links |
| 286 | List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId())); |
| 287 | // Get the affected reverse links |
| 288 | List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId())); |
| 289 | links.addAll(reverseLinks); |
| 290 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 291 | if (swStore.deleteSwitch(sw.getStringId())) { |
| 292 | // TODO publish DELETE_SWITCH event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 293 | TopologyElement topologyElement = |
| 294 | new TopologyElement(sw.getId()); |
| 295 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 296 | |
| 297 | // Publish: remove the affected ports |
| 298 | for (Short port : ports) { |
| 299 | TopologyElement topologyElementPort = |
| 300 | new TopologyElement(sw.getId(), port); |
| 301 | datagridService.notificationSendTopologyElementRemoved(topologyElementPort); |
| 302 | } |
| 303 | // Publish: remove the affected links |
| 304 | for (Link link : links) { |
| 305 | TopologyElement topologyElementLink = |
| 306 | new TopologyElement(link.getSrc(), |
| 307 | link.getSrcPort(), |
| 308 | link.getDst(), |
| 309 | link.getDstPort()); |
| 310 | datagridService.notificationSendTopologyElementRemoved(topologyElementLink); |
| 311 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
Jonathan Hart | adc6389 | 2013-11-08 14:03:55 -0800 | [diff] [blame] | 314 | */ |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public void switchPortChanged(Long switchId) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 319 | // NOTE: Event not needed here. This callback always coincide with add/remove callback. |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 322 | |
| 323 | @Override |
| 324 | public void switchPortAdded(Long switchId, OFPhysicalPort port) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 325 | if (swStore.addPort(HexString.toHexString(switchId), port)) { |
Jonathan Hart | 8a5d097 | 2013-12-04 10:02:44 -0800 | [diff] [blame] | 326 | // Allow links to be discovered on this port now that it's |
| 327 | // in the database |
| 328 | linkDiscovery.RemoveFromSuppressLLDPs(switchId, port.getPortNumber()); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 329 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 330 | // TODO publish ADD_PORT event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 331 | TopologyElement topologyElement = |
| 332 | new TopologyElement(switchId, port.getPortNumber()); |
| 333 | datagridService.notificationSendTopologyElementAdded(topologyElement); |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 334 | |
| 335 | // Add all links that might be connected already |
| 336 | List<Link> links = linkStore.getLinks(switchId, port.getPortNumber()); |
| 337 | // Add all reverse links as well |
| 338 | List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber()); |
| 339 | links.addAll(reverseLinks); |
| 340 | |
| 341 | // Publish: add the links |
| 342 | for (Link link : links) { |
| 343 | TopologyElement topologyElementLink = |
| 344 | new TopologyElement(link.getSrc(), |
| 345 | link.getSrcPort(), |
| 346 | link.getDst(), |
| 347 | link.getDstPort()); |
| 348 | datagridService.notificationSendTopologyElementAdded(topologyElementLink); |
| 349 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 350 | } |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public void switchPortRemoved(Long switchId, OFPhysicalPort port) { |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 355 | // Remove all links that might be connected already |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 356 | PerformanceMonitor.start("SwitchPortRemoved.DbAccess"); |
| 357 | |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 358 | List<Link> links = linkStore.getLinks(switchId, port.getPortNumber()); |
| 359 | // Remove all reverse links as well |
| 360 | List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber()); |
| 361 | links.addAll(reverseLinks); |
| 362 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 363 | if (swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber())) { |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 364 | PerformanceMonitor.stop("SwitchPortRemoved.DbAccess"); |
| 365 | PerformanceMonitor.start("SwitchPortRemoved.NotificationSend"); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 366 | // TODO publish DELETE_PORT event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 367 | TopologyElement topologyElement = |
| 368 | new TopologyElement(switchId, port.getPortNumber()); |
| 369 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 370 | |
| 371 | // Publish: remove the links |
| 372 | for (Link link : links) { |
| 373 | TopologyElement topologyElementLink = |
| 374 | new TopologyElement(link.getSrc(), |
| 375 | link.getSrcPort(), |
| 376 | link.getDst(), |
| 377 | link.getDstPort()); |
| 378 | datagridService.notificationSendTopologyElementRemoved(topologyElementLink); |
| 379 | } |
Pavlin Radoslavov | e249767 | 2014-01-12 18:03:35 -0800 | [diff] [blame] | 380 | PerformanceMonitor.stop("SwitchPortRemoved.NotificationSend"); |
Pavlin Radoslavov | 8bd6d11 | 2014-01-12 20:12:37 -0800 | [diff] [blame^] | 381 | PerformanceMonitor.report("SwitchPortRemoved.DbAccess"); |
| 382 | PerformanceMonitor.report("TopologyEntryRemoved.NotificationReceived"); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 383 | } |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 386 | @Override |
| 387 | public String getName() { |
HIGUCHI Yuta | 155cfc5 | 2013-06-14 15:54:16 -0700 | [diff] [blame] | 388 | return "NetworkGraphPublisher"; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | @Override |
| 392 | public void deviceAdded(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 393 | log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString()); |
| 394 | devStore.addDevice(device); |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 395 | for (int intIpv4Address : device.getIPv4Addresses()) { |
| 396 | datagridService.sendArpRequest( |
| 397 | ArpMessage.newReply(InetAddresses.fromInteger(intIpv4Address))); |
| 398 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public void deviceRemoved(IDevice device) { |
| 403 | // TODO Auto-generated method stub |
Jonathan Hart | 4fce4ed | 2013-11-01 21:29:21 -0700 | [diff] [blame] | 404 | devStore.removeDevice(device); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public void deviceMoved(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 409 | devStore.changeDeviceAttachments(device); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public void deviceIPV4AddrChanged(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 414 | devStore.changeDeviceIPv4Address(device); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public void deviceVlanChanged(IDevice device) { |
| 419 | // TODO Auto-generated method stub |
| 420 | } |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 421 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 422 | |
| 423 | @Override |
| 424 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Pavlin Radoslavov | 2806940 | 2013-10-18 18:43:11 -0700 | [diff] [blame] | 425 | Collection<Class<? extends IFloodlightService>> l = |
| 426 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 427 | l.add(INetworkGraphService.class); |
| 428 | return l; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Pavlin Radoslavov | 2806940 | 2013-10-18 18:43:11 -0700 | [diff] [blame] | 433 | Map<Class<? extends IFloodlightService>, |
| 434 | IFloodlightService> m = |
| 435 | new HashMap<Class<? extends IFloodlightService>, |
| 436 | IFloodlightService>(); |
| 437 | m.put(INetworkGraphService.class, this); |
| 438 | return m; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 443 | Collection<Class<? extends IFloodlightService>> l = |
| 444 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 445 | l.add(IFloodlightProviderService.class); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 446 | //l.add(IDeviceService.class); |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 447 | l.add(IDatagridService.class); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 448 | l.add(IThreadPoolService.class); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 449 | return l; |
| 450 | } |
| 451 | |
| 452 | @Override |
| 453 | public void init(FloodlightModuleContext context) |
| 454 | throws FloodlightModuleException { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 455 | Map<String, String> configMap = context.getConfigParams(this); |
| 456 | String conf = configMap.get(DBConfigFile); |
Toshio Koide | bfe9b92 | 2013-06-18 10:56:05 -0700 | [diff] [blame] | 457 | op = new GraphDBOperation(conf); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 458 | |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 459 | floodlightProvider = |
| 460 | context.getServiceImpl(IFloodlightProviderService.class); |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 461 | //deviceService = context.getServiceImpl(IDeviceService.class); |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 462 | linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 463 | threadPool = context.getServiceImpl(IThreadPoolService.class); |
| 464 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 465 | datagridService = context.getServiceImpl(IDatagridService.class); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 466 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 467 | devStore = new DeviceStorageImpl(); |
| 468 | devStore.init(conf); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 469 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 470 | swStore = new SwitchStorageImpl(); |
| 471 | swStore.init(conf); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 472 | |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 473 | linkStore = new LinkStorageImpl(); |
| 474 | linkStore.init(conf); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 475 | |
HIGUCHI Yuta | 155cfc5 | 2013-06-14 15:54:16 -0700 | [diff] [blame] | 476 | log.debug("Initializing NetworkGraphPublisher module with {}", conf); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 477 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | @Override |
| 481 | public void startUp(FloodlightModuleContext context) { |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 482 | Map<String, String> configMap = context.getConfigParams(this); |
| 483 | String cleanupNeeded = configMap.get(CleanupEnabled); |
| 484 | |
Jonathan Hart | d857ad6 | 2013-12-14 18:08:17 -0800 | [diff] [blame] | 485 | //deviceService.addListener(this); |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 486 | floodlightProvider.addOFSwitchListener(this); |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 487 | linkDiscovery.addListener(this); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 488 | |
Pankaj Berde | 9d6b507 | 2013-04-03 11:51:23 -0700 | [diff] [blame] | 489 | log.debug("Adding EventListener"); |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 490 | IDBConnection conn = op.getDBConnection(); |
| 491 | conn.addEventListener(new LocalTopologyEventListener((GraphDBConnection) conn)); |
Yuta HIGUCHI | 3ab1fd0 | 2013-12-17 11:01:02 -0800 | [diff] [blame] | 492 | // Setup the Cleanup task. |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 493 | if (cleanupNeeded == null || !cleanupNeeded.equals("False")) { |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 494 | ScheduledExecutorService ses = threadPool.getScheduledExecutor(); |
| 495 | cleanupTask = new SingletonTask(ses, new SwitchCleanup()); |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 496 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS); |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 497 | } |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 498 | |
| 499 | // |
| 500 | // NOTE: No need to register with the Datagrid Service, |
| 501 | // because we don't need to receive any notifications from it. |
| 502 | // |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | } |