Tor app test flow rules

Change-Id: I7b1ffcbb9f40916e40712e66822217ecc146909d
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 e106823..485fc12 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
@@ -55,6 +55,7 @@
 import org.slf4j.Logger;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -84,6 +85,9 @@
 
     private static final Map<String, AbstractUpgradableFabricApp> APP_HANDLES = Maps.newConcurrentMap();
 
+
+    private static final boolean TEST_MODE = true;
+
     // TOPO_SIZE should be the same of the --size argument when running bmv2-demo.py
     private static final int TOPO_SIZE = 2;
     private static final boolean WITH_IMBALANCED_STRIPING = false;
@@ -274,6 +278,10 @@
     public abstract List<FlowRule> generateSpineRules(DeviceId deviceId, Set<Host> dstHosts, Topology topology)
             throws FlowRuleGeneratorException;
 
+    public List<FlowRule> generateTestFlowRules(DeviceId deviceId) throws FlowRuleGeneratorException {
+        return Collections.emptyList();
+    }
+
     private void deployAllDevices() {
         if (otherAppFound && otherApp.appActive) {
             log.info("Deactivating other app...");
@@ -491,6 +499,14 @@
         @Override
         public void event(DeviceEvent event) {
             spawnTask(() -> deployDevice(event.subject()));
+            if (event.type() == DEVICE_ADDED && TEST_MODE) {
+                DeviceId deviceId = event.subject().id();
+                try {
+                    generateTestFlowRules(deviceId).forEach(flowRuleService::applyFlowRules);
+                } catch (FlowRuleGeneratorException e) {
+                    log.error("Exception while generating test flow rules for {}: {}", deviceId, e);
+                }
+            }
         }
 
         @Override
diff --git a/apps/pi-demo/tor/src/main/java/org/onosproject/pi/demo/app/tor/TorApp.java b/apps/pi-demo/tor/src/main/java/org/onosproject/pi/demo/app/tor/TorApp.java
index 6ce8697..f5d43c6 100644
--- a/apps/pi-demo/tor/src/main/java/org/onosproject/pi/demo/app/tor/TorApp.java
+++ b/apps/pi-demo/tor/src/main/java/org/onosproject/pi/demo/app/tor/TorApp.java
@@ -18,6 +18,7 @@
 
 import com.google.common.collect.Lists;
 import org.apache.felix.scr.annotations.Component;
+import org.onlab.packet.Ip4Address;
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.MacAddress;
 import org.onosproject.net.DeviceId;
@@ -40,6 +41,7 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Random;
 import java.util.Set;
 import java.util.stream.Collectors;
 
@@ -67,6 +69,42 @@
         return true;
     }
 
+    public List<FlowRule> generateTestFlowRules(DeviceId deviceId) throws FlowRuleGeneratorException {
+        Random rand = new Random();
+        List<FlowRule> rules = Lists.newArrayList();
+        MacAddress ethAddr = MacAddress.ONOS;
+        IpAddress ipAddr = Ip4Address.valueOf(rand.nextInt());
+        PortNumber outPort =  PortNumber.portNumber(1, "eth0");
+
+        // Filtering (match on eth, NoAction)
+        rules.add(
+                flowRuleBuilder(deviceId, L3_FILTER_TBL_ID)
+                        .withSelector(DefaultTrafficSelector.builder()
+                                              .matchEthDst(ethAddr)
+                                              .build())
+                        .withTreatment(DefaultTrafficTreatment.builder()
+                                               .piTableAction(PiAction.builder()
+                                                                      .withId(PiActionId.of("NoAction"))
+                                                                      .build())
+                                               .build())
+                        .build());
+
+        // Forwarding
+        rules.add(
+                flowRuleBuilder(deviceId, L3_FWD_TBL_ID)
+                        .withSelector(
+                                DefaultTrafficSelector.builder()
+                                        .matchIPDst(ipAddr.toIpPrefix())
+                                        .build())
+                        .withTreatment(
+                                DefaultTrafficTreatment.builder()
+                                        .piTableAction(nextHopAction(outPort, ethAddr, ethAddr))
+                                        .build())
+                        .build());
+
+        return rules;
+    }
+
     @Override
     public List<FlowRule> generateLeafRules(DeviceId leaf, Host localHost, Set<Host> remoteHosts,
                                             Collection<DeviceId> availableSpines, Topology topo)