[ONOS-6169] Implement codec for LispTeAddress with unit test
Change-Id: I47edf0d20e2a8abdfd38bde19afa51b5abfb7240
diff --git a/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/LispMappingExtensionCodecRegistrator.java b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/LispMappingExtensionCodecRegistrator.java
index 027aefe..d9e5e68 100644
--- a/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/LispMappingExtensionCodecRegistrator.java
+++ b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/LispMappingExtensionCodecRegistrator.java
@@ -30,6 +30,8 @@
import org.onosproject.drivers.lisp.extensions.codec.LispNonceAddressCodec;
import org.onosproject.drivers.lisp.extensions.codec.LispSegmentAddressCodec;
import org.onosproject.drivers.lisp.extensions.codec.LispSrcDstAddressCodec;
+import org.onosproject.drivers.lisp.extensions.codec.LispTeAddressCodec;
+import org.onosproject.drivers.lisp.extensions.codec.LispTeRecordCodec;
import org.onosproject.mapping.web.MappingCodecRegistrator;
import org.slf4j.Logger;
@@ -64,6 +66,8 @@
codecService.registerCodec(LispNonceAddress.class, new LispNonceAddressCodec());
codecService.registerCodec(LispSegmentAddress.class, new LispSegmentAddressCodec());
codecService.registerCodec(LispSrcDstAddress.class, new LispSrcDstAddressCodec());
+ codecService.registerCodec(LispTeAddress.class, new LispTeAddressCodec());
+ codecService.registerCodec(LispTeAddress.TeRecord.class, new LispTeRecordCodec());
log.info("Started");
}
@@ -79,6 +83,8 @@
codecService.unregisterCodec(LispNonceAddress.class);
codecService.unregisterCodec(LispSegmentAddress.class);
codecService.unregisterCodec(LispSrcDstAddress.class);
+ codecService.unregisterCodec(LispTeAddress.class);
+ codecService.unregisterCodec(LispTeAddress.TeRecord.class);
registrator.deactivate();
registrator = null;
diff --git a/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispTeAddressCodec.java b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispTeAddressCodec.java
new file mode 100644
index 0000000..ff518f4
--- /dev/null
+++ b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispTeAddressCodec.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.lisp.extensions.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Lists;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.drivers.lisp.extensions.LispTeAddress;
+
+import java.util.List;
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * LISP traffic engineering address codec.
+ */
+public final class LispTeAddressCodec extends JsonCodec<LispTeAddress> {
+
+ protected static final String TE_RECORDS = "records";
+
+ private static final String MISSING_MEMBER_MESSAGE =
+ " member is required in LispTeAddress";
+
+ @Override
+ public ObjectNode encode(LispTeAddress address, CodecContext context) {
+ checkNotNull(address, "LispTeAddress cannot be null");
+
+ final ObjectNode result = context.mapper().createObjectNode();
+ final ArrayNode jsonRecords = result.putArray(TE_RECORDS);
+
+ final JsonCodec<LispTeAddress.TeRecord> recordCodec =
+ context.codec(LispTeAddress.TeRecord.class);
+
+ for (final LispTeAddress.TeRecord record : address.getTeRecords()) {
+ jsonRecords.add(recordCodec.encode(record, context));
+ }
+
+ return result;
+ }
+
+ @Override
+ public LispTeAddress decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ final JsonCodec<LispTeAddress.TeRecord> recordCodec =
+ context.codec(LispTeAddress.TeRecord.class);
+
+ JsonNode recordsJson = nullIsIllegal(json.get(TE_RECORDS),
+ TE_RECORDS + MISSING_MEMBER_MESSAGE);
+ List<LispTeAddress.TeRecord> records = Lists.newArrayList();
+
+ if (recordsJson != null) {
+ IntStream.range(0, recordsJson.size())
+ .forEach(i -> records.add(
+ recordCodec.decode(get(recordsJson, i), context)));
+ }
+
+ return new LispTeAddress.Builder()
+ .withTeRecords(records)
+ .build();
+ }
+}
diff --git a/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispTeRecordCodec.java b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispTeRecordCodec.java
new file mode 100644
index 0000000..b5a3cc8
--- /dev/null
+++ b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispTeRecordCodec.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.lisp.extensions.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.drivers.lisp.extensions.LispTeAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * LISP traffic engineering record codec.
+ */
+public final class LispTeRecordCodec extends JsonCodec<LispTeAddress.TeRecord> {
+
+ protected static final String LOOKUP = "lookup";
+ protected static final String RLOC_PROBE = "rlocProbe";
+ protected static final String STRICT = "strict";
+ protected static final String ADDRESS = "address";
+
+ private static final String MISSING_MEMBER_MESSAGE =
+ " member is required in LispTeRecord";
+
+ @Override
+ public ObjectNode encode(LispTeAddress.TeRecord record, CodecContext context) {
+ checkNotNull(record, "LispTeRecord cannot be null");
+
+ final ObjectNode result = context.mapper().createObjectNode()
+ .put(LOOKUP, record.isLookup())
+ .put(RLOC_PROBE, record.isRlocProbe())
+ .put(STRICT, record.isStrict());
+
+ if (record.getAddress() != null) {
+ final JsonCodec<MappingAddress> addressCodec =
+ context.codec(MappingAddress.class);
+ ObjectNode address = addressCodec.encode(record.getAddress(), context);
+ result.set(ADDRESS, address);
+ }
+
+ return result;
+ }
+
+ @Override
+ public LispTeAddress.TeRecord decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ boolean isLookup = nullIsIllegal(json.get(LOOKUP),
+ LOOKUP + MISSING_MEMBER_MESSAGE).asBoolean();
+ boolean isRlocProbe = nullIsIllegal(json.get(RLOC_PROBE),
+ RLOC_PROBE + MISSING_MEMBER_MESSAGE).asBoolean();
+ boolean isStrict = nullIsIllegal(json.get(STRICT),
+ STRICT + MISSING_MEMBER_MESSAGE).asBoolean();
+
+ ObjectNode addressJson = get(json, ADDRESS);
+ MappingAddress mappingAddress = null;
+
+ if (addressJson != null) {
+ final JsonCodec<MappingAddress> addressCodec =
+ context.codec(MappingAddress.class);
+ mappingAddress = addressCodec.decode(addressJson, context);
+ }
+
+ return new LispTeAddress.TeRecord.Builder()
+ .withIsLookup(isLookup)
+ .withIsRlocProbe(isRlocProbe)
+ .withIsStrict(isStrict)
+ .withRtrRlocAddress(mappingAddress)
+ .build();
+ }
+
+}
diff --git a/drivers/lisp/src/test/java/org/onosproject/drivers/lisp/extensions/codec/LispTeAddressCodecTest.java b/drivers/lisp/src/test/java/org/onosproject/drivers/lisp/extensions/codec/LispTeAddressCodecTest.java
new file mode 100644
index 0000000..490061c
--- /dev/null
+++ b/drivers/lisp/src/test/java/org/onosproject/drivers/lisp/extensions/codec/LispTeAddressCodecTest.java
@@ -0,0 +1,202 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.lisp.extensions.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
+import com.google.common.testing.EqualsTester;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.drivers.lisp.extensions.LispMappingExtensionCodecRegistrator;
+import org.onosproject.drivers.lisp.extensions.LispTeAddress;
+import org.onosproject.mapping.addresses.MappingAddresses;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+
+/**
+ * LISP traffic engineering address codec.
+ */
+public class LispTeAddressCodecTest {
+
+ private static final boolean LOOKUP_1 = true;
+ private static final boolean RLOC_PROBE_1 = true;
+ private static final boolean STRICT_1 = true;
+ private static final IpPrefix IPV4_ADDRESS_1 = IpPrefix.valueOf("10.1.1.1/24");
+
+ private static final boolean LOOKUP_2 = false;
+ private static final boolean RLOC_PROBE_2 = false;
+ private static final boolean STRICT_2 = false;
+ private static final IpPrefix IPV4_ADDRESS_2 = IpPrefix.valueOf("10.1.1.2/24");
+
+ private LispTeAddress.TeRecord record1;
+ private LispTeAddress.TeRecord record2;
+
+ private CodecContext context;
+ private JsonCodec<LispTeAddress> teAddressCodec;
+ private LispMappingExtensionCodecRegistrator registrator;
+
+ /**
+ * Sets up for each test.
+ * Creates a context and fetches the LispTeAddress codec.
+ */
+ @Before
+ public void setUp() {
+ CodecManager manager = new CodecManager();
+ registrator = new LispMappingExtensionCodecRegistrator();
+ registrator.codecService = manager;
+ registrator.activate();
+
+ context = new LispMappingExtensionCodecContextAdapter(registrator.codecService);
+ teAddressCodec = context.codec(LispTeAddress.class);
+ assertThat("Traffic Engineering address codec should not be null",
+ teAddressCodec, notNullValue());
+
+ record1 = new LispTeAddress.TeRecord.Builder()
+ .withIsLookup(LOOKUP_1)
+ .withIsRlocProbe(RLOC_PROBE_1)
+ .withIsStrict(STRICT_1)
+ .withRtrRlocAddress(MappingAddresses.ipv4MappingAddress(IPV4_ADDRESS_1))
+ .build();
+
+ record2 = new LispTeAddress.TeRecord.Builder()
+ .withIsLookup(LOOKUP_2)
+ .withIsRlocProbe(RLOC_PROBE_2)
+ .withIsStrict(STRICT_2)
+ .withRtrRlocAddress(MappingAddresses.ipv4MappingAddress(IPV4_ADDRESS_2))
+ .build();
+ }
+
+ /**
+ * Deactivates the codec registrator.
+ */
+ @After
+ public void tearDown() {
+ registrator.deactivate();
+ }
+
+ /**
+ * Tests encoding of a LispTeAddress object.
+ */
+ @Test
+ public void testLispTeAddressEncode() {
+
+ LispTeAddress address = new LispTeAddress.Builder()
+ .withTeRecords(ImmutableList.of(record1, record2))
+ .build();
+
+ ObjectNode addressJson = teAddressCodec.encode(address, context);
+ assertThat("errors in encoding Traffic Engineering address JSON",
+ addressJson, LispTeAddressJsonMatcher.matchesTeAddress(address));
+ }
+
+ /**
+ * Tests decoding of a LispTeAddress JSON object.
+ */
+ @Test
+ public void testLispTeAddressDecode() throws IOException {
+
+ LispTeAddress address = getLispTeAddress("LispTeAddress.json");
+
+ new EqualsTester()
+ .addEqualityGroup(address.getTeRecords().get(0), record1)
+ .addEqualityGroup(address.getTeRecords().get(1), record2).testEquals();
+ }
+
+ /**
+ * Hamcrest matcher for LispTeAddress.
+ */
+ public static final class LispTeAddressJsonMatcher
+ extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final LispTeAddress address;
+
+ /**
+ * Default constructor.
+ *
+ * @param address LispTeAddress object
+ */
+ private LispTeAddressJsonMatcher(LispTeAddress address) {
+ this.address = address;
+ }
+
+ private int filteredSize(JsonNode node) {
+ return node.size();
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+ // check TE records
+ final JsonNode jsonTeRecords = jsonNode.get(LispTeAddressCodec.TE_RECORDS);
+
+ if (address.getTeRecords().size() != filteredSize(jsonTeRecords)) {
+ description.appendText("TE records array size of " +
+ Integer.toString(address.getTeRecords().size()));
+ return false;
+ }
+
+ for (int recordIndex = 0; recordIndex < jsonTeRecords.size(); recordIndex++) {
+ assertThat(jsonTeRecords.get(recordIndex),
+ LispTeRecordJsonMatcher.matchesTeRecord(address.getTeRecords().get(recordIndex)));
+ }
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(address.toString());
+ }
+
+ /**
+ * Factory to allocate a LispTeAddress matcher.
+ *
+ * @param address LispTeAddress object we are looking for
+ * @return matcher
+ */
+ public static LispTeAddressJsonMatcher matchesTeAddress(LispTeAddress address) {
+ return new LispTeAddressJsonMatcher(address);
+ }
+ }
+
+ /**
+ * Reads in a LispTeAddress from the given resource and decodes it.
+ *
+ * @param resourceName resource to use to read the JSON for the rule
+ * @return decoded LispTeAddress
+ * @throws IOException if processing the resource fails
+ */
+ private LispTeAddress getLispTeAddress(String resourceName) throws IOException {
+ InputStream jsonStream = LispTeAddressCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ assertThat("JSON string should not be null", json, notNullValue());
+ LispTeAddress teAddress = teAddressCodec.decode((ObjectNode) json, context);
+ assertThat("decoded address should not be null", teAddress, notNullValue());
+ return teAddress;
+ }
+}
\ No newline at end of file
diff --git a/drivers/lisp/src/test/java/org/onosproject/drivers/lisp/extensions/codec/LispTeRecordJsonMatcher.java b/drivers/lisp/src/test/java/org/onosproject/drivers/lisp/extensions/codec/LispTeRecordJsonMatcher.java
new file mode 100644
index 0000000..e964538
--- /dev/null
+++ b/drivers/lisp/src/test/java/org/onosproject/drivers/lisp/extensions/codec/LispTeRecordJsonMatcher.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.drivers.lisp.extensions.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.drivers.lisp.extensions.LispTeAddress;
+import org.onosproject.mapping.web.codec.MappingAddressJsonMatcher;
+
+/**
+ * Hamcrest matcher for TeRecord.
+ */
+public final class LispTeRecordJsonMatcher
+ extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final LispTeAddress.TeRecord record;
+
+ /**
+ * Default constructor.
+ *
+ * @param record TeRecord object
+ */
+ private LispTeRecordJsonMatcher(LispTeAddress.TeRecord record) {
+ this.record = record;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+ // check isLookup
+ boolean jsonLookup = jsonNode.get(LispTeRecordCodec.LOOKUP).asBoolean();
+ boolean lookup = record.isLookup();
+ if (jsonLookup != lookup) {
+ description.appendText("IsLookup was " + jsonLookup);
+ return false;
+ }
+
+ // check isRlocProbe
+ boolean jsonRlocProbe = jsonNode.get(LispTeRecordCodec.RLOC_PROBE).asBoolean();
+ boolean rlocProbe = record.isRlocProbe();
+ if (jsonRlocProbe != rlocProbe) {
+ description.appendText("IsRlocProbe was " + jsonRlocProbe);
+ return false;
+ }
+
+ // check isStrict
+ boolean jsonStrict = jsonNode.get(LispTeRecordCodec.STRICT).asBoolean();
+ boolean strict = record.isStrict();
+ if (jsonStrict != strict) {
+ description.appendText("IsStrict was " + jsonStrict);
+ return false;
+ }
+
+ // check address
+ MappingAddressJsonMatcher addressMatcher =
+ MappingAddressJsonMatcher.matchesMappingAddress(record.getAddress());
+
+ return addressMatcher.matches(jsonNode.get(LispTeRecordCodec.ADDRESS));
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(record.toString());
+ }
+
+ /**
+ * Factory to allocate a TeRecord matcher.
+ *
+ * @param record TeRecord object we are looking for
+ * @return matcher
+ */
+ public static LispTeRecordJsonMatcher matchesTeRecord(LispTeAddress.TeRecord record) {
+ return new LispTeRecordJsonMatcher(record);
+ }
+}
diff --git a/drivers/lisp/src/test/resources/org/onosproject/drivers/lisp/extensions/codec/LispTeAddress.json b/drivers/lisp/src/test/resources/org/onosproject/drivers/lisp/extensions/codec/LispTeAddress.json
new file mode 100644
index 0000000..1cc7fe2
--- /dev/null
+++ b/drivers/lisp/src/test/resources/org/onosproject/drivers/lisp/extensions/codec/LispTeAddress.json
@@ -0,0 +1,22 @@
+{
+ "records": [
+ {
+ "lookup": true,
+ "rlocProbe": true,
+ "strict": true,
+ "address": {
+ "type": "IPV4",
+ "ipv4": "10.1.1.1/24"
+ }
+ },
+ {
+ "lookup": false,
+ "rlocProbe": false,
+ "strict": false,
+ "address": {
+ "type": "IPV4",
+ "ipv4": "10.1.1.2/24"
+ }
+ }
+ ]
+}
\ No newline at end of file