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/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