Mercurial > stress-tester
changeset 1000:b104ead5034e
MACAddressMapper -> MACAddressResolver
author | Devel 2 |
---|---|
date | Thu, 03 Oct 2019 14:49:05 +0200 |
parents | e176852a95f6 |
children | 7402a22cba53 |
files | stress-tester/src/main/java/com/passus/st/emitter/MACAddressMapper.java stress-tester/src/main/java/com/passus/st/emitter/MACAddressResolver.java stress-tester/src/main/java/com/passus/st/emitter/MapBasedMACAddressAddress.java stress-tester/src/main/java/com/passus/st/emitter/MapBasedMACAddressResolver.java stress-tester/src/main/java/com/passus/st/emitter/PassThroughMACAddressMapper.java stress-tester/src/main/java/com/passus/st/emitter/StaticMACAddressResolver.java stress-tester/src/test/java/com/passus/st/emitter/MapBasedMACAddressAddressTest.java stress-tester/src/test/java/com/passus/st/emitter/MapBasedMACAddressResolverTest.java |
diffstat | 8 files changed, 173 insertions(+), 101 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/emitter/MACAddressMapper.java Mon Sep 30 14:17:07 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -package com.passus.st.emitter; - -import com.passus.net.MACAddress; - -public interface MACAddressMapper { - - MACAddress map(MACAddress mac); - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/emitter/MACAddressResolver.java Thu Oct 03 14:49:05 2019 +0200 @@ -0,0 +1,14 @@ +package com.passus.st.emitter; + +import com.passus.net.IpAddress; +import com.passus.net.MACAddress; + +public interface MACAddressResolver { + + default MACAddress resolve(String ip) { + return resolve(IpAddress.parse(ip)); + } + + MACAddress resolve(IpAddress ip); + +}
--- a/stress-tester/src/main/java/com/passus/st/emitter/MapBasedMACAddressAddress.java Mon Sep 30 14:17:07 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -package com.passus.st.emitter; - -import com.passus.net.MACAddress; -import com.passus.st.Pair; -import org.apache.commons.lang3.StringUtils; - -import java.text.ParseException; -import java.util.HashMap; -import java.util.Map; - -public class MapBasedMACAddressAddress implements MACAddressMapper { - - private static final String RULE_SEPARATOR = "->"; - - public static final MACAddress ALL_MAC = new MACAddress(new byte[]{0, 0, 0, 0, 0, 0}); - - private final Map<MACAddress, MACAddress> macMap = new HashMap<>(); - - @Override - public MACAddress map(MACAddress mac) { - return macMap.get(mac); - } - - public static Pair<MACAddress, MACAddress> parseRule(String rule) throws ParseException { - String[] parts = StringUtils.splitByWholeSeparator(rule, RULE_SEPARATOR); - if (parts.length != 2) { - throw new ParseException("Invalid rule.", 0); - } - - MACAddress srcMac; - String macStr = parts[0].trim(); - if ("*".equals(macStr)) { - srcMac = ALL_MAC; - } else { - try { - srcMac = new MACAddress(macStr); - } catch (Exception e) { - throw new ParseException("Invalid rule.", 0); - } - } - - MACAddress dstMac; - macStr = parts[1].trim(); - try { - dstMac = new MACAddress(macStr); - } catch (Exception e) { - throw new ParseException("Invalid rule.", 0); - } - - return new Pair<>(srcMac, dstMac); - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/emitter/MapBasedMACAddressResolver.java Thu Oct 03 14:49:05 2019 +0200 @@ -0,0 +1,85 @@ +package com.passus.st.emitter; + +import com.passus.commons.Assert; +import com.passus.net.Ip4Address; +import com.passus.net.IpAddress; +import com.passus.net.MACAddress; +import com.passus.st.Pair; +import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import org.apache.commons.lang3.StringUtils; + +import java.text.ParseException; + +public class MapBasedMACAddressResolver implements MACAddressResolver { + + private static final String RULE_SEPARATOR = "->"; + + public static final IpAddress ALL_IP = Ip4Address.EMPTY_ADDRESS; + + private final Int2ObjectMap<MACAddress> ip4Map = new Int2ObjectArrayMap<>(); + + private MACAddress allCatchMac; + + public void add(String rule) throws ParseException { + Pair<IpAddress, MACAddress> pair = parseRule(rule); + add(pair.getValue1(), pair.getValue2()); + } + + public void add(IpAddress ip, MACAddress mac) { + Assert.notNull(ip, "ip"); + Assert.notNull(mac, "mac"); + if (ip.getVersion() == IpAddress.IP4) { + int intVal = ((Ip4Address) ip).getIntValue(); + if (ALL_IP.equals(ip)) { + allCatchMac = mac; + } else { + ip4Map.put(intVal, mac); + } + } else { + throw new IllegalArgumentException("IPv6 not supported yet."); + } + } + + @Override + public MACAddress resolve(IpAddress ip) { + if (allCatchMac != null) { + return allCatchMac; + } + + if (ip.getVersion() == IpAddress.IP4) { + return ip4Map.get(((Ip4Address) ip).getIntValue()); + } else { + return null; + } + } + + public static Pair<IpAddress, MACAddress> parseRule(String rule) throws ParseException { + String[] parts = StringUtils.splitByWholeSeparator(rule, RULE_SEPARATOR); + if (parts.length != 2) { + throw new ParseException("Invalid rule.", 0); + } + + IpAddress ip; + String ipStr = parts[0].trim(); + if ("*".equals(ipStr)) { + ip = ALL_IP; + } else { + try { + ip = IpAddress.parse(ipStr); + } catch (Exception e) { + throw new ParseException("Invalid rule. Invalid IP address.", 0); + } + } + + MACAddress mac; + ipStr = parts[1].trim(); + try { + mac = new MACAddress(ipStr); + } catch (Exception e) { + throw new ParseException("Invalid rule.", 0); + } + + return new Pair<>(ip, mac); + } +}
--- a/stress-tester/src/main/java/com/passus/st/emitter/PassThroughMACAddressMapper.java Mon Sep 30 14:17:07 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ -package com.passus.st.emitter; - -import com.passus.net.MACAddress; - -public class PassThroughMACAddressMapper implements MACAddressMapper { - - @Override - public MACAddress map(MACAddress mac) { - return mac; - } - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/emitter/StaticMACAddressResolver.java Thu Oct 03 14:49:05 2019 +0200 @@ -0,0 +1,19 @@ +package com.passus.st.emitter; + +import com.passus.net.IpAddress; +import com.passus.net.MACAddress; + +public class StaticMACAddressResolver implements MACAddressResolver { + + private final MACAddress mac; + + public StaticMACAddressResolver(MACAddress mac) { + this.mac = mac; + } + + @Override + public MACAddress resolve(IpAddress ip) { + return mac; + } + +}
--- a/stress-tester/src/test/java/com/passus/st/emitter/MapBasedMACAddressAddressTest.java Mon Sep 30 14:17:07 2019 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -package com.passus.st.emitter; - -import com.passus.net.MACAddress; -import com.passus.st.Pair; -import org.testng.annotations.Test; - -import java.text.ParseException; - -import static com.passus.st.emitter.MapBasedMACAddressAddress.parseRule; -import static org.testng.Assert.assertEquals; - -public class MapBasedMACAddressAddressTest { - - private void assertRule(String rule, MACAddress srcMac, MACAddress dstMac) throws ParseException { - Pair<MACAddress, MACAddress> pair = parseRule(rule); - assertEquals(pair.getValue1(), srcMac); - assertEquals(pair.getValue2(), dstMac); - } - - @Test - public void testParseRule() throws ParseException { - MACAddress mac1 = new MACAddress("4C-4C-4C-4C-4C-4C"); - MACAddress mac2 = new MACAddress("4E-4E-4E-4E-4E-4E"); - assertRule("4C-4C-4C-4C-4C-4C -> 4E-4E-4E-4E-4E-4E", mac1, mac2); - assertRule("4C:4C:4C:4C:4C:4C -> 4E-4E-4E-4E-4E-4E", mac1, mac2); - assertRule("* -> 4E-4E-4E-4E-4E-4E", MapBasedMACAddressAddress.ALL_MAC, mac2); - } -} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/emitter/MapBasedMACAddressResolverTest.java Thu Oct 03 14:49:05 2019 +0200 @@ -0,0 +1,55 @@ +package com.passus.st.emitter; + +import com.passus.net.Ip4Address; +import com.passus.net.IpAddress; +import com.passus.net.MACAddress; +import com.passus.st.Pair; +import org.testng.annotations.Test; + +import java.text.ParseException; + +import static com.passus.st.emitter.MapBasedMACAddressResolver.parseRule; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertNull; + +public class MapBasedMACAddressResolverTest { + + private void assertParseRule(String rule, IpAddress ip, MACAddress mac) throws ParseException { + Pair<IpAddress, MACAddress> pair = parseRule(rule); + assertEquals(pair.getValue1(), ip); + assertEquals(pair.getValue2(), mac); + } + + @Test + public void testParseRule() throws ParseException { + IpAddress ip = new Ip4Address("1.1.1.1"); + MACAddress mac = new MACAddress("4E-4E-4E-4E-4E-4E"); + + assertParseRule("1.1.1.1 -> 4E-4E-4E-4E-4E-4E", ip, mac); + assertParseRule("1.1.1.1 -> 4E:4E:4E:4E:4E:4E", ip, mac); + assertParseRule("* -> 4E-4E-4E-4E-4E-4E", MapBasedMACAddressResolver.ALL_IP, mac); + } + + @Test + public void testResolve() throws ParseException { + MapBasedMACAddressResolver resolver = new MapBasedMACAddressResolver(); + IpAddress ip = new Ip4Address("1.1.1.1"); + MACAddress mac = new MACAddress("4E-4E-4E-4E-4E-4E"); + + resolver.add(ip + " -> " + mac); + assertEquals(mac, resolver.resolve(ip)); + assertEquals(mac, resolver.resolve(ip.toString())); + assertNull(resolver.resolve("2.2.2.2")); + } + + @Test + public void testResolveAllMatchRule() throws ParseException { + MapBasedMACAddressResolver resolver = new MapBasedMACAddressResolver(); + MACAddress mac = new MACAddress("4E-4E-4E-4E-4E-4E"); + + resolver.add("* -> " + mac); + assertEquals(mac, resolver.resolve("1.1.1.1")); + assertEquals(mac, resolver.resolve("2.2.2.2")); + assertEquals(mac, resolver.resolve("3.3.3.3")); + } +} \ No newline at end of file