From 4061a4c459e61b3eb22b77a5e43495d90a9dbada Mon Sep 17 00:00:00 2001 From: Steven Pisarski Date: Tue, 16 Jun 2015 14:01:50 -0600 Subject: [PATCH] Moved CMTS emulator to its own module so it can either be run standalone or be used for unit testing for other modules. Change-Id: Id090efb3bd3ef674fc927d1820ec0f0330a002b8 Signed-off-by: Steven Pisarski --- packetcable-driver/pom.xml | 1 + packetcable-emulator/conf/cmts.yaml | 24 +++++ packetcable-emulator/pom.xml | 48 +++++++++ .../src/main/java/org/pcmm/rcd/impl/CMTS.java | 101 ++++++++++++++---- packetcable-policy-server/pom.xml | 11 +- pom.xml | 3 + 6 files changed, 165 insertions(+), 23 deletions(-) create mode 100644 packetcable-emulator/conf/cmts.yaml create mode 100644 packetcable-emulator/pom.xml rename {packetcable-driver => packetcable-emulator}/src/main/java/org/pcmm/rcd/impl/CMTS.java (50%) diff --git a/packetcable-driver/pom.xml b/packetcable-driver/pom.xml index d40faae..d7bf8ac 100644 --- a/packetcable-driver/pom.xml +++ b/packetcable-driver/pom.xml @@ -62,6 +62,7 @@ junit junit + test ch.qos.logback diff --git a/packetcable-emulator/conf/cmts.yaml b/packetcable-emulator/conf/cmts.yaml new file mode 100644 index 0000000..5d9147e --- /dev/null +++ b/packetcable-emulator/conf/cmts.yaml @@ -0,0 +1,24 @@ +# The CMTS Emulator's communications port number +port: 3918 + +# The configured gates +gates: + - type: UPSTREAM + names: + - extrm_up + - foo_up + - type: DOWNSTREAM + names: + - extrm_dn + - foo_dn + +# The connected cable modem info +cmStatuses: + - host: 10.32.110.180 + status: true + - host: 10.32.110.179 + status: true + - host: 10.32.110.178 + status: true + - host: 99.99.99.99 + status: false diff --git a/packetcable-emulator/pom.xml b/packetcable-emulator/pom.xml new file mode 100644 index 0000000..7ee0597 --- /dev/null +++ b/packetcable-emulator/pom.xml @@ -0,0 +1,48 @@ + + + + 4.0.0 + + + org.opendaylight.packetcable + packetcable + 1.3.0-SNAPSHOT + + packetcable-emulator + jar + + + + + maven-assembly-plugin + + + jar-with-dependencies + + + + org.pcmm.rcd.impl.CMTS + + + + + + + + + + org.opendaylight.packetcable + packetcable-driver + 1.3.0-SNAPSHOT + compile + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + 2.5.0 + compile + + + + diff --git a/packetcable-driver/src/main/java/org/pcmm/rcd/impl/CMTS.java b/packetcable-emulator/src/main/java/org/pcmm/rcd/impl/CMTS.java similarity index 50% rename from packetcable-driver/src/main/java/org/pcmm/rcd/impl/CMTS.java rename to packetcable-emulator/src/main/java/org/pcmm/rcd/impl/CMTS.java index b3a4266..e53439e 100644 --- a/packetcable-driver/src/main/java/org/pcmm/rcd/impl/CMTS.java +++ b/packetcable-emulator/src/main/java/org/pcmm/rcd/impl/CMTS.java @@ -4,19 +4,20 @@ package org.pcmm.rcd.impl; -import org.pcmm.PCMMConstants; -import org.pcmm.PCMMProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import org.pcmm.gates.IGateSpec.Direction; import org.pcmm.rcd.ICMTS; +import java.io.FileInputStream; import java.io.IOException; -import java.net.InetAddress; import java.net.Socket; import java.util.*; import java.util.concurrent.ConcurrentHashMap; /** - * This class starts a mock CMTS that can be used for testing. + * Mock CMTS that can be used for testing. startServer() is called to start required threads after instantiation. */ public class CMTS extends AbstractPCMMServer implements ICMTS { @@ -80,23 +81,83 @@ public class CMTS extends AbstractPCMMServer implements ICMTS { * @throws IOException - should the server fail to start for reasons such as port contention. */ public static void main(final String[] args) throws IOException { - final Set upGates = new HashSet<>(); - upGates.add("extrm_up"); - final Set dnGates = new HashSet<>(); - dnGates.add("extrm_dn"); - final Map> gates = new HashMap<>(); - gates.put(Direction.UPSTREAM, upGates); - gates.put(Direction.DOWNSTREAM, dnGates); - - final Map cmStatus = new HashMap<>(); - final InetAddress invalidCmAddrInet = InetAddress.getByAddress(new byte[] {99, 99, 99, 99}); - cmStatus.put(InetAddress.getByAddress(new byte[]{10, 32, 110, (byte) 180}).getHostAddress(), true); - cmStatus.put(InetAddress.getByAddress(new byte[]{10, 32, 110, (byte) 179}).getHostAddress(), true); - cmStatus.put(InetAddress.getByAddress(new byte[]{10, 32, 110, (byte) 178}).getHostAddress(), true); - cmStatus.put(invalidCmAddrInet.getHostAddress(), false); - - final CMTS cmts = new CMTS(PCMMProperties.get(PCMMConstants.PCMM_PORT, Integer.class), gates, cmStatus); + final CmtsYaml config = getConfig(args[0]); + final CMTS cmts = new CMTS(config.port, config.getGates(), config.getCmStatus()); cmts.startServer(); } + /** + * Returns the object that represents the YAML file + * @param uri - the location of the YAML file + * @return - the config object + * @throws IOException - when the URI does not contain the proper YAML file + */ + private static CmtsYaml getConfig(final String uri) throws IOException { + final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + return mapper.readValue(new FileInputStream(uri), CmtsYaml.class); + } + + /** + * Class to hold configuration settings in a YAML file + */ + public static class CmtsYaml { + @JsonProperty("port") + private int port; + + @JsonProperty("gates") + private Collection gateConfigs; + + @JsonProperty("cmStatuses") + private Collection cmStatuses; + + public Map> getGates() { + final Map> out = new HashMap<>(); + + for (final GateConfigYaml gateConfig : gateConfigs) { + final Direction direction; + if (gateConfig.gateType.equalsIgnoreCase("UPSTREAM")) { + direction = Direction.UPSTREAM; + } else if (gateConfig.gateType.equalsIgnoreCase("DOWNSTREAM")) { + direction = Direction.DOWNSTREAM; + } else direction = null; + + if (direction != null) { + out.put(direction, gateConfig.gateNames); + } + } + return out; + } + + public Map getCmStatus() { + final Map out = new HashMap<>(); + + for (final CmStatusYaml cmStatus : cmStatuses) { + out.put(cmStatus.hostIp, cmStatus.status); + } + return out; + } + } + + /** + * Class to hold the YAML gate configuration values + */ + public static class GateConfigYaml { + @JsonProperty("type") + private String gateType; + + @JsonProperty("names") + private Set gateNames; + } + + /** + * Class to hold the YAML Cable Modem configuration values + */ + public static class CmStatusYaml { + @JsonProperty("host") + private String hostIp; + + @JsonProperty("status") + private boolean status; + } + } diff --git a/packetcable-policy-server/pom.xml b/packetcable-policy-server/pom.xml index e2a806d..a301112 100644 --- a/packetcable-policy-server/pom.xml +++ b/packetcable-policy-server/pom.xml @@ -59,10 +59,15 @@ junit test - - + + org.opendaylight.packetcable + packetcable-emulator + 1.3.0-SNAPSHOT + test + + org.mockito - mockito-core + mockito-core test diff --git a/pom.xml b/pom.xml index 24da768..70e8302 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,9 @@ features-packetcable-policy packetcable-policy-config packetcable-policy-karaf + + + packetcable-emulator scm:git:ssh://git.opendaylight.org:29418/packetcable.git -- 2.36.6