Added BMv2 demo apps (onos1.6 cherry-pick)

Change-Id: I19484a826acce724c1fcd5b6e9910d724bda686f
diff --git a/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/WcmpFabricApp.java b/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/WcmpFabricApp.java
new file mode 100644
index 0000000..db7567e
--- /dev/null
+++ b/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/WcmpFabricApp.java
@@ -0,0 +1,294 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.bmv2.demo.app.wcmp;
+
+import com.eclipsesource.json.Json;
+import com.eclipsesource.json.JsonObject;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.bmv2.api.context.Bmv2Configuration;
+import org.onosproject.bmv2.api.context.Bmv2DefaultConfiguration;
+import org.onosproject.bmv2.api.context.Bmv2DeviceContext;
+import org.onosproject.bmv2.api.runtime.Bmv2Action;
+import org.onosproject.bmv2.api.runtime.Bmv2DeviceAgent;
+import org.onosproject.bmv2.api.runtime.Bmv2RuntimeException;
+import org.onosproject.bmv2.api.service.Bmv2Controller;
+import org.onosproject.bmv2.demo.app.common.AbstractUpgradableFabricApp;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Host;
+import org.onosproject.net.Path;
+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.flow.criteria.ExtensionSelector;
+import org.onosproject.net.flow.instructions.ExtensionTreatment;
+import org.onosproject.net.topology.DefaultTopologyVertex;
+import org.onosproject.net.topology.Topology;
+import org.onosproject.net.topology.TopologyGraph;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Collection;
+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.bmv2.demo.app.wcmp.WcmpGroupTreatmentBuilder.groupIdOf;
+import static org.onosproject.bmv2.demo.app.wcmp.WcmpGroupTreatmentBuilder.toPrefixLengths;
+import static org.onosproject.bmv2.demo.app.wcmp.WcmpInterpreter.TABLE0;
+import static org.onosproject.bmv2.demo.app.wcmp.WcmpInterpreter.WCMP_GROUP_TABLE;
+
+/**
+ * Implementation of an upgradable fabric app for the WCMP configuration.
+ */
+@Component(immediate = true)
+public class WcmpFabricApp extends AbstractUpgradableFabricApp {
+
+    private static final String APP_NAME = "org.onosproject.bmv2-wcmp-fabric";
+    private static final String MODEL_NAME = "WCMP";
+    private static final String JSON_CONFIG_PATH = "/wcmp.json";
+
+    private static final double MULTI_PORT_WEIGHT_COEFFICIENT = 0.85;
+
+    private static final Bmv2Configuration WCMP_CONFIGURATION = loadConfiguration();
+    private static final WcmpInterpreter WCMP_INTERPRETER = new WcmpInterpreter();
+    protected static final Bmv2DeviceContext WCMP_CONTEXT = new Bmv2DeviceContext(WCMP_CONFIGURATION, WCMP_INTERPRETER);
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    private Bmv2Controller bmv2Controller;
+
+    /**
+     * TODO.
+     */
+    public WcmpFabricApp() {
+        super(APP_NAME, MODEL_NAME, WCMP_CONTEXT);
+    }
+
+
+    @Override
+    public boolean initDevice(DeviceId deviceId) {
+        try {
+            Bmv2DeviceAgent agent = bmv2Controller.getAgent(deviceId);
+            for (Map.Entry<String, Bmv2Action> entry : WCMP_INTERPRETER.defaultActionsMap().entrySet()) {
+                agent.setTableDefaultAction(entry.getKey(), entry.getValue());
+            }
+            return true;
+        } catch (Bmv2RuntimeException e) {
+            log.error("Unable to init device {}: {}", deviceId, e.explain());
+            return false;
+        }
+    }
+
+    @Override
+    public List<FlowRule> generateLeafRules(DeviceId deviceId, Host srcHost, Collection<Host> dstHosts,
+                                            Collection<DeviceId> availableSpines, Topology topo)
+            throws FlowRuleGeneratorException {
+
+        Set<PortNumber> hostPortNumbers = Sets.newHashSet();
+        Set<PortNumber> fabricPortNumbers = Sets.newHashSet();
+        deviceService.getPorts(deviceId)
+                .forEach(p -> (isFabricPort(p, topo) ? fabricPortNumbers : hostPortNumbers).add(p.number()));
+
+        if (hostPortNumbers.size() != 1 || fabricPortNumbers.size() == 0) {
+            log.error("Leaf switch has invalid port configuration: hostPorts={}, fabricPorts={}",
+                      hostPortNumbers.size(), fabricPortNumbers.size());
+            throw new FlowRuleGeneratorException();
+        }
+        PortNumber hostPort = hostPortNumbers.iterator().next();
+
+        TopologyGraph graph = topologyService.getGraph(topo);
+        // Map key: spine device id, value: leaf switch ports which connect to spine in the key.
+        Map<DeviceId, Set<PortNumber>> spineToPortsMap = Maps.newHashMap();
+        graph.getEdgesFrom(new DefaultTopologyVertex(deviceId)).forEach(edge -> {
+            spineToPortsMap.putIfAbsent(edge.dst().deviceId(), Sets.newHashSet());
+            spineToPortsMap.get(edge.dst().deviceId()).add(edge.link().src().port());
+        });
+
+        double baseWeight = 1d / spineToPortsMap.size();
+
+        int numSinglePorts = (int) spineToPortsMap.values().stream().filter(s -> s.size() == 1).count();
+        int numMultiPorts = spineToPortsMap.size() - numSinglePorts;
+
+        // Reduce weight portion assigned to multi-ports to mitigate flow assignment imbalance (measured empirically).
+        double multiPortBaseWeight = baseWeight * MULTI_PORT_WEIGHT_COEFFICIENT;
+        double excess = (baseWeight - multiPortBaseWeight) * numMultiPorts;
+        double singlePortBaseWeight = baseWeight + (excess / numSinglePorts);
+
+        Map<PortNumber, Double> weighedPortNumbers = Maps.newHashMap();
+        spineToPortsMap.forEach((did, portSet) -> {
+            double base = (portSet.size() == 1) ? singlePortBaseWeight : multiPortBaseWeight;
+            double weight = base / portSet.size();
+            portSet.forEach(portNumber -> weighedPortNumbers.put(portNumber, weight));
+        });
+
+        List<FlowRule> rules = Lists.newArrayList();
+
+
+        Pair<ExtensionTreatment, List<FlowRule>> result = provisionWcmpTreatment(deviceId, weighedPortNumbers);
+        ExtensionTreatment wcmpTreatment = result.getLeft();
+        rules.addAll(result.getRight());
+
+        // From src host to dst hosts, WCMP to all fabric ports.
+        for (Host dstHost : dstHosts) {
+            FlowRule rule = flowRuleBuilder(deviceId, TABLE0)
+                    .withSelector(
+                            DefaultTrafficSelector.builder()
+                                    .matchInPort(hostPort)
+                                    .matchEthType(IPV4.ethType().toShort())
+                                    .matchEthSrc(srcHost.mac())
+                                    .matchEthDst(dstHost.mac())
+                                    .build())
+                    .withTreatment(
+                            DefaultTrafficTreatment.builder()
+                                    .extension(wcmpTreatment, deviceId)
+                                    .build())
+                    .build();
+            rules.add(rule);
+        }
+
+        // From fabric ports to src host.
+        for (PortNumber port : fabricPortNumbers) {
+            FlowRule rule = flowRuleBuilder(deviceId, 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 (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.
+                PortNumber port = paths.iterator().next().src().port();
+                treatment = DefaultTrafficTreatment.builder().setOutput(port).build();
+            } else {
+                // Multiple paths, do WCMP.
+                Set<PortNumber> portNumbers = paths.stream().map(p -> p.src().port()).collect(toSet());
+                double weight = 1d / portNumbers.size();
+                // Same weight for all ports.
+                Map<PortNumber, Double> weightedPortNumbers = portNumbers.stream()
+                        .collect(Collectors.toMap(p -> p, p -> weight));
+                Pair<ExtensionTreatment, List<FlowRule>> result = provisionWcmpTreatment(deviceId, weightedPortNumbers);
+                rules.addAll(result.getRight());
+                treatment = DefaultTrafficTreatment.builder().extension(result.getLeft(), deviceId).build();
+            }
+
+            FlowRule rule = flowRuleBuilder(deviceId, TABLE0)
+                    .withSelector(
+                            DefaultTrafficSelector.builder()
+                                    .matchEthType(IPV4.ethType().toShort())
+                                    .matchEthDst(dstHost.mac())
+                                    .build())
+                    .withTreatment(treatment)
+                    .build();
+
+            rules.add(rule);
+        }
+
+        return rules;
+    }
+
+    private Pair<ExtensionTreatment, List<FlowRule>> provisionWcmpTreatment(DeviceId deviceId,
+                                                                            Map<PortNumber, Double> weightedFabricPorts)
+            throws FlowRuleGeneratorException {
+
+        // Install WCMP group table entries that map from hash values to fabric ports.
+
+        int groupId = groupIdOf(deviceId, weightedFabricPorts);
+        List<PortNumber> portNumbers = Lists.newArrayList();
+        List<Double> weights = Lists.newArrayList();
+        weightedFabricPorts.forEach((p, w) -> {
+            portNumbers.add(p);
+            weights.add(w);
+        });
+        List<Integer> prefixLengths;
+        try {
+            prefixLengths = toPrefixLengths(weights);
+        } catch (WcmpGroupTreatmentBuilder.WcmpGroupException e) {
+            throw new FlowRuleGeneratorException(e);
+        }
+
+        List<FlowRule> rules = Lists.newArrayList();
+        for (int i = 0; i < portNumbers.size(); i++) {
+            ExtensionSelector extSelector = new WcmpGroupTableSelectorBuilder()
+                    .withGroupId(groupId)
+                    .withPrefixLength(prefixLengths.get(i))
+                    .build();
+            FlowRule rule = flowRuleBuilder(deviceId, WCMP_GROUP_TABLE)
+                    .withSelector(DefaultTrafficSelector.builder()
+                                          .extension(extSelector, deviceId)
+                                          .build())
+                    .withTreatment(
+                            DefaultTrafficTreatment.builder()
+                                    .setOutput(portNumbers.get(i))
+                                    .build())
+                    .build();
+            rules.add(rule);
+        }
+
+        ExtensionTreatment extTreatment = new WcmpGroupTreatmentBuilder().withGroupId(groupId).build();
+
+        return Pair.of(extTreatment, rules);
+    }
+
+    private static Bmv2Configuration loadConfiguration() {
+        try {
+            JsonObject json = Json.parse(new BufferedReader(new InputStreamReader(
+                    WcmpFabricApp.class.getResourceAsStream(JSON_CONFIG_PATH)))).asObject();
+            return Bmv2DefaultConfiguration.parse(json);
+        } catch (IOException e) {
+            throw new RuntimeException("Unable to load configuration", e);
+        }
+    }
+}
\ No newline at end of file
diff --git a/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/WcmpGroupTableSelectorBuilder.java b/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/WcmpGroupTableSelectorBuilder.java
new file mode 100644
index 0000000..bac9d61
--- /dev/null
+++ b/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/WcmpGroupTableSelectorBuilder.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.bmv2.demo.app.wcmp;
+
+import com.google.common.collect.ImmutableMap;
+import org.onlab.util.ImmutableByteSequence;
+import org.onosproject.bmv2.api.runtime.Bmv2ExactMatchParam;
+import org.onosproject.bmv2.api.runtime.Bmv2ExtensionSelector;
+import org.onosproject.bmv2.api.runtime.Bmv2LpmMatchParam;
+import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils;
+import org.onosproject.net.flow.criteria.ExtensionSelector;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.fitByteSequence;
+import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.roundToBytes;
+import static org.onosproject.bmv2.demo.app.wcmp.WcmpFabricApp.WCMP_CONTEXT;
+import static org.onosproject.bmv2.demo.app.wcmp.WcmpInterpreter.*;
+
+/**
+ * Builder of WCMP group table extension selector.
+ */
+public final class WcmpGroupTableSelectorBuilder {
+
+    private int groupId;
+    private int prefixLength;
+
+    /**
+     * Sets the WCMP group ID.
+     *
+     * @param groupId an integer value
+     * @return this
+     */
+    public WcmpGroupTableSelectorBuilder withGroupId(int groupId) {
+        this.groupId = groupId;
+        return this;
+    }
+
+    /**
+     * Sets the WCMP selector's prefix length.
+     *
+     * @param prefixLength an integer value
+     * @return this
+     */
+    public WcmpGroupTableSelectorBuilder withPrefixLength(int prefixLength) {
+        this.prefixLength = prefixLength;
+        return this;
+    }
+
+    /**
+     * Returns a new extension selector.
+     *
+     * @return an extension selector
+     */
+    public ExtensionSelector build() {
+
+        final int selectorBitWidth = WCMP_CONTEXT.configuration().headerType(WCMP_META_T).field(SELECTOR).bitWidth();
+        final int groupIdBitWidth = WCMP_CONTEXT.configuration().headerType(WCMP_META_T).field(GROUP_ID).bitWidth();
+        final ImmutableByteSequence ones = ImmutableByteSequence.ofOnes(roundToBytes(selectorBitWidth));
+
+        checkArgument(prefixLength >= 1 && prefixLength <= selectorBitWidth,
+                      "prefix length must be between 1 and " + selectorBitWidth);
+        try {
+            ImmutableByteSequence groupIdBs = fitByteSequence(ImmutableByteSequence.copyFrom(groupId), groupIdBitWidth);
+            Bmv2ExactMatchParam groupIdMatch = new Bmv2ExactMatchParam(groupIdBs);
+            Bmv2LpmMatchParam selectorMatch = new Bmv2LpmMatchParam(ones, prefixLength);
+
+            return new Bmv2ExtensionSelector(ImmutableMap.of(
+                    WCMP_META + "." + GROUP_ID, groupIdMatch,
+                    WCMP_META + "." + SELECTOR, selectorMatch));
+
+        } catch (Bmv2TranslatorUtils.ByteSequenceFitException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}
diff --git a/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/WcmpGroupTreatmentBuilder.java b/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/WcmpGroupTreatmentBuilder.java
new file mode 100644
index 0000000..731b2ca
--- /dev/null
+++ b/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/WcmpGroupTreatmentBuilder.java
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.bmv2.demo.app.wcmp;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import org.onlab.util.ImmutableByteSequence;
+import org.onosproject.bmv2.api.runtime.Bmv2Action;
+import org.onosproject.bmv2.api.runtime.Bmv2ExtensionTreatment;
+import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.instructions.ExtensionTreatment;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.stream.Collectors.toList;
+import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.fitByteSequence;
+import static org.onosproject.bmv2.demo.app.wcmp.WcmpFabricApp.WCMP_CONTEXT;
+import static org.onosproject.bmv2.demo.app.wcmp.WcmpInterpreter.*;
+
+/**
+ * Builder of WCMP extension treatment.
+ */
+public final class WcmpGroupTreatmentBuilder {
+
+    private static final double MAX_ERROR = 0.0001;
+
+    private static final Map<DeviceId, Map<Map<PortNumber, Double>, Integer>> DEVICE_GROUP_ID_MAP = Maps.newHashMap();
+
+    private int groupId;
+
+    /**
+     * Sets the WCMP group ID.
+     *
+     * @param groupId an integer value
+     * @return this
+     */
+    public WcmpGroupTreatmentBuilder withGroupId(int groupId) {
+        this.groupId = groupId;
+        return this;
+    }
+
+    /**
+     * Returns a new extension treatment.
+     *
+     * @return an extension treatment
+     */
+    public ExtensionTreatment build() {
+        checkArgument(groupId >= 0, "group id must be a non-zero positive integer");
+        ImmutableByteSequence groupIdBs = ImmutableByteSequence.copyFrom(groupId);
+        final int groupIdBitWidth = WCMP_CONTEXT.configuration().headerType(WCMP_META_T).field(GROUP_ID).bitWidth();
+        try {
+            groupIdBs = fitByteSequence(groupIdBs, groupIdBitWidth);
+            return new Bmv2ExtensionTreatment(
+                    Bmv2Action.builder()
+                            .withName(WCMP_GROUP)
+                            .addParameter(groupIdBs)
+                            .build());
+        } catch (Bmv2TranslatorUtils.ByteSequenceFitException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static int groupIdOf(DeviceId did, Map<PortNumber, Double> weightedPorts) {
+        DEVICE_GROUP_ID_MAP.putIfAbsent(did, Maps.newHashMap());
+        // Counts the number of unique portNumber sets for each device ID.
+        // Each distinct set of portNumbers will have a unique ID.
+        return DEVICE_GROUP_ID_MAP.get(did).computeIfAbsent(weightedPorts,
+                                                            (pp) -> DEVICE_GROUP_ID_MAP.get(did).size() + 1);
+    }
+
+    public static List<Integer> toPrefixLengths(List<Double> weigths) throws WcmpGroupException {
+
+        double weightSum = weigths.stream()
+                .mapToDouble(Double::doubleValue)
+                .map(WcmpGroupTreatmentBuilder::roundDouble)
+                .sum();
+
+        if (Math.abs(weightSum - 1) > MAX_ERROR) {
+            throw new WcmpGroupException("weights sum is expected to be 1, found was " + weightSum);
+        }
+
+        final int selectorBitWidth = WCMP_CONTEXT.configuration().headerType(WCMP_META_T).field(SELECTOR).bitWidth();
+        final int availableBits = selectorBitWidth - 1;
+
+        List<Long> prefixDiffs = weigths.stream().map(w -> Math.round(w * availableBits)).collect(toList());
+
+        final long bitSum = prefixDiffs.stream().mapToLong(Long::longValue).sum();
+        final long error = availableBits - bitSum;
+
+        if (error != 0) {
+            // Lazy intuition here is that the error can be absorbed by the longest prefixDiff with the minor impact.
+            Long maxDiff = Collections.max(prefixDiffs);
+            int idx = prefixDiffs.indexOf(maxDiff);
+            prefixDiffs.remove(idx);
+            prefixDiffs.add(idx, maxDiff + error);
+        }
+        List<Integer> prefixLengths = Lists.newArrayList();
+
+        int prefix = 1;
+        for (Long p : prefixDiffs) {
+            prefixLengths.add(prefix);
+            prefix += p;
+        }
+        return ImmutableList.copyOf(prefixLengths);
+    }
+
+    private static double roundDouble(double n) {
+        // 5 digits precision.
+        return (double) Math.round(n * 100000d) / 100000d;
+    }
+
+    public static class WcmpGroupException extends Exception {
+        public WcmpGroupException(String s) {
+        }
+    }
+}
diff --git a/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/WcmpInterpreter.java b/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/WcmpInterpreter.java
new file mode 100644
index 0000000..19d042d
--- /dev/null
+++ b/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/WcmpInterpreter.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.bmv2.demo.app.wcmp;
+
+import com.google.common.collect.ImmutableBiMap;
+import org.onlab.util.ImmutableByteSequence;
+import org.onosproject.bmv2.api.context.Bmv2Configuration;
+import org.onosproject.bmv2.api.context.Bmv2Interpreter;
+import org.onosproject.bmv2.api.context.Bmv2InterpreterException;
+import org.onosproject.bmv2.api.runtime.Bmv2Action;
+import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils;
+import org.onosproject.net.PortNumber;
+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.OutputInstruction;
+
+import java.util.Map;
+
+import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.fitByteSequence;
+import static org.onosproject.net.PortNumber.CONTROLLER;
+
+/**
+ * Implementation of a BMv2 interpreter for the wcmp.json configuration.
+ */
+public final class WcmpInterpreter implements Bmv2Interpreter {
+
+    protected static final String WCMP_META_T = "wcmp_meta_t";
+    protected static final String WCMP_META = "wcmp_meta";
+    protected static final String SELECTOR = "selector";
+    protected static final String GROUP_ID = "groupId";
+    protected static final String WCMP_GROUP = "wcmp_group";
+    protected static final String WCMP_SET_SELECTOR = "wcmp_set_selector";
+    protected static final String WCMP_SET_SELECTOR_TABLE = "wcmp_set_selector_table";
+    protected static final String WCMP_GROUP_TABLE = "wcmp_group_table";
+    protected static final String TABLE0 = "table0";
+    protected static final String SEND_TO_CPU = "send_to_cpu";
+    protected static final String DROP = "_drop";
+    protected static final String SET_EGRESS_PORT = "set_egress_port";
+    protected static final String PORT = "port";
+
+    private static final ImmutableBiMap<Criterion.Type, String> CRITERION_TYPE_MAP = ImmutableBiMap.of(
+            Criterion.Type.IN_PORT, "standard_metadata.ingress_port",
+            Criterion.Type.ETH_DST, "ethernet.dstAddr",
+            Criterion.Type.ETH_SRC, "ethernet.srcAddr",
+            Criterion.Type.ETH_TYPE, "ethernet.etherType");
+
+    private static final ImmutableBiMap<Integer, String> TABLE_ID_MAP = ImmutableBiMap.of(
+            0, TABLE0,
+            1, WCMP_GROUP_TABLE);
+
+    private static final Map<String, Bmv2Action> DEFAULT_ACTIONS_MAP = ImmutableBiMap.of(
+            WCMP_SET_SELECTOR_TABLE, actionWithName(WCMP_SET_SELECTOR));
+
+    @Override
+    public ImmutableBiMap<Integer, String> tableIdMap() {
+        return TABLE_ID_MAP;
+    }
+
+    @Override
+    public ImmutableBiMap<Criterion.Type, String> criterionTypeMap() {
+        return CRITERION_TYPE_MAP;
+    }
+
+    public Map<String, Bmv2Action> defaultActionsMap() {
+        return DEFAULT_ACTIONS_MAP;
+    }
+
+    @Override
+    public Bmv2Action mapTreatment(TrafficTreatment treatment, Bmv2Configuration configuration)
+            throws Bmv2InterpreterException {
+
+        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 Bmv2InterpreterException("Treatment has multiple instructions");
+        }
+
+        Instruction instruction = treatment.allInstructions().get(0);
+
+        switch (instruction.type()) {
+            case OUTPUT:
+                OutputInstruction outInstruction = (OutputInstruction) instruction;
+                PortNumber port = outInstruction.port();
+                if (!port.isLogical()) {
+                    return buildEgressAction(port, configuration);
+                } else if (port.equals(CONTROLLER)) {
+                    return actionWithName(SEND_TO_CPU);
+                } else {
+                    throw new Bmv2InterpreterException("Egress on logical port not supported: " + port);
+                }
+            case NOACTION:
+                return actionWithName(DROP);
+            default:
+                throw new Bmv2InterpreterException("Instruction type not supported: " + instruction.type().name());
+        }
+    }
+
+    private static Bmv2Action buildEgressAction(PortNumber port, Bmv2Configuration configuration)
+            throws Bmv2InterpreterException {
+
+        int portBitWidth = configuration.action(SET_EGRESS_PORT).runtimeData(PORT).bitWidth();
+
+        try {
+            ImmutableByteSequence portBs = fitByteSequence(ImmutableByteSequence.copyFrom(port.toLong()), portBitWidth);
+            return Bmv2Action.builder()
+                    .withName(SET_EGRESS_PORT)
+                    .addParameter(portBs)
+                    .build();
+        } catch (Bmv2TranslatorUtils.ByteSequenceFitException e) {
+            throw new Bmv2InterpreterException(e.getMessage());
+        }
+    }
+
+    private static Bmv2Action actionWithName(String name) {
+        return Bmv2Action.builder().withName(name).build();
+    }
+}
diff --git a/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/package-info.java b/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/package-info.java
new file mode 100644
index 0000000..d4fd1e2
--- /dev/null
+++ b/apps/bmv2-demo/wcmp/src/main/java/org/onosproject/bmv2/demo/app/wcmp/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * 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.
+ */
+
+/**
+ * BMv2 demo app for the WCMP configuration.
+ */
+package org.onosproject.bmv2.demo.app.wcmp;
\ No newline at end of file