added HostProvider to SimpleHostManager
Change-Id: I13ff57fcd24ea7bd2c3f2544a3aad7d18ceda107
Showing
5 changed files
with
87 additions
and
22 deletions
... | @@ -20,6 +20,14 @@ public class DefaultHostDescription implements HostDescription { | ... | @@ -20,6 +20,14 @@ public class DefaultHostDescription implements HostDescription { |
20 | private final Set<IPAddress> ips; | 20 | private final Set<IPAddress> ips; |
21 | 21 | ||
22 | public DefaultHostDescription(MACAddress mac, VLANID vlan, | 22 | public DefaultHostDescription(MACAddress mac, VLANID vlan, |
23 | + HostLocation loc) { | ||
24 | + this.mac = mac; | ||
25 | + this.vlan = vlan; | ||
26 | + this.location = loc; | ||
27 | + this.ips = new HashSet<IPAddress>(); | ||
28 | + } | ||
29 | + | ||
30 | + public DefaultHostDescription(MACAddress mac, VLANID vlan, | ||
23 | HostLocation loc, Set<IPAddress> ips) { | 31 | HostLocation loc, Set<IPAddress> ips) { |
24 | this.mac = mac; | 32 | this.mac = mac; |
25 | this.vlan = vlan; | 33 | this.vlan = vlan; | ... | ... |
... | @@ -76,20 +76,23 @@ public class SimpleHostStore { | ... | @@ -76,20 +76,23 @@ public class SimpleHostStore { |
76 | HostDescription descr) { | 76 | HostDescription descr) { |
77 | DefaultHost updated; | 77 | DefaultHost updated; |
78 | HostEvent event; | 78 | HostEvent event; |
79 | - if (host.location().equals(descr.location())) { | 79 | + // Consider only actual location (not timestamp) change? |
80 | + if (!(host.location().port().equals(descr.location().port()))) { | ||
80 | updated = new DefaultHost(providerId, host.id(), | 81 | updated = new DefaultHost(providerId, host.id(), |
81 | host.mac(), | 82 | host.mac(), |
82 | host.vlan(), | 83 | host.vlan(), |
83 | - host.location(), | 84 | + descr.location(), |
84 | - descr.ipAddresses()); | 85 | + host.ipAddresses()); |
85 | - event = new HostEvent(HOST_UPDATED, updated); | 86 | + event = new HostEvent(HOST_MOVED, updated); |
86 | - } else { | 87 | + } else if (!(host.ipAddresses().equals(descr.ipAddresses()))) { |
87 | updated = new DefaultHost(providerId, host.id(), | 88 | updated = new DefaultHost(providerId, host.id(), |
88 | host.mac(), | 89 | host.mac(), |
89 | host.vlan(), | 90 | host.vlan(), |
90 | descr.location(), | 91 | descr.location(), |
91 | - host.ipAddresses()); | 92 | + descr.ipAddresses()); |
92 | - event = new HostEvent(HOST_MOVED, updated); | 93 | + event = new HostEvent(HOST_UPDATED, updated); |
94 | + } else { | ||
95 | + return null; | ||
93 | } | 96 | } |
94 | synchronized (this) { | 97 | synchronized (this) { |
95 | hosts.put(host.id(), updated); | 98 | hosts.put(host.id(), updated); | ... | ... |
1 | package org.onlab.onos.provider.of.host.impl; | 1 | package org.onlab.onos.provider.of.host.impl; |
2 | 2 | ||
3 | +import java.util.Set; | ||
4 | + | ||
3 | import org.apache.felix.scr.annotations.Activate; | 5 | import org.apache.felix.scr.annotations.Activate; |
4 | import org.apache.felix.scr.annotations.Component; | 6 | import org.apache.felix.scr.annotations.Component; |
5 | import org.apache.felix.scr.annotations.Deactivate; | 7 | import org.apache.felix.scr.annotations.Deactivate; |
6 | import org.apache.felix.scr.annotations.Reference; | 8 | import org.apache.felix.scr.annotations.Reference; |
7 | import org.apache.felix.scr.annotations.ReferenceCardinality; | 9 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
10 | +import org.onlab.onos.net.DeviceId; | ||
8 | import org.onlab.onos.net.Host; | 11 | import org.onlab.onos.net.Host; |
12 | +import org.onlab.onos.net.HostId; | ||
13 | +import org.onlab.onos.net.HostLocation; | ||
14 | +import org.onlab.onos.net.PortNumber; | ||
15 | +import org.onlab.onos.net.host.DefaultHostDescription; | ||
16 | +import org.onlab.onos.net.host.HostDescription; | ||
9 | import org.onlab.onos.net.host.HostProvider; | 17 | import org.onlab.onos.net.host.HostProvider; |
10 | import org.onlab.onos.net.host.HostProviderRegistry; | 18 | import org.onlab.onos.net.host.HostProviderRegistry; |
11 | import org.onlab.onos.net.host.HostProviderService; | 19 | import org.onlab.onos.net.host.HostProviderService; |
12 | import org.onlab.onos.net.provider.AbstractProvider; | 20 | import org.onlab.onos.net.provider.AbstractProvider; |
13 | import org.onlab.onos.net.provider.ProviderId; | 21 | import org.onlab.onos.net.provider.ProviderId; |
14 | import org.onlab.onos.of.controller.OpenFlowController; | 22 | import org.onlab.onos.of.controller.OpenFlowController; |
23 | +import org.onlab.onos.of.controller.OpenFlowPacketContext; | ||
24 | +import org.onlab.onos.of.controller.PacketListener; | ||
25 | +import org.onlab.packet.ARP; | ||
26 | +import org.onlab.packet.Ethernet; | ||
27 | +import org.onlab.packet.IPAddress; | ||
28 | +import org.onlab.packet.VLANID; | ||
15 | import org.slf4j.Logger; | 29 | import org.slf4j.Logger; |
16 | 30 | ||
31 | +import com.google.common.collect.Sets; | ||
32 | + | ||
17 | import static org.slf4j.LoggerFactory.getLogger; | 33 | import static org.slf4j.LoggerFactory.getLogger; |
18 | 34 | ||
19 | /** | 35 | /** |
... | @@ -33,6 +49,8 @@ public class OpenFlowHostProvider extends AbstractProvider implements HostProvid | ... | @@ -33,6 +49,8 @@ public class OpenFlowHostProvider extends AbstractProvider implements HostProvid |
33 | 49 | ||
34 | private HostProviderService providerService; | 50 | private HostProviderService providerService; |
35 | 51 | ||
52 | + private final InternalHostProvider listener = new InternalHostProvider(); | ||
53 | + | ||
36 | /** | 54 | /** |
37 | * Creates an OpenFlow host provider. | 55 | * Creates an OpenFlow host provider. |
38 | */ | 56 | */ |
... | @@ -43,13 +61,17 @@ public class OpenFlowHostProvider extends AbstractProvider implements HostProvid | ... | @@ -43,13 +61,17 @@ public class OpenFlowHostProvider extends AbstractProvider implements HostProvid |
43 | @Activate | 61 | @Activate |
44 | public void activate() { | 62 | public void activate() { |
45 | providerService = providerRegistry.register(this); | 63 | providerService = providerRegistry.register(this); |
64 | + controller.addPacketListener(0, listener); | ||
65 | + | ||
46 | log.info("Started"); | 66 | log.info("Started"); |
47 | } | 67 | } |
48 | 68 | ||
49 | @Deactivate | 69 | @Deactivate |
50 | public void deactivate() { | 70 | public void deactivate() { |
51 | providerRegistry.unregister(this); | 71 | providerRegistry.unregister(this); |
72 | + controller.removePacketListener(listener); | ||
52 | providerService = null; | 73 | providerService = null; |
74 | + | ||
53 | log.info("Stopped"); | 75 | log.info("Stopped"); |
54 | } | 76 | } |
55 | 77 | ||
... | @@ -58,4 +80,32 @@ public class OpenFlowHostProvider extends AbstractProvider implements HostProvid | ... | @@ -58,4 +80,32 @@ public class OpenFlowHostProvider extends AbstractProvider implements HostProvid |
58 | log.info("Triggering probe on device {}", host); | 80 | log.info("Triggering probe on device {}", host); |
59 | } | 81 | } |
60 | 82 | ||
83 | + private class InternalHostProvider implements PacketListener { | ||
84 | + | ||
85 | + @Override | ||
86 | + public void handlePacket(OpenFlowPacketContext pktCtx) { | ||
87 | + Ethernet eth = pktCtx.parsed(); | ||
88 | + | ||
89 | + // potentially a new or moved host | ||
90 | + if (eth.getEtherType() == Ethernet.TYPE_ARP) { | ||
91 | + VLANID vlan = VLANID.vlanId(eth.getVlanID()); | ||
92 | + HostId hid = HostId.hostId( | ||
93 | + eth.getSourceMAC(), vlan); | ||
94 | + HostLocation hloc = new HostLocation( | ||
95 | + DeviceId.deviceId("of:" + Long.toHexString(pktCtx.dpid().value())), | ||
96 | + PortNumber.portNumber(pktCtx.inPort()), | ||
97 | + System.currentTimeMillis()); | ||
98 | + ARP arp = (ARP) eth.getPayload(); | ||
99 | + Set<IPAddress> ips = Sets.newHashSet(IPAddress.valueOf(arp.getSenderProtocolAddress())); | ||
100 | + HostDescription hdescr = new DefaultHostDescription( | ||
101 | + eth.getSourceMAC(), | ||
102 | + vlan, | ||
103 | + hloc, | ||
104 | + ips); | ||
105 | + providerService.hostDetected(hid, hdescr); | ||
106 | + | ||
107 | + } | ||
108 | + } | ||
109 | + | ||
110 | + } | ||
61 | } | 111 | } | ... | ... |
... | @@ -103,32 +103,27 @@ public class IPAddress { | ... | @@ -103,32 +103,27 @@ public class IPAddress { |
103 | if (builder.length() > 0) { | 103 | if (builder.length() > 0) { |
104 | builder.append("."); | 104 | builder.append("."); |
105 | } | 105 | } |
106 | - builder.append(String.format("%02d", b)); | 106 | + builder.append(String.format("%d", b)); |
107 | } | 107 | } |
108 | return builder.toString(); | 108 | return builder.toString(); |
109 | } | 109 | } |
110 | 110 | ||
111 | @Override | 111 | @Override |
112 | public int hashCode() { | 112 | public int hashCode() { |
113 | - return octets.hashCode(); | 113 | + return Arrays.hashCode(octets); |
114 | } | 114 | } |
115 | 115 | ||
116 | @Override | 116 | @Override |
117 | public boolean equals(Object obj) { | 117 | public boolean equals(Object obj) { |
118 | - if (this == obj) { | ||
119 | - return true; | ||
120 | - } | ||
121 | - if (obj instanceof IPAddress) { | ||
122 | 118 | ||
119 | + if (obj instanceof IPAddress) { | ||
123 | IPAddress other = (IPAddress) obj; | 120 | IPAddress other = (IPAddress) obj; |
124 | 121 | ||
125 | - if (!(this.version.equals(other.version))) { | 122 | + if (this.version.equals(other.version) |
126 | - return false; | 123 | + && (Arrays.equals(this.octets, other.octets))) { |
127 | - } | 124 | + return true; |
128 | - if (!(Arrays.equals(this.octets, other.octets))) { | ||
129 | - return false; | ||
130 | } | 125 | } |
131 | } | 126 | } |
132 | - return true; | 127 | + return false; |
133 | } | 128 | } |
134 | } | 129 | } | ... | ... |
... | @@ -6,20 +6,29 @@ package org.onlab.packet; | ... | @@ -6,20 +6,29 @@ package org.onlab.packet; |
6 | public class VLANID { | 6 | public class VLANID { |
7 | 7 | ||
8 | private final short value; | 8 | private final short value; |
9 | - private static final short NONE = 0; | 9 | + // Based on convention used elsewhere? Check and change if needed |
10 | + private static final short UNTAGGED = (short) 0xffff; | ||
10 | // A VLAN ID is actually 12 bits of a VLAN tag. | 11 | // A VLAN ID is actually 12 bits of a VLAN tag. |
11 | private static final short MAX_VLAN = 4095; | 12 | private static final short MAX_VLAN = 4095; |
12 | 13 | ||
14 | + protected VLANID() { | ||
15 | + this.value = UNTAGGED; | ||
16 | + } | ||
17 | + | ||
13 | protected VLANID(short value) { | 18 | protected VLANID(short value) { |
14 | this.value = value; | 19 | this.value = value; |
15 | } | 20 | } |
16 | 21 | ||
17 | public static VLANID vlanId() { | 22 | public static VLANID vlanId() { |
18 | - return new VLANID(NONE); | 23 | + return new VLANID(UNTAGGED); |
19 | } | 24 | } |
20 | 25 | ||
21 | public static VLANID vlanId(short value) { | 26 | public static VLANID vlanId(short value) { |
22 | - if (value >= MAX_VLAN) { | 27 | + if (value == UNTAGGED) { |
28 | + return new VLANID(); | ||
29 | + } | ||
30 | + | ||
31 | + if (value > MAX_VLAN) { | ||
23 | throw new IllegalArgumentException( | 32 | throw new IllegalArgumentException( |
24 | "value exceeds allowed maximum VLAN ID value (4095)"); | 33 | "value exceeds allowed maximum VLAN ID value (4095)"); |
25 | } | 34 | } | ... | ... |
-
Please register or login to post a comment