REST API related implemetations for ODU SIGID and SIGTYPE Fields in Flow Criteria and Instruction

Change-Id: I78ddc162409d377b8c55e7f8eedd9a91939ab277
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/CriterionCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/CriterionCodecTest.java
index 54e1146..86374f8 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/CriterionCodecTest.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/CriterionCodecTest.java
@@ -31,6 +31,8 @@
 import org.onosproject.net.GridType;
 import org.onosproject.net.Lambda;
 import org.onosproject.net.OchSignalType;
+import org.onosproject.net.OduSignalId;
+import org.onosproject.net.OduSignalType;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.flow.criteria.Criteria;
 import org.onosproject.net.flow.criteria.Criterion;
@@ -54,6 +56,10 @@
     final IpPrefix ipPrefix6 = IpPrefix.valueOf("fe80::/64");
     final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
     final TpPort tpPort = TpPort.tpPort(40000);
+    final int tributaryPortNumber = 11;
+    final int tributarySlotLen = 80;
+    final byte[] tributarySlotBitmap = new byte[] {1, 2, 3, 4, 2, 3, 4, 2, 3, 4};
+
 
     /**
      * Sets up for each test.  Creates a context and fetches the criterion
@@ -427,4 +433,31 @@
         ObjectNode result = criterionCodec.encode(criterion, context);
         assertThat(result, matchesCriterion(criterion));
     }
+
+   /**
+     * Tests Odu Signal ID criterion.
+     */
+    @Test
+    public void matchOduSignalIdTest() {
+
+        OduSignalId oduSignalId = OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen, tributarySlotBitmap);
+
+        Criterion criterion = Criteria.matchOduSignalId(oduSignalId);
+        ObjectNode result = criterionCodec.encode(criterion, context);
+        assertThat(result, matchesCriterion(criterion));
+    }
+
+    /**
+     * Tests Odu Signal Type criterion.
+     */
+    @Test
+    public void matchOduSignalTypeTest() {
+
+        OduSignalType signalType = OduSignalType.ODU2;
+
+        Criterion criterion = Criteria.matchOduSignalType(signalType);
+        ObjectNode result = criterionCodec.encode(criterion, context);
+        assertThat(result, matchesCriterion(criterion));
+    }
+
 }
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/CriterionJsonMatcher.java b/core/common/src/test/java/org/onosproject/codec/impl/CriterionJsonMatcher.java
index bb3acad..b00632c 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/CriterionJsonMatcher.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/CriterionJsonMatcher.java
@@ -15,13 +15,14 @@
  */
 package org.onosproject.codec.impl;
 
-import com.google.common.base.Joiner;
+import java.util.Objects;
+
 import org.hamcrest.Description;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onlab.util.HexString;
 import org.onosproject.net.OchSignal;
+import org.onosproject.net.OduSignalId;
 import org.onosproject.net.flow.criteria.Criterion;
-
-import com.fasterxml.jackson.databind.JsonNode;
 import org.onosproject.net.flow.criteria.EthCriterion;
 import org.onosproject.net.flow.criteria.EthTypeCriterion;
 import org.onosproject.net.flow.criteria.IPCriterion;
@@ -40,6 +41,8 @@
 import org.onosproject.net.flow.criteria.MplsCriterion;
 import org.onosproject.net.flow.criteria.OchSignalCriterion;
 import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
+import org.onosproject.net.flow.criteria.OduSignalIdCriterion;
+import org.onosproject.net.flow.criteria.OduSignalTypeCriterion;
 import org.onosproject.net.flow.criteria.PortCriterion;
 import org.onosproject.net.flow.criteria.SctpPortCriterion;
 import org.onosproject.net.flow.criteria.TcpPortCriterion;
@@ -47,7 +50,8 @@
 import org.onosproject.net.flow.criteria.VlanIdCriterion;
 import org.onosproject.net.flow.criteria.VlanPcpCriterion;
 
-import java.util.Objects;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.google.common.base.Joiner;
 
 /**
  * Hamcrest matcher for criterion objects.
@@ -496,6 +500,44 @@
         return true;
     }
 
+   /**
+     * Matches an ODU signal ID criterion object.
+     *
+     * @param criterion criterion to match
+     * @return true if the JSON matches the criterion, false otherwise.
+     */
+    private boolean matchCriterion(OduSignalIdCriterion criterion) {
+        final OduSignalId oduSignal = criterion.oduSignalId();
+        final JsonNode jsonOduSignal = jsonCriterion.get(CriterionCodec.ODU_SIGNAL_ID);
+        int jsonTpn = jsonOduSignal.get(CriterionCodec.TRIBUTARY_PORT_NUMBER).intValue();
+        int jsonTsLen = jsonOduSignal.get(CriterionCodec.TRIBUTARY_SLOT_LEN).intValue();
+        byte[] jsonTributaryBitMap = HexString.fromHexString(
+                jsonOduSignal.get(CriterionCodec.TRIBUTARY_SLOT_BITMAP).asText());
+        OduSignalId jsonOduSignalId = OduSignalId.oduSignalId(jsonTpn, jsonTsLen, jsonTributaryBitMap);
+        if (!oduSignal.equals(jsonOduSignalId)) {
+            description.appendText("oduSignalId was " + criterion);
+            return false;
+        }
+       return true;
+    }
+
+    /**
+     * Matches an ODU signal Type criterion object.
+     *
+     * @param criterion criterion to match
+     * @return true if the JSON matches the criterion, false otherwise.
+     */
+    private boolean matchCriterion(OduSignalTypeCriterion criterion) {
+        final String signalType = criterion.signalType().name();
+        final String jsonOduSignalType = jsonCriterion.get("oduSignalType").textValue();
+        if (!signalType.equals(jsonOduSignalType)) {
+            description.appendText("signalType was " + signalType);
+            return false;
+        }
+        return true;
+    }
+
+
     @Override
     public boolean matchesSafely(JsonNode jsonCriterion,
                                  Description description) {
@@ -594,6 +636,12 @@
             case OCH_SIGTYPE:
                 return matchCriterion((OchSignalTypeCriterion) criterion);
 
+            case ODU_SIGID:
+                return matchCriterion((OduSignalIdCriterion) criterion);
+
+            case ODU_SIGTYPE:
+                return matchCriterion((OduSignalTypeCriterion) criterion);
+
             default:
                 // Don't know how to format this type
                 description.appendText("unknown criterion type " +
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/FlowRuleCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/FlowRuleCodecTest.java
index 6c88ac1..f6a9213 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/FlowRuleCodecTest.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/FlowRuleCodecTest.java
@@ -15,6 +15,15 @@
  */
 package org.onosproject.codec.impl;
 
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.net.NetTestTools.APP_ID;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.SortedMap;
@@ -35,6 +44,8 @@
 import org.onosproject.net.GridType;
 import org.onosproject.net.Lambda;
 import org.onosproject.net.OchSignal;
+import org.onosproject.net.OchSignalType;
+import org.onosproject.net.OduSignalType;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.flow.FlowRule;
 import org.onosproject.net.flow.criteria.Criterion;
@@ -55,6 +66,9 @@
 import org.onosproject.net.flow.criteria.IndexedLambdaCriterion;
 import org.onosproject.net.flow.criteria.MplsCriterion;
 import org.onosproject.net.flow.criteria.OchSignalCriterion;
+import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
+import org.onosproject.net.flow.criteria.OduSignalIdCriterion;
+import org.onosproject.net.flow.criteria.OduSignalTypeCriterion;
 import org.onosproject.net.flow.criteria.PortCriterion;
 import org.onosproject.net.flow.criteria.SctpPortCriterion;
 import org.onosproject.net.flow.criteria.TcpPortCriterion;
@@ -62,9 +76,6 @@
 import org.onosproject.net.flow.criteria.UdpPortCriterion;
 import org.onosproject.net.flow.criteria.VlanIdCriterion;
 import org.onosproject.net.flow.criteria.VlanPcpCriterion;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.onosproject.net.flow.instructions.Instruction;
 import org.onosproject.net.flow.instructions.Instructions;
 import org.onosproject.net.flow.instructions.L0ModificationInstruction;
@@ -72,14 +83,8 @@
 import org.onosproject.net.flow.instructions.L3ModificationInstruction;
 import org.onosproject.net.flow.instructions.L4ModificationInstruction;
 
-import static org.easymock.EasyMock.createMock;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.instanceOf;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.notNullValue;
-import static org.onosproject.net.NetTestTools.APP_ID;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 
 /**
  * Flow rule codec unit tests.
@@ -382,7 +387,7 @@
 
         checkCommonData(rule);
 
-        assertThat(rule.selector().criteria().size(), is(33));
+        assertThat(rule.selector().criteria().size(), is(36));
 
         rule.selector().criteria()
                 .stream()
@@ -518,6 +523,25 @@
         criterion = getCriterion(Criterion.Type.TUNNEL_ID);
         assertThat(((TunnelIdCriterion) criterion).tunnelId(),
                 is(100L));
+
+        criterion = getCriterion(Criterion.Type.OCH_SIGTYPE);
+        assertThat(((OchSignalTypeCriterion) criterion).signalType(),
+                is(OchSignalType.FIXED_GRID));
+
+        criterion = getCriterion(Criterion.Type.ODU_SIGTYPE);
+        assertThat(((OduSignalTypeCriterion) criterion).signalType(),
+                is(OduSignalType.ODU4));
+
+
+        criterion = getCriterion(Criterion.Type.ODU_SIGID);
+        assertThat(((OduSignalIdCriterion) criterion).oduSignalId().tributaryPortNumber(),
+                is(1));
+
+       assertThat(((OduSignalIdCriterion) criterion).oduSignalId().tributarySlotLength(),
+                is(80));
+
+       assertThat(((OduSignalIdCriterion) criterion).oduSignalId().tributarySlotBitmap(),
+                is(new byte [] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}));
     }
 
     /**
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/InstructionCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/InstructionCodecTest.java
index bafbc0f..f7b0261 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/InstructionCodecTest.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/InstructionCodecTest.java
@@ -15,6 +15,10 @@
  */
 package org.onosproject.codec.impl;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.codec.impl.InstructionJsonMatcher.matchesInstruction;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.onlab.packet.Ip4Address;
@@ -28,26 +32,23 @@
 import org.onosproject.net.GridType;
 import org.onosproject.net.IndexedLambda;
 import org.onosproject.net.Lambda;
+import org.onosproject.net.OduSignalId;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.flow.instructions.Instruction;
 import org.onosproject.net.flow.instructions.Instructions;
 import org.onosproject.net.flow.instructions.L0ModificationInstruction;
+import org.onosproject.net.flow.instructions.L1ModificationInstruction;
 import org.onosproject.net.flow.instructions.L2ModificationInstruction;
 import org.onosproject.net.flow.instructions.L3ModificationInstruction;
 
 import com.fasterxml.jackson.databind.node.ObjectNode;
 
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.notNullValue;
-import static org.onosproject.codec.impl.InstructionJsonMatcher.matchesInstruction;
-
 /**
  * Unit tests for Instruction codec.
  */
 public class InstructionCodecTest {
     CodecContext context;
     JsonCodec<Instruction> instructionCodec;
-
     /**
      * Sets up for each test.  Creates a context and fetches the instruction
      * codec.
@@ -122,6 +123,20 @@
     }
 
     /**
+     * Tests the encoding of mod ODU signal ID instructions.
+     */
+    @Test
+    public void modOduSignalIdInstructionTest() {
+        OduSignalId oduSignalId = OduSignalId.oduSignalId(1, 8, new byte [] {8, 0, 0, 0, 0, 0, 0, 0, 0, 0});
+        L1ModificationInstruction.ModOduSignalIdInstruction instruction =
+                (L1ModificationInstruction.ModOduSignalIdInstruction)
+                    Instructions.modL1OduSignalId(oduSignalId);
+        ObjectNode instructionJson =
+                instructionCodec.encode(instruction, context);
+        assertThat(instructionJson, matchesInstruction(instruction));
+    }
+
+    /**
      * Tests the encoding of mod ether instructions.
      */
     @Test
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/InstructionJsonMatcher.java b/core/common/src/test/java/org/onosproject/codec/impl/InstructionJsonMatcher.java
index c3cdca0..9ffb3c3 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/InstructionJsonMatcher.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/InstructionJsonMatcher.java
@@ -17,15 +17,25 @@
 
 import org.hamcrest.Description;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onlab.util.HexString;
+import org.onosproject.net.OduSignalId;
 import org.onosproject.net.flow.instructions.Instruction;
+import org.onosproject.net.flow.instructions.Instructions.DropInstruction;
+import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
+import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
+import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
+import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
+import org.onosproject.net.flow.instructions.L1ModificationInstruction.ModOduSignalIdInstruction;
+import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
+import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
+import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
+import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
+import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions;
+import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
+import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
 
 import com.fasterxml.jackson.databind.JsonNode;
 
-import static org.onosproject.net.flow.instructions.Instructions.*;
-import static org.onosproject.net.flow.instructions.L0ModificationInstruction.*;
-import static org.onosproject.net.flow.instructions.L2ModificationInstruction.*;
-import static org.onosproject.net.flow.instructions.L3ModificationInstruction.*;
-
 /**
  * Hamcrest matcher for instructions.
  */
@@ -133,7 +143,7 @@
     }
 
     /**
-     * Matches teh contents of a mod OCh singal instruction.
+     * Matches the contents of a mod OCh singal instruction.
      *
      * @param instructionJson JSON instruction to match
      * @param description Description object used for recording errors
@@ -184,6 +194,40 @@
     }
 
     /**
+     * Matches the contents of a mod ODU singal Id instruction.
+     *
+     * @param instructionJson JSON instruction to match
+     * @param description Description object used for recording errors
+     * @return true if contents matches, false otherwise
+     */
+    private boolean matchModOduSingalIdInstruction(JsonNode instructionJson,
+                                                   Description description) {
+        ModOduSignalIdInstruction instructionToMatch =
+                (ModOduSignalIdInstruction) instruction;
+        String jsonSubType = instructionJson.get("subtype").textValue();
+        if (!instructionToMatch.subtype().name().equals(jsonSubType)) {
+            description.appendText("subtype was " + jsonSubType);
+            return false;
+        }
+        String jsonType = instructionJson.get("type").textValue();
+        if (!instructionToMatch.type().name().equals(jsonType)) {
+            description.appendText("type was " + jsonType);
+            return false;
+        }
+        final JsonNode jsonOduSignal = instructionJson.get("oduSignalId");
+        int jsonTpn = jsonOduSignal.get("tributaryPortNumber").intValue();
+        int jsonTsLen = jsonOduSignal.get("tributarySlotLength").intValue();
+        byte [] tributaryBitMap = HexString.fromHexString(jsonOduSignal.get("tributarySlotBitmap").asText());
+        OduSignalId  jsonOduSignalId = OduSignalId.oduSignalId(jsonTpn, jsonTsLen, tributaryBitMap);
+        if (!instructionToMatch.oduSignalId().equals(jsonOduSignalId)) {
+            description.appendText("oduSignalId was " + instructionToMatch);
+            return false;
+        }
+        return true;
+    }
+
+
+    /**
      * Matches the contents of a mod Ethernet instruction.
      *
      * @param instructionJson JSON instruction to match
@@ -416,6 +460,8 @@
                                                     description);
         } else if (instruction instanceof ModMplsLabelInstruction) {
             return matchModMplsLabelInstruction(jsonInstruction, description);
+        } else if (instruction instanceof ModOduSignalIdInstruction) {
+            return matchModOduSingalIdInstruction(jsonInstruction, description);
         } else if (instruction instanceof NoActionInstruction) {
             return true;
         }