Initial implementation of IntService and sample application

This implementation has been used for P4Workshop demo (ONOS INT Support).

Change-Id: I2ff94f8a79f6d5a328b94f7ed178e575b81c3fe9
diff --git a/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/IntControl.java b/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/IntControl.java
new file mode 100644
index 0000000..efd3590
--- /dev/null
+++ b/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/IntControl.java
@@ -0,0 +1,55 @@
+/*
+ * 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.
+ */
+package org.onosproject.inbandtelemetry.app;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.core.CoreService;
+import org.onosproject.inbandtelemetry.api.IntService;
+import org.onosproject.net.host.HostService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(immediate = true)
+public class IntControl {
+    private final Logger log = LoggerFactory.getLogger(getClass());
+//    private ApplicationId appId;
+//    private static final int collectorPort = 1234;
+//    private static final IpAddress collectorIp = IpAddress.valueOf("10.0.0.3");
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected IntService intService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected HostService hostService;
+
+    @Activate
+    protected void activate() {
+        coreService.registerApplication("org.onosproject.inbandtelemetry.app");
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        log.info("Stopped");
+    }
+}
diff --git a/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/package-info.java b/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/package-info.java
new file mode 100644
index 0000000..859bdf7
--- /dev/null
+++ b/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * IntService sample application.
+ */
+package org.onosproject.inbandtelemetry.app;
diff --git a/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/ui/IntAppTableMessageHandler.java b/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/ui/IntAppTableMessageHandler.java
new file mode 100644
index 0000000..fafd4b4
--- /dev/null
+++ b/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/ui/IntAppTableMessageHandler.java
@@ -0,0 +1,148 @@
+/*
+ * 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.
+ */
+package org.onosproject.inbandtelemetry.app.ui;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableSet;
+import org.onosproject.inbandtelemetry.api.IntIntent;
+import org.onosproject.inbandtelemetry.api.IntIntentId;
+import org.onosproject.inbandtelemetry.api.IntService;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.flow.criteria.IPCriterion;
+import org.onosproject.net.flow.criteria.TcpPortCriterion;
+import org.onosproject.net.flow.criteria.UdpPortCriterion;
+import org.onosproject.ui.RequestHandler;
+import org.onosproject.ui.UiMessageHandler;
+import org.onosproject.ui.table.TableModel;
+import org.onosproject.ui.table.TableRequestHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Message handler for installed INT intents table in the application UI.
+ */
+public class IntAppTableMessageHandler extends UiMessageHandler {
+    private static final String INT_APP_INT_INTENT = "intAppIntIntent";
+    private static final String INT_APP_INT_INTENT_DATA_REQUEST = INT_APP_INT_INTENT + "DataRequest";
+    private static final String INT_APP_INT_INTENT_DATA_RESPONSE = INT_APP_INT_INTENT + "DataResponse";
+
+    private static final String INT_APP_DEL_INT_INTENT_REQ = "intAppDelIntIntentRequest";
+
+    private static final String NO_ROWS_MESSAGE = "No IntIntent found";
+
+    private static final String ID = "id";
+    private static final String SRC_ADDR = "srcAddr";
+    private static final String DST_ADDR = "dstAddr";
+    private static final String SRC_PORT = "srcPort";
+    private static final String DST_PORT = "dstPort";
+    private static final String PROTOCOL = "protocol";
+    private static final String METADATA = "metadata";
+
+    private static final String[] COLUMN_IDS = {ID, SRC_ADDR, DST_ADDR, SRC_PORT, DST_PORT, PROTOCOL, METADATA};
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    protected IntService intService;
+
+    @Override
+    protected Collection<RequestHandler> createRequestHandlers() {
+        return ImmutableSet.of(
+                new IntAppIntIntentRequestHandler(),
+                new IntAppDelIntIntentRequestHandler()
+        );
+    }
+
+    // handler for table requests
+    private final class IntAppIntIntentRequestHandler extends TableRequestHandler {
+
+        private IntAppIntIntentRequestHandler() {
+            super(INT_APP_INT_INTENT_DATA_REQUEST, INT_APP_INT_INTENT_DATA_RESPONSE, INT_APP_INT_INTENT);
+        }
+
+        @Override
+        protected String[] getColumnIds() {
+            return COLUMN_IDS;
+        }
+
+        @Override
+        protected String noRowsMessage(ObjectNode payload) {
+            return NO_ROWS_MESSAGE;
+        }
+
+        private Map<IntIntentId, IntIntent> getAllIntIntents() {
+            intService = get(IntService.class);
+            return intService.getIntIntents();
+        }
+
+        @Override
+        protected void populateTable(TableModel tm, ObjectNode payload) {
+            Map<IntIntentId, IntIntent> intentMap = getAllIntIntents();
+            intentMap.entrySet().forEach(entry ->
+                                                 populateRow(tm.addRow(), entry.getKey(), entry.getValue()));
+        }
+
+        private void populateRow(TableModel.Row row, IntIntentId intentId, IntIntent intent) {
+            IPCriterion ip4Src = (IPCriterion) intent.selector().getCriterion(Criterion.Type.IPV4_SRC);
+            IPCriterion ip4Dst = (IPCriterion) intent.selector().getCriterion(Criterion.Type.IPV4_DST);
+            TcpPortCriterion tcpSrcPort = (TcpPortCriterion) intent.selector().getCriterion(Criterion.Type.TCP_SRC);
+            TcpPortCriterion tcpDstPort = (TcpPortCriterion) intent.selector().getCriterion(Criterion.Type.TCP_DST);
+            UdpPortCriterion udpSrcPort = (UdpPortCriterion) intent.selector().getCriterion(Criterion.Type.UDP_SRC);
+            UdpPortCriterion udpDstPort = (UdpPortCriterion) intent.selector().getCriterion(Criterion.Type.UDP_DST);
+            Set<IntIntent.IntMetadataType> metadataTypes = intent.metadataTypes();
+            row.cell(ID, intentId.toString())
+                    .cell(SRC_ADDR, ip4Src == null ? "N/A" : ip4Src.ip().toString())
+                    .cell(DST_ADDR, ip4Dst == null ? "N/A" : ip4Dst.ip().toString());
+            if (tcpSrcPort != null || tcpDstPort != null) {
+                row.cell(PROTOCOL, "TCP")
+                        .cell(SRC_PORT, tcpSrcPort == null ? "N/A" : tcpSrcPort.tcpPort().toString())
+                        .cell(DST_PORT, tcpDstPort == null ? "N/A" : tcpDstPort.tcpPort().toString());
+            } else if (udpSrcPort != null || udpDstPort != null) {
+                row.cell(PROTOCOL, "UDP")
+                        .cell(SRC_PORT, udpSrcPort == null ? "N/A" : udpSrcPort.udpPort().toString())
+                        .cell(DST_PORT, udpDstPort == null ? "N/A" : udpDstPort.udpPort().toString());
+            } else {
+                row.cell(PROTOCOL, "N/A")
+                        .cell(SRC_PORT, "N/A")
+                        .cell(DST_PORT, "N/A");
+            }
+            String metaStr = "";
+            for (IntIntent.IntMetadataType metadataType : metadataTypes) {
+                metaStr += metadataType.toString();
+                metaStr += ", ";
+            }
+            row.cell(METADATA, metaStr);
+        }
+    }
+
+    private final class IntAppDelIntIntentRequestHandler extends RequestHandler {
+
+        private IntAppDelIntIntentRequestHandler() {
+            super(INT_APP_DEL_INT_INTENT_REQ);
+        }
+
+        @Override
+        public void process(ObjectNode payload) {
+            intService = get(IntService.class);
+            if (payload.get(ID) != null) {
+                intService.removeIntIntent(IntIntentId.valueOf(payload.get(ID).asLong()));
+            }
+        }
+    }
+}
diff --git a/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/ui/IntAppUiComponent.java b/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/ui/IntAppUiComponent.java
new file mode 100644
index 0000000..94c823e
--- /dev/null
+++ b/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/ui/IntAppUiComponent.java
@@ -0,0 +1,77 @@
+/*
+ * 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.
+ */
+package org.onosproject.inbandtelemetry.app.ui;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.ui.UiExtension;
+import org.onosproject.ui.UiExtensionService;
+import org.onosproject.ui.UiMessageHandlerFactory;
+import org.onosproject.ui.UiView;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+/**
+ * Skeletal ONOS UI Custom-View application component.
+ */
+@Component(immediate = true)
+public class IntAppUiComponent {
+
+    private static final String VIEW_ID = "intApp";
+    private static final String VIEW_TEXT = "In-band Telemetry Control";
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected UiExtensionService uiExtensionService;
+
+    // List of application views
+    private final List<UiView> uiViews = ImmutableList.of(
+            new UiView(UiView.Category.OTHER, VIEW_ID, VIEW_TEXT)
+    );
+
+    // Factory for UI message handlers
+    private final UiMessageHandlerFactory messageHandlerFactory =
+            () -> ImmutableList.of(
+                    new IntAppUiMessageHandler(),
+                    new IntAppTableMessageHandler()
+            );
+
+    // Application UI extension
+    protected UiExtension extension =
+            new UiExtension.Builder(getClass().getClassLoader(), uiViews)
+                    .resourcePath(VIEW_ID)
+                    .messageHandlerFactory(messageHandlerFactory)
+                    .build();
+
+    @Activate
+    protected void activate() {
+        uiExtensionService.register(extension);
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        uiExtensionService.unregister(extension);
+        log.info("Stopped");
+    }
+}
diff --git a/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/ui/IntAppUiMessageHandler.java b/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/ui/IntAppUiMessageHandler.java
new file mode 100644
index 0000000..5a81c60
--- /dev/null
+++ b/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/ui/IntAppUiMessageHandler.java
@@ -0,0 +1,202 @@
+/*
+ * 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.
+ */
+package org.onosproject.inbandtelemetry.app.ui;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.google.common.collect.ImmutableSet;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.Ip4Prefix;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.TpPort;
+import org.onosproject.inbandtelemetry.api.IntIntent;
+import org.onosproject.inbandtelemetry.api.IntIntentId;
+import org.onosproject.inbandtelemetry.api.IntService;
+import org.onosproject.inbandtelemetry.api.IntConfig;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.ui.RequestHandler;
+import org.onosproject.ui.UiMessageHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+
+public class IntAppUiMessageHandler extends UiMessageHandler {
+
+    private static final String INT_INTENT_ADD_REQUEST = "intIntentAddRequest";
+    private static final String INT_INTENT_DEL_REQUEST = "intIntentDelRequest";
+    private static final String INT_CONFIG_ADD_REQUEST = "intConfigAddRequest";
+//    private static final String INT_CONFIG_DEL_REQUEST = "intConfigDelRequest";
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private IntService intService;
+
+    @Override
+    protected Collection<RequestHandler> createRequestHandlers() {
+        return ImmutableSet.of(
+                new IntIntentAddRequestHandler(),
+                new IntIntentDelRequestHandler(),
+                new IntConfigAddRequestHandler()
+//                new intConfigDelRequestHandler()
+        );
+    }
+
+    private final class IntConfigAddRequestHandler extends RequestHandler {
+        private IntConfigAddRequestHandler() {
+            super(INT_CONFIG_ADD_REQUEST);
+        }
+
+        @Override
+        public void process(ObjectNode payload) {
+            log.info("intConfigAddRequest: {}", payload);
+
+            intService = get(IntService.class);
+            IntConfig.Builder builder = IntConfig.builder();
+
+            if (payload.get("collectorIp") != null) {
+                builder.withCollectorIp(IpAddress.valueOf(payload.get("collectorIp").asText()));
+            } else {
+                builder.withCollectorIp(IpAddress.valueOf("127.0.0.1"));
+            }
+
+            if (payload.get("collectorPort") != null) {
+                builder.withCollectorPort(TpPort.tpPort(
+                        payload.get("collectorPort").asInt()));
+            } else {
+                builder.withCollectorPort(TpPort.tpPort(1234));
+            }
+
+            builder.enabled(true)
+                    .withSinkIp(IpAddress.valueOf("10.192.19.180"))
+                    .withSinkMac(MacAddress.NONE)
+                    .withCollectorNextHopMac(MacAddress.BROADCAST);
+
+            intService.setConfig(builder.build());
+        }
+    }
+
+    private final class IntIntentDelRequestHandler extends RequestHandler {
+        private IntIntentDelRequestHandler() {
+            super(INT_INTENT_DEL_REQUEST);
+        }
+
+        @Override
+        public void process(ObjectNode payload) {
+            log.info("intIntentDelRequest: {}", payload);
+
+            intService = get(IntService.class);
+
+            if (payload.get("intentId") != null) {
+                intService.removeIntIntent(IntIntentId.valueOf(payload.get("intentId").asLong()));
+            }
+        }
+    }
+
+    private final class IntIntentAddRequestHandler extends RequestHandler {
+        private IntIntentAddRequestHandler() {
+            super(INT_INTENT_ADD_REQUEST);
+        }
+
+        @Override
+        public void process(ObjectNode payload) {
+            log.info("intIntentAddRequest: {}", payload);
+
+            intService = get(IntService.class);
+
+            TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
+            IntIntent.Builder builder = IntIntent.builder();
+
+            if (payload.get("ip4SrcPrefix") != null) {
+                sBuilder.matchIPSrc(parseIp4Prefix(payload.get("ip4SrcPrefix").asText()));
+            }
+
+            if (payload.get("ip4DstPrefix") != null) {
+                sBuilder.matchIPDst(parseIp4Prefix(payload.get("ip4DstPrefix").asText()));
+            }
+
+            if (payload.get("l4SrcPort") != null) {
+                if (payload.get("protocol") != null && payload.get("protocol").asText().equalsIgnoreCase("TCP")) {
+                    sBuilder.matchTcpSrc(TpPort.tpPort(payload.get("l4SrcPort").asInt()));
+                } else {
+                    sBuilder.matchUdpSrc(TpPort.tpPort(payload.get("l4SrcPort").asInt()));
+                }
+            }
+
+            if (payload.get("l4DstPort") != null) {
+                if (payload.get("protocol") != null && payload.get("protocol").asText().equalsIgnoreCase("TCP")) {
+                    sBuilder.matchTcpDst(TpPort.tpPort(payload.get("l4DstPort").asInt()));
+                } else {
+                    sBuilder.matchUdpDst(TpPort.tpPort(payload.get("l4DstPort").asInt()));
+                }
+            }
+
+            if (payload.get("metadata") != null) {
+                JsonNode meta = payload.get("metadata");
+                if (meta.isArray()) {
+                    for (final JsonNode json : meta) {
+                        switch (json.asText()) {
+                            case "SWITCH_ID":
+                                builder.withMetadataType(IntIntent.IntMetadataType.SWITCH_ID);
+                                break;
+                            case "PORT_ID":
+                                builder.withMetadataType(IntIntent.IntMetadataType.L1_PORT_ID);
+                                break;
+                            case "HOP_LATENCY":
+                                builder.withMetadataType(IntIntent.IntMetadataType.HOP_LATENCY);
+                                break;
+                            case "QUEUE_OCCUPANCY":
+                                builder.withMetadataType(IntIntent.IntMetadataType.QUEUE_OCCUPANCY);
+                                break;
+                            case "INGRESS_TIMESTAMP":
+                                builder.withMetadataType(IntIntent.IntMetadataType.INGRESS_TIMESTAMP);
+                                break;
+                            case "EGRESS_TIMESTAMP":
+                                builder.withMetadataType(IntIntent.IntMetadataType.EGRESS_TIMESTAMP);
+                                break;
+//                            case "QUEUE_CONGESTION":
+//                                builder.withMetadataType(IntIntent.IntMetadataType.QUEUE_CONGESTION);
+//                                break;
+                            case "EGRESS_TX_UTIL":
+                                builder.withMetadataType(IntIntent.IntMetadataType.EGRESS_TX_UTIL);
+                                break;
+                            default:
+                                break;
+                        }
+                    }
+                }
+            }
+
+            builder.withSelector(sBuilder.build())
+                    .withHeaderType(IntIntent.IntHeaderType.HOP_BY_HOP)
+                    .withReportType(IntIntent.IntReportType.TRACKED_FLOW)
+                    .withTelemetryMode(IntIntent.TelemetryMode.INBAND_TELEMETRY);
+            intService.installIntIntent(builder.build());
+        }
+
+        private Ip4Prefix parseIp4Prefix(String prefixString) {
+            if (prefixString == null) {
+                return null;
+            }
+            String[] splitString = prefixString.split("/");
+            Ip4Address ip4Address = Ip4Address.valueOf(splitString[0]);
+            int mask = splitString.length > 1 ? Integer.parseInt(splitString[1]) : 32;
+            return Ip4Prefix.valueOf(ip4Address, mask);
+        }
+    }
+}
diff --git a/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/ui/package-info.java b/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/ui/package-info.java
new file mode 100644
index 0000000..d3d4849
--- /dev/null
+++ b/apps/inbandtelemetry/app/src/main/java/org/onosproject/inbandtelemetry/app/ui/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Web UI component for IntService sample application.
+ */
+package org.onosproject.inbandtelemetry.app.ui;