[ONOS-6170] Implement codec for MappingInstruction primitives

Change-Id: Ib372de863e6d730c55326ef3450d6f79478592b5
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingCodecRegistrator.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingCodecRegistrator.java
index 2e6878c..9540cc8 100644
--- a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingCodecRegistrator.java
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingCodecRegistrator.java
@@ -22,7 +22,9 @@
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.onosproject.codec.CodecService;
 import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.instructions.MappingInstruction;
 import org.onosproject.mapping.web.codec.MappingAddressCodec;
+import org.onosproject.mapping.web.codec.MappingInstructionCodec;
 import org.slf4j.Logger;
 
 import static org.slf4j.LoggerFactory.getLogger;
@@ -41,6 +43,7 @@
     @Activate
     public void activate() {
         codecService.registerCodec(MappingAddress.class, new MappingAddressCodec());
+        codecService.registerCodec(MappingInstruction.class, new MappingInstructionCodec());
 
         log.info("Started");
     }
@@ -48,6 +51,7 @@
     @Deactivate
     public void deactivate() {
         codecService.unregisterCodec(MappingAddress.class);
+        codecService.unregisterCodec(MappingInstruction.class);
 
         log.info("Stopped");
     }
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/DecodeMappingInstructionCodecHelper.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/DecodeMappingInstructionCodecHelper.java
new file mode 100644
index 0000000..9519439
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/DecodeMappingInstructionCodecHelper.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.mapping.web.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.mapping.instructions.MappingInstruction;
+import org.onosproject.mapping.instructions.MappingInstructions;
+import org.onosproject.mapping.instructions.MulticastMappingInstruction;
+import org.onosproject.mapping.instructions.UnicastMappingInstruction;
+
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * Decoding portion of the mapping instruction codec.
+ */
+public final class DecodeMappingInstructionCodecHelper {
+    private final ObjectNode json;
+    private final CodecContext context;
+
+    /**
+     * Creates a decode mapping instruction codec object.
+     *
+     * @param json    JSON object to decode
+     * @param context codec context
+     */
+    public DecodeMappingInstructionCodecHelper(ObjectNode json, CodecContext context) {
+        this.json = json;
+        this.context = context;
+    }
+
+    /**
+     * Decodes an unicast mapping instruction.
+     *
+     * @return mapping instruction object decoded from the JSON
+     * @throws IllegalArgumentException if the JSON is invalid
+     */
+    private MappingInstruction decodeUnicast() {
+        String subType = nullIsIllegal(json.get(MappingInstructionCodec.SUBTYPE),
+                MappingInstructionCodec.SUBTYPE + MappingInstructionCodec.ERROR_MESSAGE).asText();
+
+        if (subType.equals(UnicastMappingInstruction.UnicastType.WEIGHT.name())) {
+            int weight = nullIsIllegal(json.get(MappingInstructionCodec.UNICAST_WEIGHT),
+                    MappingInstructionCodec.UNICAST_WEIGHT +
+                            MappingInstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
+            return MappingInstructions.unicastWeight(weight);
+        } else if (subType.equals(UnicastMappingInstruction.UnicastType.PRIORITY.name())) {
+            int priority = nullIsIllegal(json.get(MappingInstructionCodec.UNICAST_PRIORITY),
+                    MappingInstructionCodec.UNICAST_PRIORITY +
+                            MappingInstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
+            return MappingInstructions.unicastPriority(priority);
+        }
+        throw new IllegalArgumentException("Unicast MappingInstruction subtype "
+                + subType + " is not supported");
+    }
+
+    /**
+     * Decodes a multicast mapping instruction.
+     *
+     * @return mapping instruction object decoded from the JSON
+     * @throws IllegalArgumentException if the JSON is invalid
+     */
+    private MappingInstruction decodeMulticast() {
+        String subType = nullIsIllegal(json.get(MappingInstructionCodec.SUBTYPE),
+                MappingInstructionCodec.SUBTYPE + MappingInstructionCodec.ERROR_MESSAGE).asText();
+
+        if (subType.equals(MulticastMappingInstruction.MulticastType.WEIGHT.name())) {
+            int weight = nullIsIllegal(json.get(MappingInstructionCodec.MULTICAST_WEIGHT),
+                    MappingInstructionCodec.MULTICAST_WEIGHT +
+                            MappingInstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
+            return MappingInstructions.multicastWeight(weight);
+        } else if (subType.equals(MulticastMappingInstruction.MulticastType.PRIORITY.name())) {
+            int priority = nullIsIllegal(json.get(MappingInstructionCodec.MULTICAST_PRIORITY),
+                    MappingInstructionCodec.MULTICAST_PRIORITY +
+                            MappingInstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
+            return MappingInstructions.multicastPriority(priority);
+        }
+        throw new IllegalArgumentException("Multicast MappingInstruction subtype "
+                + subType + " is not supported");
+    }
+
+    /**
+     * Decodes the JSON into a mapping instruction object.
+     *
+     * @return MappingInstruction object
+     * @throws IllegalArgumentException if the JSON is invalid
+     */
+    public MappingInstruction decode() {
+        String type = nullIsIllegal(json.get(MappingInstructionCodec.TYPE),
+                MappingInstructionCodec.TYPE + MappingInstructionCodec.ERROR_MESSAGE).asText();
+
+        if (type.equals(MappingInstruction.Type.UNICAST.name())) {
+            return decodeUnicast();
+        } else if (type.equals(MappingInstruction.Type.MULTICAST.name())) {
+            return decodeMulticast();
+        }
+        throw new IllegalArgumentException("MappingInstruction type "
+                + type + " is not supported");
+    }
+}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/EncodeMappingInstructionCodecHelper.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/EncodeMappingInstructionCodecHelper.java
new file mode 100644
index 0000000..1ef89d1
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/EncodeMappingInstructionCodecHelper.java
@@ -0,0 +1,129 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.mapping.web.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.mapping.instructions.MappingInstruction;
+import org.onosproject.mapping.instructions.MulticastMappingInstruction;
+import org.onosproject.mapping.instructions.UnicastMappingInstruction;
+import org.slf4j.Logger;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * JSON encoding of MappingInstructions.
+ */
+public final class EncodeMappingInstructionCodecHelper {
+    protected static final Logger log = getLogger(EncodeMappingInstructionCodecHelper.class);
+    private final MappingInstruction instruction;
+    private final CodecContext context;
+
+    /**
+     * Creates a mapping instruction object encoder.
+     *
+     * @param instruction mapping instruction to encode
+     * @param context     codec context for the encoding
+     */
+    public EncodeMappingInstructionCodecHelper(MappingInstruction instruction,
+                                               CodecContext context) {
+        this.instruction = instruction;
+        this.context = context;
+    }
+
+    /**
+     * Encodes an unicast mapping instruction.
+     *
+     * @param result json node that the mapping instruction
+     *               attributes are added to
+     */
+    private void encodeUnicast(ObjectNode result) {
+        UnicastMappingInstruction unicastInstruction =
+                (UnicastMappingInstruction) instruction;
+        result.put(MappingInstructionCodec.SUBTYPE, unicastInstruction.subtype().name());
+
+        switch (unicastInstruction.subtype()) {
+            case WEIGHT:
+                UnicastMappingInstruction.WeightMappingInstruction wmi =
+                        (UnicastMappingInstruction.WeightMappingInstruction)
+                                                            unicastInstruction;
+                result.put(MappingInstructionCodec.UNICAST_WEIGHT, wmi.weight());
+                break;
+            case PRIORITY:
+                UnicastMappingInstruction.PriorityMappingInstruction pmi =
+                        (UnicastMappingInstruction.PriorityMappingInstruction)
+                                                            unicastInstruction;
+                result.put(MappingInstructionCodec.UNICAST_PRIORITY, pmi.priority());
+                break;
+            default:
+                log.info("Cannot convert unicast subtype of {}",
+                                                unicastInstruction.subtype());
+        }
+    }
+
+    /**
+     * Encodes a multicast mapping instruction.
+     *
+     * @param result json node that the mapping instruction
+     *               attributes are added to
+     */
+    private void encodeMulticast(ObjectNode result) {
+        MulticastMappingInstruction multicastMappingInstruction =
+                (MulticastMappingInstruction) instruction;
+        result.put(MappingInstructionCodec.SUBTYPE, multicastMappingInstruction.subtype().name());
+
+        switch (multicastMappingInstruction.subtype()) {
+            case WEIGHT:
+                MulticastMappingInstruction.WeightMappingInstruction wmi =
+                        (MulticastMappingInstruction.WeightMappingInstruction)
+                                                    multicastMappingInstruction;
+                result.put(MappingInstructionCodec.MULTICAST_WEIGHT, wmi.weight());
+                break;
+            case PRIORITY:
+                MulticastMappingInstruction.PriorityMappingInstruction pmi =
+                        (MulticastMappingInstruction.PriorityMappingInstruction)
+                                                    multicastMappingInstruction;
+                result.put(MappingInstructionCodec.MULTICAST_PRIORITY, pmi.priority());
+                break;
+            default:
+                log.info("Cannot convert multicast subtype of {}",
+                                            multicastMappingInstruction.subtype());
+        }
+    }
+
+    /**
+     * Encodes the given mapping instruction into JSON.
+     *
+     * @return JSON object node representing the mapping instruction.
+     */
+    public ObjectNode encode() {
+        final ObjectNode result = context.mapper().createObjectNode()
+                .put(MappingInstructionCodec.TYPE, instruction.type().toString());
+
+        switch (instruction.type()) {
+            case UNICAST:
+                encodeUnicast(result);
+                break;
+            case MULTICAST:
+                encodeMulticast(result);
+                break;
+            default:
+                log.info("Cannot convert mapping instruction type of {}", instruction.type());
+                break;
+        }
+        return result;
+    }
+}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/MappingInstructionCodec.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/MappingInstructionCodec.java
new file mode 100644
index 0000000..dff9133
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/MappingInstructionCodec.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.mapping.web.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.mapping.instructions.MappingInstruction;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Mapping instruction codec.
+ */
+public final class MappingInstructionCodec extends JsonCodec<MappingInstruction> {
+
+    protected static final Logger log = LoggerFactory.getLogger(MappingInstruction.class);
+
+    protected static final String TYPE = "type";
+    protected static final String SUBTYPE = "subtype";
+    protected static final String UNICAST_WEIGHT = "unicastWeight";
+    protected static final String UNICAST_PRIORITY = "unicastPriority";
+    protected static final String MULTICAST_WEIGHT = "multicastWeight";
+    protected static final String MULTICAST_PRIORITY = "multicastPriority";
+
+    protected static final String MISSING_MEMBER_MESSAGE =
+                                        " member is required in Instruction";
+    protected static final String ERROR_MESSAGE =
+                                        " not specified in Instruction";
+
+    @Override
+    public ObjectNode encode(MappingInstruction instruction, CodecContext context) {
+        checkNotNull(instruction, "Mapping instruction cannot be null");
+
+        return new EncodeMappingInstructionCodecHelper(instruction, context).encode();
+    }
+}