changeset 1071:06fc08a2d7d5

MACAddressResolverNodeDefinitionCreator
author Devel 2
date Tue, 28 Apr 2020 09:57:36 +0200
parents a87e4e140075
children 0cafeaef7e1f
files stress-tester/src/main/java/com/passus/st/emitter/MACAddressResolverNodeDefinitionCreator.java stress-tester/src/main/java/com/passus/st/emitter/MapBasedMACAddressResolver.java stress-tester/src/main/java/com/passus/st/emitter/MapBasedMACAddressResolverValueTransformer.java stress-tester/src/test/java/com/passus/st/emitter/MACAddressResolverNodeDefinitionCreatorTest.java
diffstat 4 files changed, 134 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/emitter/MACAddressResolverNodeDefinitionCreator.java	Tue Apr 28 09:57:36 2020 +0200
@@ -0,0 +1,22 @@
+package com.passus.st.emitter;
+
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
+
+import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
+
+public class MACAddressResolverNodeDefinitionCreator implements NodeDefinitionCreator {
+
+    public static NodeDefinition createDef() {
+        return mixedDef(
+                valueDef().setTransformer(MapBasedMACAddressResolverValueTransformer.INSTANCE),
+                listDef().setTransformer(MapBasedMACAddressResolverValueTransformer.INSTANCE)
+        );
+    }
+
+    @Override
+    public NodeDefinition create() {
+        return createDef();
+    }
+
+}
--- a/stress-tester/src/main/java/com/passus/st/emitter/MapBasedMACAddressResolver.java	Tue Apr 28 09:57:07 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/MapBasedMACAddressResolver.java	Tue Apr 28 09:57:36 2020 +0200
@@ -4,12 +4,15 @@
 import com.passus.net.Ip4Address;
 import com.passus.net.IpAddress;
 import com.passus.net.MACAddress;
+import com.passus.net.utils.AddressUtils;
 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;
+import java.util.ArrayList;
+import java.util.List;
 
 public class MapBasedMACAddressResolver implements MACAddressResolver {
 
@@ -41,6 +44,15 @@
         }
     }
 
+    public List<Pair<IpAddress, MACAddress>> getAll() {
+        List<Pair<IpAddress, MACAddress>> out = new ArrayList<>();
+        ip4Map.forEach((ipInt, mac) -> {
+            IpAddress ip = AddressUtils.getIpAddress(new int[]{ipInt});
+            out.add(new Pair<>(ip, mac));
+        });
+        return out;
+    }
+
     @Override
     public MACAddress resolve(IpAddress ip) {
         if (allCatchMac != null) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/emitter/MapBasedMACAddressResolverValueTransformer.java	Tue Apr 28 09:57:36 2020 +0200
@@ -0,0 +1,49 @@
+package com.passus.st.emitter;
+
+import com.passus.config.ConfigurationContext;
+import com.passus.config.ValueTransformer;
+import com.passus.config.validation.Errors;
+
+import java.util.List;
+
+public class MapBasedMACAddressResolverValueTransformer implements ValueTransformer {
+
+    public static final MapBasedMACAddressResolverValueTransformer INSTANCE = new MapBasedMACAddressResolverValueTransformer();
+
+    @Override
+    public Object transform(Object value, Errors errors, ConfigurationContext context) {
+        if (value == null) {
+            return null;
+        }
+
+        MapBasedMACAddressResolver resolver = new MapBasedMACAddressResolver();
+        if (value instanceof List) {
+            List list = (List) value;
+            for (Object obj : list) {
+                String str = obj.toString();
+                try {
+                    resolver.add(str);
+                } catch (Exception e) {
+                    errors.reject("Unable to convert '" + str + "' to session mapper rule. " + e.getMessage());
+                    return null;
+                }
+            }
+        } else {
+            String str = value.toString();
+            try {
+                resolver.add(str);
+            } catch (Exception e) {
+                errors.reject("Unable to convert '" + str + "' to session mapper rule. " + e.getMessage());
+                return null;
+            }
+        }
+
+        return resolver;
+    }
+
+    @Override
+    public Object reverseTransform(Object obj, Errors errors, ConfigurationContext context) {
+        throw new RuntimeException("Not implemented.");
+    }
+
+}
\ 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/MACAddressResolverNodeDefinitionCreatorTest.java	Tue Apr 28 09:57:36 2020 +0200
@@ -0,0 +1,51 @@
+package com.passus.st.emitter;
+
+import com.passus.config.*;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.validation.Errors;
+import com.passus.net.Ip4Address;
+import com.passus.net.IpAddress;
+import com.passus.net.MACAddress;
+import com.passus.st.Pair;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import java.util.List;
+
+import static org.testng.AssertJUnit.*;
+
+public class MACAddressResolverNodeDefinitionCreatorTest {
+
+    private final MACAddressResolverNodeDefinitionCreator creator = new MACAddressResolverNodeDefinitionCreator();
+
+    private final NodeDefinition nodeDef = creator.create();
+
+    @DataProvider(name = "validRules")
+    public Object[][] validRules() {
+        return new Object[][]{
+                {"macResolver: 1.1.1.1 -> 4E-4E-4E-4E-4E-4E", 1},
+                {"macResolver: ['1.1.1.1 -> 4E-4E-4E-4E-4E-4E', '2.2.2.2 -> 4A-4A-4A-4A-4A-4A']", 2}
+        };
+    }
+
+    @Test(dataProvider = "validRules")
+    public void testTransform_MapBasedMACAddressResolver(String configStr, int rulesNum) throws Exception {
+        Configuration config = YamlConfigurationReader.readFromString(configStr);
+
+        CMapNode rootNode = (CMapNode) config.getRootNode();
+        Errors errors = new Errors();
+        ConfigurationContext context = new ConfigurationContextImpl();
+        Object res = nodeDef.transformToObject(rootNode.getNode("macResolver"), errors, context);
+        assertFalse(errors.hasError());
+
+        assertTrue(res instanceof MapBasedMACAddressResolver);
+        MapBasedMACAddressResolver mapResolver = (MapBasedMACAddressResolver) res;
+        List<Pair<IpAddress, MACAddress>> pairs = mapResolver.getAll();
+        assertEquals(rulesNum, pairs.size());
+
+        Pair<IpAddress, MACAddress> pair0 = pairs.get(0);
+        assertEquals(new Ip4Address("1.1.1.1"), pair0.getValue1());
+        assertEquals(new MACAddress("4E-4E-4E-4E-4E-4E"), pair0.getValue2());
+    }
+
+}