Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.floodlightlistener; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Map; |
| 6 | |
| 7 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 8 | import net.floodlightcontroller.core.IOFSwitch; |
| 9 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 10 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 11 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 12 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 13 | import net.onrc.onos.datagrid.IDatagridService; |
| 14 | import net.onrc.onos.ofcontroller.core.IOFSwitchPortListener; |
| 15 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryListener; |
| 16 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService; |
| 17 | import net.onrc.onos.ofcontroller.networkgraph.FloodlightToOnosMappers; |
| 18 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
| 19 | import net.onrc.onos.ofcontroller.networkgraph.LinkImpl; |
| 20 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
| 21 | import net.onrc.onos.ofcontroller.networkgraph.SouthboundNetworkGraph; |
| 22 | import net.onrc.onos.ofcontroller.networkgraph.Switch; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 23 | import net.onrc.onos.ofcontroller.util.Dpid; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 24 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
| 25 | |
| 26 | import org.openflow.protocol.OFPhysicalPort; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * I've created a copy of the NetworkGraphPublisher so I can integrate |
| 32 | * the new API with ONOS while still having the old NetworkGraphPublisher |
| 33 | * to reference. I've renamed to RCNetworkGraphPublisher. |
| 34 | * TODO Remove old NetworkGraphPublisher once the integration of the new |
| 35 | * API is complete. |
| 36 | * For now, we just write to the database and don't worry about sending |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 37 | * notifications. |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 38 | * TODO Send notification after each database write |
| 39 | */ |
| 40 | public class RCNetworkGraphPublisher implements /*IOFSwitchListener,*/ |
| 41 | IOFSwitchPortListener, |
| 42 | ILinkDiscoveryListener, |
| 43 | IFloodlightModule { |
Jonathan Hart | 4701671 | 2014-02-07 12:41:35 -0800 | [diff] [blame] | 44 | private static final Logger log = LoggerFactory.getLogger(RCNetworkGraphPublisher.class); |
| 45 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 46 | private IFloodlightProviderService floodlightProvider; |
| 47 | private ILinkDiscoveryService linkDiscovery; |
| 48 | private IControllerRegistryService registryService; |
| 49 | private IDatagridService datagridService; |
| 50 | private INetworkGraphService networkGraphService; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 51 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 52 | private NetworkGraph networkGraph; |
| 53 | private SouthboundNetworkGraph southboundNetworkGraph; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 54 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 55 | |
| 56 | @Override |
| 57 | public void linkDiscoveryUpdate(LDUpdate update) { |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 58 | |
| 59 | // TODO Move this sanity check when retrieving port to common place? |
| 60 | Switch srcSw = networkGraph.getSwitch(update.getSrc()); |
| 61 | if (srcSw == null) { |
| 62 | log.error("Switch {} missing when adding Link {}", |
| 63 | new Dpid(update.getSrc()), update); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | Switch dstSw = networkGraph.getSwitch(update.getDst()); |
| 68 | if (dstSw == null) { |
| 69 | log.error("Switch {} missing when adding Link {}", |
| 70 | new Dpid(update.getDst()), update); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // XXX Is it correct to add Link object created with networkGraph to southboundNetworkGraph? |
| 75 | LinkImpl link = new LinkImpl(networkGraph, |
| 76 | srcSw.getPort((long) update.getSrcPort()), |
| 77 | dstSw.getPort((long) update.getDstPort())); |
| 78 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 79 | switch (update.getOperation()) { |
| 80 | case LINK_ADDED: |
| 81 | southboundNetworkGraph.addLink(link); |
| 82 | /* |
| 83 | TopologyElement topologyElement = |
| 84 | new TopologyElement(update.getSrc(), |
| 85 | update.getSrcPort(), |
| 86 | update.getDst(), |
| 87 | update.getDstPort()); |
| 88 | datagridService.notificationSendTopologyElementAdded(topologyElement); |
| 89 | */ |
| 90 | break; |
| 91 | case LINK_UPDATED: |
| 92 | // I don't know what a LINK_UPDATED event is. |
| 93 | // We never use it. |
| 94 | break; |
| 95 | case LINK_REMOVED: |
| 96 | southboundNetworkGraph.removeLink(link); |
| 97 | /* |
| 98 | TopologyElement topologyElement = |
| 99 | new TopologyElement(update.getSrc(), |
| 100 | update.getSrcPort(), |
| 101 | update.getDst(), |
| 102 | update.getDstPort()); |
| 103 | datagridService.notificationSendTopologyElementRemoved(topologyElement); |
| 104 | */ |
| 105 | break; |
| 106 | default: |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void switchPortAdded(Long switchId, OFPhysicalPort port) { |
| 113 | // TODO Auto-generated method stub |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 114 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void switchPortRemoved(Long switchId, OFPhysicalPort port) { |
| 119 | // TODO Auto-generated method stub |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 120 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void addedSwitch(IOFSwitch sw) { |
| 125 | // TODO Not very robust |
| 126 | if (!registryService.hasControl(sw.getId())) { |
| 127 | return; |
| 128 | } |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 129 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 130 | Switch onosSwitch = FloodlightToOnosMappers.map(networkGraph, sw); |
| 131 | southboundNetworkGraph.addSwitch(onosSwitch); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 132 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 133 | /* |
| 134 | // TODO publish ADD_SWITCH event here |
| 135 | TopologyElement topologyElement = |
| 136 | new TopologyElement(sw.getId()); |
| 137 | datagridService.notificationSendTopologyElementAdded(topologyElement); |
| 138 | |
| 139 | // Publish: add the ports |
| 140 | // TODO: Add only ports that are UP? |
| 141 | for (OFPhysicalPort port : sw.getPorts()) { |
| 142 | TopologyElement topologyElementPort = |
| 143 | new TopologyElement(sw.getId(), port.getPortNumber()); |
| 144 | datagridService.notificationSendTopologyElementAdded(topologyElementPort); |
| 145 | |
| 146 | // Allow links to be discovered on this port now that it's |
| 147 | // in the database |
| 148 | linkDiscovery.RemoveFromSuppressLLDPs(sw.getId(), port.getPortNumber()); |
| 149 | } |
| 150 | |
| 151 | // Add all links that might be connected already |
| 152 | List<Link> links = linkStore.getLinks(HexString.toHexString(sw.getId())); |
| 153 | // Add all reverse links as well |
| 154 | List<Link> reverseLinks = linkStore.getReverseLinks(HexString.toHexString(sw.getId())); |
| 155 | links.addAll(reverseLinks); |
| 156 | |
| 157 | // Publish: add the links |
| 158 | for (Link link : links) { |
| 159 | TopologyElement topologyElementLink = |
| 160 | new TopologyElement(link.getSrc(), |
| 161 | link.getSrcPort(), |
| 162 | link.getDst(), |
| 163 | link.getDstPort()); |
| 164 | datagridService.notificationSendTopologyElementAdded(topologyElementLink); |
| 165 | */ |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void removedSwitch(IOFSwitch sw) { |
| 170 | // TODO Auto-generated method stub |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 171 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public void switchPortChanged(Long switchId) { |
| 176 | // TODO Auto-generated method stub |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 177 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public String getName() { |
| 182 | // TODO Auto-generated method stub |
| 183 | return null; |
| 184 | } |
| 185 | |
| 186 | /* ***************** |
| 187 | * IFloodlightModule |
| 188 | * *****************/ |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 189 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 190 | @Override |
| 191 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 192 | return null; |
| 193 | } |
| 194 | |
| 195 | @Override |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 196 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 197 | getServiceImpls() { |
| 198 | return null; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 203 | Collection<Class<? extends IFloodlightService>> l = |
| 204 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 205 | l.add(IFloodlightProviderService.class); |
| 206 | l.add(ILinkDiscoveryService.class); |
| 207 | l.add(IControllerRegistryService.class); |
| 208 | l.add(IDatagridService.class); |
| 209 | l.add(INetworkGraphService.class); |
| 210 | return l; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void init(FloodlightModuleContext context) |
| 215 | throws FloodlightModuleException { |
| 216 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
| 217 | linkDiscovery = context.getServiceImpl(ILinkDiscoveryService.class); |
| 218 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
| 219 | datagridService = context.getServiceImpl(IDatagridService.class); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 220 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 221 | networkGraphService = context.getServiceImpl(INetworkGraphService.class); |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void startUp(FloodlightModuleContext context) { |
| 226 | // TODO enable cleanup thread |
| 227 | floodlightProvider.addOFSwitchListener(this); |
| 228 | linkDiscovery.addListener(this); |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 229 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 230 | networkGraph = networkGraphService.getNetworkGraph(); |
| 231 | southboundNetworkGraph = networkGraphService.getSouthboundNetworkGraph(); |
| 232 | } |
| 233 | } |