[ONOS-6170] Implement codec for MappingKey with unit test
Change-Id: Ib40651fbf320cbf3eb94d47ec2dc94636fb0449d
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingCodecRegistrator.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingCodecRegistrator.java
index 7bebe43..0621e19 100644
--- a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingCodecRegistrator.java
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingCodecRegistrator.java
@@ -21,6 +21,7 @@
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onosproject.codec.CodecService;
+import org.onosproject.mapping.MappingKey;
import org.onosproject.mapping.MappingTreatment;
import org.onosproject.mapping.actions.MappingAction;
import org.onosproject.mapping.addresses.MappingAddress;
@@ -28,6 +29,7 @@
import org.onosproject.mapping.web.codec.MappingActionCodec;
import org.onosproject.mapping.web.codec.MappingAddressCodec;
import org.onosproject.mapping.web.codec.MappingInstructionCodec;
+import org.onosproject.mapping.web.codec.MappingKeyCodec;
import org.onosproject.mapping.web.codec.MappingTreatmentCodec;
import org.slf4j.Logger;
@@ -50,6 +52,7 @@
codecService.registerCodec(MappingInstruction.class, new MappingInstructionCodec());
codecService.registerCodec(MappingAction.class, new MappingActionCodec());
codecService.registerCodec(MappingTreatment.class, new MappingTreatmentCodec());
+ codecService.registerCodec(MappingKey.class, new MappingKeyCodec());
log.info("Started");
}
@@ -60,6 +63,7 @@
codecService.unregisterCodec(MappingInstruction.class);
codecService.unregisterCodec(MappingAction.class);
codecService.unregisterCodec(MappingTreatment.class);
+ codecService.unregisterCodec(MappingKey.class);
log.info("Stopped");
}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/MappingKeyCodec.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/MappingKeyCodec.java
new file mode 100644
index 0000000..93dd106
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/MappingKeyCodec.java
@@ -0,0 +1,64 @@
+/*
+ * 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.mapping.web.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.mapping.DefaultMappingKey;
+import org.onosproject.mapping.MappingKey;
+import org.onosproject.mapping.addresses.MappingAddress;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Mapping key codec.
+ */
+public final class MappingKeyCodec extends JsonCodec<MappingKey> {
+
+ protected static final String ADDRESS = "address";
+
+ @Override
+ public ObjectNode encode(MappingKey key, CodecContext context) {
+ checkNotNull(key, "Mapping key cannot be null");
+
+ final ObjectNode result = context.mapper().createObjectNode();
+ final JsonCodec<MappingAddress> addressCodec =
+ context.codec(MappingAddress.class);
+
+ result.set(ADDRESS, addressCodec.encode(key.address(), context));
+
+ return result;
+ }
+
+ @Override
+ public MappingKey decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ MappingKey.Builder builder = DefaultMappingKey.builder();
+
+ ObjectNode addressJson = get(json, ADDRESS);
+ if (addressJson != null) {
+ final JsonCodec<MappingAddress> addressCodec =
+ context.codec(MappingAddress.class);
+ builder.withAddress(addressCodec.decode(addressJson, context));
+ }
+
+ return builder.build();
+ }
+}
diff --git a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingKeyCodecTest.java b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingKeyCodecTest.java
new file mode 100644
index 0000000..39f43d4
--- /dev/null
+++ b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingKeyCodecTest.java
@@ -0,0 +1,198 @@
+/*
+ * 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.mapping.web.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+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.CodecService;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.mapping.DefaultMappingKey;
+import org.onosproject.mapping.MappingKey;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.addresses.MappingAddresses;
+import org.onosproject.mapping.web.MappingCodecRegistrator;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+/**
+ * Unit tests for MappingKeyCodec.
+ */
+public class MappingKeyCodecTest {
+
+ private static final String IPV4_STRING = "1.2.3.4";
+ private static final String PORT_STRING = "32";
+ private static final IpPrefix IPV4_PREFIX =
+ IpPrefix.valueOf(IPV4_STRING + "/" + PORT_STRING);
+
+ private CodecContext context;
+ private JsonCodec<MappingKey> keyCodec;
+ private MappingCodecRegistrator registrator;
+
+ /**
+ * Sets up for each test.
+ * Creates a context and fetches the mapping key codec.
+ */
+ @Before
+ public void setUp() {
+ CodecManager manager = new CodecManager();
+ registrator = new MappingCodecRegistrator();
+ registrator.codecService = manager;
+ registrator.activate();
+
+ context = new MappingKeyCodecTest.MappingTestContext(registrator.codecService);
+ keyCodec = context.codec(MappingKey.class);
+ assertThat(keyCodec, notNullValue());
+ }
+
+ /**
+ * Deactivates the codec registrator.
+ */
+ @After
+ public void tearDown() {
+ registrator.deactivate();
+ }
+
+ /**
+ * Tests encoding of a mapping key object.
+ */
+ @Test
+ public void testMappingKeyEncode() {
+
+ MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX);
+
+ MappingKey key = DefaultMappingKey.builder()
+ .withAddress(address)
+ .build();
+
+ ObjectNode keyJson = keyCodec.encode(key, context);
+ assertThat(keyJson, MappingKeyJsonMatcher.matchesMappingKey(key));
+ }
+
+ /**
+ * Tests decoding of a mapping key JSON object.
+ */
+ @Test
+ public void testMappingKeyDecode() throws IOException {
+ MappingKey key = getKey("MappingKey.json");
+ assertThat(key.address().toString(),
+ is("IPV4:" + IPV4_STRING + "/" + PORT_STRING));
+ }
+
+ /**
+ * Hamcrest matcher for mapping key.
+ */
+ public static final class MappingKeyJsonMatcher
+ extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final MappingKey mappingKey;
+
+ /**
+ * A default constructor.
+ *
+ * @param mappingKey mapping key
+ */
+ private MappingKeyJsonMatcher(MappingKey mappingKey) {
+ this.mappingKey = mappingKey;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+ // check address
+ final JsonNode jsonAddressNode = jsonNode.get(MappingKeyCodec.ADDRESS);
+
+ assertThat(jsonAddressNode,
+ MappingAddressJsonMatcher.matchesMappingAddress(mappingKey.address()));
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(mappingKey.toString());
+ }
+
+ /**
+ * Factory to allocate a mapping treatment.
+ *
+ * @param mappingKey mapping treatment object we are looking for
+ * @return matcher
+ */
+ static MappingKeyJsonMatcher matchesMappingKey(MappingKey mappingKey) {
+ return new MappingKeyJsonMatcher(mappingKey);
+ }
+ }
+
+ /**
+ * Test mapping codec context.
+ */
+ private class MappingTestContext implements CodecContext {
+ private final ObjectMapper mapper = new ObjectMapper();
+ private final CodecService manager;
+
+ /**
+ * Constructs a new mock codec context.
+ */
+ public MappingTestContext(CodecService manager) {
+ this.manager = manager;
+ }
+
+ @Override
+ public ObjectMapper mapper() {
+ return mapper;
+ }
+
+ @Override
+ public <T> JsonCodec<T> codec(Class<T> entityClass) {
+ return manager.getCodec(entityClass);
+ }
+
+ @Override
+ public <T> T getService(Class<T> serviceClass) {
+ return null;
+ }
+ }
+
+ /**
+ * Reads in a mapping key from the given resource and decodes it.
+ *
+ * @param resourceName resource to use to read the JSON for the rule
+ * @return decoded mappingKey
+ * @throws IOException if processing the resource fails
+ */
+ private MappingKey getKey(String resourceName) throws IOException {
+ InputStream jsonStream = MappingKeyCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ assertThat(json, notNullValue());
+ MappingKey key = keyCodec.decode((ObjectNode) json, context);
+ assertThat(key, notNullValue());
+ return key;
+ }
+}
diff --git a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingTreatmentCodecTest.java b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingTreatmentCodecTest.java
index ca8b2df..6a17502 100644
--- a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingTreatmentCodecTest.java
+++ b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingTreatmentCodecTest.java
@@ -170,6 +170,12 @@
}
}
+ // check address
+ final JsonNode jsonAddressNode = jsonNode.get(MappingKeyCodec.ADDRESS);
+
+ assertThat(jsonAddressNode,
+ MappingAddressJsonMatcher.matchesMappingAddress(mappingTreatment.address()));
+
return true;
}
diff --git a/apps/mappingmanagement/web/src/test/resources/org/onosproject/mapping/web/codec/MappingKey.json b/apps/mappingmanagement/web/src/test/resources/org/onosproject/mapping/web/codec/MappingKey.json
new file mode 100644
index 0000000..aac8c5e
--- /dev/null
+++ b/apps/mappingmanagement/web/src/test/resources/org/onosproject/mapping/web/codec/MappingKey.json
@@ -0,0 +1,6 @@
+{
+ "address": {
+ "type": "IPV4",
+ "ipv4": "1.2.3.4/32"
+ }
+}