ONOS-6563 ONOS-6866 Draft porting of old ECMP P4 demo application

Also, pipeconf implementation for ecmp.p4

Change-Id: Ia8973b42ae386482e341c2b201ad887ad471091e
diff --git a/apps/pi-demo/common/BUCK b/apps/pi-demo/common/BUCK
new file mode 100644
index 0000000..132086c
--- /dev/null
+++ b/apps/pi-demo/common/BUCK
@@ -0,0 +1,7 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+]
+
+osgi_jar (
+    deps = COMPILE_DEPS,
+)
\ No newline at end of file
diff --git a/apps/pi-demo/common/src/main/java/org/onosproject/pi/demo/app/common/AbstractUpgradableFabricApp.java b/apps/pi-demo/common/src/main/java/org/onosproject/pi/demo/app/common/AbstractUpgradableFabricApp.java
new file mode 100644
index 0000000..4221eea
--- /dev/null
+++ b/apps/pi-demo/common/src/main/java/org/onosproject/pi/demo/app/common/AbstractUpgradableFabricApp.java
@@ -0,0 +1,575 @@
+/*
+ * Copyright 2017-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.pi.demo.app.common;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+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.app.ApplicationAdminService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Host;
+import org.onosproject.net.Port;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceListener;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleOperations;
+import org.onosproject.net.flow.FlowRuleService;
+import org.onosproject.net.host.HostEvent;
+import org.onosproject.net.host.HostListener;
+import org.onosproject.net.host.HostService;
+import org.onosproject.net.pi.model.PiPipeconf;
+import org.onosproject.net.pi.model.PiPipelineInterpreter;
+import org.onosproject.net.pi.runtime.PiPipeconfService;
+import org.onosproject.net.pi.runtime.PiTableId;
+import org.onosproject.net.topology.Topology;
+import org.onosproject.net.topology.TopologyEvent;
+import org.onosproject.net.topology.TopologyGraph;
+import org.onosproject.net.topology.TopologyListener;
+import org.onosproject.net.topology.TopologyService;
+import org.onosproject.net.topology.TopologyVertex;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static java.util.stream.Collectors.toSet;
+import static java.util.stream.Stream.concat;
+import static org.onlab.util.Tools.groupedThreads;
+import static org.onosproject.net.device.DeviceEvent.Type.*;
+import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Abstract implementation of an app providing fabric connectivity for a 2-stage Clos topology of P4Runtime devices.
+ */
+@Component(immediate = true)
+public abstract class AbstractUpgradableFabricApp {
+
+    private static final Map<String, AbstractUpgradableFabricApp> APP_HANDLES = Maps.newConcurrentMap();
+
+    private static final int NUM_LEAFS = 3;
+    private static final int NUM_SPINES = 3;
+    private static final int FLOW_PRIORITY = 100;
+
+    private static final int CLEANUP_SLEEP = 2000;
+
+    protected final Logger log = getLogger(getClass());
+
+    private final TopologyListener topologyListener = new InternalTopologyListener();
+    private final DeviceListener deviceListener = new InternalDeviceListener();
+    private final HostListener hostListener = new InternalHostListener();
+
+    private final ExecutorService executorService = Executors
+            .newFixedThreadPool(8, groupedThreads("onos/pi-demo-app", "pi-app-task", log));
+
+    private final String appName;
+    private final String configurationName;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected TopologyService topologyService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    private HostService hostService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    private FlowRuleService flowRuleService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    private ApplicationAdminService appService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    private CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    private PiPipeconfService piPipeconfService;
+
+    private boolean appActive = false;
+    private boolean appFreezed = false;
+
+    private boolean otherAppFound = false;
+    private AbstractUpgradableFabricApp otherApp;
+
+    private boolean flowRuleGenerated = false;
+    private ApplicationId appId;
+
+    private PiPipeconf pipeconf;
+
+    private Set<DeviceId> leafSwitches;
+    private Set<DeviceId> spineSwitches;
+
+    private Map<DeviceId, List<FlowRule>> deviceFlowRules;
+    private Map<DeviceId, Boolean> pipeconfFlags;
+    private Map<DeviceId, Boolean> ruleFlags;
+
+    private ConcurrentMap<DeviceId, Lock> deviceLocks = Maps.newConcurrentMap();
+
+    /**
+     * Creates a new PI fabric app.
+     *
+     * @param appName           app name
+     * @param configurationName a common name for the P4 program / PI configuration used by this app
+     * @param pipeconf a P4Runtime device context to be used on devices
+     */
+    protected AbstractUpgradableFabricApp(String appName, String configurationName, PiPipeconf pipeconf) {
+        this.appName = checkNotNull(appName);
+        this.configurationName = checkNotNull(configurationName);
+        this.pipeconf = checkNotNull(pipeconf);
+    }
+
+    @Activate
+    public void activate() {
+        log.info("Starting...");
+
+        appActive = true;
+        appFreezed = false;
+
+        if (APP_HANDLES.size() > 0) {
+            if (APP_HANDLES.size() > 1) {
+                throw new IllegalStateException("Found more than 1 active app handles");
+            }
+            otherAppFound = true;
+            otherApp = APP_HANDLES.values().iterator().next();
+            log.info("Found other fabric app active, signaling to freeze to {}...", otherApp.appName);
+            otherApp.setAppFreezed(true);
+        }
+
+        APP_HANDLES.put(appName, this);
+
+        appId = coreService.registerApplication(appName);
+
+        topologyService.addListener(topologyListener);
+        deviceService.addListener(deviceListener);
+        hostService.addListener(hostListener);
+        piPipeconfService.register(pipeconf);
+
+        init();
+
+        log.info("STARTED", appId.id());
+    }
+
+    @Deactivate
+    public void deactivate() {
+        log.info("Stopping...");
+        try {
+            executorService.shutdown();
+            executorService.awaitTermination(5, TimeUnit.SECONDS);
+        } catch (InterruptedException e) {
+            List<Runnable> runningTasks = executorService.shutdownNow();
+            log.warn("Unable to stop the following tasks: {}", runningTasks);
+        }
+        deviceService.removeListener(deviceListener);
+        topologyService.removeListener(topologyListener);
+        hostService.removeListener(hostListener);
+        flowRuleService.removeFlowRulesById(appId);
+        piPipeconfService.remove(pipeconf.id());
+
+        appActive = false;
+        APP_HANDLES.remove(appName);
+
+        log.info("STOPPED");
+    }
+
+    private void init() {
+
+        // Reset any previous state
+        synchronized (this) {
+            flowRuleGenerated = Boolean.FALSE;
+            leafSwitches = Sets.newHashSet();
+            spineSwitches = Sets.newHashSet();
+            deviceFlowRules = Maps.newConcurrentMap();
+            ruleFlags = Maps.newConcurrentMap();
+            pipeconfFlags = Maps.newConcurrentMap();
+        }
+
+        // Start flow rules generator...
+        spawnTask(() -> generateFlowRules(topologyService.currentTopology(), Sets.newHashSet(hostService.getHosts())));
+    }
+
+    private void setAppFreezed(boolean appFreezed) {
+        this.appFreezed = appFreezed;
+        if (appFreezed) {
+            log.info("Freezing...");
+        } else {
+            log.info("Unfreezing...!");
+        }
+    }
+
+    /**
+     * Perform device initialization. Returns true if the operation was successful, false otherwise.
+     *
+     * @param deviceId a device id
+     * @return a boolean value
+     */
+    public abstract boolean initDevice(DeviceId deviceId);
+
+    /**
+     * Generates a list of flow rules for the given leaf switch, source host, destination hosts, spine switches and
+     * topology.
+     *
+     * @param leaf     a leaf device id
+     * @param srcHost  a source host
+     * @param dstHosts a collection of destination hosts
+     * @param spines   a collection of spine device IDs
+     * @param topology a topology
+     * @return a list of flow rules
+     * @throws FlowRuleGeneratorException if flow rules cannot be generated
+     */
+    public abstract List<FlowRule> generateLeafRules(DeviceId leaf, Host srcHost, Collection<Host> dstHosts,
+                                                     Collection<DeviceId> spines, Topology topology)
+            throws FlowRuleGeneratorException;
+
+    /**
+     * Generates a list of flow rules for the given spine switch, destination hosts and topology.
+     *
+     * @param deviceId a spine device id
+     * @param dstHosts a collection of destination hosts
+     * @param topology a topology
+     * @return a list of flow rules
+     * @throws FlowRuleGeneratorException if flow rules cannot be generated
+     */
+    public abstract List<FlowRule> generateSpineRules(DeviceId deviceId, Collection<Host> dstHosts, Topology topology)
+            throws FlowRuleGeneratorException;
+
+    private void deployAllDevices() {
+        if (otherAppFound && otherApp.appActive) {
+            log.info("Deactivating other app...");
+            appService.deactivate(otherApp.appId);
+            try {
+                Thread.sleep(CLEANUP_SLEEP);
+            } catch (InterruptedException e) {
+                log.warn("Cleanup sleep interrupted!");
+                Thread.interrupted();
+            }
+        }
+
+        Stream.concat(leafSwitches.stream(), spineSwitches.stream())
+                .map(deviceService::getDevice)
+                .forEach(device -> spawnTask(() -> deployDevice(device)));
+    }
+
+    /**
+     * Executes a device deploy.
+     *
+     * @param device a device
+     */
+    public void deployDevice(Device device) {
+
+        DeviceId deviceId = device.id();
+
+        // Synchronize executions over the same device.
+        Lock lock = deviceLocks.computeIfAbsent(deviceId, k -> new ReentrantLock());
+        lock.lock();
+
+        try {
+            // Set pipeconfflag if not already done.
+            if (!pipeconfFlags.getOrDefault(deviceId, false)) {
+                if (pipeconf.id().equals(piPipeconfService.ofDevice(deviceId))) {
+                    pipeconfFlags.put(device.id(), true);
+                } else {
+                    log.warn("No pipeconf can be associated to the device {}.", deviceId);
+                }
+            }
+
+            // Initialize device.
+            if (!initDevice(deviceId)) {
+                log.warn("Failed to initialize device {}", deviceId);
+            }
+
+            // Install rules.
+            if (!ruleFlags.getOrDefault(deviceId, false)) {
+                List<FlowRule> rules = deviceFlowRules.getOrDefault(deviceId, Collections.emptyList());
+                if (rules.size() > 0) {
+                    log.info("Installing rules for {}...", deviceId);
+                    installFlowRules(rules);
+                    ruleFlags.put(deviceId, true);
+                }
+            }
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    private void spawnTask(Runnable task) {
+        executorService.execute(task);
+    }
+
+
+    private void installFlowRules(Collection<FlowRule> rules) {
+        FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder();
+        rules.forEach(opsBuilder::add);
+        flowRuleService.apply(opsBuilder.build());
+    }
+
+    private void removeFlowRules(Collection<FlowRule> rules) {
+        FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder();
+        rules.forEach(opsBuilder::remove);
+        flowRuleService.apply(opsBuilder.build());
+    }
+
+    /**
+     * Generates the flow rules to provide host-to-host connectivity for the given topology and hosts.
+     *
+     * @param topo  a topology
+     * @param hosts a collection of hosts
+     */
+    private synchronized void generateFlowRules(Topology topo, Collection<Host> hosts) {
+
+        if (flowRuleGenerated) {
+            log.debug("Flow rules have been already generated, aborting...");
+            return;
+        }
+
+        log.debug("Starting flow rules generator...");
+
+        TopologyGraph graph = topologyService.getGraph(topo);
+        Set<DeviceId> spines = Sets.newHashSet();
+        Set<DeviceId> leafs = Sets.newHashSet();
+        graph.getVertexes().stream()
+                .map(TopologyVertex::deviceId)
+                .forEach(did -> (isSpine(did, topo) ? spines : leafs).add(did));
+
+        if (spines.size() != NUM_SPINES || leafs.size() != NUM_LEAFS) {
+            log.info("Invalid leaf/spine switches count, aborting... > leafCount={}, spineCount={}",
+                     spines.size(), leafs.size());
+            return;
+        }
+
+        for (DeviceId did : spines) {
+            int portCount = deviceService.getPorts(did).size();
+            // Expected port count: num leafs + 1 redundant leaf link
+            if (portCount != (NUM_LEAFS + 1)) {
+                log.info("Invalid port count for spine, aborting... > deviceId={}, portCount={}", did, portCount);
+                return;
+            }
+        }
+        for (DeviceId did : leafs) {
+            int portCount = deviceService.getPorts(did).size();
+            // Expected port count: num spines + host port + 1 redundant spine link
+            if (portCount != (NUM_SPINES + 2)) {
+                log.info("Invalid port count for leaf, aborting... > deviceId={}, portCount={}", did, portCount);
+                return;
+            }
+        }
+
+        // Check hosts, number and exactly one per leaf
+        Map<DeviceId, Host> hostMap = Maps.newHashMap();
+        hosts.forEach(h -> hostMap.put(h.location().deviceId(), h));
+        if (hosts.size() != NUM_LEAFS || !leafs.equals(hostMap.keySet())) {
+            log.info("Wrong host configuration, aborting... > hostCount={}, hostMapz={}", hosts.size(), hostMap);
+            return;
+        }
+
+        List<FlowRule> newFlowRules = Lists.newArrayList();
+
+        try {
+            for (DeviceId deviceId : leafs) {
+                Host srcHost = hostMap.get(deviceId);
+                Set<Host> dstHosts = hosts.stream().filter(h -> h != srcHost).collect(toSet());
+                newFlowRules.addAll(generateLeafRules(deviceId, srcHost, dstHosts, spines, topo));
+            }
+            for (DeviceId deviceId : spines) {
+                newFlowRules.addAll(generateSpineRules(deviceId, hosts, topo));
+            }
+        } catch (FlowRuleGeneratorException e) {
+            log.warn("Exception while executing flow rule generator: ", e.toString());
+            return;
+        }
+
+        if (newFlowRules.size() == 0) {
+            // Something went wrong
+            log.error("0 flow rules generated, BUG?");
+            return;
+        }
+
+        // All good!
+        // Divide flow rules per device id...
+        ImmutableMap.Builder<DeviceId, List<FlowRule>> mapBuilder = ImmutableMap.builder();
+        concat(spines.stream(), leafs.stream())
+                .map(deviceId -> ImmutableList.copyOf(newFlowRules
+                                                              .stream()
+                                                              .filter(fr -> fr.deviceId().equals(deviceId))
+                                                              .iterator()))
+                .forEach(frs -> mapBuilder.put(frs.get(0).deviceId(), frs));
+        this.deviceFlowRules = mapBuilder.build();
+
+        this.leafSwitches = ImmutableSet.copyOf(leafs);
+        this.spineSwitches = ImmutableSet.copyOf(spines);
+
+        // Avoid other executions to modify the generated flow rules.
+        flowRuleGenerated = true;
+
+        log.info("Generated {} flow rules for {} devices", newFlowRules.size(), spines.size() + leafs.size());
+
+        spawnTask(this::deployAllDevices);
+    }
+
+    /**
+     * Returns a new, pre-configured flow rule builder.
+     *
+     * @param did       a device id
+     * @param tableName a table name
+     * @return a new flow rule builder
+     */
+    protected FlowRule.Builder flowRuleBuilder(DeviceId did, String tableName) throws FlowRuleGeneratorException {
+
+        final PiPipelineInterpreter interpreter;
+        try {
+            interpreter = (PiPipelineInterpreter) pipeconf.implementation(PiPipelineInterpreter.class)
+                    .orElse(null)
+                    .newInstance();
+        } catch (InstantiationException | IllegalAccessException e) {
+            throw new FlowRuleGeneratorException("Unable to instantiate interpreter of pipeconf " + pipeconf.id());
+        }
+
+        int flowRuleTableId;
+        if (interpreter.mapPiTableId(PiTableId.of(tableName)).isPresent()) {
+            flowRuleTableId = interpreter.mapPiTableId(PiTableId.of(tableName)).get().intValue();
+        } else {
+            throw new FlowRuleGeneratorException("Unknown table " + tableName);
+        }
+
+        return DefaultFlowRule.builder()
+                .forDevice(did)
+                .forTable(flowRuleTableId)
+                .fromApp(appId)
+                .withPriority(FLOW_PRIORITY)
+                .makePermanent();
+    }
+
+    private List<Port> getHostPorts(DeviceId deviceId, Topology topology) {
+        // Get all non-fabric ports.
+        return deviceService
+                .getPorts(deviceId)
+                .stream()
+                .filter(p -> !isFabricPort(p, topology))
+                .collect(Collectors.toList());
+    }
+
+    private boolean isSpine(DeviceId deviceId, Topology topology) {
+        // True if all ports are fabric.
+        return getHostPorts(deviceId, topology).size() == 0;
+    }
+
+    protected boolean isFabricPort(Port port, Topology topology) {
+        // True if the port connects this device to another infrastructure device.
+        return topologyService.isInfrastructure(topology, new ConnectPoint(port.element().id(), port.number()));
+    }
+
+    /**
+     * A listener of topology events that executes a flow rule generation task each time a device is added.
+     */
+    private class InternalTopologyListener implements TopologyListener {
+
+        @Override
+        public void event(TopologyEvent event) {
+            spawnTask(() -> generateFlowRules(event.subject(), Sets.newHashSet(hostService.getHosts())));
+        }
+
+        @Override
+        public boolean isRelevant(TopologyEvent event) {
+            return !appFreezed &&
+                    // If at least one reason is of type DEVICE_ADDED.
+                    event.reasons().stream().
+                            filter(r -> r instanceof DeviceEvent)
+                            .filter(r -> ((DeviceEvent) r).type() == DEVICE_ADDED)
+                            .findAny()
+                            .isPresent();
+        }
+    }
+
+    /**
+     * A listener of device events that executes a device deploy task each time a device is added, updated or
+     * re-connects.
+     */
+    private class InternalDeviceListener implements DeviceListener {
+        @Override
+        public void event(DeviceEvent event) {
+            spawnTask(() -> deployDevice(event.subject()));
+        }
+
+        @Override
+        public boolean isRelevant(DeviceEvent event) {
+            return !appFreezed &&
+                    (event.type() == DEVICE_ADDED ||
+                            event.type() == DEVICE_UPDATED ||
+                            (event.type() == DEVICE_AVAILABILITY_CHANGED &&
+                                    deviceService.isAvailable(event.subject().id())));
+        }
+    }
+
+    /**
+     * A listener of host events that generates flow rules each time a new host is added.
+     */
+    private class InternalHostListener implements HostListener {
+        @Override
+        public void event(HostEvent event) {
+            spawnTask(() -> generateFlowRules(topologyService.currentTopology(),
+                                              Sets.newHashSet(hostService.getHosts())));
+        }
+
+        @Override
+        public boolean isRelevant(HostEvent event) {
+            return !appFreezed && event.type() == HOST_ADDED;
+        }
+    }
+
+    /**
+     * An exception occurred while generating flow rules for this fabric.
+     */
+    public class FlowRuleGeneratorException extends Exception {
+
+        public FlowRuleGeneratorException() {
+        }
+
+        public FlowRuleGeneratorException(String msg) {
+            super(msg);
+        }
+
+        public FlowRuleGeneratorException(Exception cause) {
+            super(cause);
+        }
+    }
+}
\ No newline at end of file
diff --git a/apps/pi-demo/common/src/main/java/org/onosproject/pi/demo/app/common/package-info.java b/apps/pi-demo/common/src/main/java/org/onosproject/pi/demo/app/common/package-info.java
new file mode 100644
index 0000000..8b4570d
--- /dev/null
+++ b/apps/pi-demo/common/src/main/java/org/onosproject/pi/demo/app/common/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-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.
+ */
+
+/**
+ * PI demo app common classes.
+ */
+package org.onosproject.pi.demo.app.common;
\ No newline at end of file
diff --git a/apps/pi-demo/ecmp/BUCK b/apps/pi-demo/ecmp/BUCK
new file mode 100644
index 0000000..d33ac207
--- /dev/null
+++ b/apps/pi-demo/ecmp/BUCK
@@ -0,0 +1,24 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//lib:minimal-json',
+    '//incubator/bmv2/model:onos-incubator-bmv2-model',
+    '//apps/pi-demo/common:onos-apps-pi-demo-common',
+]
+
+osgi_jar (
+    deps = COMPILE_DEPS,
+)
+
+BUNDLES = [
+    '//apps/pi-demo/ecmp:onos-apps-pi-demo-ecmp',
+    '//apps/pi-demo/common:onos-apps-pi-demo-common',
+]
+
+onos_app (
+    app_name = 'org.onosproject.pi-ecmp-fabric',
+    title = 'PI Demo ECMP Fabric',
+    category = 'Traffic Steering',
+    url = 'http://onosproject.org',
+    description = 'Provides ECMP support for a 2-stage clos fabric topology of PI-enabled devices',
+    included_bundles = BUNDLES,
+)
diff --git a/apps/pi-demo/ecmp/src/main/java/org/onosproject/pi/demo/app/ecmp/EcmpFabricApp.java b/apps/pi-demo/ecmp/src/main/java/org/onosproject/pi/demo/app/ecmp/EcmpFabricApp.java
new file mode 100644
index 0000000..7caa8e9
--- /dev/null
+++ b/apps/pi-demo/ecmp/src/main/java/org/onosproject/pi/demo/app/ecmp/EcmpFabricApp.java
@@ -0,0 +1,275 @@
+/*
+ * Copyright 2017-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.pi.demo.app.ecmp;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.felix.scr.annotations.Component;
+import org.onlab.util.ImmutableByteSequence;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.flow.criteria.PiCriterion;
+import org.onosproject.net.pi.runtime.PiAction;
+import org.onosproject.net.pi.runtime.PiActionId;
+import org.onosproject.net.pi.runtime.PiActionParam;
+import org.onosproject.net.pi.runtime.PiActionParamId;
+import org.onosproject.net.pi.runtime.PiHeaderFieldId;
+import org.onosproject.net.pi.runtime.PiTableAction;
+import org.onosproject.pi.demo.app.common.AbstractUpgradableFabricApp;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Host;
+import org.onosproject.net.Path;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.pi.model.DefaultPiPipeconf;
+import org.onosproject.net.pi.model.PiPipeconf;
+import org.onosproject.net.pi.model.PiPipeconfId;
+import org.onosproject.net.pi.model.PiPipelineInterpreter;
+import org.onosproject.net.topology.DefaultTopologyVertex;
+import org.onosproject.net.topology.Topology;
+import org.onosproject.net.topology.TopologyGraph;
+import org.onosproject.bmv2.model.Bmv2PipelineModelParser;
+
+import java.net.URL;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static java.util.stream.Collectors.toSet;
+import static org.onlab.packet.EthType.EtherType.IPV4;
+import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON;
+import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
+import static org.onosproject.pi.demo.app.ecmp.EcmpInterpreter.*;
+
+
+/**
+ * Implementation of an upgradable fabric app for the ECMP configuration.
+ */
+@Component(immediate = true)
+public class EcmpFabricApp extends AbstractUpgradableFabricApp {
+
+    private static final String APP_NAME = "org.onosproject.pi-ecmp-fabric";
+    private static final String MODEL_NAME = "ECMP";
+    private static final String PIPECONF_ID = "org.project.pipeconf.ecmp";
+    private static final URL P4INFO_URL = EcmpFabricApp.class.getResource("ecmp.p4info");
+    private static final URL JSON_URL = EcmpFabricApp.class.getResource("ecmp.json");
+
+    private static final PiPipeconf ECMP_PIPECONF = DefaultPiPipeconf.builder()
+            .withId(new PiPipeconfId(PIPECONF_ID))
+            .withPipelineModel(Bmv2PipelineModelParser.parse(JSON_URL))
+            .addBehaviour(PiPipelineInterpreter.class, EcmpInterpreter.class)
+            .addExtension(P4_INFO_TEXT, P4INFO_URL)
+            .addExtension(BMV2_JSON, JSON_URL)
+            .build();
+
+    private static final Map<DeviceId, Map<Set<PortNumber>, Short>> DEVICE_GROUP_ID_MAP = Maps.newHashMap();
+
+    public EcmpFabricApp() {
+        super(APP_NAME, MODEL_NAME, ECMP_PIPECONF);
+    }
+
+    @Override
+    public boolean initDevice(DeviceId deviceId) {
+        // Nothing to do.
+        return true;
+    }
+
+    @Override
+    public List<FlowRule> generateLeafRules(DeviceId leaf, Host srcHost, Collection<Host> dstHosts,
+                                            Collection<DeviceId> availableSpines, Topology topo)
+            throws FlowRuleGeneratorException {
+
+        // Get ports which connect this leaf switch to hosts.
+        Set<PortNumber> hostPorts = deviceService.getPorts(leaf)
+                .stream()
+                .filter(port -> !isFabricPort(port, topo))
+                .map(Port::number)
+                .collect(Collectors.toSet());
+
+        // Get ports which connect this leaf to the given available spines.
+        TopologyGraph graph = topologyService.getGraph(topo);
+        Set<PortNumber> fabricPorts = graph.getEdgesFrom(new DefaultTopologyVertex(leaf))
+                .stream()
+                .filter(e -> availableSpines.contains(e.dst().deviceId()))
+                .map(e -> e.link().src().port())
+                .collect(Collectors.toSet());
+
+        if (hostPorts.size() != 1 || fabricPorts.size() == 0) {
+            log.error("Leaf switch has invalid port configuration: hostPorts={}, fabricPorts={}",
+                      hostPorts.size(), fabricPorts.size());
+            throw new FlowRuleGeneratorException();
+        }
+        PortNumber hostPort = hostPorts.iterator().next();
+
+        List<FlowRule> rules = Lists.newArrayList();
+
+        TrafficTreatment treatment;
+        if (fabricPorts.size() > 1) {
+            // Do ECMP.
+            Pair<PiTableAction, List<FlowRule>> result = provisionEcmpPiTableAction(leaf, fabricPorts);
+            rules.addAll(result.getRight());
+            treatment = DefaultTrafficTreatment.builder().piTableAction(result.getLeft()).build();
+        } else {
+            // Output on port.
+            PortNumber outPort = fabricPorts.iterator().next();
+            treatment = DefaultTrafficTreatment.builder().setOutput(outPort).build();
+        }
+
+        // From srHost to dstHosts.
+        for (Host dstHost : dstHosts) {
+            FlowRule rule = flowRuleBuilder(leaf, EcmpInterpreter.TABLE0)
+                    .withSelector(
+                            DefaultTrafficSelector.builder()
+                                    .matchInPort(hostPort)
+                                    .matchEthType(IPV4.ethType().toShort())
+                                    .matchEthSrc(srcHost.mac())
+                                    .matchEthDst(dstHost.mac())
+                                    .build())
+                    .withTreatment(treatment)
+                    .build();
+            rules.add(rule);
+        }
+
+        // From fabric ports to this leaf host.
+        for (PortNumber port : fabricPorts) {
+            FlowRule rule = flowRuleBuilder(leaf, EcmpInterpreter.TABLE0)
+                    .withSelector(
+                            DefaultTrafficSelector.builder()
+                                    .matchInPort(port)
+                                    .matchEthType(IPV4.ethType().toShort())
+                                    .matchEthDst(srcHost.mac())
+                                    .build())
+                    .withTreatment(
+                            DefaultTrafficTreatment.builder()
+                                    .setOutput(hostPort)
+                                    .build())
+                    .build();
+            rules.add(rule);
+        }
+
+        return rules;
+    }
+
+    @Override
+    public List<FlowRule> generateSpineRules(DeviceId deviceId, Collection<Host> dstHosts, Topology topo)
+            throws FlowRuleGeneratorException {
+
+        List<FlowRule> rules = Lists.newArrayList();
+
+        // for each host
+        for (Host dstHost : dstHosts) {
+
+            Set<Path> paths = topologyService.getPaths(topo, deviceId, dstHost.location().deviceId());
+
+            if (paths.size() == 0) {
+                log.warn("Can't find any path between spine {} and host {}", deviceId, dstHost);
+                throw new FlowRuleGeneratorException();
+            }
+
+            TrafficTreatment treatment;
+
+            if (paths.size() == 1) {
+                // Only one path, do output on port.
+                PortNumber port = paths.iterator().next().src().port();
+                treatment = DefaultTrafficTreatment.builder().setOutput(port).build();
+            } else {
+                // Multiple paths, do ECMP.
+                Set<PortNumber> portNumbers = paths.stream().map(p -> p.src().port()).collect(toSet());
+                Pair<PiTableAction, List<FlowRule>> result = provisionEcmpPiTableAction(deviceId, portNumbers);
+                rules.addAll(result.getRight());
+                treatment = DefaultTrafficTreatment.builder().piTableAction(result.getLeft()).build();
+            }
+
+            FlowRule rule = flowRuleBuilder(deviceId, EcmpInterpreter.TABLE0)
+                    .withSelector(
+                            DefaultTrafficSelector.builder()
+                                    .matchEthType(IPV4.ethType().toShort())
+                                    .matchEthDst(dstHost.mac())
+                                    .build())
+                    .withTreatment(treatment)
+                    .build();
+
+            rules.add(rule);
+        }
+
+        return rules;
+    }
+
+    private Pair<PiTableAction, List<FlowRule>> provisionEcmpPiTableAction(DeviceId deviceId,
+                                                                            Set<PortNumber> fabricPorts)
+            throws FlowRuleGeneratorException {
+
+        // Install ECMP group table entries that map from hash values to actual fabric ports...
+        int groupId = groupIdOf(deviceId, fabricPorts);
+        int groupSize = fabricPorts.size();
+        Iterator<PortNumber> portIterator = fabricPorts.iterator();
+        List<FlowRule> rules = Lists.newArrayList();
+        for (short i = 0; i < groupSize; i++) {
+            FlowRule rule = flowRuleBuilder(deviceId, EcmpInterpreter.ECMP_GROUP_TABLE)
+                    .withSelector(
+                            buildEcmpTrafficSelector(groupId, i))
+                    .withTreatment(
+                            DefaultTrafficTreatment.builder()
+                                    .setOutput(portIterator.next())
+                                    .build())
+                    .build();
+            rules.add(rule);
+        }
+
+        PiTableAction piTableAction = buildEcmpPiTableAction(groupId, groupSize);
+
+        return Pair.of(piTableAction, rules);
+    }
+
+    private PiTableAction buildEcmpPiTableAction(int groupId, int groupSize) {
+
+        return PiAction.builder()
+                .withId(PiActionId.of(ECMP_GROUP_ACTION_NAME))
+                .withParameter(new PiActionParam(PiActionParamId.of(GROUP_ID),
+                                                 ImmutableByteSequence.copyFrom(groupId)))
+                .withParameter(new PiActionParam(PiActionParamId.of(GROUP_SIZE),
+                                                 ImmutableByteSequence.copyFrom(groupSize)))
+                .build();
+    }
+
+    private TrafficSelector buildEcmpTrafficSelector(int groupId, int selector) {
+        Criterion ecmpCriterion = PiCriterion.builder()
+                .matchExact(PiHeaderFieldId.of(ECMP_METADATA_HEADER_NAME, GROUP_ID), groupId)
+                .matchExact(PiHeaderFieldId.of(ECMP_METADATA_HEADER_NAME, SELECTOR), selector)
+                .build();
+
+        return DefaultTrafficSelector.builder()
+                .matchPi((PiCriterion) ecmpCriterion)
+                .build();
+    }
+
+    public int groupIdOf(DeviceId deviceId, Set<PortNumber> ports) {
+        DEVICE_GROUP_ID_MAP.putIfAbsent(deviceId, Maps.newHashMap());
+        // Counts the number of unique portNumber sets for each deviceId.
+        // Each distinct set of portNumbers will have a unique ID.
+        return DEVICE_GROUP_ID_MAP.get(deviceId).computeIfAbsent(ports, (pp) ->
+                (short) (DEVICE_GROUP_ID_MAP.get(deviceId).size() + 1));
+    }
+}
\ No newline at end of file
diff --git a/apps/pi-demo/ecmp/src/main/java/org/onosproject/pi/demo/app/ecmp/EcmpInterpreter.java b/apps/pi-demo/ecmp/src/main/java/org/onosproject/pi/demo/app/ecmp/EcmpInterpreter.java
new file mode 100644
index 0000000..d58ff16
--- /dev/null
+++ b/apps/pi-demo/ecmp/src/main/java/org/onosproject/pi/demo/app/ecmp/EcmpInterpreter.java
@@ -0,0 +1,249 @@
+/*
+ * Copyright 2017-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.pi.demo.app.ecmp;
+
+import com.google.common.collect.ImmutableBiMap;
+import com.google.common.collect.ImmutableList;
+import org.onlab.packet.Ethernet;
+import org.onlab.util.ImmutableByteSequence;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.flow.instructions.Instruction;
+import org.onosproject.net.flow.instructions.Instructions;
+import org.onosproject.net.packet.DefaultInboundPacket;
+import org.onosproject.net.packet.InboundPacket;
+import org.onosproject.net.packet.OutboundPacket;
+import org.onosproject.net.pi.model.PiPipelineInterpreter;
+import org.onosproject.net.pi.runtime.PiAction;
+import org.onosproject.net.pi.runtime.PiActionId;
+import org.onosproject.net.pi.runtime.PiActionParam;
+import org.onosproject.net.pi.runtime.PiActionParamId;
+import org.onosproject.net.pi.runtime.PiHeaderFieldId;
+import org.onosproject.net.pi.runtime.PiPacketMetadata;
+import org.onosproject.net.pi.runtime.PiPacketMetadataId;
+import org.onosproject.net.pi.runtime.PiPacketOperation;
+import org.onosproject.net.pi.runtime.PiTableId;
+
+import java.nio.ByteBuffer;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+
+import static java.util.stream.Collectors.toList;
+import static org.onosproject.net.PortNumber.CONTROLLER;
+import static org.onosproject.net.PortNumber.FLOOD;
+import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
+import static org.onosproject.net.pi.runtime.PiPacketOperation.Type.PACKET_OUT;
+
+/**
+ * Implementation of a PiPipeline interpreter for the ecmp.json configuration.
+ */
+public class EcmpInterpreter extends AbstractHandlerBehaviour implements PiPipelineInterpreter {
+
+    protected static final String ECMP_METADATA_HEADER_NAME = "ecmp_metadata_t";
+    protected static final String ECMP_GROUP_ACTION_NAME = "ecmp_group";
+    protected static final String GROUP_ID = "group_id";
+    protected static final String SELECTOR = "selector";
+    protected static final String GROUP_SIZE = "groupSize";
+    protected static final String ECMP_GROUP_TABLE = "ecmp_group_table";
+    protected static final String TABLE0 = "table0";
+    private static final String SEND_TO_CPU = "send_to_cpu";
+    private static final String PORT = "port";
+    private static final String DROP = "drop";
+    private static final String SET_EGRESS_PORT = "set_egress_port";
+    private static final String EGRESS_PORT = "egress_port";
+    private static final int PORT_NUMBER_BIT_WIDTH = 9;
+
+    private static final PiHeaderFieldId IN_PORT_ID = PiHeaderFieldId.of("standard_metadata", "ingress_port");
+    private static final PiHeaderFieldId ETH_DST_ID = PiHeaderFieldId.of("ethernet", "dstAddr");
+    private static final PiHeaderFieldId ETH_SRC_ID = PiHeaderFieldId.of("ethernet", "srcAddr");
+    private static final PiHeaderFieldId ETH_TYPE_ID = PiHeaderFieldId.of("ethernet", "etherType");
+
+    private static final ImmutableBiMap<Criterion.Type, PiHeaderFieldId> CRITERION_MAP =
+            new ImmutableBiMap.Builder<Criterion.Type, PiHeaderFieldId>()
+                    .put(Criterion.Type.IN_PORT, IN_PORT_ID)
+                    .put(Criterion.Type.ETH_DST, ETH_DST_ID)
+                    .put(Criterion.Type.ETH_SRC, ETH_SRC_ID)
+                    .put(Criterion.Type.ETH_TYPE, ETH_TYPE_ID)
+                    .build();
+
+    private static final ImmutableBiMap<Integer, PiTableId> TABLE_MAP = ImmutableBiMap.of(
+            0, PiTableId.of(TABLE0),
+            1, PiTableId.of(ECMP_GROUP_TABLE));
+
+    public static final String INGRESS_PORT = "ingress_port";
+
+    @Override
+    public Optional<Integer> mapPiTableId(PiTableId piTableId) {
+        return Optional.ofNullable(TABLE_MAP.inverse().get(piTableId));
+    }
+
+    @Override
+    public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId)
+            throws PiInterpreterException {
+
+        if (treatment.allInstructions().size() == 0) {
+            // No instructions means drop for us.
+            return actionWithName(DROP);
+        } else if (treatment.allInstructions().size() > 1) {
+            // Otherwise, we understand treatments with only 1 instruction.
+            throw new PiPipelineInterpreter.PiInterpreterException("Treatment has multiple instructions");
+        }
+
+        Instruction instruction = treatment.allInstructions().get(0);
+
+        switch (instruction.type()) {
+            case OUTPUT:
+                Instructions.OutputInstruction outInstruction = (Instructions.OutputInstruction) instruction;
+                PortNumber port = outInstruction.port();
+                if (!port.isLogical()) {
+                    PiAction.builder()
+                            .withId(PiActionId.of(SET_EGRESS_PORT))
+                            .withParameter(new PiActionParam(PiActionParamId.of(PORT),
+                                                             ImmutableByteSequence.copyFrom(port.toLong())))
+                            .build();
+                } else if (port.equals(CONTROLLER)) {
+                    return actionWithName(SEND_TO_CPU);
+                } else {
+                    throw new PiInterpreterException("Egress on logical port not supported: " + port);
+                }
+            case NOACTION:
+                return actionWithName(DROP);
+            default:
+                throw new PiInterpreterException("Instruction type not supported: " + instruction.type().name());
+        }
+    }
+
+    private static PiAction actionWithName(String name) {
+        return PiAction.builder().withId(PiActionId.of(name)).build();
+    }
+
+    @Override
+    public Optional<PiHeaderFieldId> mapCriterionType(Criterion.Type type) {
+        return Optional.ofNullable(CRITERION_MAP.get(type));
+    }
+
+    @Override
+    public Optional<Criterion.Type> mapPiHeaderFieldId(PiHeaderFieldId headerFieldId) {
+        return Optional.ofNullable(CRITERION_MAP.inverse().get(headerFieldId));
+    }
+
+    @Override
+    public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
+        return Optional.ofNullable(TABLE_MAP.get(flowRuleTableId));
+    }
+
+    @Override
+    public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
+            throws PiInterpreterException {
+        TrafficTreatment treatment = packet.treatment();
+
+        // ecmp.p4 supports only OUTPUT instructions.
+        List<Instructions.OutputInstruction> outInstructions = treatment.allInstructions()
+                .stream()
+                .filter(i -> i.type().equals(OUTPUT))
+                .map(i -> (Instructions.OutputInstruction) i)
+                .collect(toList());
+
+        if (treatment.allInstructions().size() != outInstructions.size()) {
+            // There are other instructions that are not of type OUTPUT
+            throw new PiInterpreterException("Treatment not supported: " + treatment);
+        }
+
+        ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
+        for (Instructions.OutputInstruction outInst : outInstructions) {
+            if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
+                throw new PiInterpreterException("Logical port not supported: " +
+                                                         outInst.port());
+            } else if (outInst.port().equals(FLOOD)) {
+                //Since ecmp.p4 does not support flood for each port of the device
+                // create a packet operation to send the packet out of that specific port
+                for (Port port : handler().get(DeviceService.class).getPorts(packet.sendThrough())) {
+                    builder.add(createPiPacketOperation(packet.data(), port.number().toLong()));
+                }
+            } else {
+                builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong()));
+            }
+        }
+        return builder.build();
+    }
+
+    private PiPacketOperation createPiPacketOperation(ByteBuffer data, long portNumber) throws PiInterpreterException {
+        //create the metadata
+        PiPacketMetadata metadata = createPacketMetadata(portNumber);
+
+        //Create the Packet operation
+        return PiPacketOperation.builder()
+                .withType(PACKET_OUT)
+                .withData(ImmutableByteSequence.copyFrom(data))
+                .withMetadatas(ImmutableList.of(metadata))
+                .build();
+    }
+
+    private PiPacketMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
+        ImmutableByteSequence portValue = ImmutableByteSequence.copyFrom(portNumber);
+        //FIXME remove hardcoded bitWidth and retrieve it from pipelineModel
+        try {
+            portValue = ImmutableByteSequence.fit(portValue, PORT_NUMBER_BIT_WIDTH);
+        } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
+            throw new PiInterpreterException("Port number too big: {}" +
+                                                     portNumber + " causes " + e.getMessage());
+        }
+        return PiPacketMetadata.builder()
+                .withId(PiPacketMetadataId.of(EGRESS_PORT))
+                .withValue(portValue)
+                .build();
+    }
+
+    @Override
+    public InboundPacket mapInboundPacket(DeviceId deviceId, PiPacketOperation packetInOperation)
+            throws PiInterpreterException {
+        //We are assuming that the packet is ethernet type
+        Ethernet ethPkt = new Ethernet();
+
+        ethPkt.deserialize(packetInOperation.data().asArray(), 0, packetInOperation.data().size());
+
+        //Returns the ingress port packet metadata
+        Optional<PiPacketMetadata> packetMetadata = packetInOperation.metadatas()
+                .stream().filter(metadata -> metadata.id().name().equals(INGRESS_PORT))
+                .findFirst();
+
+        if (packetMetadata.isPresent()) {
+
+            //Obtaining the ingress port as an immutable byte sequence
+            ImmutableByteSequence portByteSequence = packetMetadata.get().value();
+
+            //Converting immutableByteSequence to short
+            short s = portByteSequence.asReadOnlyBuffer().getShort();
+
+            ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
+
+            //FIXME should be optimizable with .asReadOnlyBytebuffer
+            ByteBuffer rawData = ByteBuffer.wrap(packetInOperation.data().asArray());
+            return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
+
+        } else {
+            throw new PiInterpreterException("Can't get packet metadata for" + INGRESS_PORT);
+        }
+    }
+}
\ No newline at end of file
diff --git a/apps/pi-demo/ecmp/src/main/java/org/onosproject/pi/demo/app/ecmp/package-info.java b/apps/pi-demo/ecmp/src/main/java/org/onosproject/pi/demo/app/ecmp/package-info.java
new file mode 100644
index 0000000..0d70589
--- /dev/null
+++ b/apps/pi-demo/ecmp/src/main/java/org/onosproject/pi/demo/app/ecmp/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-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.
+ */
+
+/**
+ * PI demo app for the ECMP configuration.
+ */
+package org.onosproject.pi.demo.app.ecmp;
\ No newline at end of file
diff --git a/apps/pi-demo/ecmp/src/main/resources/ecmp.json b/apps/pi-demo/ecmp/src/main/resources/ecmp.json
new file mode 120000
index 0000000..78ebbe0
--- /dev/null
+++ b/apps/pi-demo/ecmp/src/main/resources/ecmp.json
@@ -0,0 +1 @@
+../../../../../../tools/test/p4src/p4-16/p4c-out/ecmp.json
\ No newline at end of file
diff --git a/apps/pi-demo/ecmp/src/main/resources/ecmp.p4info b/apps/pi-demo/ecmp/src/main/resources/ecmp.p4info
new file mode 120000
index 0000000..b4f5e4f
--- /dev/null
+++ b/apps/pi-demo/ecmp/src/main/resources/ecmp.p4info
@@ -0,0 +1 @@
+../../../../../../tools/test/p4src/p4-16/p4c-out/ecmp.p4info
\ No newline at end of file
diff --git a/modules.defs b/modules.defs
index c9aa174..22920c5 100644
--- a/modules.defs
+++ b/modules.defs
@@ -210,6 +210,7 @@
     '//apps/l3vpn:onos-apps-l3vpn-oar',
     '//apps/openroadm:onos-apps-openroadm-oar',
     '//apps/artemis:onos-apps-artemis-oar',
+    '//apps/pi-demo/ecmp:onos-apps-pi-demo-ecmp-oar',
     '//apps/gluon:onos-apps-gluon-oar',
     '//apps/evpnopenflow:onos-apps-evpnopenflow-oar',
     '//apps/route-service:onos-apps-route-service-oar',