Minor changes to PI-ECMP demo

- Support devices with different pipeconfs (as in the HW testbed)
- Run UDP servers in Mininet hosts
- Wait before pushing config to ONOS

Change-Id: Ic400e0ac0949375a27aa9721b32dc57d5065fb1c
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
index 227ec24..18f1e8c 100644
--- 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
@@ -16,6 +16,7 @@
 
 package org.onosproject.pi.demo.app.common;
 
+import com.google.common.base.MoreObjects;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
@@ -44,6 +45,7 @@
 import org.onosproject.net.flow.FlowRuleService;
 import org.onosproject.net.host.HostService;
 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.pi.runtime.PiPipeconfService;
 import org.onosproject.net.pi.runtime.PiTableId;
@@ -67,6 +69,7 @@
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static java.lang.String.format;
 import static java.util.stream.Collectors.toSet;
@@ -134,7 +137,7 @@
     private boolean flowRuleGenerated = false;
     private ApplicationId appId;
 
-    private PiPipeconf appPipeconf;
+    private Collection<PiPipeconf> appPipeconfs;
 
     private Set<DeviceId> leafSwitches;
     private Set<DeviceId> spineSwitches;
@@ -149,11 +152,12 @@
      * Creates a new PI fabric app.
      *
      * @param appName     app name
-     * @param appPipeconf a P4Runtime device context to be used on devices
+     * @param appPipeconfs collection of compatible pipeconfs
      */
-    protected AbstractUpgradableFabricApp(String appName, PiPipeconf appPipeconf) {
+    protected AbstractUpgradableFabricApp(String appName, Collection<PiPipeconf> appPipeconfs) {
         this.appName = checkNotNull(appName);
-        this.appPipeconf = checkNotNull(appPipeconf);
+        this.appPipeconfs = checkNotNull(appPipeconfs);
+        checkArgument(appPipeconfs.size() > 0, "appPipeconfs cannot have size 0");
     }
 
     @Activate
@@ -177,7 +181,7 @@
 
         appId = coreService.registerApplication(appName);
         deviceService.addListener(deviceListener);
-        piPipeconfService.register(appPipeconf);
+        appPipeconfs.forEach(piPipeconfService::register);
 
         init();
 
@@ -197,7 +201,9 @@
         scheduledExecutorService.shutdown();
         deviceService.removeListener(deviceListener);
         flowRuleService.removeFlowRulesById(appId);
-        piPipeconfService.remove(appPipeconf.id());
+        appPipeconfs.stream()
+                .map(PiPipeconf::id)
+                .forEach(piPipeconfService::remove);
 
         appActive = false;
         APP_HANDLES.remove(appName);
@@ -287,6 +293,11 @@
                 .forEach(device -> spawnTask(() -> deployDevice(device)));
     }
 
+    private boolean matchPipeconf(PiPipeconfId piPipeconfId) {
+        return appPipeconfs.stream()
+                .anyMatch(p -> p.id().equals(piPipeconfId));
+    }
+
     /**
      * Executes a device deploy.
      *
@@ -304,11 +315,12 @@
             // Set pipeconf flag if not already done.
             if (!pipeconfFlags.getOrDefault(deviceId, false)) {
                 if (piPipeconfService.ofDevice(deviceId).isPresent() &&
-                        appPipeconf.id().equals(piPipeconfService.ofDevice(deviceId).get())) {
+                        matchPipeconf(piPipeconfService.ofDevice(deviceId).get())) {
                     pipeconfFlags.put(device.id(), true);
                 } else {
                     log.warn("Wrong pipeconf for {}, expecting {}, but found {}, aborting deploy",
-                             deviceId, appPipeconf.id(), piPipeconfService.ofDevice(deviceId).get());
+                             deviceId, MoreObjects.toStringHelper(appPipeconfs),
+                             piPipeconfService.ofDevice(deviceId).get());
                     return;
                 }
             }
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
index b66c160..fccbab5 100644
--- 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
@@ -21,14 +21,11 @@
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.felix.scr.annotations.Component;
 import org.onlab.util.ImmutableByteSequence;
-import org.onosproject.bmv2.model.Bmv2PipelineModelParser;
-import org.onosproject.driver.pipeline.DefaultSingleTablePipeline;
 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.behaviour.Pipeliner;
 import org.onosproject.net.flow.DefaultTrafficSelector;
 import org.onosproject.net.flow.DefaultTrafficTreatment;
 import org.onosproject.net.flow.FlowRule;
@@ -36,10 +33,6 @@
 import org.onosproject.net.flow.TrafficTreatment;
 import org.onosproject.net.flow.criteria.Criterion;
 import org.onosproject.net.flow.criteria.PiCriterion;
-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.pi.runtime.PiAction;
 import org.onosproject.net.pi.runtime.PiActionId;
 import org.onosproject.net.pi.runtime.PiActionParam;
@@ -51,7 +44,6 @@
 import org.onosproject.net.topology.TopologyGraph;
 import org.onosproject.pi.demo.app.common.AbstractUpgradableFabricApp;
 
-import java.net.URL;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
@@ -62,8 +54,6 @@
 import static java.lang.String.format;
 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.*;
 
 
@@ -74,23 +64,11 @@
 public class EcmpFabricApp extends AbstractUpgradableFabricApp {
 
     private static final String APP_NAME = "org.onosproject.pi-ecmp-fabric";
-    private static final String PIPECONF_ID = "pi-demo-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)
-            .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.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, ECMP_PIPECONF);
+        super(APP_NAME, EcmpPipeconfFactory.getAll());
     }
 
     @Override
diff --git a/apps/pi-demo/ecmp/src/main/java/org/onosproject/pi/demo/app/ecmp/EcmpPipeconfFactory.java b/apps/pi-demo/ecmp/src/main/java/org/onosproject/pi/demo/app/ecmp/EcmpPipeconfFactory.java
new file mode 100644
index 0000000..a6800a8
--- /dev/null
+++ b/apps/pi-demo/ecmp/src/main/java/org/onosproject/pi/demo/app/ecmp/EcmpPipeconfFactory.java
@@ -0,0 +1,63 @@
+/*
+ * 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 org.onosproject.bmv2.model.Bmv2PipelineModelParser;
+import org.onosproject.driver.pipeline.DefaultSingleTablePipeline;
+import org.onosproject.drivers.p4runtime.DefaultP4PortStatisticsDiscovery;
+import org.onosproject.net.behaviour.Pipeliner;
+import org.onosproject.net.device.PortStatisticsDiscovery;
+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 java.net.URL;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON;
+import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
+
+final class EcmpPipeconfFactory {
+
+    private static final String BMV2_PIPECONF_ID = "pi-demo-ecmp";
+    private static final URL BMV2_P4INFO_URL = EcmpFabricApp.class.getResource("/ecmp.p4info");
+    private static final URL BMV2_JSON_URL = EcmpFabricApp.class.getResource("/ecmp.json");
+
+    private static final PiPipeconf BMV2_PIPECONF = buildBmv2Pipeconf();
+
+    private EcmpPipeconfFactory() {
+        // Hides constructor.
+    }
+
+    static Collection<PiPipeconf> getAll() {
+        return Collections.singleton(BMV2_PIPECONF);
+    }
+
+    private static PiPipeconf buildBmv2Pipeconf() {
+        return DefaultPiPipeconf.builder()
+                .withId(new PiPipeconfId(BMV2_PIPECONF_ID))
+                .withPipelineModel(Bmv2PipelineModelParser.parse(BMV2_JSON_URL))
+                .addBehaviour(PiPipelineInterpreter.class, EcmpInterpreter.class)
+                .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class)
+                .addBehaviour(PortStatisticsDiscovery.class, DefaultP4PortStatisticsDiscovery.class)
+                .addExtension(P4_INFO_TEXT, BMV2_P4INFO_URL)
+                .addExtension(BMV2_JSON, BMV2_JSON_URL)
+                .build();
+    }
+}