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