TUNNEL_ID field support in flow matching and actions

Change-Id: I886123a7c8d57dc4a3e12727ec8a9be4920da79e
diff --git a/core/api/src/main/java/org/onosproject/net/flow/DefaultTrafficSelector.java b/core/api/src/main/java/org/onosproject/net/flow/DefaultTrafficSelector.java
index 67e057e..09c6327 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/DefaultTrafficSelector.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/DefaultTrafficSelector.java
@@ -294,6 +294,11 @@
         }
 
         @Override
+        public TrafficSelector.Builder matchTunnelId(long tunnelId) {
+            return add(Criteria.matchTunnelId(tunnelId));
+        }
+
+        @Override
         public Builder matchIPv6ExthdrFlags(short exthdrFlags) {
             return add(Criteria.matchIPv6ExthdrFlags(exthdrFlags));
         }
diff --git a/core/api/src/main/java/org/onosproject/net/flow/DefaultTrafficTreatment.java b/core/api/src/main/java/org/onosproject/net/flow/DefaultTrafficTreatment.java
index 96abbe9..fe657e6 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/DefaultTrafficTreatment.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/DefaultTrafficTreatment.java
@@ -385,6 +385,11 @@
         }
 
         @Override
+        public Builder setTunnelId(long tunnelId) {
+            return add(Instructions.modTunnelId(tunnelId));
+        }
+
+        @Override
         public TrafficTreatment build() {
             //Don't add DROP instruction by default when instruction
             //set is empty. This will be handled in DefaultSingleTablePipeline
diff --git a/core/api/src/main/java/org/onosproject/net/flow/TrafficSelector.java b/core/api/src/main/java/org/onosproject/net/flow/TrafficSelector.java
index 3b28093..6661cb6 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/TrafficSelector.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/TrafficSelector.java
@@ -301,6 +301,14 @@
         Builder matchMplsLabel(MplsLabel mplsLabel);
 
         /**
+         * Matches a tunnel id.
+         *
+         * @param tunnelId a tunnel id
+         * @return a selection builder
+         */
+        Builder matchTunnelId(long tunnelId);
+
+        /**
          * Matches on IPv6 Extension Header pseudo-field flags.
          *
          * @param exthdrFlags the IPv6 Extension Header pseudo-field flags
diff --git a/core/api/src/main/java/org/onosproject/net/flow/TrafficTreatment.java b/core/api/src/main/java/org/onosproject/net/flow/TrafficTreatment.java
index 3802bf2..35ef6d3 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/TrafficTreatment.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/TrafficTreatment.java
@@ -311,6 +311,14 @@
         Builder writeMetadata(long value, long mask);
 
         /**
+         * Sets the tunnel id.
+         *
+         * @param tunnelId a tunnel id.
+         * @return a treatment builder.
+         */
+        Builder setTunnelId(long tunnelId);
+
+        /**
          * Builds an immutable traffic treatment descriptor.
          * <p>
          * If the treatment is empty when build() is called, it will add a default
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 0347694..91221e2 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
@@ -356,6 +356,16 @@
     }
 
     /**
+     * Creates a match on Tunnel ID.
+     *
+     * @param tunnelId Tunnel ID (64 bits)
+     * @return match criterion
+     */
+    public static Criterion matchTunnelId(long tunnelId) {
+        return new TunnelIdCriterion(tunnelId);
+    }
+
+    /**
      * Creates a match on IPv6 Extension Header pseudo-field fiags.
      * Those are defined in Criterion.IPv6ExthdrFlags.
      *
diff --git a/core/api/src/main/java/org/onosproject/net/flow/criteria/TunnelIdCriterion.java b/core/api/src/main/java/org/onosproject/net/flow/criteria/TunnelIdCriterion.java
new file mode 100644
index 0000000..3362c73
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/flow/criteria/TunnelIdCriterion.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2015 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.net.flow.criteria;
+
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+/**
+ * Implementation of Tunnel ID criterion.
+ */
+public class TunnelIdCriterion implements Criterion {
+    private final long tunnelId;
+
+    /**
+     * Constructor.
+     *
+     * @param tunnelId a Tunnel ID to match(64 bits)
+     */
+    TunnelIdCriterion(long tunnelId) {
+        this.tunnelId = tunnelId;
+    }
+
+    @Override
+    public Type type() {
+        return Type.TUNNEL_ID;
+    }
+
+    /**
+     * Gets the Tunnel ID to match.
+     *
+     * @return the Tunnel ID to match (64 bits)
+     */
+    public long tunnelId() {
+        return tunnelId;
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(type().toString())
+                .add("tunnelId", Long.toHexString(tunnelId))
+                .toString();
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(type().ordinal(), tunnelId);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof TunnelIdCriterion) {
+            TunnelIdCriterion that = (TunnelIdCriterion) obj;
+            return Objects.equals(tunnelId, that.tunnelId) &&
+                    Objects.equals(this.type(), that.type());
+        }
+        return false;
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/flow/instructions/Instructions.java b/core/api/src/main/java/org/onosproject/net/flow/instructions/Instructions.java
index e8101f1..0467a91 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/instructions/Instructions.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/instructions/Instructions.java
@@ -350,6 +350,17 @@
     }
 
     /**
+     * Creates a Tunnel ID modification.
+     *
+     * @param tunnelId the Tunnel ID to modify to
+     * @return a L2 modification
+     */
+    public static L2ModificationInstruction modTunnelId(long tunnelId) {
+        checkNotNull(tunnelId, "Tunnel id cannot be null");
+        return new L2ModificationInstruction.ModTunnelIdInstruction(tunnelId);
+    }
+
+    /**
      *  Drop instruction.
      */
     public static final class DropInstruction implements Instruction {
diff --git a/core/api/src/main/java/org/onosproject/net/flow/instructions/L2ModificationInstruction.java b/core/api/src/main/java/org/onosproject/net/flow/instructions/L2ModificationInstruction.java
index 2fd809f..bfb6300 100644
--- a/core/api/src/main/java/org/onosproject/net/flow/instructions/L2ModificationInstruction.java
+++ b/core/api/src/main/java/org/onosproject/net/flow/instructions/L2ModificationInstruction.java
@@ -81,7 +81,12 @@
         /**
          * VLAN Push modification.
          */
-        VLAN_PUSH
+        VLAN_PUSH,
+
+        /**
+         * Tunnle id modification.
+         */
+        TUNNEL_ID
     }
 
     // TODO: Create factory class 'Instructions' that will have various factory
@@ -401,4 +406,50 @@
             return false;
         }
     }
+
+    /**
+     * Represents a Tunnel id modification.
+     */
+    public static final class ModTunnelIdInstruction
+            extends L2ModificationInstruction {
+
+        private final long tunnelId;
+
+        ModTunnelIdInstruction(long tunnelId) {
+            this.tunnelId = tunnelId;
+        }
+
+        public long tunnelId() {
+            return this.tunnelId;
+        }
+
+        @Override
+        public L2SubType subtype() {
+            return L2SubType.TUNNEL_ID;
+        }
+
+        @Override
+        public String toString() {
+            return toStringHelper(subtype().toString())
+                    .add("id", Long.toHexString(tunnelId))
+                    .toString();
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(type(), subtype(), tunnelId);
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (obj instanceof ModTunnelIdInstruction) {
+                ModTunnelIdInstruction that = (ModTunnelIdInstruction) obj;
+                return  Objects.equals(tunnelId, that.tunnelId);
+            }
+            return false;
+        }
+    }
 }