adding EthType to secure handling ether types

this will also pretty print ethertypes in flow output

Change-Id: I9363070ad308f3c756735e29b3992c500e503636
diff --git a/core/api/src/main/java/org/onosproject/net/flow/criteria/Criteria.java b/core/api/src/main/java/org/onosproject/net/flow/criteria/Criteria.java
index a163cea..f285496 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/criteria/Criteria.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/criteria/Criteria.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.net.flow.criteria;
 
+import org.onlab.packet.EthType;
 import org.onosproject.net.IndexedLambda;
 import org.onosproject.net.Lambda;
 import org.onosproject.net.OchSignal;
@@ -101,6 +102,16 @@
     }
 
     /**
+     * Creates a match on ETH_TYPE field using the specified value.
+     *
+     * @param ethType eth type value
+     * @return match criterion
+     */
+    public static Criterion matchEthType(EthType ethType) {
+        return new EthTypeCriterion(ethType);
+    }
+
+    /**
      * Creates a match on VLAN ID field using the specified value.
      *
      * @param vlanId vlan id value
diff --git a/core/api/src/main/java/org/onosproject/net/flow/criteria/EthTypeCriterion.java b/core/api/src/main/java/org/onosproject/net/flow/criteria/EthTypeCriterion.java
index a42d8d3..465de6b 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/criteria/EthTypeCriterion.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/criteria/EthTypeCriterion.java
@@ -15,6 +15,8 @@
  */
 package org.onosproject.net.flow.criteria;
 
+import org.onlab.packet.EthType;
+
 import java.util.Objects;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
@@ -24,7 +26,7 @@
  */
 public final class EthTypeCriterion implements Criterion {
     private static final int MASK = 0xffff;
-    private final int ethType;              // Ethernet type value: 16 bits
+    private final EthType ethType;              // Ethernet type value: 16 bits
 
     /**
      * Constructor.
@@ -33,7 +35,16 @@
      * integer)
      */
     EthTypeCriterion(int ethType) {
-        this.ethType = ethType & MASK;
+        this.ethType = new EthType(ethType & MASK);
+    }
+
+    /**
+     * Constructor.
+     *
+     * @param ethType the Ethernet frame type to match
+     */
+    EthTypeCriterion(EthType ethType) {
+        this.ethType = ethType;
     }
 
     @Override
@@ -46,14 +57,14 @@
      *
      * @return the Ethernet frame type to match (16 bits unsigned integer)
      */
-    public int ethType() {
+    public EthType ethType() {
         return ethType;
     }
 
     @Override
     public String toString() {
         return toStringHelper(type().toString())
-                .add("ethType", Long.toHexString(ethType))
+                .add("ethType", ethType.toString())
                 .toString();
     }
 
diff --git a/core/api/src/test/java/org/onosproject/net/flow/criteria/CriteriaTest.java b/core/api/src/test/java/org/onosproject/net/flow/criteria/CriteriaTest.java
index bad60a0..4733cb9 100644
--- a/core/api/src/test/java/org/onosproject/net/flow/criteria/CriteriaTest.java
+++ b/core/api/src/test/java/org/onosproject/net/flow/criteria/CriteriaTest.java
@@ -16,6 +16,7 @@
 package org.onosproject.net.flow.criteria;
 
 import org.junit.Test;
+import org.onlab.packet.EthType;
 import org.onosproject.net.ChannelSpacing;
 import org.onosproject.net.GridType;
 import org.onosproject.net.Lambda;
@@ -418,8 +419,8 @@
      */
     @Test
     public void testMatchEthTypeMethod() {
-        int ethType = 12;
-        Criterion matchEthType = Criteria.matchEthType(ethType);
+        EthType ethType = new EthType(12);
+        Criterion matchEthType = Criteria.matchEthType(new EthType(12));
         EthTypeCriterion ethTypeCriterion =
                 checkAndConvert(matchEthType,
                                 Criterion.Type.ETH_TYPE,
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/EncodeCriterionCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/EncodeCriterionCodec.java
index 0ca344d..04cdb4f 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/EncodeCriterionCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/EncodeCriterionCodec.java
@@ -165,7 +165,7 @@
         public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
             final EthTypeCriterion ethTypeCriterion =
                     (EthTypeCriterion) criterion;
-            return root.put(CriterionCodec.ETH_TYPE, ethTypeCriterion.ethType());
+            return root.put(CriterionCodec.ETH_TYPE, ethTypeCriterion.ethType().toShort());
         }
     }
 
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 2bc611c..2a4c138 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
@@ -134,7 +134,7 @@
      * @return true if the JSON matches the criterion, false otherwise.
      */
     private boolean matchCriterion(EthTypeCriterion criterion) {
-        final int ethType = criterion.ethType();
+        final int ethType = criterion.ethType().toShort();
         final int jsonEthType = jsonCriterion.get("ethType").intValue();
         if (ethType != jsonEthType) {
             description.appendText("ethType was "
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 2e0e8b3..2a93b55 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
@@ -22,6 +22,7 @@
 
 import org.junit.Before;
 import org.junit.Test;
+import org.onlab.packet.EthType;
 import org.onlab.packet.Ethernet;
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.IpPrefix;
@@ -148,7 +149,7 @@
         assertThat(rule.selector().criteria().size(), is(1));
         Criterion criterion1 = rule.selector().criteria().iterator().next();
         assertThat(criterion1.type(), is(Criterion.Type.ETH_TYPE));
-        assertThat(((EthTypeCriterion) criterion1).ethType(), is(2054));
+        assertThat(((EthTypeCriterion) criterion1).ethType(), is(new EthType(2054)));
 
         assertThat(rule.treatment().allInstructions().size(), is(1));
         Instruction instruction1 = rule.treatment().allInstructions().get(0);
@@ -356,7 +357,7 @@
         Criterion criterion;
 
         criterion = getCriterion(Criterion.Type.ETH_TYPE);
-        assertThat(((EthTypeCriterion) criterion).ethType(), is(2054));
+        assertThat(((EthTypeCriterion) criterion).ethType(), is(new EthType(2054)));
 
         criterion = getCriterion(Criterion.Type.ETH_DST);
         assertThat(((EthCriterion) criterion).mac(),
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java
index f9e445a..c133483 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java
@@ -243,7 +243,7 @@
             Criterion c = intent.selector().getCriterion(Criterion.Type.ETH_TYPE);
             if (c != null && c instanceof EthTypeCriterion) {
                 EthTypeCriterion ethertype = (EthTypeCriterion) c;
-                treat.popMpls((short) ethertype.ethType());
+                treat.popMpls(ethertype.ethType().toShort());
             } else {
                 treat.popMpls(Ethernet.TYPE_IPV4);
             }
diff --git a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
index f930f8c..3c14cb0 100644
--- a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
+++ b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
@@ -20,6 +20,7 @@
 import com.google.common.collect.ImmutableSet;
 
 import org.onlab.packet.ChassisId;
+import org.onlab.packet.EthType;
 import org.onlab.packet.Ip4Address;
 import org.onlab.packet.Ip4Prefix;
 import org.onlab.packet.Ip6Address;
@@ -293,6 +294,7 @@
                     PortCriterion.class,
                     MetadataCriterion.class,
                     EthCriterion.class,
+                    EthType.class,
                     EthTypeCriterion.class,
                     VlanIdCriterion.class,
                     VlanPcpCriterion.class,