BgpRouter: Added treatment to delete flows

Deleting routes now works.

Change-Id: I7ed682474e571aa2f402a1762a2037190c2d9284
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 66e4b25..f151bc3 100644
--- a/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java
+++ b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java
@@ -211,18 +211,7 @@
     }
 
     private void installFlow(FibEntry entry, Group group) {
-        TrafficSelector selector = DefaultTrafficSelector.builder()
-                .matchEthType(Ethernet.TYPE_IPV4)
-                .matchIPDst(entry.prefix())
-                .build();
-
-        TrafficTreatment treatment = DefaultTrafficTreatment.builder()
-                .group(group.id())
-                .build();
-
-        FlowRule flowRule = new DefaultFlowRule(deviceId, selector, treatment,
-                                                PRIORITY, appId, 0, true,
-                                                FlowRule.Type.IP);
+        FlowRule flowRule = generateRibFlowRule(entry.prefix(), group);
 
         flowService.applyFlowRules(flowRule);
     }
@@ -231,21 +220,33 @@
         for (FibUpdate update : withdraws) {
             FibEntry entry = update.entry();
 
-            deleteNextHop(entry.prefix());
+            Group group = deleteNextHop(entry.prefix());
+            if (group == null) {
+                log.warn("Group not found when deleting {}", entry);
+                return;
+            }
 
-            TrafficSelector selector = DefaultTrafficSelector.builder()
-                    .matchEthType(Ethernet.TYPE_IPV4)
-                    .matchIPDst(update.entry().prefix())
-                    .build();
-
-            FlowRule flowRule = new DefaultFlowRule(deviceId, selector, null,
-                                                    PRIORITY, appId, 0, true,
-                                                    FlowRule.Type.IP);
+            FlowRule flowRule = generateRibFlowRule(entry.prefix(), group);
 
             flowService.removeFlowRules(flowRule);
         }
     }
 
+    private FlowRule generateRibFlowRule(IpPrefix prefix, Group group) {
+        TrafficSelector selector = DefaultTrafficSelector.builder()
+                .matchEthType(Ethernet.TYPE_IPV4)
+                .matchIPDst(prefix)
+                .build();
+
+        TrafficTreatment treatment = DefaultTrafficTreatment.builder()
+                .group(group.id())
+                .build();
+
+        return new DefaultFlowRule(deviceId, selector, treatment,
+                                   PRIORITY, appId, 0, true,
+                                   FlowRule.Type.IP);
+    }
+
     private synchronized void addNextHop(FibEntry entry) {
         prefixToNextHop.put(entry.prefix(), entry.nextHopIp());
         if (nextHopsCount.count(entry.nextHopIp()) == 0) {
@@ -288,14 +289,16 @@
         nextHopsCount.add(entry.nextHopIp());
     }
 
-    private synchronized void deleteNextHop(IpPrefix prefix) {
+    private synchronized Group deleteNextHop(IpPrefix prefix) {
         IpAddress nextHopIp = prefixToNextHop.remove(prefix);
         NextHop nextHop = nextHops.get(nextHopIp);
         if (nextHop == null) {
             log.warn("No next hop found when removing prefix {}", prefix);
-            return;
+            return null;
         }
 
+        Group group = groupService.getGroup(deviceId, nextHop.group());
+
         if (nextHopsCount.remove(nextHopIp, 1) <= 1) {
             // There was one or less next hops, so there are now none
 
@@ -305,6 +308,8 @@
 
             groupService.removeGroup(deviceId, nextHop.group(), appId);
         }
+
+        return group;
     }
 
     private class InternalFibListener implements FibListener {
diff --git a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilderVer13.java b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilderVer13.java
index cf4f490..dc6a857 100644
--- a/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilderVer13.java
+++ b/providers/openflow/flow/src/main/java/org/onosproject/provider/of/flow/impl/FlowModBuilderVer13.java
@@ -180,6 +180,7 @@
                 .setMatch(match)
                 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
                 .setPriority(flowRule().priority())
+                .setTableId(TableId.of(flowRule().type().ordinal()))
                 .build();
 
         return fm;