[ONOS-6170] Implement codec for MappingInstruction primitives
Change-Id: Ib372de863e6d730c55326ef3450d6f79478592b5
diff --git a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressCodecTest.java b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressCodecTest.java
index 0d004b0..41913c3 100644
--- a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressCodecTest.java
+++ b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressCodecTest.java
@@ -41,7 +41,7 @@
private CodecContext context;
private JsonCodec<MappingAddress> addressCodec;
- MappingCodecRegistrator registrator;
+ private MappingCodecRegistrator registrator;
private static final IpPrefix IPV4_PREFIX = IpPrefix.valueOf("10.1.1.0/24");
private static final IpPrefix IPV6_PREFIX = IpPrefix.valueOf("fe80::/64");
private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01");
diff --git a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingInstructionCodecTest.java b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingInstructionCodecTest.java
new file mode 100644
index 0000000..715af50
--- /dev/null
+++ b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingInstructionCodecTest.java
@@ -0,0 +1,153 @@
+/*
+ * 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.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+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.instructions.MappingInstruction;
+import org.onosproject.mapping.instructions.MappingInstructions;
+import org.onosproject.mapping.instructions.MulticastMappingInstruction;
+import org.onosproject.mapping.instructions.UnicastMappingInstruction;
+import org.onosproject.mapping.web.MappingCodecRegistrator;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.mapping.web.codec.MappingInstructionJsonMatcher.matchesInstruction;
+
+/**
+ * Unit tests for MappingInstructionCodec.
+ */
+public class MappingInstructionCodecTest {
+
+ private CodecContext context;
+ private JsonCodec<MappingInstruction> instructionCodec;
+ private MappingCodecRegistrator registrator;
+
+ private static final int UNICAST_WEIGHT = 1;
+ private static final int UNICAST_PRIORITY = 1;
+ private static final int MULTICAST_WEIGHT = 2;
+ private static final int MULTICAST_PRIORITY = 2;
+
+ /**
+ * Sets up for each test.
+ * Creates a context and fetches the mapping instruction codec.
+ */
+ @Before
+ public void setUp() {
+ CodecManager manager = new CodecManager();
+ registrator = new MappingCodecRegistrator();
+ registrator.codecService = manager;
+ registrator.activate();
+
+ context = new MappingInstructionCodecTest.MappingTestContext(registrator.codecService);
+
+ instructionCodec = context.codec(MappingInstruction.class);
+ assertThat(instructionCodec, notNullValue());
+ }
+
+ @After
+ public void tearDown() {
+ registrator.deactivate();
+ }
+
+ /**
+ * Tests the encoding of unicast weight instruction.
+ */
+ @Test
+ public void unicastWeightInstrutionTest() {
+ final UnicastMappingInstruction.WeightMappingInstruction instruction =
+ (UnicastMappingInstruction.WeightMappingInstruction)
+ MappingInstructions.unicastWeight(UNICAST_WEIGHT);
+ final ObjectNode instructionJson =
+ instructionCodec.encode(instruction, context);
+ assertThat(instructionJson, matchesInstruction(instruction));
+ }
+
+ /**
+ * Tests the encoding of unicast priority instruction.
+ */
+ @Test
+ public void unicastPriorityInstructionTest() {
+ final UnicastMappingInstruction.PriorityMappingInstruction instruction =
+ (UnicastMappingInstruction.PriorityMappingInstruction)
+ MappingInstructions.unicastPriority(UNICAST_PRIORITY);
+ final ObjectNode instructionJson =
+ instructionCodec.encode(instruction, context);
+ assertThat(instructionJson, matchesInstruction(instruction));
+ }
+
+ /**
+ * Tests the encoding of multicast weight instruction.
+ */
+ @Test
+ public void multicastWeightInstructionTest() {
+ final MulticastMappingInstruction.WeightMappingInstruction instruction =
+ (MulticastMappingInstruction.WeightMappingInstruction)
+ MappingInstructions.multicastWeight(MULTICAST_WEIGHT);
+ final ObjectNode instructionJson =
+ instructionCodec.encode(instruction, context);
+ assertThat(instructionJson, matchesInstruction(instruction));
+ }
+
+ /**
+ * Tests the encoding of multicast priority instruction.
+ */
+ @Test
+ public void multicastPriorityInstructionTest() {
+ final MulticastMappingInstruction.PriorityMappingInstruction instruction =
+ (MulticastMappingInstruction.PriorityMappingInstruction)
+ MappingInstructions.multicastPriority(MULTICAST_PRIORITY);
+ final ObjectNode instructionJson =
+ instructionCodec.encode(instruction, context);
+ assertThat(instructionJson, matchesInstruction(instruction)); }
+
+ /**
+ * 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;
+ }
+ }
+}
diff --git a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingInstructionJsonMatcher.java b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingInstructionJsonMatcher.java
new file mode 100644
index 0000000..c1d0cce
--- /dev/null
+++ b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingInstructionJsonMatcher.java
@@ -0,0 +1,214 @@
+/*
+ * 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 org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.mapping.instructions.MappingInstruction;
+import org.onosproject.mapping.instructions.MulticastMappingInstruction;
+import org.onosproject.mapping.instructions.UnicastMappingInstruction;
+
+/**
+ * Hamcrest matcher for mapping instructions.
+ */
+public final class MappingInstructionJsonMatcher
+ extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final MappingInstruction instruction;
+
+ /**
+ * A default constructor.
+ *
+ * @param instruction mapping instruction
+ */
+ private MappingInstructionJsonMatcher(MappingInstruction instruction) {
+ this.instruction = instruction;
+ }
+
+ /**
+ * Matches the contents of an unicast weight mapping instruction.
+ *
+ * @param node JSON instruction to match
+ * @param description object used for recording errors
+ * @return true if contents match, false otherwise
+ */
+ private boolean matchUnicastWeightInstruction(JsonNode node,
+ Description description) {
+ UnicastMappingInstruction.WeightMappingInstruction instructionToMatch =
+ (UnicastMappingInstruction.WeightMappingInstruction) instruction;
+ final String jsonSubtype = node.get(MappingInstructionCodec.SUBTYPE).textValue();
+ if (!instructionToMatch.subtype().name().equals(jsonSubtype)) {
+ description.appendText("subtype was " + jsonSubtype);
+ return false;
+ }
+
+ final String jsonType = node.get(MappingInstructionCodec.TYPE).textValue();
+ if (!instructionToMatch.type().name().equals(jsonType)) {
+ description.appendText("type was " + jsonType);
+ return false;
+ }
+
+ final int jsonWeight = node.get(MappingInstructionCodec.UNICAST_WEIGHT).intValue();
+ final int weight = instructionToMatch.weight();
+ if (jsonWeight != weight) {
+ description.appendText("Unicast weight was " + jsonWeight);
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Matches the contents of an unicast priority mapping instruction.
+ *
+ * @param node JSON instruction to match
+ * @param description object used for recording errors
+ * @return true if contents match, false otherwise
+ */
+ private boolean matchUnicastPriorityInstruction(JsonNode node,
+ Description description) {
+ UnicastMappingInstruction.PriorityMappingInstruction instructionToMatch =
+ (UnicastMappingInstruction.PriorityMappingInstruction) instruction;
+ final String jsonSubtype = node.get(MappingInstructionCodec.SUBTYPE).textValue();
+ if (!instructionToMatch.subtype().name().equals(jsonSubtype)) {
+ description.appendText("subtype was " + jsonSubtype);
+ return false;
+ }
+
+ final String jsonType = node.get(MappingInstructionCodec.TYPE).textValue();
+ if (!instructionToMatch.type().name().equals(jsonType)) {
+ description.appendText("type was " + jsonType);
+ return false;
+ }
+
+ final int jsonPriority = node.get(MappingInstructionCodec.UNICAST_PRIORITY).intValue();
+ final int priority = instructionToMatch.priority();
+ if (jsonPriority != priority) {
+ description.appendText("Unicast priority was " + jsonPriority);
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Matches the contents of a multicast weight mapping instruction.
+ *
+ * @param node JSON instruction to match
+ * @param description object used for recording errors
+ * @return true if contents match, false otherwise
+ */
+ private boolean matchMulticastWeightInstruction(JsonNode node,
+ Description description) {
+ MulticastMappingInstruction.WeightMappingInstruction instructionToMatch =
+ (MulticastMappingInstruction.WeightMappingInstruction) instruction;
+ final String jsonSubtype = node.get(MappingInstructionCodec.SUBTYPE).textValue();
+ if (!instructionToMatch.subtype().name().equals(jsonSubtype)) {
+ description.appendText("subtype was " + jsonSubtype);
+ return false;
+ }
+
+ final String jsonType = node.get(MappingInstructionCodec.TYPE).textValue();
+ if (!instructionToMatch.type().name().equals(jsonType)) {
+ description.appendText("type was " + jsonType);
+ return false;
+ }
+
+ final int jsonWeight = node.get(MappingInstructionCodec.MULTICAST_WEIGHT).intValue();
+ final int weight = instructionToMatch.weight();
+ if (jsonWeight != weight) {
+ description.appendText("Multicast weight was " + jsonWeight);
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Matches the contents of a multicast priority mapping instruction.
+ *
+ * @param node JSON instruction to match
+ * @param description object used for recording errors
+ * @return true if contents match, false otherwise
+ */
+ private boolean matchMulticastPriorityInstruction(JsonNode node,
+ Description description) {
+ MulticastMappingInstruction.PriorityMappingInstruction instructionToMatch =
+ (MulticastMappingInstruction.PriorityMappingInstruction) instruction;
+ final String jsonSubtype = node.get(MappingInstructionCodec.SUBTYPE).textValue();
+ if (!instructionToMatch.subtype().name().equals(jsonSubtype)) {
+ description.appendText("subtype was " + jsonSubtype);
+ return false;
+ }
+
+ final String jsonType = node.get(MappingInstructionCodec.TYPE).textValue();
+ if (!instructionToMatch.type().name().equals(jsonType)) {
+ description.appendText("type was " + jsonType);
+ return false;
+ }
+
+ final int jsonPriority = node.get(MappingInstructionCodec.MULTICAST_PRIORITY).intValue();
+ final int priority = instructionToMatch.priority();
+ if (jsonPriority != priority) {
+ description.appendText("Multicast priority was " + jsonPriority);
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+ // check type
+ final JsonNode jsonTypeNode = jsonNode.get(MappingInstructionCodec.TYPE);
+ final String jsonType = jsonTypeNode.textValue();
+ final String type = instruction.type().name();
+ if (!jsonType.equals(type)) {
+ description.appendText("type was " + type);
+ return false;
+ }
+
+ if (instruction instanceof UnicastMappingInstruction.WeightMappingInstruction) {
+ return matchUnicastWeightInstruction(jsonNode, description);
+ } else if (instruction instanceof UnicastMappingInstruction.PriorityMappingInstruction) {
+ return matchUnicastPriorityInstruction(jsonNode, description);
+ } else if (instruction instanceof MulticastMappingInstruction.WeightMappingInstruction) {
+ return matchMulticastWeightInstruction(jsonNode, description);
+ } else if (instruction instanceof MulticastMappingInstruction.PriorityMappingInstruction) {
+ return matchMulticastPriorityInstruction(jsonNode, description);
+ }
+
+ return false;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(instruction.toString());
+ }
+
+ /**
+ * Factory to allocate a mapping instruction matcher.
+ *
+ * @param instruction instruction object we are looking for
+ * @return matcher
+ */
+ public static MappingInstructionJsonMatcher matchesInstruction(
+ MappingInstruction instruction) {
+ return new MappingInstructionJsonMatcher(instruction);
+ }
+}