From b19de48103893e26dc1b37b5411a872afa1e2ec8 Mon Sep 17 00:00:00 2001 From: haniotak Date: Tue, 29 Oct 2024 17:52:58 -0700 Subject: [PATCH] migrate files to github --- build-and-run.sh | 2 + input/data.json | 69 +++++++++++++++++++++++ input/garbage.json | 1 + input/noncompliant.json | 3 + pom.xml | 97 +++++++++++++++++++++++++++++++++ src/main/java/EthernetPort.java | 21 +++++++ src/main/java/IntRange.java | 20 +++++++ src/main/java/Main.java | 77 ++++++++++++++++++++++++++ src/main/java/Router.java | 21 +++++++ src/main/java/Utility.java | 30 ++++++++++ src/main/java/Vlan.java | 23 ++++++++ 11 files changed, 364 insertions(+) create mode 100755 build-and-run.sh create mode 100644 input/data.json create mode 100644 input/garbage.json create mode 100644 input/noncompliant.json create mode 100644 pom.xml create mode 100644 src/main/java/EthernetPort.java create mode 100644 src/main/java/IntRange.java create mode 100644 src/main/java/Main.java create mode 100644 src/main/java/Router.java create mode 100644 src/main/java/Utility.java create mode 100644 src/main/java/Vlan.java diff --git a/build-and-run.sh b/build-and-run.sh new file mode 100755 index 0000000..da104d4 --- /dev/null +++ b/build-and-run.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +mvn package exec:java \ No newline at end of file diff --git a/input/data.json b/input/data.json new file mode 100644 index 0000000..7c671a5 --- /dev/null +++ b/input/data.json @@ -0,0 +1,69 @@ +[ + { + "name": "alpha", + "ports": [ + { + "name": "ge-1/1/0", + "vlans": [ + { + "vlanId": 10, + "customer": 1 + }, + { + "vlanId": 20, + "customer": 2 + } + ] + } + ] + }, + { + "name": "beta", + "ports": [ + { + "name": "ge-2/1/0", + "vlans": [ + { + "vlanId": 50, + "customer": 3 + }, + { + "vlanId": 201, + "customer": 3 + } + ] + }, + { + "name": "ge-3/1/0", + "vlans": [ + { + "vlanId": 102, + "customer": 3 + }, + { + "vlanId": 105, + "customer": 1 + } + ] + } + ] + }, + { + "name": "gamma", + "ports": [ + { + "name": "ge-2/1/0", + "vlans": [ + { + "vlanId": 1331, + "customer": 1 + }, + { + "vlanId": 401, + "customer": 2 + } + ] + } + ] + } +] \ No newline at end of file diff --git a/input/garbage.json b/input/garbage.json new file mode 100644 index 0000000..9b4a83e --- /dev/null +++ b/input/garbage.json @@ -0,0 +1 @@ +I'm not even a json file at all. \ No newline at end of file diff --git a/input/noncompliant.json b/input/noncompliant.json new file mode 100644 index 0000000..70d625a --- /dev/null +++ b/input/noncompliant.json @@ -0,0 +1,3 @@ +{ + "does this file deserialize to a Router[]?": false +} \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4d8da2d --- /dev/null +++ b/pom.xml @@ -0,0 +1,97 @@ + + + 4.0.0 + + + net.es + java-code-assessment + 1.0-SNAPSHOT + + 21 + 21 + UTF-8 + + + + + org.apache.commons + commons-lang3 + 3.14.0 + + + + ch.qos.logback + logback-classic + 1.5.2 + + + + ch.qos.logback + logback-core + 1.5.2 + + + org.projectlombok + lombok + 1.18.32 + provided + + + com.fasterxml.jackson.core + jackson-databind + 2.18.0 + + + + org.slf4j + slf4j-api + 2.0.12 + + + org.junit.jupiter + junit-jupiter-engine + 5.7.0 + test + + + + + + + + org.codehaus.mojo + exec-maven-plugin + 3.5.0 + + + + + java + + + + + + Main + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + + + org.projectlombok + lombok + 1.18.32 + + + + + + + + \ No newline at end of file diff --git a/src/main/java/EthernetPort.java b/src/main/java/EthernetPort.java new file mode 100644 index 0000000..da93472 --- /dev/null +++ b/src/main/java/EthernetPort.java @@ -0,0 +1,21 @@ +import lombok.*; +import java.util.*; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +/* + * Holds data about a particular Ethernet Port and all the Vlans that exist on it + */ +public class EthernetPort { + String name; + List vlans; + + /* + * the @Data annotation provides compile-time generation of + * getters, setters, equals and hashCode (among others) + */ + + +} \ No newline at end of file diff --git a/src/main/java/IntRange.java b/src/main/java/IntRange.java new file mode 100644 index 0000000..9d9b7f7 --- /dev/null +++ b/src/main/java/IntRange.java @@ -0,0 +1,20 @@ +import lombok.*; + + +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Data +public class IntRange { + private int floor; + private int ceiling; + + + /* + * the @Data annotation provides compile-time generation of + * getters, setters, equals and hashCode (among others) + */ + + + +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..3993d44 --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,77 @@ + +import com.fasterxml.jackson.databind.ObjectMapper; + +import java.io.FileInputStream; +import java.io.InputStream; + +public class Main { + + public void run() { + try { + InputStream is = new FileInputStream("input/data.json"); + + Router[] routers = new ObjectMapper().readValue(is, Router[].class); + for (Router r : routers) { + System.out.println(r.getName()); + } + } catch (Exception ex) { + System.out.println("An exception!" + ex.getMessage()); + } + + /* + ==================================================================== + Task 1 + ==================================================================== + Instead of printing a list of router names, print out a report by + customers, including all the router / port / vlan ids they are associated with: + + customer 1: + - alpha : ge-1/1/0 : 10 + - beta : ge-2/1/0 : 103 + - beta : ge-2/1/0 : 105 + + customer 2: + ... + ==================================================================== + */ + + + + + /* + ==================================================================== + Task 2 + ==================================================================== + Print out a report of all unused VLAN ids by router and port in this format: + alpha: + - ge-1/1/0: 0 to 9, 11 to 19, 21 to 4095 + etc + + ==================================================================== + */ + + + + /* + ==================================================================== + Task 3 + ==================================================================== + Generalize this program to use ALL .json files in a directory + as input (instead of just specifically input/data.json). + + You may skip any files that cannot be deserialized to Router[], + generating a warning message to the console. + ==================================================================== + */ + + } + + + + public static void main(String[] args) { + Main main = new Main(); + main.run(); + } + + +} \ No newline at end of file diff --git a/src/main/java/Router.java b/src/main/java/Router.java new file mode 100644 index 0000000..704aeb1 --- /dev/null +++ b/src/main/java/Router.java @@ -0,0 +1,21 @@ +import lombok.*; +import java.util.*; + +/* + * holds data about a particular Router device all the EthernetPorts that exist on it + */ + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class Router { + String name; + List ports; + + /* + * the @Data annotation provides compile-time generation of + * getters, setters, equals and hashCode (among others) + */ + +} \ No newline at end of file diff --git a/src/main/java/Utility.java b/src/main/java/Utility.java new file mode 100644 index 0000000..9153f7e --- /dev/null +++ b/src/main/java/Utility.java @@ -0,0 +1,30 @@ +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; + +public class Utility { + + /* + * this takes a collection of IntRange objects and returns a customizable + * human-readable representation. Candidate may modify it, but that should + * not be necessary. + * + * sample retvalue (separator = " to ", rangeseparator = ", ") + * "-2 to 30, 43 to 500, 121 to 122" + */ + public static String asString(Collection ranges, String separator, String rangeSeparator) { + List listOfRanges = new ArrayList<>(ranges); + listOfRanges.sort(Comparator.comparing(IntRange::getFloor)); + + List parts = new ArrayList<>(); + listOfRanges.forEach(r -> { + if (r.getCeiling() == r.getFloor()) { + parts.add(r.getCeiling() + ""); + } else { + parts.add(r.getFloor() + separator + r.getCeiling()); + } + }); + return String.join(rangeSeparator, parts); + } +} diff --git a/src/main/java/Vlan.java b/src/main/java/Vlan.java new file mode 100644 index 0000000..7f13fdb --- /dev/null +++ b/src/main/java/Vlan.java @@ -0,0 +1,23 @@ +import lombok.*; + + +/* + holds data about a particular Vlan, specifically the vlan-id (0-4095) and the customer id + associated with it. The customer id is an opaque int, guaranteed to be the same everywehre + for a particular customer. + */ + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class Vlan { + int vlanId; + int customer; + + /* + * the @Data annotation provides compile-time generation of + * getters, setters, equals and hashCode (among others) + */ + +} \ No newline at end of file