[ONOS-6170] Implement codec for MappingEntry with unit test
Change-Id: Ie5ba1df98ce5dc00c54a5f30d0b2e1c5d0d1062d
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/MappingCodecRegistrator.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/MappingCodecRegistrator.java
index d4d91d2..02ee2c6 100644
--- a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/MappingCodecRegistrator.java
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/MappingCodecRegistrator.java
@@ -23,6 +23,7 @@
import org.onosproject.codec.CodecService;
import org.onosproject.mapping.actions.MappingAction;
import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.codec.MappingEntryCodec;
import org.onosproject.mapping.instructions.MappingInstruction;
import org.onosproject.mapping.codec.MappingActionCodec;
import org.onosproject.mapping.codec.MappingAddressCodec;
@@ -53,6 +54,7 @@
codecService.registerCodec(MappingTreatment.class, new MappingTreatmentCodec());
codecService.registerCodec(MappingKey.class, new MappingKeyCodec());
codecService.registerCodec(MappingValue.class, new MappingValueCodec());
+ codecService.registerCodec(MappingEntry.class, new MappingEntryCodec());
log.info("Started");
}
@@ -65,6 +67,7 @@
codecService.unregisterCodec(MappingTreatment.class);
codecService.unregisterCodec(MappingKey.class);
codecService.unregisterCodec(MappingValue.class);
+ codecService.unregisterCodec(MappingEntry.class);
log.info("Stopped");
}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingEntryCodec.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingEntryCodec.java
new file mode 100644
index 0000000..0966008
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingEntryCodec.java
@@ -0,0 +1,62 @@
+/*
+ * 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.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.mapping.MappingEntry;
+import org.onosproject.mapping.MappingKey;
+import org.onosproject.mapping.MappingValue;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Mapping entry JSON codec.
+ */
+public final class MappingEntryCodec extends JsonCodec<MappingEntry> {
+
+ protected static final String KEY = "key";
+ protected static final String VALUE = "value";
+ protected static final String ID = "id";
+ protected static final String DEVICE_ID = "deviceId";
+ protected static final String STATE = "state";
+
+ @Override
+ public ObjectNode encode(MappingEntry mappingEntry, CodecContext context) {
+
+ checkNotNull(mappingEntry, "Mapping entry cannot be null");
+
+ final ObjectNode result = context.mapper().createObjectNode()
+ .put(ID, Long.toString(mappingEntry.id().value()))
+ .put(DEVICE_ID, mappingEntry.deviceId().toString())
+ .put(STATE, mappingEntry.state().toString());
+
+ if (mappingEntry.key() != null) {
+ final JsonCodec<MappingKey> keyCodec =
+ context.codec(MappingKey.class);
+ result.set(KEY, keyCodec.encode(mappingEntry.key(), context));
+ }
+
+ if (mappingEntry.value() != null) {
+ final JsonCodec<MappingValue> valueCodec =
+ context.codec(MappingValue.class);
+ result.set(VALUE, valueCodec.encode(mappingEntry.value(), context));
+ }
+
+ return result;
+ }
+}
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingEntryCodecTest.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingEntryCodecTest.java
new file mode 100644
index 0000000..c76de5f
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingEntryCodecTest.java
@@ -0,0 +1,218 @@
+/*
+ * 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.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+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.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.mapping.DefaultMapping;
+import org.onosproject.mapping.DefaultMappingEntry;
+import org.onosproject.mapping.DefaultMappingKey;
+import org.onosproject.mapping.DefaultMappingTreatment;
+import org.onosproject.mapping.DefaultMappingValue;
+import org.onosproject.mapping.Mapping;
+import org.onosproject.mapping.MappingCodecRegistrator;
+import org.onosproject.mapping.MappingEntry;
+import org.onosproject.mapping.MappingEntry.MappingEntryState;
+import org.onosproject.mapping.MappingKey;
+import org.onosproject.mapping.MappingTreatment;
+import org.onosproject.mapping.MappingValue;
+import org.onosproject.mapping.actions.MappingAction;
+import org.onosproject.mapping.actions.MappingActions;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.addresses.MappingAddresses;
+import org.onosproject.mapping.codec.MappingKeyCodecTest.MappingKeyJsonMatcher;
+import org.onosproject.mapping.codec.MappingValueCodecTest.MappingValueJsonMatcher;
+import org.onosproject.mapping.instructions.MappingInstruction;
+import org.onosproject.mapping.instructions.MappingInstructions;
+import org.onosproject.net.DeviceId;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+
+/**
+ * Unit tests for MappingEntryCodec.
+ */
+public class MappingEntryCodecTest {
+
+ 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 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;
+
+ private static final long ID = 1L;
+ private static final DeviceId DEVICE_ID = DeviceId.deviceId("lisp:5.6.7.8");
+ private static final MappingEntryState STATE = MappingEntryState.ADDED;
+
+ private CodecContext context;
+ private JsonCodec<MappingEntry> entryCodec;
+ private MappingCodecRegistrator registrator;
+
+ /**
+ * Sets up for each test.
+ * Creates a context and fetches the mapping entry codec.
+ */
+ @Before
+ public void setUp() {
+ CodecManager manager = new CodecManager();
+ registrator = new MappingCodecRegistrator();
+ registrator.codecService = manager;
+ registrator.activate();
+
+ context = new MappingCodecContextAdapter(registrator.codecService);
+ entryCodec = context.codec(MappingEntry.class);
+ assertThat(entryCodec, notNullValue());
+ }
+
+ /**
+ * Deactivates the codec registrator.
+ */
+ @After
+ public void tearDown() {
+ registrator.deactivate();
+ }
+
+ /**
+ * Tests encoding of a mapping entry object.
+ */
+ @Test
+ public void testMappingEntryEncode() {
+ MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX);
+
+ MappingInstruction unicastWeight = MappingInstructions.unicastWeight(UNICAST_WEIGHT);
+ MappingInstruction unicastPriority = MappingInstructions.unicastPriority(UNICAST_PRIORITY);
+ MappingInstruction multicastWeight = MappingInstructions.multicastWeight(MULTICAST_WEIGHT);
+ MappingInstruction multicastPriority = MappingInstructions.multicastPriority(MULTICAST_PRIORITY);
+
+ MappingKey key = DefaultMappingKey.builder()
+ .withAddress(address)
+ .build();
+
+ MappingTreatment treatment = DefaultMappingTreatment.builder()
+ .add(unicastWeight)
+ .add(unicastPriority)
+ .add(multicastWeight)
+ .add(multicastPriority)
+ .withAddress(address)
+ .build();
+
+ MappingAction action = MappingActions.noAction();
+
+ MappingValue value = DefaultMappingValue.builder()
+ .add(treatment)
+ .withAction(action)
+ .build();
+
+ Mapping mapping = DefaultMapping.builder()
+ .withId(ID)
+ .forDevice(DEVICE_ID)
+ .withKey(key)
+ .withValue(value)
+ .build();
+
+ MappingEntry entry = new DefaultMappingEntry(mapping, STATE);
+
+ ObjectNode entryJson = entryCodec.encode(entry, context);
+ assertThat(entryJson, MappingEntryJsonMatcher.matchesMappingEntry(entry));
+ }
+
+ /**
+ * Hamcrest matcher for mapping entry.
+ */
+ public static final class MappingEntryJsonMatcher
+ extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final MappingEntry mappingEntry;
+
+ /**
+ * A default constructor.
+ *
+ * @param mappingEntry mapping entry
+ */
+ private MappingEntryJsonMatcher(MappingEntry mappingEntry) {
+ this.mappingEntry = mappingEntry;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+ // check mapping id
+ final JsonNode jsonIdNode = jsonNode.get(MappingEntryCodec.ID);
+ final long jsonId = jsonIdNode.asLong();
+ final long id = mappingEntry.id().value();
+ if (jsonId != id) {
+ description.appendText("mapping id was " + id);
+ return false;
+ }
+
+ // check device id
+ final JsonNode jsonDeviceIdNode = jsonNode.get(MappingEntryCodec.DEVICE_ID);
+ final String jsonDeviceId = jsonDeviceIdNode.textValue();
+ final String deviceId = mappingEntry.deviceId().toString();
+ if (!jsonDeviceId.equals(deviceId)) {
+ description.appendText("device id was " + deviceId);
+ return false;
+ }
+
+ // check state
+ final JsonNode jsonStateNode = jsonNode.get(MappingEntryCodec.STATE);
+ final String jsonState = jsonStateNode.textValue();
+ final String state = mappingEntry.state().name();
+ if (!jsonState.equals(state)) {
+ description.appendText("state was " + state);
+ return false;
+ }
+
+ // check mapping key
+ final JsonNode jsonKeyNode = jsonNode.get(MappingEntryCodec.KEY);
+ assertThat(jsonKeyNode, MappingKeyJsonMatcher.matchesMappingKey(mappingEntry.key()));
+
+ // check mapping value
+ final JsonNode jsonValueNode = jsonNode.get(MappingEntryCodec.VALUE);
+ assertThat(jsonValueNode, MappingValueJsonMatcher.matchesMappingValue(mappingEntry.value()));
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(mappingEntry.toString());
+ }
+
+ /**
+ * Factory to allocate a mapping entry matcher.
+ *
+ * @param entry mapping entry object we are looking for
+ * @return matcher
+ */
+ public static MappingEntryJsonMatcher matchesMappingEntry(MappingEntry entry) {
+ return new MappingEntryJsonMatcher(entry);
+ }
+ }
+}
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingValueCodecTest.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingValueCodecTest.java
index 22fa13a..f4b394e 100644
--- a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingValueCodecTest.java
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingValueCodecTest.java
@@ -94,7 +94,7 @@
* Tests encoding of a mapping value object.
*/
@Test
- public void testMapingValueEncode() {
+ public void testMappingValueEncode() {
MappingInstruction unicastWeight = MappingInstructions.unicastWeight(UNICAST_WEIGHT);
MappingInstruction unicastPriority = MappingInstructions.unicastPriority(UNICAST_PRIORITY);
MappingInstruction multicastWeight = MappingInstructions.multicastWeight(MULTICAST_WEIGHT);