Tests for network maps and cost maps 31/25331/1
authorKai GAO <gaok12@mails.tsinghua.edu.cn>
Sat, 15 Aug 2015 02:40:24 +0000 (10:40 +0800)
committerKai GAO <gaok12@mails.tsinghua.edu.cn>
Sat, 15 Aug 2015 02:40:24 +0000 (10:40 +0800)
Change-Id: Ib452646f38cd500aba207b245192fb1d34c2289a
Signed-off-by: Kai GAO <gaok12@mails.tsinghua.edu.cn>
alto-commons/pom.xml
alto-commons/src/main/java/org/opendaylight/alto/commons/types/rfc7285/RFC7285CostMap.java
alto-commons/src/main/java/org/opendaylight/alto/commons/types/rfc7285/RFC7285IRD.java
alto-commons/src/main/java/org/opendaylight/alto/commons/types/rfc7285/RFC7285JSONMapper.java
alto-commons/src/test/java/org/opendaylight/alto/commons/types/rfc7285/TestRFC7285Types.java [new file with mode: 0644]

index 17e75d5525733a219556018950c7e7326f6ee85b..eaadcfe42e14560ba89524381dab05665d97d17c 100644 (file)
   </build>
 
   <dependencies>
+    <dependency>
+      <artifactId>junit</artifactId>
+      <groupId>junit</groupId>
+    </dependency>
+
     <dependency>
       <groupId>${project.groupId}</groupId>
       <artifactId>alto-model</artifactId>
index 176f8689dcc9a7083de3b483ad6c86e15796d15c..1d7bc5ea903ae0b4d840da954a8fff28433888a4 100644 (file)
@@ -38,7 +38,7 @@ public class RFC7285CostMap {
         public RFC7285CostType costType;
 
         @JsonProperty("pids")
-        public RFC7285QueryPairs pids;
+        public RFC7285QueryPairs pids = new RFC7285QueryPairs();
     }
 
     @JsonProperty("meta")
index 7364400c0e0d510d86214c77432f79f071ae68b4..4db1ea1d18e14f33e275696d8eb8bc80776316f3 100644 (file)
@@ -19,15 +19,10 @@ public class RFC7285IRD {
     public class Meta extends Extensible {
 
         @JsonProperty("default-alto-network-map")
-        public String defaultAltoNetworkMap;
+        public String defaultAltoNetworkMap = "";
 
         @JsonProperty("cost-types")
-        public Map<String, RFC7285CostType> costTypes;
-
-        public Meta() {
-            defaultAltoNetworkMap = null;
-            costTypes = new LinkedHashMap<String, RFC7285CostType>();
-        }
+        public Map<String, RFC7285CostType> costTypes = new LinkedHashMap<String, RFC7285CostType>();
 
     }
 
@@ -49,7 +44,7 @@ public class RFC7285IRD {
     }
 
     @JsonProperty("meta")
-    public Meta meta;
+    public Meta meta = new Meta();
 
     @JsonProperty("resources")
     public Map<String, Entry> resources = new LinkedHashMap<String, Entry>();
index 0a6bd2adbff5bebbb63f188ac4445bda5368c877..8adea966d9c9d1bd7fe235184df6a0362dd45967 100644 (file)
@@ -11,9 +11,14 @@ package org.opendaylight.alto.commons.types.rfc7285;
 import java.util.Arrays;
 import java.util.List;
 
+import java.io.IOException;
+
+import com.fasterxml.jackson.core.JsonParseException;
+
 import com.fasterxml.jackson.annotation.JsonInclude.Include;
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.JsonMappingException;
 
 public class RFC7285JSONMapper {
 
@@ -21,75 +26,93 @@ public class RFC7285JSONMapper {
                             .setSerializationInclusion(Include.NON_DEFAULT)
                             .disable(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES);
 
-    public RFC7285Endpoint.AddressGroup asAddressGroup(String json) throws Exception {
+    public RFC7285Endpoint.AddressGroup asAddressGroup(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285Endpoint.AddressGroup.class);
     }
 
-    public RFC7285Endpoint.PropertyRequest asPropertyRequest(String json) throws Exception {
+    public RFC7285Endpoint.PropertyRequest asPropertyRequest(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285Endpoint.PropertyRequest.class);
     }
 
-    public RFC7285Endpoint.PropertyResponse asPropertyResponse(String json) throws Exception {
+    public RFC7285Endpoint.PropertyResponse asPropertyResponse(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285Endpoint.PropertyResponse.class);
     }
 
-    public RFC7285Endpoint.CostRequest asCostRequest(String json) throws Exception {
+    public RFC7285Endpoint.CostRequest asCostRequest(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285Endpoint.CostRequest.class);
     }
 
-    public RFC7285Endpoint.CostResponse asCostResponse(String json) throws Exception {
+    public RFC7285Endpoint.CostResponse asCostResponse(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285Endpoint.CostResponse.class);
     }
 
-    public RFC7285CostMap asCostMap(String json) throws Exception {
+    public RFC7285CostMap asCostMap(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285CostMap.class);
     }
 
-    public List<RFC7285CostMap> asCostMapList(String json) throws Exception {
+    public List<RFC7285CostMap> asCostMapList(String json)
+            throws IOException, JsonParseException, JsonMappingException {
       return Arrays.asList(mapper.readValue(json, RFC7285CostMap[].class));
     }
 
-    public RFC7285CostType asCostType(String json) throws Exception {
+    public RFC7285CostType asCostType(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285CostType.class);
     }
 
-    public RFC7285Endpoint asEndpoint(String json) throws Exception {
+    public RFC7285Endpoint asEndpoint(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285Endpoint.class);
     }
 
-    public Extensible asExtensible(String json) throws Exception {
+    public Extensible asExtensible(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, Extensible.class);
     }
 
-    public RFC7285IRD asIRD(String json) throws Exception {
+    public RFC7285IRD asIRD(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285IRD.class);
     }
 
-    public RFC7285NetworkMap asNetworkMap(String json) throws Exception {
+    public RFC7285NetworkMap asNetworkMap(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285NetworkMap.class);
     }
 
-    public List<RFC7285NetworkMap> asNetworkMapList(String json) throws Exception {
+    public List<RFC7285NetworkMap> asNetworkMapList(String json)
+            throws IOException, JsonParseException, JsonMappingException {
       return Arrays.asList(mapper.readValue(json, RFC7285NetworkMap[].class));
     }
 
-    public RFC7285NetworkMap.Filter asNetworkMapFilter(String json) throws Exception {
+    public RFC7285NetworkMap.Filter asNetworkMapFilter(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285NetworkMap.Filter.class);
     }
 
-    public RFC7285CostMap.Filter asCostMapFilter(String json) throws Exception {
+    public RFC7285CostMap.Filter asCostMapFilter(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285CostMap.Filter.class);
     }
 
-    public RFC7285VersionTag asVersionTag(String json) throws Exception {
+    public RFC7285VersionTag asVersionTag(String json)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.readValue(json, RFC7285VersionTag.class);
     }
 
-    public RFC7285EndpointPropertyMap asEndpointPropMap(String json) throws Exception {
+    public RFC7285EndpointPropertyMap asEndpointPropMap(String json)
+            throws IOException, JsonParseException, JsonMappingException {
       return mapper.readValue(json, RFC7285EndpointPropertyMap.class);
   }
 
-    public String asJSON(Object obj) throws Exception {
+    public String asJSON(Object obj)
+            throws IOException, JsonParseException, JsonMappingException {
         return mapper.writeValueAsString(obj);
     }
 }
diff --git a/alto-commons/src/test/java/org/opendaylight/alto/commons/types/rfc7285/TestRFC7285Types.java b/alto-commons/src/test/java/org/opendaylight/alto/commons/types/rfc7285/TestRFC7285Types.java
new file mode 100644 (file)
index 0000000..33939e7
--- /dev/null
@@ -0,0 +1,301 @@
+package org.opendaylight.alto.commons.types.rfc7285;
+
+import java.util.Collection;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class TestRFC7285Types {
+
+    public RFC7285NetworkMap makeNetworkMap() {
+        /*
+         *
+         *      {
+         *          "meta" : {
+         *              "vtag": {
+         *                  "resource-id": "my-default-network-map",
+         *                  "tag": "da65eca2eb7a10ce8b059740b0b2e3f8eb1d4785"
+         *              }
+         *          },
+         *          "network-map": {
+         *              "PID1" : {
+         *                  "ipv4" : [
+         *                      "192.0.2.0/24",
+         *                      "198.51.100.0/25"
+         *                  ]
+         *              },
+         *              "PID2" : {
+         *                  "ipv4" : [
+         *                      "198.51.100.128/25"
+         *                  ]
+         *              },
+         *              "PID3" : {
+         *                  "ipv4" : [
+         *                      "0.0.0.0/0"
+         *                  ],
+         *                  "ipv6" : [
+         *                      "::/0"
+         *                  ]
+         *              }
+         *          }
+         *      }
+         *
+         * */
+        RFC7285NetworkMap nm = new RFC7285NetworkMap();
+        nm.meta.vtag = new RFC7285VersionTag("my-default-network-map",
+                                             "da65eca2eb7a10ce8b059740b0b2e3f8eb1d4785");
+
+        nm.map.put("PID1", new RFC7285Endpoint.AddressGroup());
+        nm.map.get("PID1").ipv4.add("192.0.2.0/24");
+        nm.map.get("PID1").ipv4.add("198.51.100.0/25");
+
+        nm.map.put("PID2", new RFC7285Endpoint.AddressGroup());
+        nm.map.get("PID2").ipv4.add("198.51.100.128/25");
+
+        nm.map.put("PID3", new RFC7285Endpoint.AddressGroup());
+        nm.map.get("PID3").ipv4.add("0.0.0.0/0");
+        nm.map.get("PID3").ipv6.add("::/0");
+
+        return nm;
+    }
+
+    @Test
+    public void test() {
+    }
+
+    public <T> void assertCollectionEquals(Collection<T> lhs, Collection<T> rhs) {
+        Set<T> _lhs = new HashSet<T>(lhs);
+        Set<T> _rhs = new HashSet<T>(rhs);
+        assertEquals(lhs.size(), rhs.size());
+
+        for (T obj: lhs) {
+            assertTrue(_rhs.contains(obj));
+        }
+    }
+
+    @Test
+    public void testNetworkMap() throws Exception {
+        RFC7285JSONMapper mapper = new RFC7285JSONMapper();
+
+        RFC7285NetworkMap nm = makeNetworkMap();
+        String nmText = mapper.asJSON(nm);
+        RFC7285NetworkMap _nm = mapper.asNetworkMap(nmText);
+
+        assertEquals(nm.meta.vtag, _nm.meta.vtag);
+        assertEquals(nm.map.size(), _nm.map.size());
+        assertCollectionEquals(nm.map.get("PID1").ipv4, _nm.map.get("PID1").ipv4);
+        assertCollectionEquals(nm.map.get("PID2").ipv4, _nm.map.get("PID2").ipv4);
+        assertCollectionEquals(nm.map.get("PID3").ipv4, _nm.map.get("PID3").ipv4);
+        assertCollectionEquals(nm.map.get("PID3").ipv6, _nm.map.get("PID3").ipv6);
+
+        String addrGroupString = mapper.asJSON(nm.map.get("PID3"));
+        RFC7285Endpoint.AddressGroup _ag = mapper.asAddressGroup(addrGroupString);
+        assertCollectionEquals(nm.map.get("PID3").ipv4, _ag.ipv4);
+        assertCollectionEquals(nm.map.get("PID3").ipv6, _ag.ipv6);
+    }
+
+    @Test
+    public void testNetworkMapFilter() throws Exception {
+        /*
+         *
+         *      {
+         *          "pids": [ "PID1", "PID2" ]
+         *      }
+         * */
+
+        RFC7285JSONMapper mapper = new RFC7285JSONMapper();
+
+        RFC7285NetworkMap.Filter filter = new RFC7285NetworkMap.Filter();
+
+        filter.pids.add("PID1");
+        filter.pids.add("PID2");
+
+        String nmfString = mapper.asJSON(filter);
+        RFC7285NetworkMap.Filter _filter = mapper.asNetworkMapFilter(nmfString);
+        assertCollectionEquals(filter.pids, _filter.pids);
+    }
+
+    public RFC7285CostMap makeCostMap() {
+        /*
+         *  {
+         *      "meta": {
+         *          "dependent-vtags" : [
+         *              {
+         *                  "resource-id": "my-default-network-map",
+         *                  "tag": "3ee2cb7e8d63d9fab71b9b34cbf764436315542e"
+         *              }
+         *          ],
+         *          "cost-type" : {
+         *              "cost-mode": "numerical",
+         *              "cost-metric": "routingcost"
+         *          }
+         *      },
+         *      "cost-map" : {
+         *          "PID1": { "PID1": 1,  "PID2": 5,  "PID3": 10 },
+         *          "PID2": { "PID1": 5,  "PID2": 1,  "PID3": 15 },
+         *          "PID3": { "PID1": 20, "PID2": 15  }
+         *      }
+         *  }
+         * */
+
+        RFC7285CostMap cm = new RFC7285CostMap();
+        cm.meta.costType = new RFC7285CostType("numerical", "routingcost");
+        cm.meta.netmap_tags.add(new RFC7285VersionTag("my-default-network-map",
+                                                      "3ee2cb7e8d63d9fab71b9b34cbf764436315542e"));
+        cm.map.put("PID1", new LinkedHashMap<String, Object>());
+        cm.map.get("PID1").put("PID1", new Integer(1));
+        cm.map.get("PID1").put("PID2", new Integer(5));
+        cm.map.get("PID1").put("PID3", new Integer(10));
+        cm.map.put("PID2", new LinkedHashMap<String, Object>());
+        cm.map.get("PID2").put("PID1", new Integer(5));
+        cm.map.get("PID2").put("PID2", new Integer(1));
+        cm.map.get("PID2").put("PID3", new Integer(15));
+        cm.map.put("PID3", new LinkedHashMap<String, Object>());
+        cm.map.get("PID3").put("PID1", new Integer(20));
+        cm.map.get("PID3").put("PID2", new Integer(15));
+
+        return cm;
+    }
+
+    @Test
+    public void testCostMap() throws Exception {
+        RFC7285JSONMapper mapper = new RFC7285JSONMapper();
+
+        RFC7285CostMap cm = makeCostMap();
+
+        String cmString = mapper.asJSON(cm);
+        RFC7285CostMap _cm = mapper.asCostMap(cmString);
+
+        assertCollectionEquals(cm.meta.netmap_tags, _cm.meta.netmap_tags);
+        assertEquals(cm.meta.costType, _cm.meta.costType);
+
+        String pids[] = { "PID1", "PID2", "PID3" };
+        for (String pid: pids) {
+            assertCollectionEquals(cm.map.get(pid).entrySet(), _cm.map.get(pid).entrySet());
+        }
+    }
+
+    @Test
+    public void testCostMapFilter() throws Exception {
+        /*
+         *  {
+         *      "cost-type" : {
+         *          "cost-mode": "numerical",
+         *          "cost-metric": "routingcost"
+         *      },
+         *      "pids" : {
+         *          "srcs" : [ "PID1" ],
+         *          "dsts" : [ "PID1", "PID2", "PID3" ]
+         *      }
+         *  }
+         * */
+
+        RFC7285JSONMapper mapper = new RFC7285JSONMapper();
+
+        RFC7285CostMap.Filter filter = new RFC7285CostMap.Filter();
+        filter.costType = new RFC7285CostType("numerical", "routingcost", "test");
+        filter.pids.src.add("PID1");
+        filter.pids.dst.add("PID1");
+        filter.pids.dst.add("PID2");
+        filter.pids.dst.add("PID3");
+
+        String cmfString = mapper.asJSON(filter);
+        RFC7285CostMap.Filter _filter = mapper.asCostMapFilter(cmfString);
+
+        assertEquals(filter.costType, _filter.costType);
+        assertCollectionEquals(filter.pids.src, _filter.pids.src);
+        assertCollectionEquals(filter.pids.dst, _filter.pids.dst);
+    }
+
+    @Test
+    public void testECSRequest() throws Exception {
+        /*
+         *  {
+         *      "cost-type" : {
+         *          "cost-mode": "ordinal",
+         *          "cost-metric": "routingcost"
+         *      },
+         *      "endpoints": {
+         *          "srcs": [ "ipv4:192.0.2.2" ],
+         *          "dsts": [
+         *              "ipv4:192.0.2.89",
+         *              "ipv4:198.51.100.34",
+         *              "ipv4:203.0.113.45"
+         *          ]
+         *      }
+         *  }
+         * */
+
+        RFC7285JSONMapper mapper = new RFC7285JSONMapper();
+
+        RFC7285Endpoint.CostRequest req = new RFC7285Endpoint.CostRequest();
+        req.costType = new RFC7285CostType("ordinal", "routingcost", "test");
+        req.endpoints.src.add("ipv4:192.0.2.2");
+        req.endpoints.dst.add("ipv4:192.0.2.89");
+        req.endpoints.dst.add("ipv4:198.51.100.34");
+        req.endpoints.dst.add("ipv4:203.0.113.45");
+
+        String ecsrString = mapper.asJSON(req);
+        RFC7285Endpoint.CostRequest _req = mapper.asCostRequest(ecsrString);
+
+        assertEquals(req.costType, _req.costType);
+        assertCollectionEquals(req.endpoints.src, _req.endpoints.src);
+        assertCollectionEquals(req.endpoints.dst, _req.endpoints.dst);
+    }
+
+    public RFC7285Endpoint.CostResponse makeECSResponse() {
+        /*
+         *  {
+         *      "meta": {
+         *          "cost-type" : {
+         *              "cost-mode": "ordinal",
+         *              "cost-metric": "routingcost"
+         *          }
+         *      },
+         *      "endpoint-cost-map" : {
+         *          "ipv4:192.0.2.2": {
+         *              "ipv4:192.0.2.89": 1,
+         *              "ipv4:198.51.100.34": 2,
+         *              "ipv4:203.0.113.45": 3
+         *          },
+         *      }
+         *  }
+         * */
+
+        String src[] = { "ipv4:192.0.2.2" };
+        String dst[] = { "ipv4:192.0.2.89", "ipv4:198.51.100.34", "ipv4:203.0.113.45" };
+
+        RFC7285Endpoint.CostResponse ecsr = new RFC7285Endpoint.CostResponse();
+        ecsr.meta.costType = new RFC7285CostType("ordinal", "routingcost");
+        ecsr.answer.put(src[0], new LinkedHashMap<String, Object>());
+        ecsr.answer.get(src[0]).put(dst[0], new Integer(1));
+        ecsr.answer.get(src[0]).put(dst[1], new Integer(2));
+        ecsr.answer.get(src[0]).put(dst[2], new Integer(3));
+
+        return ecsr;
+    }
+
+
+    @Test
+    public void testECSAnswer() throws Exception {
+        RFC7285JSONMapper mapper = new RFC7285JSONMapper();
+
+        RFC7285Endpoint.CostResponse ecsr = makeECSResponse();
+
+        String ecsrString = mapper.asJSON(ecsr);
+        RFC7285Endpoint.CostResponse _ecsr = mapper.asCostResponse(ecsrString);
+
+        assertEquals(ecsr.meta.costType, _ecsr.meta.costType);
+
+        String endpoints[] = { "ipv4:192.0.2.2" };
+        for (String endpoint: endpoints) {
+            assertCollectionEquals(ecsr.answer.get(endpoint).entrySet(),
+                                   _ecsr.answer.get(endpoint).entrySet());
+        }
+    }
+}