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"); |
yoshi | 55e1e8f | 2013-12-05 11:10:38 -0800 | [diff] [blame] | 91 | op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 92 | switchCleanup(); |
| 93 | } |
| 94 | catch (Exception e) { |
| 95 | log.error("Error in cleanup thread", e); |
| 96 | } finally { |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 97 | op.close(); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 98 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 99 | TimeUnit.SECONDS); |
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); |
| 119 | |
| 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 | } |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 149 | } |
| 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() { |
yoshi | 8b972bc | 2013-12-05 17:10:04 -0800 | [diff] [blame] | 154 | //op.close(); |
yoshi | f96c585 | 2013-12-06 09:23:58 -0800 | [diff] [blame^] | 155 | op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf"); |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 156 | Iterable<ISwitchObject> switches = op.getActiveSwitches(); |
Jonathan Hart | f02a093 | 2013-03-18 18:30:48 -0700 | [diff] [blame] | 157 | |
| 158 | log.debug("Checking for inactive switches"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 159 | // For each switch check if a controller exists in controller registry |
| 160 | for (ISwitchObject sw: switches) { |
Jonathan Hart | f02a093 | 2013-03-18 18:30:48 -0700 | [diff] [blame] | 161 | //log.debug("checking if switch is inactive: {}", sw.getDPID()); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 162 | try { |
| 163 | long dpid = HexString.toLong(sw.getDPID()); |
| 164 | String controller = registryService.getControllerForSwitch(dpid); |
| 165 | if (controller == null) { |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 166 | log.debug("request Control to set inactive sw {}", HexString.toHexString(dpid)); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 167 | registryService.requestControl(dpid, new SwitchCleanup()); |
Umesh Krishnaswamy | 255b988 | 2013-04-02 19:55:43 -0700 | [diff] [blame] | 168 | //} else { |
| 169 | // log.debug("sw {} is controlled by controller: {}",HexString.toHexString(dpid),controller); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 170 | } |
| 171 | } catch (NumberFormatException e) { |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 172 | log.debug("Caught NumberFormatException trying to requestControl in cleanup thread"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 173 | e.printStackTrace(); |
| 174 | } catch (RegistryException e) { |
Jonathan Hart | 4baf3be | 2013-03-21 18:26:13 -0700 | [diff] [blame] | 175 | log.debug("Caught RegistryException trying to requestControl in cleanup thread"); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 176 | e.printStackTrace(); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 177 | } |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 178 | } |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 179 | op.close(); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 180 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 181 | |
| 182 | @Override |
| 183 | public void linkDiscoveryUpdate(LDUpdate update) { |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 184 | Link lt = new Link(update.getSrc(),update.getSrcPort(),update.getDst(),update.getDstPort()); |
Jonathan Hart | e7c2d2f | 2013-07-27 18:08:34 +1200 | [diff] [blame] | 185 | //log.debug("{}:LinkDicoveryUpdate(): Updating Link {}",this.getClass(), lt); |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 186 | |
Jonathan Hart | e7c2d2f | 2013-07-27 18:08:34 +1200 | [diff] [blame] | 187 | switch (update.getOperation()) { |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 188 | case LINK_REMOVED: |
Jonathan Hart | e7c2d2f | 2013-07-27 18:08:34 +1200 | [diff] [blame] | 189 | log.debug("LinkDiscoveryUpdate(): Removing link {}", lt); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 190 | |
| 191 | if (linkStore.deleteLink(lt)) { |
| 192 | // TODO publish DELETE_LINK event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 193 | TopologyElement topologyElement = |
| 194 | new TopologyElement(update.getSrc(), |
| 195 | update.getSrcPort(), |
| 196 | update.getDst(), |
| 197 | update.getDstPort()); |
| 198 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 199 | } |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 200 | break; |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 201 | case LINK_UPDATED: |
Jonathan Hart | e7c2d2f | 2013-07-27 18:08:34 +1200 | [diff] [blame] | 202 | log.debug("LinkDiscoveryUpdate(): Updating link {}", lt); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 203 | |
Naoki Shiota | b2d17e8 | 2013-10-18 18:08:16 -0700 | [diff] [blame] | 204 | LinkInfo linfo = linkStore.getLinkInfo(lt); |
| 205 | // TODO update "linfo" using portState derived using "update" |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 206 | if (linkStore.update(lt, linfo, DM_OPERATION.UPDATE)) { |
| 207 | // TODO publish UPDATE_LINK event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 208 | // |
| 209 | // TODO NOTE: Here we assume that updated |
| 210 | // link is UP. |
| 211 | // |
| 212 | TopologyElement topologyElement = |
| 213 | new TopologyElement(update.getSrc(), |
| 214 | update.getSrcPort(), |
| 215 | update.getDst(), |
| 216 | update.getDstPort()); |
| 217 | datagridService.notificationSendTopologyElementUpdated(topologyElement); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 218 | } |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 219 | break; |
| 220 | case LINK_ADDED: |
Jonathan Hart | e7c2d2f | 2013-07-27 18:08:34 +1200 | [diff] [blame] | 221 | log.debug("LinkDiscoveryUpdate(): Adding link {}", lt); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 222 | |
| 223 | if (linkStore.addLink(lt)) { |
| 224 | // TODO publish ADD_LINK event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 225 | TopologyElement topologyElement = |
| 226 | new TopologyElement(update.getSrc(), |
| 227 | update.getSrcPort(), |
| 228 | update.getDst(), |
| 229 | update.getDstPort()); |
| 230 | datagridService.notificationSendTopologyElementAdded(topologyElement); |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 231 | } |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 232 | break; |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 233 | default: |
| 234 | break; |
| 235 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 236 | |
| 237 | } |
| 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); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 247 | |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 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()) { |
Pavlin Radoslavov | 649c97d | 2013-11-04 16:00:23 -0800 | [diff] [blame] | 251 | TopologyElement topologyElementPort = |
| 252 | new TopologyElement(sw.getId(), |
| 253 | port.getPortNumber()); |
| 254 | datagridService.notificationSendTopologyElementAdded(topologyElementPort); |
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 | da80957 | 2013-02-22 15:31:20 -0800 | [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); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 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)) { |
| 323 | // TODO publish ADD_PORT event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 324 | TopologyElement topologyElement = |
| 325 | new TopologyElement(switchId, port.getPortNumber()); |
| 326 | datagridService.notificationSendTopologyElementAdded(topologyElement); |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 327 | |
| 328 | // Add all links that might be connected already |
| 329 | List<Link> links = linkStore.getLinks(switchId, port.getPortNumber()); |
| 330 | // Add all reverse links as well |
| 331 | List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber()); |
| 332 | links.addAll(reverseLinks); |
| 333 | |
| 334 | // Publish: add the links |
| 335 | for (Link link : links) { |
| 336 | TopologyElement topologyElementLink = |
| 337 | new TopologyElement(link.getSrc(), |
| 338 | link.getSrcPort(), |
| 339 | link.getDst(), |
| 340 | link.getDstPort()); |
| 341 | datagridService.notificationSendTopologyElementAdded(topologyElementLink); |
| 342 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 343 | } |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public void switchPortRemoved(Long switchId, OFPhysicalPort port) { |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 348 | // Remove all links that might be connected already |
| 349 | List<Link> links = linkStore.getLinks(switchId, port.getPortNumber()); |
| 350 | // Remove all reverse links as well |
| 351 | List<Link> reverseLinks = linkStore.getReverseLinks(switchId, port.getPortNumber()); |
| 352 | links.addAll(reverseLinks); |
| 353 | |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 354 | if (swStore.deletePort(HexString.toHexString(switchId), port.getPortNumber())) { |
| 355 | // TODO publish DELETE_PORT event here |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 356 | TopologyElement topologyElement = |
| 357 | new TopologyElement(switchId, port.getPortNumber()); |
| 358 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
Pavlin Radoslavov | fb15728 | 2013-11-05 08:40:13 -0800 | [diff] [blame] | 359 | |
| 360 | // Publish: remove the links |
| 361 | for (Link link : links) { |
| 362 | TopologyElement topologyElementLink = |
| 363 | new TopologyElement(link.getSrc(), |
| 364 | link.getSrcPort(), |
| 365 | link.getDst(), |
| 366 | link.getDstPort()); |
| 367 | datagridService.notificationSendTopologyElementRemoved(topologyElementLink); |
| 368 | } |
Naoki Shiota | 987a572 | 2013-10-23 11:59:36 -0700 | [diff] [blame] | 369 | } |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 372 | @Override |
| 373 | public String getName() { |
HIGUCHI Yuta | 155cfc5 | 2013-06-14 15:54:16 -0700 | [diff] [blame] | 374 | return "NetworkGraphPublisher"; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public void deviceAdded(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 379 | log.debug("{}:deviceAdded(): Adding device {}",this.getClass(),device.getMACAddressString()); |
| 380 | devStore.addDevice(device); |
Jonathan Hart | d300325 | 2013-11-15 09:44:46 -0800 | [diff] [blame] | 381 | for (int intIpv4Address : device.getIPv4Addresses()) { |
| 382 | datagridService.sendArpRequest( |
| 383 | ArpMessage.newReply(InetAddresses.fromInteger(intIpv4Address))); |
| 384 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | @Override |
| 388 | public void deviceRemoved(IDevice device) { |
| 389 | // TODO Auto-generated method stub |
Jonathan Hart | 4fce4ed | 2013-11-01 21:29:21 -0700 | [diff] [blame] | 390 | devStore.removeDevice(device); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public void deviceMoved(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 395 | devStore.changeDeviceAttachments(device); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public void deviceIPV4AddrChanged(IDevice device) { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 400 | devStore.changeDeviceIPv4Address(device); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public void deviceVlanChanged(IDevice device) { |
| 405 | // TODO Auto-generated method stub |
| 406 | } |
| 407 | |
| 408 | |
| 409 | @Override |
| 410 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Pavlin Radoslavov | 2806940 | 2013-10-18 18:43:11 -0700 | [diff] [blame] | 411 | Collection<Class<? extends IFloodlightService>> l = |
| 412 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 413 | l.add(INetworkGraphService.class); |
| 414 | return l; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() { |
Pavlin Radoslavov | 2806940 | 2013-10-18 18:43:11 -0700 | [diff] [blame] | 419 | Map<Class<? extends IFloodlightService>, |
| 420 | IFloodlightService> m = |
| 421 | new HashMap<Class<? extends IFloodlightService>, |
| 422 | IFloodlightService>(); |
| 423 | m.put(INetworkGraphService.class, this); |
| 424 | return m; |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 429 | Collection<Class<? extends IFloodlightService>> l = |
| 430 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 431 | l.add(IFloodlightProviderService.class); |
| 432 | l.add(IDeviceService.class); |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 433 | l.add(IDatagridService.class); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 434 | l.add(IThreadPoolService.class); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 435 | return l; |
| 436 | } |
| 437 | |
| 438 | @Override |
| 439 | public void init(FloodlightModuleContext context) |
| 440 | throws FloodlightModuleException { |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 441 | Map<String, String> configMap = context.getConfigParams(this); |
| 442 | String conf = configMap.get(DBConfigFile); |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 443 | String dbStore = configMap.get(GraphDBStore); |
yoshi | a0839b5 | 2013-11-25 16:42:10 -0800 | [diff] [blame] | 444 | System.out.println("conf" + conf + "dbStore" + dbStore); |
yoshi | d38cd31 | 2013-12-02 19:54:44 -0800 | [diff] [blame] | 445 | op = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf"); |
| 446 | //op = GraphDBManager.getDBOperation(dbStore, conf); |
yoshi | a0839b5 | 2013-11-25 16:42:10 -0800 | [diff] [blame] | 447 | if (op == null) { |
| 448 | System.out.println("publisher op is null"); |
| 449 | } |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 450 | |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 451 | floodlightProvider = |
| 452 | context.getServiceImpl(IFloodlightProviderService.class); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 453 | deviceService = context.getServiceImpl(IDeviceService.class); |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 454 | linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 455 | threadPool = context.getServiceImpl(IThreadPoolService.class); |
| 456 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 457 | datagridService = context.getServiceImpl(IDatagridService.class); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 458 | |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 459 | devStore = new DeviceStorageImpl(); |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 460 | devStore.init(dbStore, conf); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 461 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 462 | swStore = new SwitchStorageImpl(); |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 463 | swStore.init(dbStore, conf); |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 464 | |
| 465 | linkStore = new LinkStorageImpl(); |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 466 | linkStore.init(dbStore, conf); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 467 | |
HIGUCHI Yuta | 155cfc5 | 2013-06-14 15:54:16 -0700 | [diff] [blame] | 468 | log.debug("Initializing NetworkGraphPublisher module with {}", conf); |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 469 | |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public void startUp(FloodlightModuleContext context) { |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 474 | Map<String, String> configMap = context.getConfigParams(this); |
| 475 | String cleanupNeeded = configMap.get(CleanupEnabled); |
| 476 | |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 477 | deviceService.addListener(this); |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 478 | floodlightProvider.addOFSwitchListener(this); |
Pankaj Berde | 00e9088 | 2013-06-10 21:25:05 -0700 | [diff] [blame] | 479 | linkDiscovery.addListener(this); |
Pankaj Berde | 9d6b507 | 2013-04-03 11:51:23 -0700 | [diff] [blame] | 480 | |
| 481 | log.debug("Adding EventListener"); |
yoshi | 34bcb3e | 2013-11-25 16:49:25 -0800 | [diff] [blame] | 482 | System.out.println("start Up op " + op); |
Toshio Koide | 70ba38b | 2013-06-13 14:05:05 -0700 | [diff] [blame] | 483 | IDBConnection conn = op.getDBConnection(); |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 484 | conn.addEventListener(new LocalTopologyEventListener((DBConnection) conn)); |
Pankaj Berde | f08d5ff | 2013-03-14 17:03:07 -0700 | [diff] [blame] | 485 | // Setup the Cleanup task. |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 486 | if (cleanupNeeded == null || !cleanupNeeded.equals("False")) { |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 487 | ScheduledExecutorService ses = threadPool.getScheduledExecutor(); |
| 488 | cleanupTask = new SingletonTask(ses, new SwitchCleanup()); |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 489 | cleanupTask.reschedule(CLEANUP_TASK_INTERVAL, TimeUnit.SECONDS); |
Pankaj Berde | 1e3e5ba | 2013-03-15 13:17:53 -0700 | [diff] [blame] | 490 | } |
Pavlin Radoslavov | 8442e49 | 2013-10-25 21:54:13 -0700 | [diff] [blame] | 491 | |
| 492 | // |
| 493 | // NOTE: No need to register with the Datagrid Service, |
| 494 | // because we don't need to receive any notifications from it. |
| 495 | // |
Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | } |