[ONOS-6170] Implement codec for MappingInstruction primitives
Change-Id: Ib372de863e6d730c55326ef3450d6f79478592b5
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 2e6878c..9540cc8 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
@@ -22,7 +22,9 @@
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onosproject.codec.CodecService;
import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.instructions.MappingInstruction;
import org.onosproject.mapping.web.codec.MappingAddressCodec;
+import org.onosproject.mapping.web.codec.MappingInstructionCodec;
import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;
@@ -41,6 +43,7 @@
@Activate
public void activate() {
codecService.registerCodec(MappingAddress.class, new MappingAddressCodec());
+ codecService.registerCodec(MappingInstruction.class, new MappingInstructionCodec());
log.info("Started");
}
@@ -48,6 +51,7 @@
@Deactivate
public void deactivate() {
codecService.unregisterCodec(MappingAddress.class);
+ codecService.unregisterCodec(MappingInstruction.class);
log.info("Stopped");
}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/DecodeMappingInstructionCodecHelper.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/DecodeMappingInstructionCodecHelper.java
new file mode 100644
index 0000000..9519439
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/DecodeMappingInstructionCodecHelper.java
@@ -0,0 +1,113 @@
+/*
+ * 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.mapping.instructions.MappingInstruction;
+import org.onosproject.mapping.instructions.MappingInstructions;
+import org.onosproject.mapping.instructions.MulticastMappingInstruction;
+import org.onosproject.mapping.instructions.UnicastMappingInstruction;
+
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * Decoding portion of the mapping instruction codec.
+ */
+public final class DecodeMappingInstructionCodecHelper {
+ private final ObjectNode json;
+ private final CodecContext context;
+
+ /**
+ * Creates a decode mapping instruction codec object.
+ *
+ * @param json JSON object to decode
+ * @param context codec context
+ */
+ public DecodeMappingInstructionCodecHelper(ObjectNode json, CodecContext context) {
+ this.json = json;
+ this.context = context;
+ }
+
+ /**
+ * Decodes an unicast mapping instruction.
+ *
+ * @return mapping instruction object decoded from the JSON
+ * @throws IllegalArgumentException if the JSON is invalid
+ */
+ private MappingInstruction decodeUnicast() {
+ String subType = nullIsIllegal(json.get(MappingInstructionCodec.SUBTYPE),
+ MappingInstructionCodec.SUBTYPE + MappingInstructionCodec.ERROR_MESSAGE).asText();
+
+ if (subType.equals(UnicastMappingInstruction.UnicastType.WEIGHT.name())) {
+ int weight = nullIsIllegal(json.get(MappingInstructionCodec.UNICAST_WEIGHT),
+ MappingInstructionCodec.UNICAST_WEIGHT +
+ MappingInstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
+ return MappingInstructions.unicastWeight(weight);
+ } else if (subType.equals(UnicastMappingInstruction.UnicastType.PRIORITY.name())) {
+ int priority = nullIsIllegal(json.get(MappingInstructionCodec.UNICAST_PRIORITY),
+ MappingInstructionCodec.UNICAST_PRIORITY +
+ MappingInstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
+ return MappingInstructions.unicastPriority(priority);
+ }
+ throw new IllegalArgumentException("Unicast MappingInstruction subtype "
+ + subType + " is not supported");
+ }
+
+ /**
+ * Decodes a multicast mapping instruction.
+ *
+ * @return mapping instruction object decoded from the JSON
+ * @throws IllegalArgumentException if the JSON is invalid
+ */
+ private MappingInstruction decodeMulticast() {
+ String subType = nullIsIllegal(json.get(MappingInstructionCodec.SUBTYPE),
+ MappingInstructionCodec.SUBTYPE + MappingInstructionCodec.ERROR_MESSAGE).asText();
+
+ if (subType.equals(MulticastMappingInstruction.MulticastType.WEIGHT.name())) {
+ int weight = nullIsIllegal(json.get(MappingInstructionCodec.MULTICAST_WEIGHT),
+ MappingInstructionCodec.MULTICAST_WEIGHT +
+ MappingInstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
+ return MappingInstructions.multicastWeight(weight);
+ } else if (subType.equals(MulticastMappingInstruction.MulticastType.PRIORITY.name())) {
+ int priority = nullIsIllegal(json.get(MappingInstructionCodec.MULTICAST_PRIORITY),
+ MappingInstructionCodec.MULTICAST_PRIORITY +
+ MappingInstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
+ return MappingInstructions.multicastPriority(priority);
+ }
+ throw new IllegalArgumentException("Multicast MappingInstruction subtype "
+ + subType + " is not supported");
+ }
+
+ /**
+ * Decodes the JSON into a mapping instruction object.
+ *
+ * @return MappingInstruction object
+ * @throws IllegalArgumentException if the JSON is invalid
+ */
+ public MappingInstruction decode() {
+ String type = nullIsIllegal(json.get(MappingInstructionCodec.TYPE),
+ MappingInstructionCodec.TYPE + MappingInstructionCodec.ERROR_MESSAGE).asText();
+
+ if (type.equals(MappingInstruction.Type.UNICAST.name())) {
+ return decodeUnicast();
+ } else if (type.equals(MappingInstruction.Type.MULTICAST.name())) {
+ return decodeMulticast();
+ }
+ throw new IllegalArgumentException("MappingInstruction type "
+ + type + " is not supported");
+ }
+}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/EncodeMappingInstructionCodecHelper.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/EncodeMappingInstructionCodecHelper.java
new file mode 100644
index 0000000..1ef89d1
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/EncodeMappingInstructionCodecHelper.java
@@ -0,0 +1,129 @@
+/*
+ * 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.mapping.instructions.MappingInstruction;
+import org.onosproject.mapping.instructions.MulticastMappingInstruction;
+import org.onosproject.mapping.instructions.UnicastMappingInstruction;
+import org.slf4j.Logger;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * JSON encoding of MappingInstructions.
+ */
+public final class EncodeMappingInstructionCodecHelper {
+ protected static final Logger log = getLogger(EncodeMappingInstructionCodecHelper.class);
+ private final MappingInstruction instruction;
+ private final CodecContext context;
+
+ /**
+ * Creates a mapping instruction object encoder.
+ *
+ * @param instruction mapping instruction to encode
+ * @param context codec context for the encoding
+ */
+ public EncodeMappingInstructionCodecHelper(MappingInstruction instruction,
+ CodecContext context) {
+ this.instruction = instruction;
+ this.context = context;
+ }
+
+ /**
+ * Encodes an unicast mapping instruction.
+ *
+ * @param result json node that the mapping instruction
+ * attributes are added to
+ */
+ private void encodeUnicast(ObjectNode result) {
+ UnicastMappingInstruction unicastInstruction =
+ (UnicastMappingInstruction) instruction;
+ result.put(MappingInstructionCodec.SUBTYPE, unicastInstruction.subtype().name());
+
+ switch (unicastInstruction.subtype()) {
+ case WEIGHT:
+ UnicastMappingInstruction.WeightMappingInstruction wmi =
+ (UnicastMappingInstruction.WeightMappingInstruction)
+ unicastInstruction;
+ result.put(MappingInstructionCodec.UNICAST_WEIGHT, wmi.weight());
+ break;
+ case PRIORITY:
+ UnicastMappingInstruction.PriorityMappingInstruction pmi =
+ (UnicastMappingInstruction.PriorityMappingInstruction)
+ unicastInstruction;
+ result.put(MappingInstructionCodec.UNICAST_PRIORITY, pmi.priority());
+ break;
+ default:
+ log.info("Cannot convert unicast subtype of {}",
+ unicastInstruction.subtype());
+ }
+ }
+
+ /**
+ * Encodes a multicast mapping instruction.
+ *
+ * @param result json node that the mapping instruction
+ * attributes are added to
+ */
+ private void encodeMulticast(ObjectNode result) {
+ MulticastMappingInstruction multicastMappingInstruction =
+ (MulticastMappingInstruction) instruction;
+ result.put(MappingInstructionCodec.SUBTYPE, multicastMappingInstruction.subtype().name());
+
+ switch (multicastMappingInstruction.subtype()) {
+ case WEIGHT:
+ MulticastMappingInstruction.WeightMappingInstruction wmi =
+ (MulticastMappingInstruction.WeightMappingInstruction)
+ multicastMappingInstruction;
+ result.put(MappingInstructionCodec.MULTICAST_WEIGHT, wmi.weight());
+ break;
+ case PRIORITY:
+ MulticastMappingInstruction.PriorityMappingInstruction pmi =
+ (MulticastMappingInstruction.PriorityMappingInstruction)
+ multicastMappingInstruction;
+ result.put(MappingInstructionCodec.MULTICAST_PRIORITY, pmi.priority());
+ break;
+ default:
+ log.info("Cannot convert multicast subtype of {}",
+ multicastMappingInstruction.subtype());
+ }
+ }
+
+ /**
+ * Encodes the given mapping instruction into JSON.
+ *
+ * @return JSON object node representing the mapping instruction.
+ */
+ public ObjectNode encode() {
+ final ObjectNode result = context.mapper().createObjectNode()
+ .put(MappingInstructionCodec.TYPE, instruction.type().toString());
+
+ switch (instruction.type()) {
+ case UNICAST:
+ encodeUnicast(result);
+ break;
+ case MULTICAST:
+ encodeMulticast(result);
+ break;
+ default:
+ log.info("Cannot convert mapping instruction type of {}", instruction.type());
+ break;
+ }
+ return result;
+ }
+}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/MappingInstructionCodec.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/MappingInstructionCodec.java
new file mode 100644
index 0000000..dff9133
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/MappingInstructionCodec.java
@@ -0,0 +1,52 @@
+/*
+ * 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.instructions.MappingInstruction;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Mapping instruction codec.
+ */
+public final class MappingInstructionCodec extends JsonCodec<MappingInstruction> {
+
+ protected static final Logger log = LoggerFactory.getLogger(MappingInstruction.class);
+
+ protected static final String TYPE = "type";
+ protected static final String SUBTYPE = "subtype";
+ protected static final String UNICAST_WEIGHT = "unicastWeight";
+ protected static final String UNICAST_PRIORITY = "unicastPriority";
+ protected static final String MULTICAST_WEIGHT = "multicastWeight";
+ protected static final String MULTICAST_PRIORITY = "multicastPriority";
+
+ protected static final String MISSING_MEMBER_MESSAGE =
+ " member is required in Instruction";
+ protected static final String ERROR_MESSAGE =
+ " not specified in Instruction";
+
+ @Override
+ public ObjectNode encode(MappingInstruction instruction, CodecContext context) {
+ checkNotNull(instruction, "Mapping instruction cannot be null");
+
+ return new EncodeMappingInstructionCodecHelper(instruction, context).encode();
+ }
+}
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);
+ }
+}