Implement default kubevirt pipeline, support provider network

Change-Id: I82eaaed05668067090703ec3c10ae6151c2ea815
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java
new file mode 100644
index 0000000..ca0eecd
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.api;
+
+import org.onlab.packet.MacAddress;
+
+/**
+ * Provides constants used in KubevirtNetworking.
+ */
+public final class Constants {
+
+    private Constants() {
+    }
+
+    public static final String KUBEVIRT_NETWORKING_APP_ID =
+            "org.onosproject.kubevirtnetworking";
+    public static final String DEFAULT_GATEWAY_MAC_STR = "fe:00:00:00:00:02";
+    public static final MacAddress DEFAULT_GATEWAY_MAC =
+                        MacAddress.valueOf(DEFAULT_GATEWAY_MAC_STR);
+
+    // flow table index
+    public static final int STAT_INBOUND_TABLE = 0;
+    public static final int VTAP_INBOUND_TABLE = 1;
+    public static final int STAT_FLAT_OUTBOUND_TABLE = 10;
+    public static final int DHCP_TABLE = 5;
+    public static final int VTAG_TABLE = 30;
+    public static final int PRE_FLAT_TABLE = 31;
+    public static final int FLAT_TABLE = 32;
+    public static final int ARP_TABLE = 35;
+    public static final int ACL_EGRESS_TABLE = 40;
+    public static final int ACL_INGRESS_TABLE = 44;
+    public static final int CT_TABLE = 45;
+    public static final int ACL_RECIRC_TABLE = 43;
+    public static final int JUMP_TABLE = 50;
+    public static final int ROUTING_TABLE = 60;
+    public static final int STAT_OUTBOUND_TABLE = 70;
+    public static final int VTAP_OUTBOUND_TABLE = 71;
+    public static final int FORWARDING_TABLE = 80;
+    public static final int ERROR_TABLE = 100;
+
+    // flow rule priority
+    public static final int PRIORITY_SWITCHING_RULE = 30000;
+}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtFlowRuleService.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtFlowRuleService.java
new file mode 100644
index 0000000..e9196c8
--- /dev/null
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtFlowRuleService.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.api;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+
+/**
+ * Service for setting flow rules.
+ */
+public interface KubevirtFlowRuleService {
+
+    /**
+     * Sets the flow rule.
+     *
+     * @param appId application ID
+     * @param deviceId  device ID
+     * @param selector matches of the flow rule
+     * @param treatment actions of the flow rule
+     * @param priority priority of the flow rule
+     * @param tableType table number to put the flow rule
+     * @param install add the rule if true, remove it otherwise
+     */
+    void setRule(ApplicationId appId,
+                 DeviceId deviceId,
+                 TrafficSelector selector,
+                 TrafficTreatment treatment,
+                 int priority,
+                 int tableType,
+                 boolean install);
+
+    /**
+     * Install table miss entry (drop rule) in the table.
+     *
+     * @param deviceId device ID
+     * @param table table number
+     */
+    void setUpTableMissEntry(DeviceId deviceId, int table);
+
+    /**
+     * Install a flor rule for transition from table A to table B.
+     *
+     * @param deviceId device Id
+     * @param fromTable table number of table A
+     * @param toTable table number of table B
+     */
+    void connectTables(DeviceId deviceId, int fromTable, int toTable);
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtFlowRuleManager.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtFlowRuleManager.java
new file mode 100644
index 0000000..fa5aeda
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtFlowRuleManager.java
@@ -0,0 +1,385 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.impl;
+
+import org.onlab.util.Tools;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.cfg.ConfigProperty;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.LeadershipService;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnetworking.api.KubevirtFlowRuleService;
+import org.onosproject.kubevirtnode.api.KubevirtNode;
+import org.onosproject.kubevirtnode.api.KubevirtNodeEvent;
+import org.onosproject.kubevirtnode.api.KubevirtNodeListener;
+import org.onosproject.kubevirtnode.api.KubevirtNodeService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleOperations;
+import org.onosproject.net.flow.FlowRuleOperationsContext;
+import org.onosproject.net.flow.FlowRuleService;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Modified;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.slf4j.Logger;
+
+import java.util.Dictionary;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import static org.onlab.util.Tools.groupedThreads;
+import static org.onosproject.kubevirtnetworking.api.Constants.ACL_EGRESS_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.ACL_INGRESS_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.ARP_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.DEFAULT_GATEWAY_MAC;
+import static org.onosproject.kubevirtnetworking.api.Constants.DHCP_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.FLAT_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.FORWARDING_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.JUMP_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.KUBEVIRT_NETWORKING_APP_ID;
+import static org.onosproject.kubevirtnetworking.api.Constants.PRE_FLAT_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.ROUTING_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.STAT_INBOUND_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.STAT_OUTBOUND_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.VTAG_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.VTAP_INBOUND_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.VTAP_OUTBOUND_TABLE;
+import static org.onosproject.kubevirtnetworking.impl.OsgiPropertyConstants.PROVIDER_NETWORK_ONLY;
+import static org.onosproject.kubevirtnetworking.impl.OsgiPropertyConstants.PROVIDER_NETWORK_ONLY_DEFAULT;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.getPropertyValueAsBoolean;
+import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.WORKER;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Sets flow rules directly using FlowRuleService.
+ */
+@Component(
+        immediate = true,
+        service = KubevirtFlowRuleService.class,
+        property = {
+                PROVIDER_NETWORK_ONLY + ":Boolean=" + PROVIDER_NETWORK_ONLY_DEFAULT
+        }
+)
+public class KubevirtFlowRuleManager implements KubevirtFlowRuleService {
+
+    private final Logger log = getLogger(getClass());
+
+    private static final int DROP_PRIORITY = 0;
+    private static final int LOW_PRIORITY = 10000;
+    private static final int MID_PRIORITY = 20000;
+    private static final int HIGH_PRIORITY = 30000;
+    private static final int TIMEOUT_SNAT_RULE = 60;
+
+    /** Use provider network only. */
+    private boolean providerNetworkOnly = PROVIDER_NETWORK_ONLY_DEFAULT;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected FlowRuleService flowRuleService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected ClusterService clusterService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected LeadershipService leadershipService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected DeviceService deviceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected ComponentConfigService configService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected KubevirtNodeService nodeService;
+
+    private final ExecutorService deviceEventExecutor = Executors.newSingleThreadExecutor(
+                    groupedThreads(getClass().getSimpleName(), "device-event"));
+    private final KubevirtNodeListener internalNodeListener = new InternalKubevirtNodeListener();
+
+    private ApplicationId appId;
+    private NodeId localNodeId;
+
+    @Activate
+    protected void activate() {
+        appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID);
+        coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID);
+        configService.registerProperties(getClass());
+        nodeService.addListener(internalNodeListener);
+        localNodeId = clusterService.getLocalNode().id();
+        leadershipService.runForLeadership(appId.name());
+        nodeService.completeNodes(WORKER)
+                .forEach(node -> initializePipeline(node.intgBridge()));
+
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        nodeService.removeListener(internalNodeListener);
+        configService.unregisterProperties(getClass(), false);
+        leadershipService.withdraw(appId.name());
+        deviceEventExecutor.shutdown();
+
+        log.info("Stopped");
+    }
+
+    @Modified
+    protected void modified(ComponentContext context) {
+        Dictionary<?, ?> properties = context.getProperties();
+        Boolean flag;
+
+        flag = Tools.isPropertyEnabled(properties, PROVIDER_NETWORK_ONLY);
+        if (flag == null) {
+            log.info("providerNetworkOnly is not configured, " +
+                    "using current value of {}", providerNetworkOnly);
+        } else {
+            providerNetworkOnly = flag;
+            log.info("Configured. providerNetworkOnly is {}",
+                    providerNetworkOnly ? "enabled" : "disabled");
+        }
+    }
+
+    @Override
+    public void setRule(ApplicationId appId, DeviceId deviceId,
+                        TrafficSelector selector, TrafficTreatment treatment,
+                        int priority, int tableType, boolean install) {
+
+        FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder()
+                .forDevice(deviceId)
+                .withSelector(selector)
+                .withTreatment(treatment)
+                .withPriority(priority)
+                .fromApp(appId)
+                .forTable(tableType)
+                .makePermanent();
+
+        applyRule(flowRuleBuilder.build(), install);
+    }
+
+    @Override
+    public void setUpTableMissEntry(DeviceId deviceId, int table) {
+        TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+        TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
+
+        treatment.drop();
+
+        FlowRule flowRule = DefaultFlowRule.builder()
+                .forDevice(deviceId)
+                .withSelector(selector.build())
+                .withTreatment(treatment.build())
+                .withPriority(DROP_PRIORITY)
+                .fromApp(appId)
+                .makePermanent()
+                .forTable(table)
+                .build();
+
+        applyRule(flowRule, true);
+    }
+
+    @Override
+    public void connectTables(DeviceId deviceId, int fromTable, int toTable) {
+        TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+        TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
+
+        treatment.transition(toTable);
+
+        FlowRule flowRule = DefaultFlowRule.builder()
+                .forDevice(deviceId)
+                .withSelector(selector.build())
+                .withTreatment(treatment.build())
+                .withPriority(DROP_PRIORITY)
+                .fromApp(appId)
+                .makePermanent()
+                .forTable(fromTable)
+                .build();
+
+        applyRule(flowRule, true);
+    }
+
+    private void applyRule(FlowRule flowRule, boolean install) {
+        FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
+
+        flowOpsBuilder = install ? flowOpsBuilder.add(flowRule) : flowOpsBuilder.remove(flowRule);
+
+        flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
+            @Override
+            public void onSuccess(FlowRuleOperations ops) {
+                log.debug("Provisioned vni or forwarding table");
+            }
+
+            @Override
+            public void onError(FlowRuleOperations ops) {
+                log.debug("Failed to provision vni or forwarding table");
+            }
+        }));
+    }
+
+    protected void initializePipeline(DeviceId deviceId) {
+        // for inbound table transition
+        connectTables(deviceId, STAT_INBOUND_TABLE, VTAP_INBOUND_TABLE);
+        connectTables(deviceId, VTAP_INBOUND_TABLE, DHCP_TABLE);
+
+        // for DHCP and vTag table transition
+        connectTables(deviceId, DHCP_TABLE, VTAG_TABLE);
+
+        if (getProviderNetworkOnlyFlag()) {
+            // we directly transit from vTag table to PRE_FLAT table for provider
+            // network only mode, because there is no need to differentiate ARP
+            // and IP packets on this mode
+            connectTables(deviceId, VTAG_TABLE, PRE_FLAT_TABLE);
+        } else {
+            // for vTag and ARP table transition
+            connectTables(deviceId, VTAG_TABLE, ARP_TABLE);
+        }
+
+        // for PRE_FLAT and FLAT table transition
+        connectTables(deviceId, PRE_FLAT_TABLE, FLAT_TABLE);
+
+        // for ARP and ACL table transition
+        connectTables(deviceId, ARP_TABLE, ACL_INGRESS_TABLE);
+
+        // for ACL and JUMP table transition
+        connectTables(deviceId, ACL_EGRESS_TABLE, JUMP_TABLE);
+
+        // for outbound table transition
+        connectTables(deviceId, STAT_OUTBOUND_TABLE, VTAP_OUTBOUND_TABLE);
+        connectTables(deviceId, VTAP_OUTBOUND_TABLE, FORWARDING_TABLE);
+
+        // for JUMP table transition
+        // we need JUMP table for bypassing routing table which contains large
+        // amount of flow rules which might cause performance degradation during
+        // table lookup
+        setupJumpTable(deviceId);
+
+        // for setting up default FLAT table behavior which is NORMAL
+        setupFlatTable(deviceId);
+    }
+
+    private void setupJumpTable(DeviceId deviceId) {
+        TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+        TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
+
+        selector.matchEthDst(DEFAULT_GATEWAY_MAC);
+        treatment.transition(ROUTING_TABLE);
+
+        FlowRule flowRule = DefaultFlowRule.builder()
+                .forDevice(deviceId)
+                .withSelector(selector.build())
+                .withTreatment(treatment.build())
+                .withPriority(HIGH_PRIORITY)
+                .fromApp(appId)
+                .makePermanent()
+                .forTable(JUMP_TABLE)
+                .build();
+
+        applyRule(flowRule, true);
+
+        selector = DefaultTrafficSelector.builder();
+        treatment = DefaultTrafficTreatment.builder();
+
+        treatment.transition(STAT_OUTBOUND_TABLE);
+
+        flowRule = DefaultFlowRule.builder()
+                .forDevice(deviceId)
+                .withSelector(selector.build())
+                .withTreatment(treatment.build())
+                .withPriority(DROP_PRIORITY)
+                .fromApp(appId)
+                .makePermanent()
+                .forTable(JUMP_TABLE)
+                .build();
+
+        applyRule(flowRule, true);
+    }
+
+    private void setupFlatTable(DeviceId deviceId) {
+        TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+        TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
+                .setOutput(PortNumber.NORMAL);
+
+        FlowRule flowRule = DefaultFlowRule.builder()
+                .forDevice(deviceId)
+                .withSelector(selector.build())
+                .withTreatment(treatment.build())
+                .withPriority(LOW_PRIORITY)
+                .fromApp(appId)
+                .makePermanent()
+                .forTable(FLAT_TABLE)
+                .build();
+
+        applyRule(flowRule, true);
+    }
+
+    private boolean getProviderNetworkOnlyFlag() {
+        Set<ConfigProperty> properties =
+                configService.getProperties(getClass().getName());
+        return getPropertyValueAsBoolean(properties, PROVIDER_NETWORK_ONLY);
+    }
+
+    private class InternalKubevirtNodeListener implements KubevirtNodeListener {
+
+        @Override
+        public boolean isRelevant(KubevirtNodeEvent event) {
+            return event.subject().type().equals(WORKER);
+        }
+
+        private boolean isRelevantHelper() {
+            return Objects.equals(localNodeId, leadershipService.getLeader(appId.name()));
+        }
+
+        @Override
+        public void event(KubevirtNodeEvent event) {
+            KubevirtNode node = event.subject();
+
+            switch (event.type()) {
+                case KUBEVIRT_NODE_COMPLETE:
+                    deviceEventExecutor.execute(() -> {
+                        log.info("COMPLETE node {} is detected", node.hostname());
+
+                        if (!isRelevantHelper()) {
+                            return;
+                        }
+
+                        initializePipeline(node.intgBridge());
+                    });
+                    break;
+                case KUBEVIRT_NODE_CREATED:
+                case KUBEVIRT_NODE_UPDATED:
+                case KUBEVIRT_NODE_REMOVED:
+                default:
+                    // do nothing
+                    break;
+            }
+        }
+    }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtSwitchingPhysicalHandler.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtSwitchingPhysicalHandler.java
new file mode 100644
index 0000000..96a31da
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/KubevirtSwitchingPhysicalHandler.java
@@ -0,0 +1,182 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.impl;
+
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.LeadershipService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnetworking.api.KubevirtFlowRuleService;
+import org.onosproject.kubevirtnode.api.KubevirtNode;
+import org.onosproject.kubevirtnode.api.KubevirtNodeService;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceListener;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.DriverService;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.slf4j.Logger;
+
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.stream.Collectors;
+
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.onosproject.kubevirtnetworking.api.Constants.KUBEVIRT_NETWORKING_APP_ID;
+import static org.onosproject.kubevirtnetworking.api.Constants.PRE_FLAT_TABLE;
+import static org.onosproject.kubevirtnetworking.api.Constants.PRIORITY_SWITCHING_RULE;
+import static org.onosproject.kubevirtnetworking.api.Constants.VTAG_TABLE;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.structurePortName;
+import static org.onosproject.kubevirtnode.api.Constants.INTEGRATION_TO_PHYSICAL_PREFIX;
+import static org.onosproject.net.AnnotationKeys.PORT_NAME;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Populates switching flow rules on OVS for the physical interfaces.
+ */
+@Component(immediate = true)
+public class KubevirtSwitchingPhysicalHandler {
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected CoreService coreService;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected MastershipService mastershipService;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected DeviceService deviceService;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected DriverService driverService;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected ClusterService clusterService;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected LeadershipService leadershipService;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected KubevirtFlowRuleService flowRuleService;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected KubevirtNodeService nodeService;
+
+    private final ExecutorService eventExecutor = newSingleThreadExecutor(
+            groupedThreads(this.getClass().getSimpleName(), "event-handler"));
+    private final InternalDeviceListener internalDeviceListener = new InternalDeviceListener();
+    private ApplicationId appId;
+
+    @Activate
+    protected void activate() {
+        appId = coreService.registerApplication(KUBEVIRT_NETWORKING_APP_ID);
+        deviceService.addListener(internalDeviceListener);
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        eventExecutor.shutdown();
+        deviceService.removeListener(internalDeviceListener);
+        log.info("Stopped");
+    }
+
+    private boolean containsPhyPatchPort(KubevirtNode node, Port port) {
+        Set<String> intPatchPorts = node.phyIntfs().stream()
+                .map(pi -> structurePortName(INTEGRATION_TO_PHYSICAL_PREFIX
+                        + pi.network())).collect(Collectors.toSet());
+        String portName = port.annotations().value(PORT_NAME);
+        return intPatchPorts.contains(portName);
+    }
+
+    private void setFlatJumpRuleForPatchPort(DeviceId deviceId,
+                                             PortNumber portNumber,
+                                             boolean install) {
+        TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
+                .matchInPort(portNumber);
+
+        TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
+                .transition(PRE_FLAT_TABLE);
+
+        flowRuleService.setRule(
+                appId,
+                deviceId,
+                selector.build(),
+                treatment.build(),
+                PRIORITY_SWITCHING_RULE,
+                VTAG_TABLE,
+                install);
+    }
+
+    private class InternalDeviceListener implements DeviceListener {
+
+        @Override
+        public boolean isRelevant(DeviceEvent event) {
+            Port port = event.port();
+            if (port == null) {
+                return false;
+            }
+
+            KubevirtNode node = nodeService.node(event.subject().id());
+            if (node == null) {
+                return false;
+            }
+
+            return containsPhyPatchPort(node, port);
+        }
+
+        private boolean isRelevantHelper(DeviceEvent event) {
+            return mastershipService.isLocalMaster(event.subject().id());
+        }
+
+        @Override
+        public void event(DeviceEvent event) {
+            log.info("Device event occurred with type {}", event.type());
+
+            switch (event.type()) {
+                case PORT_ADDED:
+                case PORT_UPDATED:
+                    eventExecutor.execute(() -> processPortAddition(event));
+                    break;
+                case PORT_REMOVED:
+                    eventExecutor.execute(() -> processPortRemoval(event));
+                    break;
+                default:
+                    break;
+            }
+        }
+
+        private void processPortAddition(DeviceEvent event) {
+            if (!isRelevantHelper(event)) {
+                return;
+            }
+            setFlatJumpRuleForPatchPort(event.subject().id(),
+                    event.port().number(), true);
+        }
+        private void processPortRemoval(DeviceEvent event) {
+            if (!isRelevantHelper(event)) {
+                return;
+            }
+            setFlatJumpRuleForPatchPort(event.subject().id(),
+                    event.port().number(), false);
+        }
+    }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/OsgiPropertyConstants.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/OsgiPropertyConstants.java
new file mode 100644
index 0000000..68b15ea
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/impl/OsgiPropertyConstants.java
@@ -0,0 +1,31 @@
+/*
+ * 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.kubevirtnetworking.impl;
+
+/**
+ * Name/Value constants for properties.
+ */
+public final class OsgiPropertyConstants {
+    private OsgiPropertyConstants() {
+    }
+
+    static final String PROVIDER_NETWORK_ONLY = "providerNetworkOnly";
+    static final boolean PROVIDER_NETWORK_ONLY_DEFAULT = true;
+
+    static final String DHCP_SERVER_MAC = "dhcpServerMac";
+    static final String DHCP_SERVER_MAC_DEFAULT = "fe:00:00:00:00:02";
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java
new file mode 100644
index 0000000..967c23b
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.util;
+
+import org.apache.commons.lang.StringUtils;
+import org.onosproject.cfg.ConfigProperty;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * An utility that used in KubeVirt networking app.
+ */
+public final class KubevirtNetworkingUtil {
+
+    private static final Logger log = LoggerFactory.getLogger(KubevirtNetworkingUtil.class);
+
+    private static final int PORT_NAME_MAX_LENGTH = 15;
+
+    /**
+     * Prevents object installation from external.
+     */
+    private KubevirtNetworkingUtil() {
+    }
+
+    /**
+     * Obtains the boolean property value with specified property key name.
+     *
+     * @param properties    a collection of properties
+     * @param name          key name
+     * @return mapping value
+     */
+    public static boolean getPropertyValueAsBoolean(Set<ConfigProperty> properties,
+                                                    String name) {
+        Optional<ConfigProperty> property =
+                properties.stream().filter(p -> p.name().equals(name)).findFirst();
+
+        return property.map(ConfigProperty::asBoolean).orElse(false);
+    }
+
+    /**
+     * Re-structures the OVS port name.
+     * The length of OVS port name should be not large than 15.
+     *
+     * @param portName  original port name
+     * @return re-structured OVS port name
+     */
+    public static String structurePortName(String portName) {
+
+        // The size of OVS port name should not be larger than 15
+        if (portName.length() > PORT_NAME_MAX_LENGTH) {
+            return StringUtils.substring(portName, 0, PORT_NAME_MAX_LENGTH);
+        }
+
+        return portName;
+    }
+}