[ONOS-7675] Initial implementation of IntProgrammable

Change-Id: I277ea7a56bb3a38debbb2959823df394a4103541
diff --git a/apps/inbandtelemetry/api/BUCK b/apps/inbandtelemetry/api/BUCK
new file mode 100644
index 0000000..97564d9
--- /dev/null
+++ b/apps/inbandtelemetry/api/BUCK
@@ -0,0 +1,14 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//lib:KRYO',
+    '//core/store/serializers:onos-core-serializers',
+]
+
+TEST_DEPS = [
+    '//lib:TEST_ADAPTERS',
+]
+
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+    test_deps = TEST_DEPS,
+)
diff --git a/apps/inbandtelemetry/api/pom.xml b/apps/inbandtelemetry/api/pom.xml
new file mode 100644
index 0000000..a6c79aa
--- /dev/null
+++ b/apps/inbandtelemetry/api/pom.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2016-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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <artifactId>onos-apps-inbandtelemetry</artifactId>
+        <groupId>org.onosproject</groupId>
+        <version>1.14.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>onos-apps-inbandtelemetry-api</artifactId>
+    <packaging>bundle</packaging>
+
+    <url>http://onosproject.org</url>
+
+    <description>P4 INT Management Application API</description>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onlab-junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-core-serializers</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+</project>
diff --git a/apps/inbandtelemetry/intmgr/src/main/java/org/onosproject/inbandtelemetry/api/IntConfig.java b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntConfig.java
similarity index 85%
rename from apps/inbandtelemetry/intmgr/src/main/java/org/onosproject/inbandtelemetry/api/IntConfig.java
rename to apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntConfig.java
index dc93312..a3e6148 100644
--- a/apps/inbandtelemetry/intmgr/src/main/java/org/onosproject/inbandtelemetry/api/IntConfig.java
+++ b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntConfig.java
@@ -18,7 +18,9 @@
 import com.google.common.annotations.Beta;
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.MacAddress;
-import org.onosproject.net.Port;
+import org.onlab.packet.TpPort;
+
+import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
  * Network-level INT configuration.
@@ -46,14 +48,14 @@
     }
 
     private final IpAddress collectorIp;
-    private final Port collectorPort;
+    private final TpPort collectorPort;
     private final MacAddress collectorNextHopMac;
     private final IpAddress sinkIp;
     private final MacAddress sinkMac;
     private final TelemetrySpec spec;
     private boolean enabled;
 
-    private IntConfig(IpAddress collectorIp, Port collectorPort, MacAddress collectorNextHopMac,
+    private IntConfig(IpAddress collectorIp, TpPort collectorPort, MacAddress collectorNextHopMac,
                       IpAddress sinkIp, MacAddress sinkMac, TelemetrySpec spec, boolean enabled) {
         this.collectorIp = collectorIp;
         this.collectorPort = collectorPort;
@@ -82,7 +84,7 @@
      *
      * @return collector UDP port number
      */
-    public Port collectorPort() {
+    public TpPort collectorPort() {
         return collectorPort;
     }
 
@@ -125,7 +127,7 @@
     }
 
     /**
-     * Returns the type of telemetry spec (P4INT or IOAM).
+     * Returns the type of telemetry spec as per {@link TelemetrySpec}.
      *
      * @return telemetry spec
      */
@@ -143,12 +145,21 @@
     }
 
     /**
+     * Returns a new builder.
+     *
+     * @return new builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
      * An IntConfig object builder.
      */
     public static final class Builder {
 
         private IpAddress collectorIp;
-        private Port collectorPort;
+        private TpPort collectorPort;
         private MacAddress collectorNextHopMac;
         private IpAddress sinkIp;
         private MacAddress sinkMac;
@@ -172,7 +183,7 @@
          * @param collectorPort UDP port number of the collector
          * @return an IntConfig builder
          */
-        public IntConfig.Builder withCollectorPort(Port collectorPort) {
+        public IntConfig.Builder withCollectorPort(TpPort collectorPort) {
             this.collectorPort = collectorPort;
             return this;
         }
@@ -229,17 +240,22 @@
          * @param enabled the status of INT
          * @return an IntConfig builder
          */
-        public IntConfig.Builder withEnabled(boolean enabled) {
+        public IntConfig.Builder enabled(boolean enabled) {
             this.enabled = enabled;
             return this;
         }
 
         /**
-         * Bulids the IntConfig object.
+         * Builds the IntConfig object.
          *
          * @return an IntConfig object
          */
         public IntConfig build() {
+            checkNotNull(collectorIp, "Collector IP should be specified.");
+            checkNotNull(collectorPort, "Collector port number should be specified.");
+            checkNotNull(collectorNextHopMac, "Next hop MAC address for report packets should be provided.");
+            checkNotNull(sinkIp, "Sink IP address for report packets should be specified.");
+            checkNotNull(sinkMac, "Sink MAC address for report packets should be specified.");
             return new IntConfig(collectorIp, collectorPort, collectorNextHopMac,
                                  sinkIp, sinkMac, spec, enabled);
         }
diff --git a/apps/inbandtelemetry/intmgr/src/main/java/org/onosproject/inbandtelemetry/api/IntIntent.java b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntIntent.java
similarity index 98%
rename from apps/inbandtelemetry/intmgr/src/main/java/org/onosproject/inbandtelemetry/api/IntIntent.java
rename to apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntIntent.java
index 4cc1ff2..21d5381 100644
--- a/apps/inbandtelemetry/intmgr/src/main/java/org/onosproject/inbandtelemetry/api/IntIntent.java
+++ b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntIntent.java
@@ -204,6 +204,15 @@
     }
 
     /**
+     * Returns a new builder.
+     *
+     * @return new builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
      * An IntIntent builder.
      */
     public static final class Builder {
diff --git a/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntIntentId.java b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntIntentId.java
new file mode 100644
index 0000000..293cbdb
--- /dev/null
+++ b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntIntentId.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2018-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.inbandtelemetry.api;
+
+import org.onlab.util.Identifier;
+
+/**
+ * Representation of a IntIntent ID.
+ */
+public final class IntIntentId extends Identifier<Long> {
+    private IntIntentId(long id) {
+        super(id);
+    }
+
+    /**
+     * Creates an IntIntent ID from a given long value.
+     *
+     * @param id long value
+     * @return IntIntent ID
+     */
+    public static IntIntentId valueOf(long id) {
+        return new IntIntentId(id);
+    }
+
+    /**
+     * Gets the IntIntent ID value.
+     *
+     * @return IntIntent ID value as long
+     */
+    public long value() {
+        return this.identifier;
+    }
+
+    @Override
+    public String toString() {
+        return Long.toHexString(this.identifier);
+    }
+}
diff --git a/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntObjective.java b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntObjective.java
new file mode 100644
index 0000000..da8dc29
--- /dev/null
+++ b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntObjective.java
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2018-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.inbandtelemetry.api;
+
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.TrafficSelector;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.inbandtelemetry.api.IntIntent.IntMetadataType;
+import static org.onosproject.inbandtelemetry.api.IntIntent.IntHeaderType;
+
+public final class IntObjective {
+
+    private static final int DEFAULT_PRIORITY = 10;
+
+    // TrafficSelector to describe target flows to monitor
+    private final TrafficSelector selector;
+    // Set of metadata types to collect
+    private final Set<IntMetadataType> metadataTypes;
+    // Type of header (either hop-by-hop or destination)
+    private final IntHeaderType headerType;
+
+    /**
+     * Creates an IntObjective.
+     *
+     * @param selector      the traffic selector that identifies traffic to enable INT
+     * @param metadataTypes a set of metadata types to collect
+     * @param headerType    the type of INT header
+     */
+    private IntObjective(TrafficSelector selector, Set<IntMetadataType> metadataTypes,
+                         IntHeaderType headerType) {
+        this.selector = selector;
+        this.metadataTypes = metadataTypes;
+        this.headerType = headerType;
+    }
+
+    /**
+     * Returns traffic selector of this objective.
+     *
+     * @return traffic selector
+     */
+    public TrafficSelector selector() {
+        return selector;
+    }
+
+    /**
+     * Returns a set of metadata types specified in this objective.
+     *
+     * @return instruction bitmap
+     */
+    public Set<IntMetadataType> metadataTypes() {
+        return metadataTypes;
+    }
+
+    /**
+     * Returns a INT header type specified in this objective.
+     *
+     * @return INT header type
+     */
+    public IntHeaderType headerType() {
+        return headerType;
+    }
+
+    /**
+     * An IntObjective builder.
+     */
+    public static final class Builder {
+        private TrafficSelector selector = DefaultTrafficSelector.emptySelector();
+        private Set<IntMetadataType> metadataTypes = new HashSet<>();
+        private IntHeaderType headerType = IntHeaderType.HOP_BY_HOP;
+
+        /**
+         * Assigns a selector to the IntObjective.
+         *
+         * @param selector a traffic selector
+         * @return an IntObjective builder
+         */
+        public IntObjective.Builder withSelector(TrafficSelector selector) {
+            this.selector = selector;
+            return this;
+        }
+
+        /**
+         * Add a metadata type to the IntObjective.
+         *
+         * @param metadataTypes a set of metadata types
+         * @return an IntObjective builder
+         */
+        public IntObjective.Builder withMetadataTypes(Set<IntMetadataType> metadataTypes) {
+            this.metadataTypes.addAll(metadataTypes);
+            return this;
+        }
+
+        /**
+         * Assigns a header type to the IntObjective.
+         *
+         * @param headerType a header type
+         * @return an IntObjective builder
+         */
+        public IntObjective.Builder withHeaderType(IntHeaderType headerType) {
+            this.headerType = headerType;
+            return this;
+        }
+
+        /**
+         * Builds the IntObjective.
+         *
+         * @return an IntObjective
+         */
+        public IntObjective build() {
+            checkArgument(!selector.criteria().isEmpty(), "Empty selector cannot match any flow.");
+            checkArgument(!metadataTypes.isEmpty(), "Metadata types cannot be empty");
+            checkNotNull(headerType, "Header type cannot be null.");
+
+            return new IntObjective(selector, metadataTypes, headerType);
+        }
+    }
+}
diff --git a/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntProgrammable.java b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntProgrammable.java
new file mode 100644
index 0000000..452b787
--- /dev/null
+++ b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntProgrammable.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2018-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.inbandtelemetry.api;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.net.driver.HandlerBehaviour;
+
+import java.util.concurrent.CompletableFuture;
+
+@Beta
+public interface IntProgrammable extends HandlerBehaviour {
+
+    /**
+     * Initializes the pipeline, by installing required flow rules
+     * not relevant to specific watchlist, report and event.
+     */
+    void init();
+
+    /**
+     * Adds a given IntObjective to the device.
+     *
+     * @param obj an IntObjective
+     * @return true if the objective is successfully added; false otherwise.
+     */
+    CompletableFuture<Boolean> addIntObjective(IntObjective obj);
+
+    /**
+     * Removes a given IntObjective entry from the device.
+     *
+     * @param obj an IntObjective
+     * @return true if the objective is successfully removed; false otherwise.
+     */
+    CompletableFuture<Boolean> removeIntObjective(IntObjective obj);
+
+    /**
+     * Set up report-related configuration.
+     *
+     * @param config a configuration regarding to the collector
+     * @return true if the objective is successfully added; false otherwise.
+     */
+    CompletableFuture<Boolean> setupIntConfig(IntConfig config);
+
+    //TODO: [ONOS-7616] Design IntEvent and related APIs
+}
diff --git a/apps/inbandtelemetry/intmgr/src/main/java/org/onosproject/inbandtelemetry/api/IntService.java b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntService.java
similarity index 84%
rename from apps/inbandtelemetry/intmgr/src/main/java/org/onosproject/inbandtelemetry/api/IntService.java
rename to apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntService.java
index a666a1b..ef2e1ad 100644
--- a/apps/inbandtelemetry/intmgr/src/main/java/org/onosproject/inbandtelemetry/api/IntService.java
+++ b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/IntService.java
@@ -18,6 +18,7 @@
 import com.google.common.annotations.Beta;
 import org.onosproject.net.DeviceId;
 
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -88,24 +89,31 @@
      * Installs an IntIntent to devices.
      *
      * @param intIntent an IntIntent
-     * @return an ID corresponding to given intIntent
+     * @return an IntIntent ID corresponding to given intIntent
      */
-    int installIntIntent(IntIntent intIntent);
+    IntIntentId installIntIntent(IntIntent intIntent);
 
     /**
      * Removes an IntIntent from devices.
      *
      * @param intentId ID of the intIntent to remove
      */
-    void removeIntIntent(int intentId);
+    void removeIntIntent(IntIntentId intentId);
 
     /**
-     * Returns an intIntent for given intent ID.
+     * Returns an IntIntent for given intent ID.
      *
-     * @param intentId id of the intIntent to retrieve
+     * @param intentId ID of the intIntent to retrieve
      * @return an IntIntent
      */
-    IntIntent getIntIntent(int intentId);
+    IntIntent getIntIntent(IntIntentId intentId);
+
+    /**
+     * Returns all IntIntents installed in the network.
+     *
+     * @return an IntIntent
+     */
+    Map<IntIntentId, IntIntent> getIntIntents();
 
     //TODO: [ONOS-7616] Design IntEvent and related APIs
 }
\ No newline at end of file
diff --git a/apps/inbandtelemetry/intmgr/src/main/java/org/onosproject/inbandtelemetry/api/package-info.java b/apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/package-info.java
similarity index 100%
rename from apps/inbandtelemetry/intmgr/src/main/java/org/onosproject/inbandtelemetry/api/package-info.java
rename to apps/inbandtelemetry/api/src/main/java/org/onosproject/inbandtelemetry/api/package-info.java
diff --git a/apps/inbandtelemetry/pom.xml b/apps/inbandtelemetry/pom.xml
new file mode 100644
index 0000000..0105fbe
--- /dev/null
+++ b/apps/inbandtelemetry/pom.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2015-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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-apps</artifactId>
+        <version>1.14.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>onos-apps-inbandtelemetry</artifactId>
+    <packaging>pom</packaging>
+
+    <description>ONOS In-band Network Telemetry applications</description>
+
+    <modules>
+        <module>api</module>
+        <module>impl</module>
+    </modules>
+</project>