add methods to directly set MacAddress
Change-Id: Idab9dce493927d8943789177830df630f3b1326a
Showing
10 changed files
with
58 additions
and
41 deletions
| ... | @@ -67,6 +67,7 @@ public class HostMonitor implements TimerTask { | ... | @@ -67,6 +67,7 @@ public class HostMonitor implements TimerTask { |
| 67 | private final ConcurrentMap<ProviderId, HostProvider> hostProviders; | 67 | private final ConcurrentMap<ProviderId, HostProvider> hostProviders; |
| 68 | 68 | ||
| 69 | private static final long DEFAULT_PROBE_RATE = 30000; // milliseconds | 69 | private static final long DEFAULT_PROBE_RATE = 30000; // milliseconds |
| 70 | + private static final byte[] ZERO_MAC_ADDRESS = MacAddress.ZERO.toBytes(); | ||
| 70 | private long probeRate = DEFAULT_PROBE_RATE; | 71 | private long probeRate = DEFAULT_PROBE_RATE; |
| 71 | 72 | ||
| 72 | private Timeout timeout; | 73 | private Timeout timeout; |
| ... | @@ -215,15 +216,15 @@ public class HostMonitor implements TimerTask { | ... | @@ -215,15 +216,15 @@ public class HostMonitor implements TimerTask { |
| 215 | .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH) | 216 | .setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH) |
| 216 | .setOpCode(ARP.OP_REQUEST); | 217 | .setOpCode(ARP.OP_REQUEST); |
| 217 | 218 | ||
| 218 | - arp.setSenderHardwareAddress(sourceMac.getAddress()) | 219 | + arp.setSenderHardwareAddress(sourceMac.toBytes()) |
| 219 | .setSenderProtocolAddress(sourceIp.toOctets()) | 220 | .setSenderProtocolAddress(sourceIp.toOctets()) |
| 220 | - .setTargetHardwareAddress(MacAddress.ZERO_MAC_ADDRESS) | 221 | + .setTargetHardwareAddress(ZERO_MAC_ADDRESS) |
| 221 | .setTargetProtocolAddress(targetIp.toOctets()); | 222 | .setTargetProtocolAddress(targetIp.toOctets()); |
| 222 | 223 | ||
| 223 | Ethernet ethernet = new Ethernet(); | 224 | Ethernet ethernet = new Ethernet(); |
| 224 | ethernet.setEtherType(Ethernet.TYPE_ARP) | 225 | ethernet.setEtherType(Ethernet.TYPE_ARP) |
| 225 | - .setDestinationMACAddress(MacAddress.BROADCAST_MAC) | 226 | + .setDestinationMACAddress(MacAddress.BROADCAST) |
| 226 | - .setSourceMACAddress(sourceMac.getAddress()) | 227 | + .setSourceMACAddress(sourceMac) |
| 227 | .setPayload(arp); | 228 | .setPayload(arp); |
| 228 | 229 | ||
| 229 | return ethernet; | 230 | return ethernet; | ... | ... |
| ... | @@ -360,8 +360,8 @@ public class ProxyArpManager implements ProxyArpService { | ... | @@ -360,8 +360,8 @@ public class ProxyArpManager implements ProxyArpService { |
| 360 | Ethernet request) { | 360 | Ethernet request) { |
| 361 | 361 | ||
| 362 | Ethernet eth = new Ethernet(); | 362 | Ethernet eth = new Ethernet(); |
| 363 | - eth.setDestinationMACAddress(request.getSourceMACAddress()); | 363 | + eth.setDestinationMACAddress(request.getSourceMAC()); |
| 364 | - eth.setSourceMACAddress(srcMac.getAddress()); | 364 | + eth.setSourceMACAddress(srcMac); |
| 365 | eth.setEtherType(Ethernet.TYPE_ARP); | 365 | eth.setEtherType(Ethernet.TYPE_ARP); |
| 366 | eth.setVlanID(request.getVlanID()); | 366 | eth.setVlanID(request.getVlanID()); |
| 367 | 367 | ||
| ... | @@ -372,7 +372,7 @@ public class ProxyArpManager implements ProxyArpService { | ... | @@ -372,7 +372,7 @@ public class ProxyArpManager implements ProxyArpService { |
| 372 | 372 | ||
| 373 | arp.setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH); | 373 | arp.setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH); |
| 374 | arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); | 374 | arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); |
| 375 | - arp.setSenderHardwareAddress(srcMac.getAddress()); | 375 | + arp.setSenderHardwareAddress(srcMac.toBytes()); |
| 376 | arp.setTargetHardwareAddress(request.getSourceMACAddress()); | 376 | arp.setTargetHardwareAddress(request.getSourceMACAddress()); |
| 377 | 377 | ||
| 378 | arp.setTargetProtocolAddress(((ARP) request.getPayload()) | 378 | arp.setTargetProtocolAddress(((ARP) request.getPayload()) | ... | ... |
| ... | @@ -20,11 +20,9 @@ import static org.easymock.EasyMock.expect; | ... | @@ -20,11 +20,9 @@ import static org.easymock.EasyMock.expect; |
| 20 | import static org.easymock.EasyMock.expectLastCall; | 20 | import static org.easymock.EasyMock.expectLastCall; |
| 21 | import static org.easymock.EasyMock.replay; | 21 | import static org.easymock.EasyMock.replay; |
| 22 | import static org.easymock.EasyMock.verify; | 22 | import static org.easymock.EasyMock.verify; |
| 23 | -import static org.junit.Assert.assertEquals; | 23 | +import static org.junit.Assert.*; |
| 24 | -import static org.junit.Assert.assertTrue; | ||
| 25 | 24 | ||
| 26 | import java.util.ArrayList; | 25 | import java.util.ArrayList; |
| 27 | -import java.util.Arrays; | ||
| 28 | import java.util.Collections; | 26 | import java.util.Collections; |
| 29 | import java.util.List; | 27 | import java.util.List; |
| 30 | import java.util.Set; | 28 | import java.util.Set; |
| ... | @@ -155,17 +153,20 @@ public class HostMonitorTest { | ... | @@ -155,17 +153,20 @@ public class HostMonitorTest { |
| 155 | Instruction instruction = packet.treatment().instructions().get(0); | 153 | Instruction instruction = packet.treatment().instructions().get(0); |
| 156 | assertTrue(instruction instanceof OutputInstruction); | 154 | assertTrue(instruction instanceof OutputInstruction); |
| 157 | OutputInstruction oi = (OutputInstruction) instruction; | 155 | OutputInstruction oi = (OutputInstruction) instruction; |
| 158 | - assertTrue(oi.port().equals(portNum)); | 156 | + assertEquals(portNum, oi.port()); |
| 159 | 157 | ||
| 160 | // Check the output packet is correct (well the important bits anyway) | 158 | // Check the output packet is correct (well the important bits anyway) |
| 161 | Ethernet eth = new Ethernet(); | 159 | Ethernet eth = new Ethernet(); |
| 162 | - eth.deserialize(packet.data().array(), 0, packet.data().array().length); | 160 | + final byte[] pktData = new byte[packet.data().remaining()]; |
| 161 | + packet.data().get(pktData); | ||
| 162 | + eth.deserialize(pktData, 0, pktData.length); | ||
| 163 | ARP arp = (ARP) eth.getPayload(); | 163 | ARP arp = (ARP) eth.getPayload(); |
| 164 | - assertTrue(Arrays.equals(arp.getSenderProtocolAddress(), | 164 | + assertArrayEquals(SOURCE_ADDR.toOctets(), |
| 165 | - SOURCE_ADDR.toOctets())); | 165 | + arp.getSenderProtocolAddress()); |
| 166 | - assertTrue(Arrays.equals(arp.getSenderHardwareAddress(), sourceMac.toBytes())); | 166 | + assertArrayEquals(sourceMac.toBytes(), |
| 167 | - assertTrue(Arrays.equals(arp.getTargetProtocolAddress(), | 167 | + arp.getSenderHardwareAddress()); |
| 168 | - TARGET_IP_ADDR.toOctets())); | 168 | + assertArrayEquals(TARGET_IP_ADDR.toOctets(), |
| 169 | + arp.getTargetProtocolAddress()); | ||
| 169 | } | 170 | } |
| 170 | 171 | ||
| 171 | class TestPacketService implements PacketService { | 172 | class TestPacketService implements PacketService { | ... | ... |
| ... | @@ -19,12 +19,9 @@ import static org.easymock.EasyMock.anyObject; | ... | @@ -19,12 +19,9 @@ import static org.easymock.EasyMock.anyObject; |
| 19 | import static org.easymock.EasyMock.createMock; | 19 | import static org.easymock.EasyMock.createMock; |
| 20 | import static org.easymock.EasyMock.expect; | 20 | import static org.easymock.EasyMock.expect; |
| 21 | import static org.easymock.EasyMock.replay; | 21 | import static org.easymock.EasyMock.replay; |
| 22 | -import static org.junit.Assert.assertEquals; | 22 | +import static org.junit.Assert.*; |
| 23 | -import static org.junit.Assert.assertFalse; | ||
| 24 | -import static org.junit.Assert.assertTrue; | ||
| 25 | 23 | ||
| 26 | import java.util.ArrayList; | 24 | import java.util.ArrayList; |
| 27 | -import java.util.Arrays; | ||
| 28 | import java.util.Collections; | 25 | import java.util.Collections; |
| 29 | import java.util.Comparator; | 26 | import java.util.Comparator; |
| 30 | import java.util.List; | 27 | import java.util.List; |
| ... | @@ -91,6 +88,7 @@ public class ProxyArpManagerTest { | ... | @@ -91,6 +88,7 @@ public class ProxyArpManagerTest { |
| 91 | private static final PortNumber P1 = PortNumber.portNumber(1); | 88 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 92 | private static final HostLocation LOC1 = new HostLocation(DID1, P1, 123L); | 89 | private static final HostLocation LOC1 = new HostLocation(DID1, P1, 123L); |
| 93 | private static final HostLocation LOC2 = new HostLocation(DID2, P1, 123L); | 90 | private static final HostLocation LOC2 = new HostLocation(DID2, P1, 123L); |
| 91 | + private static final byte[] ZERO_MAC_ADDRESS = MacAddress.ZERO.toBytes(); | ||
| 94 | 92 | ||
| 95 | private ProxyArpManager proxyArp; | 93 | private ProxyArpManager proxyArp; |
| 96 | 94 | ||
| ... | @@ -483,7 +481,7 @@ public class ProxyArpManagerTest { | ... | @@ -483,7 +481,7 @@ public class ProxyArpManagerTest { |
| 483 | */ | 481 | */ |
| 484 | private void verifyPacketOut(Ethernet expected, ConnectPoint outPort, | 482 | private void verifyPacketOut(Ethernet expected, ConnectPoint outPort, |
| 485 | OutboundPacket actual) { | 483 | OutboundPacket actual) { |
| 486 | - assertTrue(Arrays.equals(expected.serialize(), actual.data().array())); | 484 | + assertArrayEquals(expected.serialize(), actual.data().array()); |
| 487 | assertEquals(1, actual.treatment().instructions().size()); | 485 | assertEquals(1, actual.treatment().instructions().size()); |
| 488 | assertEquals(outPort.deviceId(), actual.sendThrough()); | 486 | assertEquals(outPort.deviceId(), actual.sendThrough()); |
| 489 | Instruction instruction = actual.treatment().instructions().get(0); | 487 | Instruction instruction = actual.treatment().instructions().get(0); |
| ... | @@ -520,12 +518,12 @@ public class ProxyArpManagerTest { | ... | @@ -520,12 +518,12 @@ public class ProxyArpManagerTest { |
| 520 | Ethernet eth = new Ethernet(); | 518 | Ethernet eth = new Ethernet(); |
| 521 | 519 | ||
| 522 | if (dstMac == null) { | 520 | if (dstMac == null) { |
| 523 | - eth.setDestinationMACAddress(MacAddress.BROADCAST_MAC); | 521 | + eth.setDestinationMACAddress(MacAddress.BROADCAST); |
| 524 | } else { | 522 | } else { |
| 525 | - eth.setDestinationMACAddress(dstMac.getAddress()); | 523 | + eth.setDestinationMACAddress(dstMac); |
| 526 | } | 524 | } |
| 527 | 525 | ||
| 528 | - eth.setSourceMACAddress(srcMac.getAddress()); | 526 | + eth.setSourceMACAddress(srcMac); |
| 529 | eth.setEtherType(Ethernet.TYPE_ARP); | 527 | eth.setEtherType(Ethernet.TYPE_ARP); |
| 530 | eth.setVlanID(VLAN1.toShort()); | 528 | eth.setVlanID(VLAN1.toShort()); |
| 531 | 529 | ||
| ... | @@ -536,12 +534,12 @@ public class ProxyArpManagerTest { | ... | @@ -536,12 +534,12 @@ public class ProxyArpManagerTest { |
| 536 | 534 | ||
| 537 | arp.setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH); | 535 | arp.setProtocolAddressLength((byte) IpAddress.INET_BYTE_LENGTH); |
| 538 | arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); | 536 | arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH); |
| 539 | - arp.setSenderHardwareAddress(srcMac.getAddress()); | 537 | + arp.setSenderHardwareAddress(srcMac.toBytes()); |
| 540 | 538 | ||
| 541 | if (dstMac == null) { | 539 | if (dstMac == null) { |
| 542 | - arp.setTargetHardwareAddress(MacAddress.ZERO_MAC_ADDRESS); | 540 | + arp.setTargetHardwareAddress(ZERO_MAC_ADDRESS); |
| 543 | } else { | 541 | } else { |
| 544 | - arp.setTargetHardwareAddress(dstMac.getAddress()); | 542 | + arp.setTargetHardwareAddress(dstMac.toBytes()); |
| 545 | } | 543 | } |
| 546 | 544 | ||
| 547 | arp.setSenderProtocolAddress(srcIp.toOctets()); | 545 | arp.setSenderProtocolAddress(srcIp.toOctets()); | ... | ... |
| ... | @@ -36,7 +36,7 @@ public class MacAddressSerializer extends Serializer<MacAddress> { | ... | @@ -36,7 +36,7 @@ public class MacAddressSerializer extends Serializer<MacAddress> { |
| 36 | 36 | ||
| 37 | @Override | 37 | @Override |
| 38 | public void write(Kryo kryo, Output output, MacAddress object) { | 38 | public void write(Kryo kryo, Output output, MacAddress object) { |
| 39 | - output.writeBytes(object.getAddress()); | 39 | + output.writeBytes(object.toBytes()); |
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | @Override | 42 | @Override | ... | ... |
| ... | @@ -216,7 +216,7 @@ public class HostLocationProviderTest { | ... | @@ -216,7 +216,7 @@ public class HostLocationProviderTest { |
| 216 | eth.setEtherType(Ethernet.TYPE_ARP) | 216 | eth.setEtherType(Ethernet.TYPE_ARP) |
| 217 | .setVlanID(VLAN.toShort()) | 217 | .setVlanID(VLAN.toShort()) |
| 218 | .setSourceMACAddress(MAC.toBytes()) | 218 | .setSourceMACAddress(MAC.toBytes()) |
| 219 | - .setDestinationMACAddress(BCMAC.getAddress()) | 219 | + .setDestinationMACAddress(BCMAC) |
| 220 | .setPayload(arp); | 220 | .setPayload(arp); |
| 221 | ConnectPoint receivedFrom = new ConnectPoint(DeviceId.deviceId(deviceId), | 221 | ConnectPoint receivedFrom = new ConnectPoint(DeviceId.deviceId(deviceId), |
| 222 | PortNumber.portNumber(INPORT)); | 222 | PortNumber.portNumber(INPORT)); | ... | ... |
| ... | @@ -213,8 +213,8 @@ public class OpenFlowHostProviderTest { | ... | @@ -213,8 +213,8 @@ public class OpenFlowHostProviderTest { |
| 213 | Ethernet eth = new Ethernet(); | 213 | Ethernet eth = new Ethernet(); |
| 214 | eth.setEtherType(Ethernet.TYPE_ARP) | 214 | eth.setEtherType(Ethernet.TYPE_ARP) |
| 215 | .setVlanID(VLAN.toShort()) | 215 | .setVlanID(VLAN.toShort()) |
| 216 | - .setSourceMACAddress(MAC.toBytes()) | 216 | + .setSourceMACAddress(MAC) |
| 217 | - .setDestinationMACAddress(BCMAC.getAddress()) | 217 | + .setDestinationMACAddress(BCMAC) |
| 218 | .setPayload(arp); | 218 | .setPayload(arp); |
| 219 | 219 | ||
| 220 | return eth; | 220 | return eth; | ... | ... |
| ... | @@ -18,6 +18,8 @@ | ... | @@ -18,6 +18,8 @@ |
| 18 | 18 | ||
| 19 | package org.onlab.packet; | 19 | package org.onlab.packet; |
| 20 | 20 | ||
| 21 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 22 | + | ||
| 21 | import java.nio.ByteBuffer; | 23 | import java.nio.ByteBuffer; |
| 22 | import java.util.Arrays; | 24 | import java.util.Arrays; |
| 23 | import java.util.HashMap; | 25 | import java.util.HashMap; |
| ... | @@ -86,6 +88,17 @@ public class Ethernet extends BasePacket { | ... | @@ -86,6 +88,17 @@ public class Ethernet extends BasePacket { |
| 86 | * @param destMac the destination MAC to set | 88 | * @param destMac the destination MAC to set |
| 87 | * @return the Ethernet frame | 89 | * @return the Ethernet frame |
| 88 | */ | 90 | */ |
| 91 | + public Ethernet setDestinationMACAddress(final MacAddress destMac) { | ||
| 92 | + this.destinationMACAddress = checkNotNull(destMac); | ||
| 93 | + return this; | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + /** | ||
| 97 | + * Sets the destination MAC address. | ||
| 98 | + * | ||
| 99 | + * @param destMac the destination MAC to set | ||
| 100 | + * @return the Ethernet frame | ||
| 101 | + */ | ||
| 89 | public Ethernet setDestinationMACAddress(final byte[] destMac) { | 102 | public Ethernet setDestinationMACAddress(final byte[] destMac) { |
| 90 | this.destinationMACAddress = MacAddress.valueOf(destMac); | 103 | this.destinationMACAddress = MacAddress.valueOf(destMac); |
| 91 | return this; | 104 | return this; |
| ... | @@ -126,6 +139,17 @@ public class Ethernet extends BasePacket { | ... | @@ -126,6 +139,17 @@ public class Ethernet extends BasePacket { |
| 126 | * @param sourceMac the source MAC to set | 139 | * @param sourceMac the source MAC to set |
| 127 | * @return the Ethernet frame | 140 | * @return the Ethernet frame |
| 128 | */ | 141 | */ |
| 142 | + public Ethernet setSourceMACAddress(final MacAddress sourceMac) { | ||
| 143 | + this.sourceMACAddress = checkNotNull(sourceMac); | ||
| 144 | + return this; | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + /** | ||
| 148 | + * Sets the source MAC address. | ||
| 149 | + * | ||
| 150 | + * @param sourceMac the source MAC to set | ||
| 151 | + * @return the Ethernet frame | ||
| 152 | + */ | ||
| 129 | public Ethernet setSourceMACAddress(final byte[] sourceMac) { | 153 | public Ethernet setSourceMACAddress(final byte[] sourceMac) { |
| 130 | this.sourceMACAddress = MacAddress.valueOf(sourceMac); | 154 | this.sourceMACAddress = MacAddress.valueOf(sourceMac); |
| 131 | return this; | 155 | return this; | ... | ... |
| ... | @@ -25,9 +25,6 @@ public class MacAddress { | ... | @@ -25,9 +25,6 @@ public class MacAddress { |
| 25 | public static final MacAddress ZERO = valueOf("00:00:00:00:00:00"); | 25 | public static final MacAddress ZERO = valueOf("00:00:00:00:00:00"); |
| 26 | public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff"); | 26 | public static final MacAddress BROADCAST = valueOf("ff:ff:ff:ff:ff:ff"); |
| 27 | 27 | ||
| 28 | - public static final byte[] ZERO_MAC_ADDRESS = ZERO.getAddress(); | ||
| 29 | - public static final byte[] BROADCAST_MAC = BROADCAST.getAddress(); | ||
| 30 | - | ||
| 31 | private static final byte[] LL = new byte[]{ | 28 | private static final byte[] LL = new byte[]{ |
| 32 | 0x01, (byte) 0x80, (byte) 0xc2, 0x00, 0x00, | 29 | 0x01, (byte) 0x80, (byte) 0xc2, 0x00, 0x00, |
| 33 | 0x00, 0x0e, 0x03 | 30 | 0x00, 0x0e, 0x03 |
| ... | @@ -217,8 +214,4 @@ public class MacAddress { | ... | @@ -217,8 +214,4 @@ public class MacAddress { |
| 217 | } | 214 | } |
| 218 | return builder.toString(); | 215 | return builder.toString(); |
| 219 | } | 216 | } |
| 220 | - | ||
| 221 | - public byte[] getAddress() { | ||
| 222 | - return this.address; | ||
| 223 | - } | ||
| 224 | } | 217 | } | ... | ... |
| ... | @@ -64,7 +64,7 @@ public class ONOSLLDP extends LLDP { | ... | @@ -64,7 +64,7 @@ public class ONOSLLDP extends LLDP { |
| 64 | setName(DEFAULT_NAME); | 64 | setName(DEFAULT_NAME); |
| 65 | setDevice(DEFAULT_DEVICE); | 65 | setDevice(DEFAULT_DEVICE); |
| 66 | setOptionalTLVList(Lists.<LLDPTLV>newArrayList(nameTLV, deviceTLV)); | 66 | setOptionalTLVList(Lists.<LLDPTLV>newArrayList(nameTLV, deviceTLV)); |
| 67 | - setTtl(new LLDPTLV().setType((byte) TTL_TLV_TYPE) | 67 | + setTtl(new LLDPTLV().setType(TTL_TLV_TYPE) |
| 68 | .setLength((short) ttlValue.length) | 68 | .setLength((short) ttlValue.length) |
| 69 | .setValue(ttlValue)); | 69 | .setValue(ttlValue)); |
| 70 | 70 | ||
| ... | @@ -94,7 +94,7 @@ public class ONOSLLDP extends LLDP { | ... | @@ -94,7 +94,7 @@ public class ONOSLLDP extends LLDP { |
| 94 | public void setChassisId(final ChassisId chassisId) { | 94 | public void setChassisId(final ChassisId chassisId) { |
| 95 | MacAddress chassisMac = MacAddress.valueOf(chassisId.value()); | 95 | MacAddress chassisMac = MacAddress.valueOf(chassisId.value()); |
| 96 | byte[] chassis = ArrayUtils.addAll(new byte[] {CHASSIS_TLV_SUBTYPE}, | 96 | byte[] chassis = ArrayUtils.addAll(new byte[] {CHASSIS_TLV_SUBTYPE}, |
| 97 | - chassisMac.getAddress()); | 97 | + chassisMac.toBytes()); |
| 98 | 98 | ||
| 99 | LLDPTLV chassisTLV = new LLDPTLV(); | 99 | LLDPTLV chassisTLV = new LLDPTLV(); |
| 100 | chassisTLV.setLength(CHASSIS_TLV_SIZE); | 100 | chassisTLV.setLength(CHASSIS_TLV_SIZE); | ... | ... |
-
Please register or login to post a comment