Fix: Split codec package into api module to fix maven build failure
Change-Id: I5f57163f329ab21bb362dcf2bef54a3a6664ce10
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
new file mode 100644
index 0000000..d4d91d2
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/MappingCodecRegistrator.java
@@ -0,0 +1,71 @@
+/*
+ * 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;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.codec.CodecService;
+import org.onosproject.mapping.actions.MappingAction;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.instructions.MappingInstruction;
+import org.onosproject.mapping.codec.MappingActionCodec;
+import org.onosproject.mapping.codec.MappingAddressCodec;
+import org.onosproject.mapping.codec.MappingInstructionCodec;
+import org.onosproject.mapping.codec.MappingKeyCodec;
+import org.onosproject.mapping.codec.MappingTreatmentCodec;
+import org.onosproject.mapping.codec.MappingValueCodec;
+import org.slf4j.Logger;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of the JSON codec brokering service for mapping primitives.
+ */
+@Component(immediate = true)
+public class MappingCodecRegistrator {
+
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ public CodecService codecService;
+
+ @Activate
+ public void activate() {
+ codecService.registerCodec(MappingAddress.class, new MappingAddressCodec());
+ codecService.registerCodec(MappingInstruction.class, new MappingInstructionCodec());
+ codecService.registerCodec(MappingAction.class, new MappingActionCodec());
+ codecService.registerCodec(MappingTreatment.class, new MappingTreatmentCodec());
+ codecService.registerCodec(MappingKey.class, new MappingKeyCodec());
+ codecService.registerCodec(MappingValue.class, new MappingValueCodec());
+
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ codecService.unregisterCodec(MappingAddress.class);
+ codecService.unregisterCodec(MappingInstruction.class);
+ codecService.unregisterCodec(MappingAction.class);
+ codecService.unregisterCodec(MappingTreatment.class);
+ codecService.unregisterCodec(MappingKey.class);
+ codecService.unregisterCodec(MappingValue.class);
+
+ log.info("Stopped");
+ }
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/DecodeMappingActionCodecHelper.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/DecodeMappingActionCodecHelper.java
new file mode 100644
index 0000000..d0c1065
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/DecodeMappingActionCodecHelper.java
@@ -0,0 +1,97 @@
+/*
+ * 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.mapping.actions.MappingAction;
+import org.onosproject.mapping.actions.MappingActions;
+
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * Decode portion of the mapping action codec.
+ */
+public final class DecodeMappingActionCodecHelper {
+ private final ObjectNode json;
+
+ /**
+ * Creates a decode mapping action codec object.
+ *
+ * @param json JSON object to decode
+ */
+ public DecodeMappingActionCodecHelper(ObjectNode json) {
+ this.json = json;
+ }
+
+ /**
+ * Decodes a no mapping action.
+ *
+ * @return mapping action object decoded from the JSON
+ */
+ private MappingAction decodeNoAction() {
+ return MappingActions.noAction();
+ }
+
+ /**
+ * Decodes a forward mapping action.
+ *
+ * @return mapping action object decoded from the JSON
+ */
+ private MappingAction decodeForwardAction() {
+ return MappingActions.forward();
+ }
+
+ /**
+ * Decodes a native forward mapping action.
+ *
+ * @return mapping action object decoded from the JSON
+ */
+ private MappingAction decodeNativeForwardAction() {
+ return MappingActions.nativeForward();
+ }
+
+ /**
+ * Decodes a drop mapping action.
+ *
+ * @return mapping action object decoded from the JSON
+ */
+ private MappingAction decodeDropAction() {
+ return MappingActions.drop();
+ }
+
+ /**
+ * Decodes the JSON into a mapping action object.
+ *
+ * @return MappingAction object
+ * @throws IllegalArgumentException if the JSON is invalid
+ */
+ public MappingAction decode() {
+ String type = nullIsIllegal(json.get(MappingActionCodec.TYPE),
+ MappingActionCodec.TYPE + MappingActionCodec.ERROR_MESSAGE).asText();
+
+ if (type.equals(MappingAction.Type.DROP.name())) {
+ return decodeDropAction();
+ } else if (type.equals(MappingAction.Type.FORWARD.name())) {
+ return decodeForwardAction();
+ } else if (type.equals(MappingAction.Type.NATIVE_FORWARD.name())) {
+ return decodeNativeForwardAction();
+ } else if (type.equals(MappingAction.Type.NO_ACTION.name())) {
+ return decodeNoAction();
+ }
+ throw new IllegalArgumentException("MappingAction type "
+ + type + " is not supported");
+ }
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/DecodeMappingAddressCodecHelper.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/DecodeMappingAddressCodecHelper.java
new file mode 100644
index 0000000..32eb130
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/DecodeMappingAddressCodecHelper.java
@@ -0,0 +1,148 @@
+/*
+ * 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 com.google.common.collect.Maps;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.addresses.MappingAddresses;
+
+import java.util.Map;
+
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * Decode portion of the mapping address codec.
+ */
+public final class DecodeMappingAddressCodecHelper {
+
+ private final ObjectNode json;
+
+ protected static final String MISSING_MEMBER_MESSAGE =
+ " member is required in Mapping Address";
+
+ private final Map<String, MappingAddressDecoder> decoderMap;
+
+ /**
+ * Creates a decode mapping address codec object.
+ * Initializes the lookup map for mapping address subclass decoders.
+ *
+ * @param json JSON object to decode
+ */
+ public DecodeMappingAddressCodecHelper(ObjectNode json) {
+ this.json = json;
+ decoderMap = Maps.newHashMap();
+
+ decoderMap.put(MappingAddress.Type.IPV4.name(), new Ipv4TypeDecoder());
+ decoderMap.put(MappingAddress.Type.IPV6.name(), new Ipv6TypeDecoder());
+ decoderMap.put(MappingAddress.Type.AS.name(), new AsTypeDecoder());
+ decoderMap.put(MappingAddress.Type.DN.name(), new DnTypeDecoder());
+ decoderMap.put(MappingAddress.Type.ETH.name(), new EthTypeDecoder());
+ }
+
+ /**
+ * An interface of mapping address type decoder.
+ */
+ private interface MappingAddressDecoder {
+ MappingAddress decodeMappingAddress(ObjectNode json);
+ }
+
+ /**
+ * Implementation of IPv4 mapping address decoder.
+ */
+ private class Ipv4TypeDecoder implements MappingAddressDecoder {
+
+ @Override
+ public MappingAddress decodeMappingAddress(ObjectNode json) {
+ String ip = nullIsIllegal(json.get(MappingAddressCodec.IPV4),
+ MappingAddressCodec.IPV4 + MISSING_MEMBER_MESSAGE).asText();
+ return MappingAddresses.ipv4MappingAddress(IpPrefix.valueOf(ip));
+ }
+ }
+
+ /**
+ * Implementation of IPv6 mapping address decoder.
+ */
+ private class Ipv6TypeDecoder implements MappingAddressDecoder {
+
+ @Override
+ public MappingAddress decodeMappingAddress(ObjectNode json) {
+ String ip = nullIsIllegal(json.get(MappingAddressCodec.IPV6),
+ MappingAddressCodec.IPV6 + MISSING_MEMBER_MESSAGE).asText();
+ return MappingAddresses.ipv6MappingAddress(IpPrefix.valueOf(ip));
+ }
+ }
+
+ /**
+ * Implementation of AS mapping address decoder.
+ */
+ private class AsTypeDecoder implements MappingAddressDecoder {
+
+ @Override
+ public MappingAddress decodeMappingAddress(ObjectNode json) {
+ String as = nullIsIllegal(json.get(MappingAddressCodec.AS),
+ MappingAddressCodec.AS + MISSING_MEMBER_MESSAGE).asText();
+ return MappingAddresses.asMappingAddress(as);
+ }
+ }
+
+ /**
+ * Implementation of DN mapping address decoder.
+ */
+ private class DnTypeDecoder implements MappingAddressDecoder {
+
+ @Override
+ public MappingAddress decodeMappingAddress(ObjectNode json) {
+ String dn = nullIsIllegal(json.get(MappingAddressCodec.DN),
+ MappingAddressCodec.DN + MISSING_MEMBER_MESSAGE).asText();
+ return MappingAddresses.dnMappingAddress(dn);
+ }
+ }
+
+ /**
+ * Implementation of Ethernet mapping address decoder.
+ */
+ private class EthTypeDecoder implements MappingAddressDecoder {
+
+ @Override
+ public MappingAddress decodeMappingAddress(ObjectNode json) {
+ MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(MappingAddressCodec.MAC),
+ MappingAddressCodec.MAC + MISSING_MEMBER_MESSAGE).asText());
+ return MappingAddresses.ethMappingAddress(mac);
+ }
+ }
+
+ /**
+ * Decodes the JSON into a mapping address object.
+ *
+ * @return MappingAddress object
+ * @throws IllegalArgumentException if the JSON is invalid
+ */
+ public MappingAddress decode() {
+ String type =
+ nullIsIllegal(json.get(MappingAddressCodec.TYPE),
+ "Type not specified").asText();
+
+ MappingAddressDecoder decoder = decoderMap.get(type);
+ if (decoder != null) {
+ return decoder.decodeMappingAddress(json);
+ }
+
+ throw new IllegalArgumentException("Type " + type + " is unknown");
+ }
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/DecodeMappingInstructionCodecHelper.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/DecodeMappingInstructionCodecHelper.java
new file mode 100644
index 0000000..1b94692
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/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.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/api/src/main/java/org/onosproject/mapping/codec/EncodeMappingActionCodecHelper.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/EncodeMappingActionCodecHelper.java
new file mode 100644
index 0000000..7c42544
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/EncodeMappingActionCodecHelper.java
@@ -0,0 +1,117 @@
+/*
+ * 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.mapping.actions.MappingAction;
+import org.onosproject.mapping.actions.NoMappingAction;
+import org.onosproject.mapping.actions.DropMappingAction;
+import org.onosproject.mapping.actions.ForwardMappingAction;
+import org.onosproject.mapping.actions.NativeForwardMappingAction;
+import org.slf4j.Logger;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Encode portion of the mapping action codec.
+ */
+public final class EncodeMappingActionCodecHelper {
+ protected static final Logger log = getLogger(EncodeMappingActionCodecHelper.class);
+ private final MappingAction action;
+ private final CodecContext context;
+
+ /**
+ * Creates a mapping action object encoder.
+ *
+ * @param action mapping action to encode
+ * @param context codec context for the encoding
+ */
+ public EncodeMappingActionCodecHelper(MappingAction action,
+ CodecContext context) {
+ this.action = action;
+ this.context = context;
+ }
+
+ /**
+ * Encodes a no mapping action.
+ *
+ * @param result json node that the mapping action attributes are added to
+ */
+ private void encodeNoMappingAction(ObjectNode result) {
+ NoMappingAction noMappingAction = (NoMappingAction) action;
+ result.put(MappingActionCodec.TYPE, noMappingAction.type().name());
+ }
+
+ /**
+ * Encodes a drop mapping action.
+ *
+ * @param result json node that the mapping action attributes are added to
+ */
+ private void encodeDropMappingAction(ObjectNode result) {
+ DropMappingAction dropMappingAction = (DropMappingAction) action;
+ result.put(MappingActionCodec.TYPE, dropMappingAction.type().name());
+ }
+
+ /**
+ * Encodes a forward mapping action.
+ *
+ * @param result json node that the mapping action attributes are added to
+ */
+ private void encodeForwardMappingAction(ObjectNode result) {
+ ForwardMappingAction forwardMappingAction = (ForwardMappingAction) action;
+ result.put(MappingActionCodec.TYPE, forwardMappingAction.type().name());
+ }
+
+ /**
+ * Encodes a native forward mapping action.
+ *
+ * @param result json node that the mapping action attributes are added to
+ */
+ private void encodeNativeForwardMappingAction(ObjectNode result) {
+ NativeForwardMappingAction nativeMappingAction = (NativeForwardMappingAction) action;
+ result.put(MappingActionCodec.TYPE, nativeMappingAction.type().name());
+ }
+
+ /**
+ * Encodes the given mapping instruction into JSON.
+ *
+ * @return JSON object node representing the mapping action
+ */
+ public ObjectNode encode() {
+ final ObjectNode result = context.mapper().createObjectNode()
+ .put(MappingActionCodec.TYPE, action.type().toString());
+
+ switch (action.type()) {
+ case DROP:
+ encodeDropMappingAction(result);
+ break;
+ case FORWARD:
+ encodeForwardMappingAction(result);
+ break;
+ case NATIVE_FORWARD:
+ encodeNativeForwardMappingAction(result);
+ break;
+ case NO_ACTION:
+ encodeNoMappingAction(result);
+ break;
+ default:
+ log.info("Cannot convert mapping action type of {}", action.type());
+ break;
+ }
+ return result;
+ }
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/EncodeMappingAddressCodecHelper.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/EncodeMappingAddressCodecHelper.java
new file mode 100644
index 0000000..8847bca
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/EncodeMappingAddressCodecHelper.java
@@ -0,0 +1,168 @@
+/*
+ * 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.mapping.addresses.ASMappingAddress;
+import org.onosproject.mapping.addresses.DNMappingAddress;
+import org.onosproject.mapping.addresses.EthMappingAddress;
+import org.onosproject.mapping.addresses.IPMappingAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+
+import java.util.EnumMap;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Encode portion of the mapping address codec.
+ */
+public final class EncodeMappingAddressCodecHelper {
+
+ private final MappingAddress address;
+ private final CodecContext context;
+
+ private final EnumMap<MappingAddress.Type, MappingAddressTypeFormatter> formatMap;
+
+ /**
+ * Creates an encoder object for a mapping address.
+ * Initializes the formatter lookup map for the mapping address subclasses.
+ *
+ * @param address MappingAddress to encode
+ * @param context context of the JSON encoding
+ */
+ public EncodeMappingAddressCodecHelper(MappingAddress address, CodecContext context) {
+ this.address = address;
+ this.context = context;
+
+ formatMap = new EnumMap<>(MappingAddress.Type.class);
+
+ formatMap.put(MappingAddress.Type.IPV4, new FormatIpv4());
+ formatMap.put(MappingAddress.Type.IPV6, new FormatIpv6());
+ formatMap.put(MappingAddress.Type.AS, new FormatAs());
+ formatMap.put(MappingAddress.Type.DN, new FormatDn());
+ formatMap.put(MappingAddress.Type.ETH, new FormatEth());
+
+ // TODO: not process extension mapping address for now
+ formatMap.put(MappingAddress.Type.EXTENSION, new FormatUnknown());
+ }
+
+ /**
+ * An interface of mapping address type formatter.
+ */
+ private interface MappingAddressTypeFormatter {
+ ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address);
+ }
+
+ /**
+ * Implementation of IPv4 mapping address type formatter.
+ */
+ private static class FormatIpv4 implements MappingAddressTypeFormatter {
+
+ @Override
+ public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+ final IPMappingAddress ipv4 = (IPMappingAddress) address;
+ return root.put(MappingAddressCodec.IPV4, ipv4.ip().toString());
+ }
+ }
+
+ /**
+ * Implementation of IPv6 mapping address type formatter.
+ */
+ private static class FormatIpv6 implements MappingAddressTypeFormatter {
+
+ @Override
+ public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+ final IPMappingAddress ipv6 = (IPMappingAddress) address;
+ return root.put(MappingAddressCodec.IPV6, ipv6.ip().toString());
+ }
+ }
+
+ /**
+ * Implementation of AS mapping address type formatter.
+ */
+ private static class FormatAs implements MappingAddressTypeFormatter {
+
+ @Override
+ public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+ final ASMappingAddress as = (ASMappingAddress) address;
+ return root.put(MappingAddressCodec.AS, as.asNumber());
+ }
+ }
+
+ /**
+ * Implementation of Distinguished Name mapping address type formatter.
+ */
+ private static class FormatDn implements MappingAddressTypeFormatter {
+
+ @Override
+ public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+ final DNMappingAddress dn = (DNMappingAddress) address;
+ return root.put(MappingAddressCodec.DN, dn.name());
+ }
+ }
+
+ /**
+ * Implementation of Ethernet mapping address type formatter.
+ */
+ private static class FormatEth implements MappingAddressTypeFormatter {
+
+ @Override
+ public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+ final EthMappingAddress eth = (EthMappingAddress) address;
+ return root.put(MappingAddressCodec.MAC, eth.mac().toString());
+ }
+ }
+
+ /**
+ * Implementation of Extension mapping address type formatter.
+ */
+ private static class FormatExtension implements MappingAddressTypeFormatter {
+
+ @Override
+ public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+ return null;
+ }
+ }
+
+ /**
+ * Implementation of Unknown mapping address type formatter.
+ */
+ private static class FormatUnknown implements MappingAddressTypeFormatter {
+
+ @Override
+ public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+ return root;
+ }
+ }
+
+ /**
+ * Encodes a mapping address into a JSON node.
+ *
+ * @return encoded JSON object for the given mapping address
+ */
+ public ObjectNode encode() {
+ final ObjectNode result = context.mapper().createObjectNode()
+ .put(MappingAddressCodec.TYPE, address.type().toString());
+
+ MappingAddressTypeFormatter formatter =
+ checkNotNull(formatMap.get(address.type()),
+ "No formatter found for mapping address type "
+ + address.type().toString());
+
+ return formatter.encodeMappingAddress(result, address);
+ }
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/EncodeMappingInstructionCodecHelper.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/EncodeMappingInstructionCodecHelper.java
new file mode 100644
index 0000000..53343a6
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/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.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/api/src/main/java/org/onosproject/mapping/codec/MappingActionCodec.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingActionCodec.java
new file mode 100644
index 0000000..511f862
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingActionCodec.java
@@ -0,0 +1,54 @@
+/*
+ * 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.actions.MappingAction;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Mapping action codec.
+ */
+public final class MappingActionCodec extends JsonCodec<MappingAction> {
+
+ protected static final Logger log =
+ LoggerFactory.getLogger(MappingActionCodec.class);
+
+ protected static final String TYPE = "type";
+ protected static final String ERROR_MESSAGE =
+ " not specified in MappingAction";
+
+ @Override
+ public ObjectNode encode(MappingAction action, CodecContext context) {
+ EncodeMappingActionCodecHelper encoder =
+ new EncodeMappingActionCodecHelper(action, context);
+ return encoder.encode();
+ }
+
+ @Override
+ public MappingAction decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ DecodeMappingActionCodecHelper decoder =
+ new DecodeMappingActionCodecHelper(json);
+ return decoder.decode();
+ }
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingAddressCodec.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingAddressCodec.java
new file mode 100644
index 0000000..7b756b6
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingAddressCodec.java
@@ -0,0 +1,57 @@
+/*
+ * 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.addresses.MappingAddress;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Mapping address codec.
+ */
+public final class MappingAddressCodec extends JsonCodec<MappingAddress> {
+
+ protected static final Logger log =
+ LoggerFactory.getLogger(MappingAddressCodec.class);
+
+ protected static final String TYPE = "type";
+ protected static final String IPV4 = "ipv4";
+ protected static final String IPV6 = "ipv6";
+ protected static final String MAC = "mac";
+ protected static final String DN = "dn";
+ protected static final String AS = "as";
+
+ @Override
+ public ObjectNode encode(MappingAddress address, CodecContext context) {
+ EncodeMappingAddressCodecHelper encoder =
+ new EncodeMappingAddressCodecHelper(address, context);
+ return encoder.encode();
+ }
+
+ @Override
+ public MappingAddress decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ DecodeMappingAddressCodecHelper decoder =
+ new DecodeMappingAddressCodecHelper(json);
+ return decoder.decode();
+ }
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingInstructionCodec.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingInstructionCodec.java
new file mode 100644
index 0000000..c881269
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingInstructionCodec.java
@@ -0,0 +1,61 @@
+/*
+ * 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.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();
+ }
+
+ @Override
+ public MappingInstruction decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ return new DecodeMappingInstructionCodecHelper(json, context).decode();
+ }
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingKeyCodec.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingKeyCodec.java
new file mode 100644
index 0000000..1b24545
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/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.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/api/src/main/java/org/onosproject/mapping/codec/MappingTreatmentCodec.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingTreatmentCodec.java
new file mode 100644
index 0000000..6fd37ff
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingTreatmentCodec.java
@@ -0,0 +1,90 @@
+/*
+ * 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.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.mapping.DefaultMappingTreatment;
+import org.onosproject.mapping.MappingTreatment;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.instructions.MappingInstruction;
+
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Mapping treatment codec.
+ */
+public final class MappingTreatmentCodec extends JsonCodec<MappingTreatment> {
+
+ private static final String INSTRUCTIONS = "instructions";
+ private static final String ADDRESS = "address";
+
+ @Override
+ public ObjectNode encode(MappingTreatment treatment, CodecContext context) {
+ checkNotNull(treatment, "Mapping treatment cannot be null");
+
+ final ObjectNode result = context.mapper().createObjectNode();
+ final ArrayNode jsonInstructions = result.putArray(INSTRUCTIONS);
+
+ final JsonCodec<MappingInstruction> instructionCodec =
+ context.codec(MappingInstruction.class);
+
+ final JsonCodec<MappingAddress> addressCodec =
+ context.codec(MappingAddress.class);
+
+ for (final MappingInstruction instruction : treatment.instructions()) {
+ jsonInstructions.add(instructionCodec.encode(instruction, context));
+ }
+
+ result.set(ADDRESS, addressCodec.encode(treatment.address(), context));
+
+ return result;
+ }
+
+ @Override
+ public MappingTreatment decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ final JsonCodec<MappingInstruction> instructionCodec =
+ context.codec(MappingInstruction.class);
+
+ JsonNode instructionJson = json.get(INSTRUCTIONS);
+ MappingTreatment.Builder builder = DefaultMappingTreatment.builder();
+
+ if (instructionJson != null) {
+ IntStream.range(0, instructionJson.size())
+ .forEach(i -> builder.add(
+ instructionCodec.decode(get(instructionJson, i),
+ context)));
+ }
+
+ 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/api/src/main/java/org/onosproject/mapping/codec/MappingValueCodec.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingValueCodec.java
new file mode 100644
index 0000000..d97635b
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/MappingValueCodec.java
@@ -0,0 +1,90 @@
+/*
+ * 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.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.mapping.DefaultMappingValue;
+import org.onosproject.mapping.MappingTreatment;
+import org.onosproject.mapping.MappingValue;
+import org.onosproject.mapping.actions.MappingAction;
+
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Mapping value codec.
+ */
+public final class MappingValueCodec extends JsonCodec<MappingValue> {
+
+ protected static final String ACTION = "action";
+ protected static final String TREATMENTS = "treatments";
+
+ @Override
+ public ObjectNode encode(MappingValue value, CodecContext context) {
+ checkNotNull(value, "Mapping value cannot be null");
+
+ final ObjectNode result = context.mapper().createObjectNode();
+ final ArrayNode jsonTreatments = result.putArray(TREATMENTS);
+
+ final JsonCodec<MappingTreatment> treatmentCodec =
+ context.codec(MappingTreatment.class);
+
+ final JsonCodec<MappingAction> actionCodec =
+ context.codec(MappingAction.class);
+
+ for (final MappingTreatment treatment : value.treatments()) {
+ jsonTreatments.add(treatmentCodec.encode(treatment, context));
+ }
+
+ result.set(ACTION, actionCodec.encode(value.action(), context));
+
+ return result;
+ }
+
+ @Override
+ public MappingValue decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ final JsonCodec<MappingTreatment> treatmentCodec =
+ context.codec(MappingTreatment.class);
+
+ JsonNode treatmentJson = json.get(TREATMENTS);
+ MappingValue.Builder builder = DefaultMappingValue.builder();
+
+ if (treatmentJson != null) {
+ IntStream.range(0, treatmentJson.size())
+ .forEach(i -> builder.add(
+ treatmentCodec.decode(get(treatmentJson, i),
+ context)));
+ }
+
+ ObjectNode actionJson = get(json, ACTION);
+ if (actionJson != null) {
+ final JsonCodec<MappingAction> actionCodec =
+ context.codec(MappingAction.class);
+ builder.withAction(actionCodec.decode(actionJson, context));
+ }
+
+ return builder.build();
+ }
+}
diff --git a/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/package-info.java b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/package-info.java
new file mode 100644
index 0000000..68b4778
--- /dev/null
+++ b/apps/mappingmanagement/api/src/main/java/org/onosproject/mapping/codec/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+/**
+ * Implementations of the codec broker and
+ * built-in entity JSON codecs for mapping address.
+ */
+package org.onosproject.mapping.codec;
\ No newline at end of file
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingActionCodecTest.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingActionCodecTest.java
new file mode 100644
index 0000000..555b67d
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingActionCodecTest.java
@@ -0,0 +1,143 @@
+/*
+ * 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.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.mapping.actions.MappingAction;
+import org.onosproject.mapping.actions.MappingActions;
+import org.onosproject.mapping.actions.NoMappingAction;
+import org.onosproject.mapping.actions.DropMappingAction;
+import org.onosproject.mapping.actions.ForwardMappingAction;
+import org.onosproject.mapping.actions.NativeForwardMappingAction;
+import org.onosproject.mapping.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;
+import static org.onosproject.mapping.codec.MappingActionJsonMatcher.matchesAction;
+
+/**
+ * Unit tests for MappingActionCodec.
+ */
+public class MappingActionCodecTest {
+
+ private static final String NO_ACTION_STRING = "NO_ACTION";
+
+ private CodecContext context;
+ private JsonCodec<MappingAction> actionCodec;
+ private MappingCodecRegistrator registrator;
+
+ /**
+ * Sets up for each test.
+ * Creates a context and fetches the mapping action codec.
+ */
+ @Before
+ public void setUp() {
+ CodecManager manager = new CodecManager();
+ registrator = new MappingCodecRegistrator();
+ registrator.codecService = manager;
+ registrator.activate();
+
+ context = new MappingCodecContextAdapter(registrator.codecService);
+ actionCodec = context.codec(MappingAction.class);
+ assertThat(actionCodec, notNullValue());
+ }
+
+ /**
+ * Deactivates the codec registrator.
+ */
+ @After
+ public void tearDown() {
+ registrator.deactivate();
+ }
+
+ /**
+ * Tests the encoding of no mapping action.
+ */
+ @Test
+ public void noActionTest() {
+ final NoMappingAction action = MappingActions.noAction();
+ final ObjectNode actionJson = actionCodec.encode(action, context);
+ assertThat(actionJson, matchesAction(action));
+ }
+
+ /**
+ * Tests the encoding of drop mapping action.
+ */
+ @Test
+ public void dropActionTest() {
+ final DropMappingAction action = MappingActions.drop();
+ final ObjectNode actionJson = actionCodec.encode(action, context);
+ assertThat(actionJson, matchesAction(action));
+ }
+
+ /**
+ * Tests the encoding of forward mapping action.
+ */
+ @Test
+ public void forwardActionTest() {
+ final ForwardMappingAction action = MappingActions.forward();
+ final ObjectNode actionJson = actionCodec.encode(action, context);
+ assertThat(actionJson, matchesAction(action));
+ }
+
+ /**
+ * Tests the encoding of native forwarding mapping action.
+ */
+ @Test
+ public void nativeForwardActionTest() {
+ final NativeForwardMappingAction action = MappingActions.nativeForward();
+ final ObjectNode actionJson = actionCodec.encode(action, context);
+ assertThat(actionJson, matchesAction(action));
+ }
+
+ /**
+ * Tests decoding of a mapping key JSON object.
+ *
+ * @throws IOException if processing the resource fails
+ */
+ @Test
+ public void testMappingActionDecode() throws IOException {
+ MappingAction action = getAction("MappingAction.json");
+ assertThat(action.toString(), is(NO_ACTION_STRING));
+ }
+
+ /**
+ * Reads in a mapping action from the given resource and decodes it.
+ *
+ * @param resourceName resource to use to read the JSON for the rule
+ * @return decoded mappingAction
+ * @throws IOException if processing the resource fails
+ */
+ private MappingAction getAction(String resourceName) throws IOException {
+ InputStream jsonStream = MappingActionCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ assertThat(json, notNullValue());
+ MappingAction action = actionCodec.decode((ObjectNode) json, context);
+ assertThat(action, notNullValue());
+ return action;
+ }
+ }
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingActionJsonMatcher.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingActionJsonMatcher.java
new file mode 100644
index 0000000..3cef35a
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingActionJsonMatcher.java
@@ -0,0 +1,150 @@
+/*
+ * 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 org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.mapping.actions.MappingAction;
+import org.onosproject.mapping.actions.NoMappingAction;
+import org.onosproject.mapping.actions.DropMappingAction;
+import org.onosproject.mapping.actions.ForwardMappingAction;
+import org.onosproject.mapping.actions.NativeForwardMappingAction;
+/**
+ * Hamcrest matcher for mapping actions.
+ */
+public final class MappingActionJsonMatcher
+ extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final MappingAction action;
+
+ /**
+ * A default constructor.
+ *
+ * @param action mapping action
+ */
+ private MappingActionJsonMatcher(MappingAction action) {
+ this.action = action;
+ }
+
+ /**
+ * Matches the contents of a no mapping action.
+ *
+ * @param node JSON action to match
+ * @param description object used for recording errors
+ * @return true if contents match, false otherwise
+ */
+ private boolean matchNoAction(JsonNode node, Description description) {
+ NoMappingAction actionToMatch = (NoMappingAction) action;
+ final String jsonType = node.get(MappingActionCodec.TYPE).textValue();
+ if (!actionToMatch.type().name().equals(jsonType)) {
+ description.appendText("type was " + jsonType);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Matches the contents of a drop mapping action.
+ *
+ * @param node JSON action to match
+ * @param description object used for recording errors
+ * @return true if the contents match, false otherwise
+ */
+ private boolean matchDropAction(JsonNode node, Description description) {
+ DropMappingAction actionToMatch = (DropMappingAction) action;
+ final String jsonType = node.get(MappingActionCodec.TYPE).textValue();
+ if (!actionToMatch.type().name().equals(jsonType)) {
+ description.appendText("type was " + jsonType);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Matches the contents of a forward mapping action.
+ *
+ * @param node JSON action to match
+ * @param description object used for recording errors
+ * @return true if the contents match, false otherwise
+ */
+ private boolean matchForwardAction(JsonNode node, Description description) {
+ ForwardMappingAction actionToMatch = (ForwardMappingAction) action;
+ final String jsonType = node.get(MappingActionCodec.TYPE).textValue();
+ if (!actionToMatch.type().name().equals(jsonType)) {
+ description.appendText("type was " + jsonType);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Matches the contents of a native forward mapping action.
+ *
+ * @param node JSON action to match
+ * @param description object used for recording errors
+ * @return true if the contents match, false otherwise
+ */
+ private boolean matchNativeForwardAction(JsonNode node, Description description) {
+ NativeForwardMappingAction actionToMatch = (NativeForwardMappingAction) action;
+ final String jsonType = node.get(MappingActionCodec.TYPE).textValue();
+ if (!actionToMatch.type().name().equals(jsonType)) {
+ description.appendText("type was " + jsonType);
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+ // check type
+ final JsonNode jsonTypeNode = jsonNode.get(MappingActionCodec.TYPE);
+ final String jsonType = jsonTypeNode.textValue();
+ final String type = action.type().name();
+ if (!jsonType.equals(type)) {
+ description.appendText("type was " + type);
+ return false;
+ }
+
+ if (action instanceof NoMappingAction) {
+ return matchNoAction(jsonNode, description);
+ } else if (action instanceof DropMappingAction) {
+ return matchDropAction(jsonNode, description);
+ } else if (action instanceof ForwardMappingAction) {
+ return matchForwardAction(jsonNode, description);
+ } else if (action instanceof NativeForwardMappingAction) {
+ return matchNativeForwardAction(jsonNode, description);
+ }
+
+ return false;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(action.toString());
+ }
+
+ /**
+ * Factory to allocate a mapping action matcher.
+ *
+ * @param action action object we are looking for
+ * @return matcher
+ */
+ public static MappingActionJsonMatcher matchesAction(MappingAction action) {
+ return new MappingActionJsonMatcher(action);
+ }
+}
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingAddressCodecTest.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingAddressCodecTest.java
new file mode 100644
index 0000000..bf114bb
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingAddressCodecTest.java
@@ -0,0 +1,156 @@
+/*
+ * 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.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.addresses.MappingAddresses;
+import org.onosproject.mapping.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;
+import static org.onosproject.mapping.codec.MappingAddressJsonMatcher.matchesMappingAddress;
+
+/**
+ * Unit tests for MappingAddressCodec.
+ */
+public class MappingAddressCodecTest {
+
+ private CodecContext context;
+ private JsonCodec<MappingAddress> addressCodec;
+ private MappingCodecRegistrator registrator;
+ 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 IpPrefix IPV6_PREFIX = IpPrefix.valueOf("fe80::/64");
+ private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01");
+ private static final String DN = "onos";
+ private static final String AS = "AS1000";
+
+ /**
+ * Sets up for each test.
+ * Creates a context and fetches the mapping address codec.
+ */
+ @Before
+ public void setUp() {
+ CodecManager manager = new CodecManager();
+ registrator = new MappingCodecRegistrator();
+ registrator.codecService = manager;
+ registrator.activate();
+
+ context = new MappingCodecContextAdapter(registrator.codecService);
+
+ addressCodec = context.codec(MappingAddress.class);
+ assertThat(addressCodec, notNullValue());
+ }
+
+ @After
+ public void tearDown() {
+ registrator.deactivate();
+ }
+
+ /**
+ * Tests AS mapping address.
+ */
+ @Test
+ public void asMappingAddressTest() {
+ MappingAddress address = MappingAddresses.asMappingAddress(AS);
+ ObjectNode result = addressCodec.encode(address, context);
+ assertThat(result, matchesMappingAddress(address));
+ }
+
+ /**
+ * Tests DN mapping address.
+ */
+ @Test
+ public void dnMappingAddressTest() {
+ MappingAddress address = MappingAddresses.dnMappingAddress(DN);
+ ObjectNode result = addressCodec.encode(address, context);
+ assertThat(result, matchesMappingAddress(address));
+ }
+
+ /**
+ * Tests IPv4 mapping address.
+ */
+ @Test
+ public void ipv4MappingAddressTest() {
+ MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX);
+ ObjectNode result = addressCodec.encode(address, context);
+ assertThat(result, matchesMappingAddress(address));
+ }
+
+ /**
+ * Tests IPv6 mapping address.
+ */
+ @Test
+ public void ipv6MappingAddressTest() {
+ MappingAddress address = MappingAddresses.ipv6MappingAddress(IPV6_PREFIX);
+ ObjectNode result = addressCodec.encode(address, context);
+ assertThat(result, matchesMappingAddress(address));
+ }
+
+ /**
+ * Tests Ethernet mapping address.
+ */
+ @Test
+ public void ethMappingAddressTest() {
+ MappingAddress address = MappingAddresses.ethMappingAddress(MAC);
+ ObjectNode result = addressCodec.encode(address, context);
+ assertThat(result, matchesMappingAddress(address));
+ }
+
+ /**
+ * Tests the decoding of mapping address from JSON object.
+ *
+ * @throws IOException if processing the resource fails
+ */
+ @Test
+ public void testMappingAddressDecode() throws IOException {
+ MappingAddress address = getAddress("MappingAddress.json");
+ assertThat(address.toString(),
+ is("IPV4:" + IPV4_STRING + "/" + PORT_STRING));
+ }
+
+ /**
+ * Reads in a mapping address from the given resource and decodes it.
+ *
+ * @param resourceName resource to use to read the JSON for the rule
+ * @return decoded mappingAddress
+ * @throws IOException if processing the resource fails
+ */
+ private MappingAddress getAddress(String resourceName) throws IOException {
+ InputStream jsonStream = MappingAddressCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ assertThat(json, notNullValue());
+ MappingAddress address = addressCodec.decode((ObjectNode) json, context);
+ assertThat(address, notNullValue());
+ return address;
+ }
+}
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingAddressJsonMatcher.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingAddressJsonMatcher.java
new file mode 100644
index 0000000..abef57d
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingAddressJsonMatcher.java
@@ -0,0 +1,164 @@
+/*
+ * 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 org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.mapping.addresses.ASMappingAddress;
+import org.onosproject.mapping.addresses.DNMappingAddress;
+import org.onosproject.mapping.addresses.EthMappingAddress;
+import org.onosproject.mapping.addresses.IPMappingAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+
+/**
+ * Hamcrest matcher for mapping objects.
+ */
+public final class MappingAddressJsonMatcher extends
+ TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final MappingAddress address;
+ private Description description;
+ private JsonNode node;
+
+ /**
+ * Constructs a matcher object.
+ *
+ * @param address mapping address to match
+ */
+ private MappingAddressJsonMatcher(MappingAddress address) {
+ this.address = address;
+ }
+
+ /**
+ * Factory to allocate a mapping address matcher.
+ *
+ * @param address mapping address object we are looking for
+ * @return matcher
+ */
+ public static MappingAddressJsonMatcher matchesMappingAddress(MappingAddress address) {
+ return new MappingAddressJsonMatcher(address);
+ }
+
+ /**
+ * Matches an AS mapping address object.
+ *
+ * @param address mapping address to match
+ * @return true if the JSON matches the mapping address, false otherwise
+ */
+ private boolean matchMappingAddress(ASMappingAddress address) {
+ final String as = address.asNumber();
+ final String jsonAs = node.get(MappingAddressCodec.AS).textValue();
+ if (!as.equals(jsonAs)) {
+ description.appendText("AS was " + jsonAs);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Matches a Distinguished Name mapping address object.
+ *
+ * @param address mapping address to match
+ * @return true if the JSON matches the mapping address, false otherwise
+ */
+ private boolean matchMappingAddress(DNMappingAddress address) {
+ final String dn = address.name();
+ final String jsonDn = node.get(MappingAddressCodec.DN).textValue();
+ if (!dn.equals(jsonDn)) {
+ description.appendText("Distinguished Name was " + jsonDn);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Matches an IP mapping address object.
+ *
+ * @param address mapping address to match
+ * @return true if the JSON matches the mapping address, false otherwise
+ */
+ private boolean matchMappingAddress(IPMappingAddress address) {
+ final String ip = address.ip().toString();
+ String jsonIp = null;
+ if (address.type() == MappingAddress.Type.IPV4) {
+ jsonIp = node.get(MappingAddressCodec.IPV4).textValue();
+
+ } else if (address.type() == MappingAddress.Type.IPV6) {
+ jsonIp = node.get(MappingAddressCodec.IPV6).textValue();
+ }
+ if (!ip.equals(jsonIp)) {
+ description.appendText("IP was " + jsonIp);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Matches a MAC mapping address object.
+ *
+ * @param address mapping address to match
+ * @return true if the JSON matches the mapping address, false otherwise
+ */
+ private boolean matchMappingAddress(EthMappingAddress address) {
+ final String mac = address.mac().toString();
+ final String jsonMac = node.get(MappingAddressCodec.MAC).textValue();
+ if (!mac.equals(jsonMac)) {
+ description.appendText("MAC was " + jsonMac);
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+ this.description = description;
+ this.node = jsonNode;
+ final String type = address.type().name();
+ final String jsonType = jsonNode.get(MappingAddressCodec.TYPE).asText();
+ if (!type.equals(jsonType)) {
+ description.appendText("type was " + type);
+ return false;
+ }
+
+ switch (address.type()) {
+
+ case IPV4:
+ case IPV6:
+ return matchMappingAddress((IPMappingAddress) address);
+
+ case AS:
+ return matchMappingAddress((ASMappingAddress) address);
+
+ case DN:
+ return matchMappingAddress((DNMappingAddress) address);
+
+ case ETH:
+ return matchMappingAddress((EthMappingAddress) address);
+
+ default:
+ // Don't know how to format this type
+ description.appendText("unknown mapping address type " + address.type());
+ return false;
+ }
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(address.toString());
+ }
+}
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingCodecContextAdapter.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingCodecContextAdapter.java
new file mode 100644
index 0000000..24e141e
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingCodecContextAdapter.java
@@ -0,0 +1,54 @@
+/*
+ * 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.ObjectMapper;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.JsonCodec;
+
+/**
+ * An adapter that provides mapping codec context.
+ */
+public class MappingCodecContextAdapter implements CodecContext {
+
+ private final ObjectMapper mapper = new ObjectMapper();
+ private final CodecService manager;
+
+ /**
+ * Constructs a new mock codec context.
+ *
+ * @param manager codec manager
+ */
+ public MappingCodecContextAdapter(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/api/src/test/java/org/onosproject/mapping/codec/MappingInstructionCodecTest.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingInstructionCodecTest.java
new file mode 100644
index 0000000..35cdbb6
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingInstructionCodecTest.java
@@ -0,0 +1,158 @@
+/*
+ * 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.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.codec.CodecContext;
+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.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;
+import static org.onosproject.mapping.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;
+
+ private static final String UNICAST_TYPE_STRING = "UNICAST";
+ private static final String WEIGHT_SUBTYPE_STRING = "WEIGHT";
+
+ /**
+ * 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 MappingCodecContextAdapter(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));
+ }
+
+ /**
+ * Tests the decoding of mapping instruction from JSON object.
+ *
+ * @throws IOException if processing the resource fails
+ */
+ @Test
+ public void testMappingInstructionDecode() throws IOException {
+ UnicastMappingInstruction instruction = (UnicastMappingInstruction) getInstruction("MappingInstruction.json");
+ assertThat(instruction.type().toString(), is(UNICAST_TYPE_STRING));
+ assertThat(instruction.subtype().toString(), is(WEIGHT_SUBTYPE_STRING));
+ }
+
+ /**
+ * Reads in a mapping instruction from the given resource and decodes it.
+ *
+ * @param resourceName resource to use to read the JSON for the rule
+ * @return decoded mappingInstruction
+ * @throws IOException if processing the resource fails
+ */
+ private MappingInstruction getInstruction(String resourceName) throws IOException {
+ InputStream jsonStream = MappingInstructionCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ assertThat(json, notNullValue());
+ MappingInstruction instruction = instructionCodec.decode((ObjectNode) json, context);
+ assertThat(instruction, notNullValue());
+ return instruction;
+ }
+}
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingInstructionJsonMatcher.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingInstructionJsonMatcher.java
new file mode 100644
index 0000000..9644ce6
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/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.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);
+ }
+}
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingKeyCodecTest.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingKeyCodecTest.java
new file mode 100644
index 0000000..25d6ff4
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingKeyCodecTest.java
@@ -0,0 +1,166 @@
+/*
+ * 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.DefaultMappingKey;
+import org.onosproject.mapping.MappingKey;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.addresses.MappingAddresses;
+import org.onosproject.mapping.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 MappingCodecContextAdapter(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);
+ }
+ }
+
+ /**
+ * 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/api/src/test/java/org/onosproject/mapping/codec/MappingTreatmentCodecTest.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingTreatmentCodecTest.java
new file mode 100644
index 0000000..f66b18d
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingTreatmentCodecTest.java
@@ -0,0 +1,211 @@
+/*
+ * 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 com.google.common.collect.ImmutableSet;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.mapping.DefaultMappingTreatment;
+import org.onosproject.mapping.MappingTreatment;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.addresses.MappingAddresses;
+import org.onosproject.mapping.instructions.MappingInstruction;
+import org.onosproject.mapping.instructions.MappingInstructions;
+import org.onosproject.mapping.MappingCodecRegistrator;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+/**
+ * Unit tests for MappingTreatmentCodec.
+ */
+public class MappingTreatmentCodecTest {
+
+ private static final String INSTRUCTIONS = "instructions";
+ private static final String TYPE = "type";
+
+ 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 String ASN_NUMBER = "ASN2000";
+
+ private CodecContext context;
+ private JsonCodec<MappingTreatment> treatmentCodec;
+ private MappingCodecRegistrator registrator;
+
+ /**
+ * Sets up for each test.
+ * Creates a context and fetches the mapping action codec.
+ */
+ @Before
+ public void setUp() {
+ CodecManager manager = new CodecManager();
+ registrator = new MappingCodecRegistrator();
+ registrator.codecService = manager;
+ registrator.activate();
+
+ context = new MappingCodecContextAdapter(registrator.codecService);
+ treatmentCodec = context.codec(MappingTreatment.class);
+ assertThat(treatmentCodec, notNullValue());
+ }
+
+ /**
+ * Deactivates the codec registrator.
+ */
+ @After
+ public void tearDown() {
+ registrator.deactivate();
+ }
+
+ /**
+ * Tests encoding of a mapping treatment object.
+ */
+ @Test
+ public void testMappingTreatmentEncode() {
+ MappingInstruction unicastWeight = MappingInstructions.unicastWeight(UNICAST_WEIGHT);
+ MappingInstruction unicastPriority = MappingInstructions.unicastPriority(UNICAST_PRIORITY);
+ MappingInstruction multicastWeight = MappingInstructions.multicastWeight(MULTICAST_WEIGHT);
+ MappingInstruction multicastPriority = MappingInstructions.multicastPriority(MULTICAST_PRIORITY);
+
+ MappingAddress address = MappingAddresses.asMappingAddress(ASN_NUMBER);
+
+ MappingTreatment treatment = DefaultMappingTreatment.builder()
+ .add(unicastWeight)
+ .add(unicastPriority)
+ .add(multicastWeight)
+ .add(multicastPriority)
+ .withAddress(address)
+ .build();
+
+ ObjectNode treatmentJson = treatmentCodec.encode(treatment, context);
+ assertThat(treatmentJson, MappingTreatmentJsonMatcher.matchesMappingTreatment(treatment));
+ }
+
+ /**
+ * Tests decoding of a mapping treatment JSON object.
+ */
+ @Test
+ public void testMappingTreatmentDecode() throws IOException {
+ MappingTreatment treatment = getTreatment("MappingTreatment.json");
+
+ List<MappingInstruction> insts = treatment.instructions();
+ assertThat(insts.size(), is(2));
+
+ ImmutableSet<String> types = ImmutableSet.of("UNICAST", "MULTICAST");
+ assertThat(types.contains(insts.get(0).type().name()), is(true));
+ assertThat(types.contains(insts.get(1).type().name()), is(true));
+ }
+
+ /**
+ * Hamcrest matcher for mapping treatment.
+ */
+ public static final class MappingTreatmentJsonMatcher
+ extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final MappingTreatment mappingTreatment;
+
+ /**
+ * A default constructor.
+ *
+ * @param mappingTreatment mapping treatment
+ */
+ private MappingTreatmentJsonMatcher(MappingTreatment mappingTreatment) {
+ this.mappingTreatment = mappingTreatment;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+ // check mapping instruction
+ final JsonNode jsonInstructions = jsonNode.get(INSTRUCTIONS);
+ if (mappingTreatment.instructions().size() != jsonInstructions.size()) {
+ description.appendText("mapping instructions array size of " +
+ Integer.toString(mappingTreatment.instructions().size()));
+ return false;
+ }
+
+ for (final MappingInstruction instruction : mappingTreatment.instructions()) {
+ boolean instructionFound = false;
+ for (int instructionIdx = 0; instructionIdx < jsonInstructions.size();
+ instructionIdx++) {
+ final String jsonType =
+ jsonInstructions.get(instructionIdx).get(TYPE).asText();
+ final String instructionType = instruction.type().name();
+ if (jsonType.equals(instructionType)) {
+ instructionFound = true;
+ }
+ }
+ if (!instructionFound) {
+ description.appendText("mapping instruction " + instruction.toString());
+ return false;
+ }
+ }
+
+ // check address
+ final JsonNode jsonAddressNode = jsonNode.get(MappingKeyCodec.ADDRESS);
+
+ assertThat(jsonAddressNode,
+ MappingAddressJsonMatcher.matchesMappingAddress(mappingTreatment.address()));
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(mappingTreatment.toString());
+ }
+
+ /**
+ * Factory to allocate a mapping treatment.
+ *
+ * @param mappingTreatment mapping treatment object we are looking for
+ * @return matcher
+ */
+ static MappingTreatmentJsonMatcher matchesMappingTreatment(MappingTreatment mappingTreatment) {
+ return new MappingTreatmentJsonMatcher(mappingTreatment);
+ }
+ }
+
+ /**
+ * Reads in a mapping treatment from the given resource and decodes it.
+ *
+ * @param resourceName resource to use to read the JSON for the rule
+ * @return decoded mappingTreatment
+ * @throws IOException if processing the resource fails
+ */
+ private MappingTreatment getTreatment(String resourceName) throws IOException {
+ InputStream jsonStream = MappingTreatmentCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ assertThat(json, notNullValue());
+ MappingTreatment treatment = treatmentCodec.decode((ObjectNode) json, context);
+ assertThat(treatment, notNullValue());
+ return treatment;
+ }
+}
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
new file mode 100644
index 0000000..22fa13a
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/codec/MappingValueCodecTest.java
@@ -0,0 +1,221 @@
+/*
+ * 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.DefaultMappingTreatment;
+import org.onosproject.mapping.DefaultMappingValue;
+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.instructions.MappingInstruction;
+import org.onosproject.mapping.instructions.MappingInstructions;
+import org.onosproject.mapping.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 MappingValueCodec.
+ */
+public class MappingValueCodecTest {
+
+ private static final String TREATMENTS = "treatments";
+ private static final String ADDRESS = "address";
+ 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 CodecContext context;
+ private JsonCodec<MappingValue> valueCodec;
+ private MappingCodecRegistrator registrator;
+
+ /**
+ * Sets up for each test.
+ * Creates a context and fetches the mapping value codec.
+ */
+ @Before
+ public void setUp() {
+ CodecManager manager = new CodecManager();
+ registrator = new MappingCodecRegistrator();
+ registrator.codecService = manager;
+ registrator.activate();
+
+ context = new MappingCodecContextAdapter(registrator.codecService);
+ valueCodec = context.codec(MappingValue.class);
+ assertThat(valueCodec, notNullValue());
+ }
+
+ /**
+ * Deactivates the codec registrator.
+ */
+ @After
+ public void tearDown() {
+ registrator.deactivate();
+ }
+
+ /**
+ * Tests encoding of a mapping value object.
+ */
+ @Test
+ public void testMapingValueEncode() {
+ MappingInstruction unicastWeight = MappingInstructions.unicastWeight(UNICAST_WEIGHT);
+ MappingInstruction unicastPriority = MappingInstructions.unicastPriority(UNICAST_PRIORITY);
+ MappingInstruction multicastWeight = MappingInstructions.multicastWeight(MULTICAST_WEIGHT);
+ MappingInstruction multicastPriority = MappingInstructions.multicastPriority(MULTICAST_PRIORITY);
+
+ MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX);
+
+ 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();
+
+ ObjectNode valueJson = valueCodec.encode(value, context);
+ assertThat(valueJson, MappingValueJsonMatcher.matchesMappingValue(value));
+ }
+
+ /**
+ * Tests decoding of a mapping value JSON object.
+ */
+ @Test
+ public void testMappingValueDecode() throws IOException {
+ MappingValue value = getValue("MappingValue.json");
+ assertThat(value.treatments().get(0).address().toString(),
+ is("IPV4:" + IPV4_STRING + "/" + PORT_STRING));
+ }
+
+
+ /**
+ * Hamcrest matcher for mapping value.
+ */
+ public static final class MappingValueJsonMatcher
+ extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final MappingValue mappingValue;
+
+ /**
+ * A default constructor.
+ *
+ * @param mappingValue mapping value
+ */
+ private MappingValueJsonMatcher(MappingValue mappingValue) {
+ this.mappingValue = mappingValue;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+ // check mapping treatments
+ final JsonNode jsonTreatments = jsonNode.get(TREATMENTS);
+ if (mappingValue.treatments().size() != jsonTreatments.size()) {
+ description.appendText("mapping treatments array size of " +
+ Integer.toString(mappingValue.treatments().size()));
+ return false;
+ }
+
+ for (final MappingTreatment treatment : mappingValue.treatments()) {
+ boolean treatmentFound = false;
+ for (int treatmentIdx = 0; treatmentIdx < jsonTreatments.size();
+ treatmentIdx++) {
+ final String jsonAddress =
+ jsonTreatments.get(treatmentIdx).get(ADDRESS)
+ .get(MappingAddressCodec.IPV4).textValue();
+ final String address = treatment.address().toString();
+ if (address.contains(jsonAddress)) {
+ treatmentFound = true;
+ }
+ }
+
+ if (!treatmentFound) {
+ description.appendText("mapping treatment " + treatment.toString());
+ return false;
+ }
+ }
+
+ // check mapping action
+ final JsonNode jsonActionNode = jsonNode.get(MappingValueCodec.ACTION);
+
+ assertThat(jsonActionNode, MappingActionJsonMatcher.matchesAction(mappingValue.action()));
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(mappingValue.toString());
+ }
+
+ /**
+ * Factory to allocate a mapping value.
+ *
+ * @param mappingValue mapping value object we are looking for
+ * @return matcher
+ */
+ static MappingValueJsonMatcher matchesMappingValue(MappingValue mappingValue) {
+ return new MappingValueJsonMatcher(mappingValue);
+ }
+ }
+
+ /**
+ * Reads in a mapping value 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 MappingValue getValue(String resourceName) throws IOException {
+ InputStream jsonStream = MappingValueCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ assertThat(json, notNullValue());
+ MappingValue value = valueCodec.decode((ObjectNode) json, context);
+ assertThat(value, notNullValue());
+ return value;
+ }
+}
diff --git a/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingAction.json b/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingAction.json
new file mode 100644
index 0000000..6180224
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingAction.json
@@ -0,0 +1,3 @@
+{
+ "type": "NO_ACTION"
+}
\ No newline at end of file
diff --git a/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingAddress.json b/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingAddress.json
new file mode 100644
index 0000000..376f313
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingAddress.json
@@ -0,0 +1,4 @@
+{
+ "type": "IPV4",
+ "ipv4": "1.2.3.4/32"
+}
\ No newline at end of file
diff --git a/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingInstruction.json b/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingInstruction.json
new file mode 100644
index 0000000..244563f
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingInstruction.json
@@ -0,0 +1,5 @@
+{
+ "type": "UNICAST",
+ "subtype": "WEIGHT",
+ "unicastWeight": 1
+}
\ No newline at end of file
diff --git a/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingKey.json b/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingKey.json
new file mode 100644
index 0000000..aac8c5e
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingKey.json
@@ -0,0 +1,6 @@
+{
+ "address": {
+ "type": "IPV4",
+ "ipv4": "1.2.3.4/32"
+ }
+}
diff --git a/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingTreatment.json b/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingTreatment.json
new file mode 100644
index 0000000..21e49a0
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingTreatment.json
@@ -0,0 +1,14 @@
+{
+ "instructions": [
+ {
+ "type": "UNICAST",
+ "subtype": "WEIGHT",
+ "unicastWeight": 1
+ },
+ {
+ "type": "MULTICAST",
+ "subtype": "WEIGHT",
+ "multicastWeight": 2
+ }
+ ]
+}
diff --git a/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingValue.json b/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingValue.json
new file mode 100644
index 0000000..de871a0
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/resources/org/onosproject/mapping/codec/MappingValue.json
@@ -0,0 +1,24 @@
+{
+ "treatments": [
+ {
+ "instructions": [
+ {
+ "type": "UNICAST",
+ "subtype": "WEIGHT",
+ "unicastWeight": 1
+ },
+ {
+ "type": "MULTICAST",
+ "subtype": "WEIGHT",
+ "multicastWeight": 2
+ }
+ ],
+ "address": {
+ "type": "IPV4",
+ "ipv4": "1.2.3.4/32"
+ }
+ }
+ ],
+ "action": "NO_ACTION"
+}
+