[ONOS-3405] Change EthType Json encoding to Hex and extend decoding with Hex support alongside int

Change-Id: I629f52016256d6c5cc258ab9e1c5a7b916991d5b
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/DecodeCriterionCodecHelper.java b/core/common/src/main/java/org/onosproject/codec/impl/DecodeCriterionCodecHelper.java
index 69c5e79..88cc332 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/DecodeCriterionCodecHelper.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/DecodeCriterionCodecHelper.java
@@ -15,11 +15,8 @@
  */
 package org.onosproject.codec.impl;
 
-import static org.onlab.util.Tools.nullIsIllegal;
-
-import java.util.HashMap;
-import java.util.Map;
-
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.onlab.packet.Ip6Address;
 import org.onlab.packet.IpPrefix;
 import org.onlab.packet.MacAddress;
@@ -37,8 +34,10 @@
 import org.onosproject.net.flow.criteria.Criteria;
 import org.onosproject.net.flow.criteria.Criterion;
 
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.onlab.util.Tools.nullIsIllegal;
 
 /**
  * Decode portion of the criterion codec.
@@ -106,12 +105,16 @@
     private class EthTypeDecoder implements CriterionDecoder {
         @Override
         public Criterion decodeCriterion(ObjectNode json) {
-            int ethType = nullIsIllegal(json.get(CriterionCodec.ETH_TYPE),
-                    CriterionCodec.ETH_TYPE + MISSING_MEMBER_MESSAGE).asInt();
+            int ethType;
+            if (json.get(CriterionCodec.ETH_TYPE).isInt()) {
+                ethType = nullIsIllegal(json.get(CriterionCodec.ETH_TYPE),
+                                        CriterionCodec.ETH_TYPE + MISSING_MEMBER_MESSAGE).asInt();
+            } else {
+                ethType = Integer.decode(json.get(CriterionCodec.ETH_TYPE).textValue());
+            }
             return Criteria.matchEthType(ethType);
         }
     }
-
     private class EthDstDecoder implements CriterionDecoder {
         @Override
         public Criterion decodeCriterion(ObjectNode json) {
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/EncodeCriterionCodecHelper.java b/core/common/src/main/java/org/onosproject/codec/impl/EncodeCriterionCodecHelper.java
index 8fc6bbc..33dd46a 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/EncodeCriterionCodecHelper.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/EncodeCriterionCodecHelper.java
@@ -15,8 +15,7 @@
  */
 package org.onosproject.codec.impl;
 
-import java.util.EnumMap;
-
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.onlab.util.HexString;
 import org.onosproject.codec.CodecContext;
 import org.onosproject.net.OchSignal;
@@ -50,7 +49,7 @@
 import org.onosproject.net.flow.criteria.VlanIdCriterion;
 import org.onosproject.net.flow.criteria.VlanPcpCriterion;
 
-import com.fasterxml.jackson.databind.node.ObjectNode;
+import java.util.EnumMap;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
@@ -69,7 +68,7 @@
      * Initializes the formatter lookup map for the criterion subclasses.
      *
      * @param criterion Criterion to encode
-     * @param context context of the JSON encoding
+     * @param context   context of the JSON encoding
      */
     public EncodeCriterionCodecHelper(Criterion criterion, CodecContext context) {
         this.criterion = criterion;
@@ -171,7 +170,8 @@
         public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
             final EthTypeCriterion ethTypeCriterion =
                     (EthTypeCriterion) criterion;
-            return root.put(CriterionCodec.ETH_TYPE, ethTypeCriterion.ethType().toShort());
+            return root.put(CriterionCodec.ETH_TYPE, "0x"
+                    + Integer.toHexString(ethTypeCriterion.ethType().toShort() & 0xffff));
         }
     }
 
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 b00632c..7d8580a 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
@@ -138,8 +138,8 @@
      * @return true if the JSON matches the criterion, false otherwise.
      */
     private boolean matchCriterion(EthTypeCriterion criterion) {
-        final int ethType = criterion.ethType().toShort();
-        final int jsonEthType = jsonCriterion.get("ethType").intValue();
+        final int ethType = criterion.ethType().toShort() & 0xffff;
+        final int jsonEthType = Integer.decode(jsonCriterion.get("ethType").textValue()) & 0xffff;
         if (ethType != jsonEthType) {
             description.appendText("ethType was "
                     + Integer.toString(jsonEthType));
diff --git a/core/common/src/test/resources/org/onosproject/codec/impl/criteria-flow.json b/core/common/src/test/resources/org/onosproject/codec/impl/criteria-flow.json
index ccb2e16..82c0f59 100644
--- a/core/common/src/test/resources/org/onosproject/codec/impl/criteria-flow.json
+++ b/core/common/src/test/resources/org/onosproject/codec/impl/criteria-flow.json
@@ -9,7 +9,7 @@
             {"type":"IN_PORT", "port":23},
             {"type":"IN_PHY_PORT", "port":44},
             {"type":"METADATA", "metadata":123456},
-            {"type":"ETH_TYPE","ethType":2054},
+            {"type":"ETH_TYPE","ethType":"0x806"},
             {"type":"ETH_SRC","mac":"00:11:22:33:44:55"},
             {"type":"ETH_DST","mac":"00:11:22:33:44:55"},
             {"type":"VLAN_VID","vlanId":777},
diff --git a/core/common/src/test/resources/org/onosproject/codec/impl/instructions-flow.json b/core/common/src/test/resources/org/onosproject/codec/impl/instructions-flow.json
index 74b9546..2a184b3 100644
--- a/core/common/src/test/resources/org/onosproject/codec/impl/instructions-flow.json
+++ b/core/common/src/test/resources/org/onosproject/codec/impl/instructions-flow.json
@@ -35,5 +35,5 @@
       ],
       "deferred":[]
   },
-  "selector": {"criteria":[{"type":"ETH_TYPE","ethType":2054}]}
+  "selector": {"criteria":[{"type":"ETH_TYPE","ethType":"0x806"}]}
 }
diff --git a/tools/package/maven-plugin/src/main/java/org/onosproject/maven/OnosSwaggerMojo.java b/tools/package/maven-plugin/src/main/java/org/onosproject/maven/OnosSwaggerMojo.java
index 5d5c591..0ee5382 100644
--- a/tools/package/maven-plugin/src/main/java/org/onosproject/maven/OnosSwaggerMojo.java
+++ b/tools/package/maven-plugin/src/main/java/org/onosproject/maven/OnosSwaggerMojo.java
@@ -262,7 +262,6 @@
 
         addSummaryDescriptions(methodNode, comment);
         addJsonSchemaDefinition(definitions, tag);
-        addJsonSchemaDefinition(definitions, tag);
 
         processParameters(javaMethod, methodNode, method, tag);
 
diff --git a/web/api/src/main/resources/definitions/Flows.json b/web/api/src/main/resources/definitions/Flows.json
index ed0767c..7428d73 100644
--- a/web/api/src/main/resources/definitions/Flows.json
+++ b/web/api/src/main/resources/definitions/Flows.json
@@ -168,7 +168,7 @@
               "ethType": {
                 "type": "integer",
                 "format": "int64",
-                "example": -30516
+                "example": "0x88cc"
               }
             }
           }
diff --git a/web/api/src/main/resources/definitions/FlowsPost.json b/web/api/src/main/resources/definitions/FlowsPost.json
index c514105..11738da 100644
--- a/web/api/src/main/resources/definitions/FlowsPost.json
+++ b/web/api/src/main/resources/definitions/FlowsPost.json
@@ -24,7 +24,7 @@
     },
     "deviceId": {
       "type": "string",
-      "example": "of:0000000000000003"
+      "example": "of:0000000000000001"
     },
     "treatment": {
       "type": "object",
@@ -91,7 +91,7 @@
               "ethType": {
                 "type": "integer",
                 "format": "int64",
-                "example": -30516
+                "example": "0x88cc"
               }
             }
           }
diff --git a/web/api/src/test/resources/org/onosproject/rest/post-flow.json b/web/api/src/test/resources/org/onosproject/rest/post-flow.json
index 939b447..154ef67 100644
--- a/web/api/src/test/resources/org/onosproject/rest/post-flow.json
+++ b/web/api/src/test/resources/org/onosproject/rest/post-flow.json
@@ -13,7 +13,7 @@
     "criteria": [
       {
         "type": "ETH_TYPE",
-        "ethType": 2054
+        "ethType": "0x806"
       }
     ]
   }