changeset 839:6b4a56251b57

EventSource configuration in progress
author Devel 2
date Mon, 22 Jan 2018 12:46:19 +0100
parents 6122fb555251
children 69f9920bc532
files stress-tester/src/main/java/com/passus/st/config/EventSourceConfigurator.java stress-tester/src/main/java/com/passus/st/source/EventSource.java stress-tester/src/main/java/com/passus/st/source/EventSourceConfigurator.java stress-tester/src/main/java/com/passus/st/source/EventSourceNodeDefCreator.java stress-tester/src/main/java/com/passus/st/source/NcEventSource.java stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSourceNodeDefCreator.java stress-tester/src/test/java/com/passus/st/source/EventSourceConfiguratorTest.java
diffstat 8 files changed, 202 insertions(+), 94 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/config/EventSourceConfigurator.java	Mon Jan 22 11:41:12 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,75 +0,0 @@
-package com.passus.st.config;
-
-import com.passus.config.CCompositeNode;
-import com.passus.config.Configuration;
-import com.passus.config.ConfigurationContext;
-import com.passus.config.DomainConfigurator;
-import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
-import com.passus.config.schema.MapNodeDefinition;
-import com.passus.config.schema.NodeDefinition;
-import com.passus.config.validation.Errors;
-import com.passus.st.source.EventSourceFactory;
-import com.passus.st.source.EventSourceNodeDefCreator;
-import com.passus.st.utils.PeriodValueTransformer;
-
-/**
- *
- * @author Mirosław Hawrot
- */
-public class EventSourceConfigurator implements DomainConfigurator {
-
-    public static final String DOMAIN = "eventSource";
-
-    private final EventSourceFactory factory = EventSourceFactory.getInstance();
-
-    private final NodeDefinition eventSourceNodeDef = EventSourceNodeDefCreator.createDef();
-
-    @Override
-    public String getDomain() {
-        return DOMAIN;
-    }
-
-    private NodeDefinition createNodeDef() {
-        MapNodeDefinition cacheDef = mapDef(
-                tupleDef("enabled", valueDefBool()).setRequired(false),
-                tupleDef("maxSize", valueDefInteger()).setRequired(false)
-        );
-
-        MapNodeDefinition bufferDef = mapDef(
-                tupleDef("delayTime",
-                        valueDef(Long.class).setTransformer(PeriodValueTransformer.INSTANCE)
-                ).setRequired(false),
-                tupleDef("maxSize", valueDefInteger()).setRequired(false)
-        );
-
-        return mapDef(
-                tupleDef("cache", cacheDef).setRequired(false),
-                tupleDef("buffer", bufferDef).setRequired(false),
-                tupleDef("sources", dynaKeyValueVaryListDef("type", factory))
-        );
-    }
-
-    private void configureCache(Configuration config, ConfigurationContext context) {
-
-    }
-
-    private void configureBuffer(Configuration config, ConfigurationContext context) {
-
-    }
-
-    @Override
-    public void validate(Configuration config, Errors errors, ConfigurationContext context) {
-        CCompositeNode rootNode = config.getRootNode();
-        eventSourceNodeDef.validate(rootNode, errors,  context);
-    }
-
-    @Override
-    public void configure(Configuration config, Errors errors, ConfigurationContext context) {
-        CCompositeNode rootNode = config.getRootNode();
-        eventSourceNodeDef.transform(rootNode, errors, context);
-        if(!errors.hasError()) {
-
-        }
-    }
-
-}
--- a/stress-tester/src/main/java/com/passus/st/source/EventSource.java	Mon Jan 22 11:41:12 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/source/EventSource.java	Mon Jan 22 12:46:19 2018 +0100
@@ -11,6 +11,8 @@
  */
 public interface EventSource extends Service, Configurable {
 
+    public static final int DEFAULT_LOOPS = 1;
+
     public String getType();
 
     public String getName();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/source/EventSourceConfigurator.java	Mon Jan 22 12:46:19 2018 +0100
@@ -0,0 +1,60 @@
+package com.passus.st.source;
+
+import com.passus.config.CCompositeNode;
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.DomainConfigurator;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.validation.Errors;
+import com.passus.st.utils.ConfigurationContextConsts;
+
+import java.util.List;
+
+/**
+ * @author Mirosław Hawrot
+ */
+public class EventSourceConfigurator implements DomainConfigurator {
+
+    public static final String DOMAIN = "eventSource";
+
+    private final EventSourceFactory factory = EventSourceFactory.getInstance();
+
+    private EventSourceNodeDefCreator creator = new EventSourceNodeDefCreator();
+
+    private final NodeDefinition eventSourceNodeDef = creator.create();
+
+    @Override
+    public String getDomain() {
+        return DOMAIN;
+    }
+
+    private void configureCache(Configuration config, ConfigurationContext context) {
+
+    }
+
+    private void configureBuffer(Configuration config, ConfigurationContext context) {
+
+    }
+
+    @Override
+    public void validate(Configuration config, Errors errors, ConfigurationContext context) {
+        CCompositeNode rootNode = config.getRootNode();
+        eventSourceNodeDef.validate(rootNode, errors, context);
+    }
+
+    @Override
+    public void configure(Configuration config, Errors errors, ConfigurationContext context) {
+        CCompositeNode rootNode = config.getRootNode();
+        eventSourceNodeDef.transform(rootNode, errors, context);
+        if (!errors.hasError()) {
+            List<EventSource> sources = (List<EventSource>) config.get("sources");
+            if (sources == null || sources.isEmpty()) {
+                errors.reject("At least one event source should be defined.");
+                return;
+            }
+
+            context.add(ConfigurationContextConsts.EVENT_SOURCE_SOURCES, sources);
+        }
+    }
+
+}
--- a/stress-tester/src/main/java/com/passus/st/source/EventSourceNodeDefCreator.java	Mon Jan 22 11:41:12 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/source/EventSourceNodeDefCreator.java	Mon Jan 22 12:46:19 2018 +0100
@@ -1,30 +1,43 @@
 package com.passus.st.source;
 
+import com.passus.commons.Assert;
+import com.passus.commons.plugin.PluginFactory;
 import com.passus.config.schema.DynaKeyValueVaryListNodeDefinition;
 import com.passus.config.schema.NodeDefinition;
 import com.passus.config.schema.NodeDefinitionCreator;
-import com.passus.config.validation.LongValidator;
 import com.passus.st.utils.PeriodValueTransformer;
 
 import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
 
 public class EventSourceNodeDefCreator implements NodeDefinitionCreator {
 
-    public static NodeDefinition createEventSourcesListDef() {
+    private PluginFactory<EventSource> eventSourceFactory = EventSourceFactory.getInstance();
+
+    public PluginFactory<EventSource> getEventSourceFactory() {
+        return eventSourceFactory;
+    }
+
+    public void setEventSourceFactory(PluginFactory<EventSource> eventSourceFactory) {
+        Assert.notNull(eventSourceFactory, "eventSourceFactory");
+        this.eventSourceFactory = eventSourceFactory;
+    }
+
+    public NodeDefinition createEventSourcesListDef() {
         return createEventSourcesListDef(false);
     }
 
-    public static NodeDefinition createEventSourcesListDef(boolean transformToObject) {
-        return new DynaKeyValueVaryListNodeDefinition("type", EventSourceFactory.getInstance())
+    public NodeDefinition createEventSourcesListDef(boolean transformToObject) {
+        return new DynaKeyValueVaryListNodeDefinition("type", eventSourceFactory)
                 .setTransformToPluginObject(transformToObject);
     }
 
-    public static NodeDefinition createDef() {
+    @Override
+    public NodeDefinition create() {
         return mapDef(
-                tupleDef("cache", mapDef(
+                /*tupleDef("cache", mapDef(
                         tupleDef("enabled", valueDefBool()).setRequired(false),
                         tupleDef("maxSize", valueDefInteger().addValidator(LongValidator.GREATER_ZERO)).setRequired(false)
-                )).setRequired(false),
+                )).setRequired(false),*/
                 tupleDef("buffer", mapDef(
                         tupleDef("delayTime", valueDefInteger().setTransformer(PeriodValueTransformer.INSTANCE)).setRequired(false),
                         tupleDef("maxSize", valueDefInteger()).setRequired(false)
@@ -32,9 +45,4 @@
                 tupleDef("sources", createEventSourcesListDef(true))
         );
     }
-
-    @Override
-    public NodeDefinition create() {
-        return createDef();
-    }
 }
--- a/stress-tester/src/main/java/com/passus/st/source/NcEventSource.java	Mon Jan 22 11:41:12 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/source/NcEventSource.java	Mon Jan 22 12:46:19 2018 +0100
@@ -4,6 +4,11 @@
 import com.passus.commons.annotations.Plugin;
 import com.passus.commons.service.ServiceException;
 import com.passus.commons.utils.UniqueIdGenerator;
+import com.passus.config.Configuration;
+import com.passus.config.annotations.NodeDefinitionCreate;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
+import com.passus.config.validation.LongValidator;
 import com.passus.data.Allocator;
 import com.passus.data.ByteBuff;
 import com.passus.data.ByteBuffDataSource;
@@ -21,15 +26,18 @@
 import com.passus.st.reader.nc.NcDataBlockReader;
 import com.passus.st.reader.nc.NcSessionPayloadBlock;
 import com.passus.st.reader.nc.NcSessionStatusBlock;
-import java.io.File;
-import java.io.IOException;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+import java.io.File;
+import java.io.IOException;
+
+import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
+
 /**
- *
  * @author Mirosław Hawrot
  */
+@NodeDefinitionCreate(NcEventSource.NcEventSourceNodeDefinitionCreator.class)
 @Plugin(name = NcEventSource.TYPE, category = PluginConstants.CATEGORY_EVENT_SOURCE)
 public class NcEventSource implements EventSource {
 
@@ -83,6 +91,14 @@
         this.name = name;
     }
 
+    public String getNcFile() {
+        return ncFile;
+    }
+
+    public void setNcFile(String ncFile) {
+        this.ncFile = ncFile;
+    }
+
     public Allocator getAllocator() {
         return allocator;
     }
@@ -128,6 +144,12 @@
     }
 
     @Override
+    public void configure(Configuration config) {
+        ncFile = config.getString("fileName");
+        loops = config.getInteger("loops");
+    }
+
+    @Override
     public void start() {
         if (started) {
             return;
@@ -280,4 +302,17 @@
 
     }
 
+    public static final class NcEventSourceNodeDefinitionCreator implements NodeDefinitionCreator {
+
+        @Override
+        public NodeDefinition create() {
+            return mapDef(
+                    tupleDef("fileName", valueDef()),
+                    tupleDef("loops",
+                            valueDefInteger().addValidator(LongValidator.GREATER_EQUAL_ZERO)
+                    ).setRequired(false)
+            );
+        }
+
+    }
 }
--- a/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Mon Jan 22 11:41:12 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Mon Jan 22 12:46:19 2018 +0100
@@ -170,6 +170,15 @@
         this.loops = loops;
     }
 
+    public long getLoopDelay() {
+        return loopDelay;
+    }
+
+    public void setLoopDelay(long loopDelay) {
+        Assert.greaterOrEqualZero(loopDelay, "loopDelay");
+        this.loopDelay = loopDelay;
+    }
+
     @Override
     public EventHandler getHandler() {
         return handler;
@@ -183,7 +192,7 @@
 
     @Override
     public void configure(Configuration config) {
-        pcapFile = config.getString("pcapFileName");
+        pcapFile = config.getString("fileName");
         loops = config.getInteger("loops", 1);
         loopDelay = config.getLong("loopDelay", 0L);
     }
@@ -620,9 +629,9 @@
         @Override
         public NodeDefinition create() {
             return mapDef(
-                    tupleDef("pcapFileName", valueDef()),
+                    tupleDef("fileName", valueDef()),
                     tupleDef("loops", valueDefInteger().addValidator(LongValidator.GREATER_EQUAL_ZERO)),
-                    tupleDef("loopDelay", valueDefInteger().addValidator(LongValidator.GREATER_EQUAL_ZERO))
+                    tupleDef("loopDelay", valueDefLong().addValidator(LongValidator.GREATER_EQUAL_ZERO))
             );
         }
     }
--- a/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSourceNodeDefCreator.java	Mon Jan 22 11:41:12 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSourceNodeDefCreator.java	Mon Jan 22 12:46:19 2018 +0100
@@ -13,7 +13,7 @@
     @Override
     public NodeDefinition create() {
         return mapDef(
-                tupleDef("pcapFileName", valueDef()),
+                tupleDef("fileName", valueDef()),
                 tupleDef("loops", valueDefInteger().setDefaultValue(1)).setRequired(false),
                 tupleDef("loopDelay", valueDefInteger().setDefaultValue(200)).setRequired(false)
         );
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/source/EventSourceConfiguratorTest.java	Mon Jan 22 12:46:19 2018 +0100
@@ -0,0 +1,69 @@
+package com.passus.st.source;
+
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.ConfigurationContextImpl;
+import com.passus.config.YamlConfigurationReader;
+import com.passus.config.validation.Errors;
+import com.passus.st.utils.ConfigurationContextConsts;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.Test;
+
+import java.util.List;
+
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertFalse;
+
+public class EventSourceConfiguratorTest {
+
+    private final Errors errors = new Errors();
+
+    @AfterMethod
+    public void afterMethod() {
+        errors.clear();
+    }
+
+    @Test
+    public void testConfigure() throws Exception {
+        String configStr = "sources:\n" +
+                "   - type: pcap\n" +
+                "     fileName: /test.pcap\n" +
+                "     loops: 2\n" +
+                "     loopDelay: 3\n" +
+                "   - type: nc\n" +
+                "     fileName: /test.nc\n" +
+                "     loops: 4\n";
+        /*        "          sessionProc:\n" +
+                "              tcp:\n" +
+                "                maxStreamWindowSize: '20'\n" +
+                "                streamWindowTime: '50'\n" +
+                "                allowPartialSession: true\n" +
+                "                timeouts:\n" +
+                "                    synSent: '30000'\n" +
+                "                    synRecv: '30000'\n" +
+                "                    established: '30000'\n" +
+                "                    finWait: '20000'\n" +
+                "                    closeWait: '20000'\n" +
+                "                    lastAck: '20000'\n" +
+                "                    timeWait: '100'";*/
+
+        Configuration config = YamlConfigurationReader.readFromString(configStr);
+        EventSourceConfigurator configurator = new EventSourceConfigurator();
+
+        ConfigurationContext context = new ConfigurationContextImpl();
+        configurator.configure(config, errors, context);
+        assertFalse(errors.hasError());
+
+        List<EventSource> sources = (List<EventSource>) context.get(ConfigurationContextConsts.EVENT_SOURCE_SOURCES);
+        assertEquals(2, sources.size());
+
+        PcapSessionEventSource pcapEventSource = (PcapSessionEventSource) sources.get(0);
+        assertEquals("/test.pcap", pcapEventSource.getPcapFile());
+        assertEquals(2, pcapEventSource.getLoops());
+        assertEquals(3, pcapEventSource.getLoopDelay());
+
+        NcEventSource ncEventSource = (NcEventSource) sources.get(1);
+        assertEquals("/test.nc", ncEventSource.getNcFile());
+        assertEquals(4, ncEventSource.getLoops());
+    }
+}
\ No newline at end of file