alshabib | e1cf87d | 2014-10-17 09:23:50 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.provider.host.impl; |
| 2 | |
| 3 | import org.apache.felix.scr.annotations.Activate; |
| 4 | import org.apache.felix.scr.annotations.Component; |
| 5 | import org.apache.felix.scr.annotations.Deactivate; |
| 6 | import org.apache.felix.scr.annotations.Reference; |
| 7 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 8 | import org.onlab.onos.net.ConnectPoint; |
| 9 | import org.onlab.onos.net.Host; |
| 10 | import org.onlab.onos.net.HostId; |
| 11 | import org.onlab.onos.net.HostLocation; |
| 12 | import org.onlab.onos.net.host.DefaultHostDescription; |
| 13 | import org.onlab.onos.net.host.HostDescription; |
| 14 | import org.onlab.onos.net.host.HostProvider; |
| 15 | import org.onlab.onos.net.host.HostProviderRegistry; |
| 16 | import org.onlab.onos.net.host.HostProviderService; |
| 17 | import org.onlab.onos.net.packet.PacketContext; |
| 18 | import org.onlab.onos.net.packet.PacketProcessor; |
| 19 | import org.onlab.onos.net.packet.PacketService; |
| 20 | import org.onlab.onos.net.provider.AbstractProvider; |
| 21 | import org.onlab.onos.net.provider.ProviderId; |
| 22 | import org.onlab.onos.net.topology.Topology; |
| 23 | import org.onlab.onos.net.topology.TopologyService; |
| 24 | |
| 25 | import org.onlab.packet.ARP; |
| 26 | import org.onlab.packet.Ethernet; |
| 27 | |
| 28 | import org.onlab.packet.IpPrefix; |
| 29 | import org.onlab.packet.VlanId; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
| 33 | |
| 34 | /** |
| 35 | * Provider which uses an OpenFlow controller to detect network |
| 36 | * end-station hosts. |
| 37 | */ |
| 38 | @Component(immediate = true) |
| 39 | public class HostLocationProvider extends AbstractProvider implements HostProvider { |
| 40 | |
| 41 | private final Logger log = getLogger(getClass()); |
| 42 | |
| 43 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 44 | protected HostProviderRegistry providerRegistry; |
| 45 | |
| 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 47 | protected PacketService pktService; |
| 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 50 | protected TopologyService topologyService; |
| 51 | |
| 52 | private HostProviderService providerService; |
| 53 | |
| 54 | private final InternalHostProvider processor = new InternalHostProvider(); |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * Creates an OpenFlow host provider. |
| 59 | */ |
| 60 | public HostLocationProvider() { |
| 61 | super(new ProviderId("of", "org.onlab.onos.provider.host")); |
| 62 | } |
| 63 | |
| 64 | @Activate |
| 65 | public void activate() { |
| 66 | providerService = providerRegistry.register(this); |
| 67 | pktService.addProcessor(processor, 1); |
| 68 | log.info("Started"); |
| 69 | } |
| 70 | |
| 71 | @Deactivate |
| 72 | public void deactivate() { |
| 73 | providerRegistry.unregister(this); |
| 74 | pktService.removeProcessor(processor); |
| 75 | providerService = null; |
| 76 | log.info("Stopped"); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void triggerProbe(Host host) { |
| 81 | log.info("Triggering probe on device {}", host); |
| 82 | } |
| 83 | |
| 84 | private class InternalHostProvider implements PacketProcessor { |
| 85 | |
| 86 | @Override |
| 87 | public void process(PacketContext context) { |
| 88 | Ethernet eth = context.inPacket().parsed(); |
| 89 | |
| 90 | VlanId vlan = VlanId.vlanId(eth.getVlanID()); |
| 91 | ConnectPoint heardOn = context.inPacket().receivedFrom(); |
| 92 | |
| 93 | // If this is not an edge port, bail out. |
| 94 | Topology topology = topologyService.currentTopology(); |
| 95 | if (topologyService.isInfrastructure(topology, heardOn)) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | HostLocation hloc = new HostLocation(heardOn, System.currentTimeMillis()); |
| 100 | |
| 101 | HostId hid = HostId.hostId(eth.getSourceMAC(), vlan); |
| 102 | |
| 103 | // Potentially a new or moved host |
| 104 | if (eth.getEtherType() == Ethernet.TYPE_ARP) { |
| 105 | ARP arp = (ARP) eth.getPayload(); |
| 106 | IpPrefix ip = IpPrefix.valueOf(arp.getSenderProtocolAddress()); |
| 107 | HostDescription hdescr = |
| 108 | new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc, ip); |
| 109 | providerService.hostDetected(hid, hdescr); |
| 110 | |
| 111 | } else if (eth.getEtherType() == Ethernet.TYPE_IPV4) { |
| 112 | //Do not learn new ip from ip packet. |
| 113 | HostDescription hdescr = |
| 114 | new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc); |
| 115 | providerService.hostDetected(hid, hdescr); |
| 116 | |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |