WIP: Started implementing static flows for corsa vanderweken pipeline.

Modified to not use Corsa driver by default.

Change-Id: If8df5769cf084346121d34bf4490f578e6b6f2d0
diff --git a/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java
index 317bf0d..62098b4 100644
--- a/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java
+++ b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java
@@ -23,6 +23,7 @@
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.onlab.packet.Ethernet;
+import org.onlab.packet.MacAddress;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.net.DeviceId;
@@ -30,6 +31,8 @@
 import org.onosproject.net.flow.DefaultTrafficSelector;
 import org.onosproject.net.flow.DefaultTrafficTreatment;
 import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleOperations;
+import org.onosproject.net.flow.FlowRuleOperationsContext;
 import org.onosproject.net.flow.FlowRuleService;
 import org.onosproject.net.flow.TrafficSelector;
 import org.onosproject.net.flow.TrafficTreatment;
@@ -90,15 +93,19 @@
     private final Multiset<NextHop> nextHops = ConcurrentHashMultiset.create();
     private final Map<NextHop, NextHopGroupKey> groups = new HashMap<>();
 
-    private DeviceId deviceId = DeviceId.deviceId("of:00000000000000a1"); // TODO config
+    private DeviceId deviceId = DeviceId.deviceId("of:0000000000000001"); // TODO config
 
     private TunnellingConnectivityManager connectivityManager;
 
+    private InternalTableHandler provisionStaticTables = new InternalTableHandler();
+
     @Activate
     protected void activate() {
         log.info("Bgp1Router started");
         appId = coreService.registerApplication(BGP_ROUTER_APP);
 
+        provisionStaticTables.provision(true);
+
         connectivityManager = new TunnellingConnectivityManager(appId,
                                                                 configService,
                                                                 packetService);
@@ -114,6 +121,7 @@
     protected void deactivate() {
         routingService.stop();
         connectivityManager.stop();
+        provisionStaticTables.provision(false);
 
         log.info("BgpRouter stopped");
     }
@@ -224,4 +232,303 @@
             BgpRouter.this.updateFibEntry(updates);
         }
     }
+
+    private class InternalTableHandler {
+
+        private static final int CONTROLLER_PRIORITY = 255;
+        private static final int DROP_PRIORITY = 0;
+
+
+        public void provision(boolean install) {
+
+            processTableZero(install);
+            processTableOne(install);
+            processTableTwo(install);
+            processTableThree(install);
+            processTableFive(install);
+            processTableSix(install);
+            processTableNine(install);
+
+        }
+
+        private void processTableZero(boolean install) {
+            TrafficSelector.Builder selector;
+            TrafficTreatment.Builder treatment;
+
+            selector = DefaultTrafficSelector.builder();
+            treatment = DefaultTrafficTreatment.builder();
+
+            selector.matchEthDst(MacAddress.BROADCAST);
+            treatment.transition(FlowRule.Type.VLAN_MPLS);
+
+            FlowRule rule = new DefaultFlowRule(deviceId, selector.build(),
+                                                treatment.build(), CONTROLLER_PRIORITY,
+                                                appId, 0, true);
+
+            FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+
+            //Drop rule
+            selector = DefaultTrafficSelector.builder();
+            treatment = DefaultTrafficTreatment.builder();
+
+            treatment.drop();
+
+            rule = new DefaultFlowRule(deviceId, selector.build(),
+                                       treatment.build(), DROP_PRIORITY,
+                                       appId, 0, true, FlowRule.Type.VLAN_MPLS);
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+            flowService.apply(ops.build(new FlowRuleOperationsContext() {
+                @Override
+                public void onSuccess(FlowRuleOperations ops) {
+                    log.info("Provisioned default table for bgp router");
+                }
+
+                @Override
+                public void onError(FlowRuleOperations ops) {
+                    log.info("Failed to provision default table for bgp router");
+                }
+            }));
+
+        }
+
+        private void processTableOne(boolean install) {
+            TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+            TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
+            FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+            FlowRule rule;
+
+            selector.matchEthType(Ethernet.TYPE_IPV4);
+            treatment.transition(FlowRule.Type.VLAN);
+
+            rule = new DefaultFlowRule(deviceId, selector.build(),
+                                  treatment.build(), CONTROLLER_PRIORITY,
+                                  appId, 0, true, FlowRule.Type.VLAN_MPLS);
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+            selector = DefaultTrafficSelector.builder();
+            treatment = DefaultTrafficTreatment.builder();
+
+            selector.matchEthType(Ethernet.TYPE_ARP);
+            treatment.transition(FlowRule.Type.VLAN);
+
+            rule = new DefaultFlowRule(deviceId, selector.build(),
+                                       treatment.build(), CONTROLLER_PRIORITY,
+                                       appId, 0, true, FlowRule.Type.VLAN_MPLS);
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+            selector = DefaultTrafficSelector.builder();
+            treatment = DefaultTrafficTreatment.builder();
+
+            selector.matchEthType(Ethernet.TYPE_VLAN);
+            treatment.transition(FlowRule.Type.VLAN);
+
+            rule = new DefaultFlowRule(deviceId, selector.build(),
+                                       treatment.build(), CONTROLLER_PRIORITY,
+                                       appId, 0, true, FlowRule.Type.VLAN_MPLS);
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+            //Drop rule
+            selector = DefaultTrafficSelector.builder();
+            treatment = DefaultTrafficTreatment.builder();
+
+            treatment.drop();
+
+            rule = new DefaultFlowRule(deviceId, selector.build(),
+                                       treatment.build(), DROP_PRIORITY,
+                                       appId, 0, true, FlowRule.Type.VLAN_MPLS);
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+            flowService.apply(ops.build(new FlowRuleOperationsContext() {
+                @Override
+                public void onSuccess(FlowRuleOperations ops) {
+                    log.info("Provisioned vlan/mpls table for bgp router");
+                }
+
+                @Override
+                public void onError(FlowRuleOperations ops) {
+                    log.info("Failed to provision vlan/mpls table for bgp router");
+                }
+            }));
+
+        }
+
+        private void processTableTwo(boolean install) {
+            TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+            TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
+            FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+            FlowRule rule;
+
+            //Drop rule
+
+            treatment.drop();
+
+            rule = new DefaultFlowRule(deviceId, selector.build(),
+                                       treatment.build(), DROP_PRIORITY,
+                                       appId, 0, true, FlowRule.Type.VLAN);
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+            flowService.apply(ops.build(new FlowRuleOperationsContext() {
+                @Override
+                public void onSuccess(FlowRuleOperations ops) {
+                    log.info("Provisioned vlan table for bgp router");
+                }
+
+                @Override
+                public void onError(FlowRuleOperations ops) {
+                    log.info("Failed to provision vlan table for bgp router");
+                }
+            }));
+        }
+
+
+
+        private void processTableThree(boolean install) {
+            TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+            TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
+            FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+            FlowRule rule;
+
+            selector.matchEthType(Ethernet.TYPE_ARP);
+            treatment.punt();
+
+            rule = new DefaultFlowRule(deviceId, selector.build(),
+                                       treatment.build(), CONTROLLER_PRIORITY,
+                                       appId, 0, true, FlowRule.Type.ETHER);
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+            selector = DefaultTrafficSelector.builder();
+            treatment = DefaultTrafficTreatment.builder();
+
+            selector.matchEthType(Ethernet.TYPE_IPV4);
+            treatment.transition(FlowRule.Type.COS);
+
+            rule = new DefaultFlowRule(deviceId, selector.build(),
+                                       treatment.build(), CONTROLLER_PRIORITY,
+                                       appId, 0, true, FlowRule.Type.ETHER);
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+            //Drop rule
+            selector = DefaultTrafficSelector.builder();
+            treatment = DefaultTrafficTreatment.builder();
+
+            treatment.drop();
+
+            rule = new DefaultFlowRule(deviceId, selector.build(),
+                                       treatment.build(), DROP_PRIORITY,
+                                       appId, 0, true, FlowRule.Type.VLAN_MPLS);
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+            flowService.apply(ops.build(new FlowRuleOperationsContext() {
+                @Override
+                public void onSuccess(FlowRuleOperations ops) {
+                    log.info("Provisioned ether table for bgp router");
+                }
+
+                @Override
+                public void onError(FlowRuleOperations ops) {
+                    log.info("Failed to provision ether table for bgp router");
+                }
+            }));
+
+
+        }
+
+        private void processTableFive(boolean install) {
+            TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+            TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
+            FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+            FlowRule rule;
+
+            treatment.transition(FlowRule.Type.IP);
+
+            rule = new DefaultFlowRule(deviceId, selector.build(),
+                                       treatment.build(), DROP_PRIORITY,
+                                       appId, 0, true, FlowRule.Type.COS);
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+            flowService.apply(ops.build(new FlowRuleOperationsContext() {
+                @Override
+                public void onSuccess(FlowRuleOperations ops) {
+                    log.info("Provisioned cos table for bgp router");
+                }
+
+                @Override
+                public void onError(FlowRuleOperations ops) {
+                    log.info("Failed to provision cos table for bgp router");
+                }
+            }));
+
+        }
+
+        private void processTableSix(boolean install) {
+            TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+            TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
+            FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+            FlowRule rule;
+
+            //Drop rule
+
+            treatment.drop();
+
+            rule = new DefaultFlowRule(deviceId, selector.build(),
+                                       treatment.build(), DROP_PRIORITY,
+                                       appId, 0, true, FlowRule.Type.IP);
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+            flowService.apply(ops.build(new FlowRuleOperationsContext() {
+                @Override
+                public void onSuccess(FlowRuleOperations ops) {
+                    log.info("Provisioned FIB table for bgp router");
+                }
+
+                @Override
+                public void onError(FlowRuleOperations ops) {
+                    log.info("Failed to provision FIB table for bgp router");
+                }
+            }));
+        }
+
+        private void processTableNine(boolean install) {
+            TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+            TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
+            FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
+            FlowRule rule;
+
+            treatment.punt();
+
+            rule = new DefaultFlowRule(deviceId, selector.build(),
+                                       treatment.build(), CONTROLLER_PRIORITY,
+                                       appId, 0, true, FlowRule.Type.ACL);
+
+            ops = install ? ops.add(rule) : ops.remove(rule);
+
+            flowService.apply(ops.build(new FlowRuleOperationsContext() {
+                @Override
+                public void onSuccess(FlowRuleOperations ops) {
+                    log.info("Provisioned Local table for bgp router");
+                }
+
+                @Override
+                public void onError(FlowRuleOperations ops) {
+                    log.info("Failed to provision Local table for bgp router");
+                }
+            }));
+        }
+
+    }
 }