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