[SDFAB-834] Add application filtering to ONOS UPF programmable APIs

Change-Id: I3e3d44d5d160470c3f4bfca32e7a83e194d155d8
(cherry picked from commit 97fd80c104570275937d4c24227916c83bf0dd4c)
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfApplication.java b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfApplication.java
new file mode 100644
index 0000000..83bb820
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfApplication.java
@@ -0,0 +1,245 @@
+/*
+ * Copyright 2022-present Open Networking Foundation
+ *
+ * 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.behaviour.upf;
+
+import com.google.common.annotations.Beta;
+import com.google.common.collect.Range;
+import org.onlab.packet.Ip4Prefix;
+
+import java.util.Objects;
+import java.util.Optional;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * A structure representing the application filtering for the UPF-programmable device.
+ */
+@Beta
+public final class UpfApplication implements UpfEntity {
+    // Match Keys
+    private final Ip4Prefix ipPrefix;
+    private final Range<Short> l4PortRange;
+    private final Byte ipProto;
+    // Action parameter
+    private final byte appId;
+
+    private final int priority;
+
+    private UpfApplication(Ip4Prefix ipPrefix, Range<Short> l4PortRange,
+                           Byte ipProto, byte appId, int priority) {
+        this.ipPrefix = ipPrefix;
+        this.l4PortRange = l4PortRange;
+        this.ipProto = ipProto;
+        this.appId = appId;
+        this.priority = priority;
+    }
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    @Override
+    public boolean equals(Object object) {
+        if (object == this) {
+            return true;
+        }
+        if (object == null) {
+            return false;
+        }
+        if (getClass() != object.getClass()) {
+            return false;
+        }
+
+        UpfApplication that = (UpfApplication) object;
+
+        return Objects.equals(this.ipPrefix, that.ipPrefix) &&
+                Objects.equals(this.l4PortRange, that.l4PortRange) &&
+                Objects.equals(this.ipProto, that.ipProto) &&
+                this.appId == that.appId &&
+                this.priority == that.priority;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(ipPrefix, l4PortRange, ipProto, appId, priority);
+    }
+
+    @Override
+    public String toString() {
+        return "UpfApplication{priority=" + this.priority + ", " + matchString() + " -> " + actionString() + "}";
+    }
+
+    private String matchString() {
+        StringBuilder matchStrBuilder = new StringBuilder("Match(");
+        if (this.ipPrefix != null) {
+            matchStrBuilder.append("ip_prefix=")
+                    .append(this.ipPrefix)
+                    .append(", ");
+        }
+        if (this.l4PortRange != null) {
+            matchStrBuilder.append("l4_port_range=")
+                    .append(l4PortRange)
+                    .append(", ");
+        }
+        if (this.ipProto != null) {
+            matchStrBuilder.append("ip_proto=")
+                    .append(this.ipProto)
+                    .append(", ");
+        }
+        matchStrBuilder.delete(matchStrBuilder.length() - 2, matchStrBuilder.length());
+        return matchStrBuilder.append(")").toString();
+    }
+
+    private String actionString() {
+        return "(app_id=" + this.appId + ")";
+    }
+
+    /**
+     * Gets the IPv4 prefix of this UPF application rule.
+     *
+     * @return The IPv4 prefix, Empty if none.
+     */
+    public Optional<Ip4Prefix> ip4Prefix() {
+        return Optional.ofNullable(ipPrefix);
+    }
+
+    /**
+     * Gets the L4 port range of this application filtering rule.
+     *
+     * @return A bounded range of L4 port
+     */
+    public Optional<Range<Short>> l4PortRange() {
+        return Optional.ofNullable(l4PortRange);
+    }
+
+    /**
+     * Gets the IP protocol field value of this UPF application rule.
+     *
+     * @return IP protocol field, Empty if none
+     */
+    public Optional<Byte> ipProto() {
+        return Optional.ofNullable(ipProto);
+    }
+
+    /**
+     * Get the application ID of this UPF application rule.
+     *
+     * @return Application ID
+     */
+    public byte appId() {
+        return appId;
+    }
+
+    /**
+     * Get the priority of this UPF application rule.
+     *
+     * @return Priority
+     */
+    public int priority() {
+        return priority;
+    }
+
+    @Override
+    public UpfEntityType type() {
+        return UpfEntityType.APPLICATION;
+    }
+
+    /**
+     * Builder of UpfApplication object.
+     */
+    public static class Builder {
+        // Match Keys
+        private Ip4Prefix ipPrefix = null;
+        private Range<Short> l4PortRange = null;
+        private Byte ipProto = null;
+        // Action parameters
+        private Byte appId = null;
+
+        private Integer priority = null;
+
+        public Builder() {
+
+        }
+
+        /**
+         * Set the IP prefix of the UPF application rule.
+         *
+         * @param ipPrefix IPv4 prefix
+         * @return This builder object
+         */
+        public Builder withIp4Prefix(Ip4Prefix ipPrefix) {
+            this.ipPrefix = ipPrefix;
+            return this;
+        }
+
+        /**
+         * Set the L4 port range of the UPF application rule.
+         *
+         * @param l4PortRange bounded range of L4 port
+         * @return This builder object
+         */
+        public Builder withL4PortRange(Range<Short> l4PortRange) {
+            checkArgument(l4PortRange.hasLowerBound() && l4PortRange.hasUpperBound(),
+                          "Range must be provided with bounds");
+            this.l4PortRange = l4PortRange;
+            return this;
+        }
+
+        /**
+         * Set the IP protocol field value of the UPF application rule.
+         *
+         * @param ipProto IP protocol field
+         * @return This builder object
+         */
+        public Builder withIpProto(byte ipProto) {
+            this.ipProto = ipProto;
+            return this;
+        }
+
+        /**
+         * Set the application ID of the UPF application rule.
+         *
+         * @param appId Application ID
+         * @return This builder object
+         */
+        public Builder withAppId(byte appId) {
+            this.appId = appId;
+            return this;
+        }
+
+        /**
+         * Set the priority of the UPF application rule.
+         *
+         * @param priority Priority
+         * @return This builder object
+         */
+        public Builder withPriority(int priority) {
+            this.priority = priority;
+            return this;
+        }
+
+        public UpfApplication build() {
+            checkArgument(ipPrefix != null || l4PortRange != null ||
+                                  ipProto != null,
+                          "At least one match field is required");
+            checkNotNull(appId, "Application ID must be provided");
+            checkNotNull(priority, "Priority must be provided");
+            return new UpfApplication(ipPrefix, l4PortRange, ipProto, appId, priority);
+        }
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfEntity.java b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfEntity.java
index ad1fbaa..81dd28a 100644
--- a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfEntity.java
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfEntity.java
@@ -24,6 +24,11 @@
 @Beta
 public interface UpfEntity {
     /**
+     * Default Application ID, to be used if application filtering is performed.
+     */
+    byte DEFAULT_APP_ID = 0;
+
+    /**
      * Returns the type of this entity.
      *
      * @return entity type
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfEntityType.java b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfEntityType.java
index 7ec3b19..e61abf1 100644
--- a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfEntityType.java
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfEntityType.java
@@ -29,7 +29,8 @@
     SESSION_DOWNLINK("session_downlink"),
     SESSION_UPLINK("session_downlink"),
     TUNNEL_PEER("tunnel_peer"),
-    COUNTER("counter");
+    COUNTER("counter"),
+    APPLICATION("application");
 
     private final String humanReadableName;
 
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfTerminationDownlink.java b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfTerminationDownlink.java
index 1c11dcc..cbb601b 100644
--- a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfTerminationDownlink.java
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfTerminationDownlink.java
@@ -26,12 +26,13 @@
 /**
  * A structure representing the UE Termination in the downlink direction on the
  * UPF-programmable device.
- * Provides means to configure the traffic behavior (e.g. set Traffic Class, GTP TEID, or QFI).
+ * Provide means to configure the traffic behavior (e.g. set Traffic Class, GTP TEID, or QFI).
  */
 @Beta
 public final class UpfTerminationDownlink implements UpfEntity {
     // Match Keys
     private final Ip4Address ueSessionId; // UE Session ID, use UE IP address to uniquely identify a session.
+    private final byte applicationId; // Application ID, defaults to DEFAULT_APP_ID
     // Action parameters
     private final Integer ctrId;  // Counter ID unique to this UPF Termination Rule
     private final Byte trafficClass;
@@ -39,9 +40,10 @@
     private final Byte qfi; // QoS Flow Identifier
     private final boolean dropping;
 
-    private UpfTerminationDownlink(Ip4Address ueSessionId, Integer ctrId, Byte trafficClass,
+    private UpfTerminationDownlink(Ip4Address ueSessionId, byte applicationId, Integer ctrId, Byte trafficClass,
                                    Integer teid, Byte qfi, boolean dropping) {
         this.ueSessionId = ueSessionId;
+        this.applicationId = applicationId;
         this.ctrId = ctrId;
         this.trafficClass = trafficClass;
         this.teid = teid;
@@ -69,6 +71,7 @@
         // Safe comparisons between potentially null objects
         return this.dropping == that.dropping &&
                 Objects.equals(this.ueSessionId, that.ueSessionId) &&
+                Objects.equals(this.applicationId, that.applicationId) &&
                 Objects.equals(this.ctrId, that.ctrId) &&
                 Objects.equals(this.trafficClass, that.trafficClass) &&
                 Objects.equals(this.teid, that.teid) &&
@@ -77,7 +80,7 @@
 
     @Override
     public int hashCode() {
-        return Objects.hash(ueSessionId, ctrId, trafficClass, teid, qfi, dropping);
+        return Objects.hash(ueSessionId, applicationId, ctrId, trafficClass, teid, qfi, dropping);
     }
 
     /**
@@ -90,6 +93,15 @@
     }
 
     /**
+     * Get the application ID associated with UPF Termination rule.
+     *
+     * @return the application ID
+     */
+    public byte applicationId() {
+        return applicationId;
+    }
+
+    /**
      * Get PDR Counter ID associated with UPF Termination rule.
      *
      * @return PDR counter cell ID
@@ -141,15 +153,20 @@
 
     @Override
     public String toString() {
-        return "TerminationDL{" + matchString() + "->" + actionString() + "}";
+        return "TerminationDL{" + matchString() + " -> " + actionString() + "}";
     }
 
     private String matchString() {
-        return "Match(ue_addr=" + this.ueSessionId() + ")";
+        return "Match(ue_addr=" + this.ueSessionId() + ", app_id=" + this.applicationId + ")";
     }
 
     private String actionString() {
-        return "(TEID=" + this.teid() +
+        String fwd = "FWD";
+        if (this.needsDropping()) {
+            fwd = "DROP";
+        }
+        return "(" + fwd +
+                ", TEID=" + this.teid() +
                 ", CTR_ID=" + this.counterId() +
                 ", QFI=" + this.qfi() +
                 ", TC=" + this.trafficClass() +
@@ -158,6 +175,7 @@
 
     public static class Builder {
         private Ip4Address ueSessionId = null;
+        private Byte applicationId = null;
         private Integer ctrId = null;
         private Byte trafficClass = null;
         private Integer teid = null;
@@ -180,6 +198,17 @@
         }
 
         /**
+         * Set the ID of the application.
+         *
+         * @param applicationId Application ID
+         * @return This builder object
+         */
+        public Builder withApplicationId(byte applicationId) {
+            this.applicationId = applicationId;
+            return this;
+        }
+
+        /**
          * Set the dataplane counter cell ID.
          *
          * @param ctrId PDR counter cell ID
@@ -226,7 +255,7 @@
         /**
          * Sets whether to drop downlink UPF termination traffic or not.
          *
-         * @param drop True if request to buffer, false otherwise
+         * @param drop True if request to drop, false otherwise
          * @return This builder object
          */
         public Builder needsDropping(boolean drop) {
@@ -234,16 +263,17 @@
             return this;
         }
 
-
         public UpfTerminationDownlink build() {
             // Match fields must be provided
             checkNotNull(ueSessionId, "UE session ID must be provided");
-
+            if (applicationId == null) {
+                applicationId = DEFAULT_APP_ID;
+            }
             checkNotNull(ctrId, "Counter ID must be provided");
             // TODO: should we verify that when dropping no other fields are provided
             return new UpfTerminationDownlink(
-                    this.ueSessionId, this.ctrId, this.trafficClass, this.teid,
-                    this.qfi, this.drop
+                    this.ueSessionId, this.applicationId, this.ctrId, this.trafficClass,
+                    this.teid, this.qfi, this.drop
             );
         }
 
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfTerminationUplink.java b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfTerminationUplink.java
index f759202..1c6dd56 100644
--- a/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfTerminationUplink.java
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/upf/UpfTerminationUplink.java
@@ -26,20 +26,22 @@
 /**
  * A structure representing the UE Termination in the uplink direction on the
  * UPF-programmable device.
- * Provides means to configure the traffic behavior (e.g. set Traffic Class).
+ * Provide means to configure the traffic behavior (e.g. set Traffic Class).
  */
 @Beta
 public final class UpfTerminationUplink implements UpfEntity {
     // Match Keys
     private final Ip4Address ueSessionId; // UE Session ID, use UE IP address to uniquely identify a session.
+    private final byte applicationId; // Application ID defaults to DEFAULT_APP_ID
     // Action parameters
     private final Integer ctrId;  // Counter ID unique to this UPF Termination Rule
     private final Byte trafficClass;
     private final boolean dropping;
 
-    private UpfTerminationUplink(Ip4Address ueSessionId, Integer ctrId, Byte trafficClass,
+    private UpfTerminationUplink(Ip4Address ueSessionId, byte applicationId, Integer ctrId, Byte trafficClass,
                                  boolean dropping) {
         this.ueSessionId = ueSessionId;
+        this.applicationId = applicationId;
         this.ctrId = ctrId;
         this.trafficClass = trafficClass;
         this.dropping = dropping;
@@ -65,13 +67,14 @@
         // Safe comparisons between potentially null objects
         return this.dropping == that.dropping &&
                 Objects.equals(this.ueSessionId, that.ueSessionId) &&
+                Objects.equals(this.applicationId, that.applicationId) &&
                 Objects.equals(this.ctrId, that.ctrId) &&
                 Objects.equals(this.trafficClass, that.trafficClass);
     }
 
     @Override
     public int hashCode() {
-        return Objects.hash(ueSessionId, ctrId, trafficClass, dropping);
+        return Objects.hash(ueSessionId, applicationId, ctrId, trafficClass, dropping);
     }
 
     /**
@@ -84,6 +87,15 @@
     }
 
     /**
+     * Get the application ID associated with UPF Termination rule.
+     *
+     * @return Application ID
+     */
+    public byte applicationId() {
+        return applicationId;
+    }
+
+    /**
      * Get PDR Counter ID associated with UPF Termination rule.
      *
      * @return PDR counter cell ID
@@ -117,19 +129,27 @@
 
     @Override
     public String toString() {
-        return "TerminationUL{" + matchString() + "->" + actionString() + "}";
+        return "TerminationUL{" + matchString() + " -> " + actionString() + "}";
     }
 
     private String matchString() {
-        return "Match(ue_addr=" + this.ueSessionId() + ")";
+        return "Match(ue_addr=" + this.ueSessionId() + ", app_id=" + this.applicationId() + ")";
     }
 
     private String actionString() {
-        return "(CTR_ID=" + this.counterId() + ", TC=" + this.trafficClass() + ")";
+        String fwd = "FWD";
+        if (this.needsDropping()) {
+            fwd = "DROP";
+        }
+        return "(" + fwd +
+                ", CTR_ID=" + this.counterId() +
+                ", TC=" + this.trafficClass() +
+                ")";
     }
 
     public static class Builder {
         private Ip4Address ueSessionId = null;
+        private Byte applicationId = null;
         private Integer ctrId = null;
         private Byte trafficClass = null;
         private boolean dropping = false;
@@ -150,6 +170,17 @@
         }
 
         /**
+         * Set the ID of the application.
+         *
+         * @param applicationId Application ID
+         * @return This builder object
+         */
+        public Builder withApplicationId(byte applicationId) {
+            this.applicationId = applicationId;
+            return this;
+        }
+
+        /**
          * Set the dataplane counter cell ID.
          *
          * @param ctrId PDR counter cell ID
@@ -174,7 +205,7 @@
         /**
          * Sets whether to drop uplink UPF termination traffic or not.
          *
-         * @param dropping True if request to buffer, false otherwise
+         * @param dropping True if request to drop, false otherwise
          * @return This builder object
          */
         public Builder needsDropping(boolean dropping) {
@@ -185,11 +216,15 @@
         public UpfTerminationUplink build() {
             // Match fields must be provided
             checkNotNull(ueSessionId, "UE session ID must be provided");
-
+            if (applicationId == null) {
+                applicationId = DEFAULT_APP_ID;
+            }
             checkNotNull(ctrId, "Counter ID must be provided");
             // TODO: should we verify that when dropping no other fields are provided
             return new UpfTerminationUplink(
-                    this.ueSessionId, this.ctrId, this.trafficClass, this.dropping);
+                    this.ueSessionId, this.applicationId, this.ctrId,
+                    this.trafficClass, this.dropping
+            );
         }
 
     }