Jonathan Hart

BgpRouter: Added treatment to delete flows

Deleting routes now works.

Change-Id: I7ed682474e571aa2f402a1762a2037190c2d9284
...@@ -211,18 +211,7 @@ public class BgpRouter { ...@@ -211,18 +211,7 @@ public class BgpRouter {
211 } 211 }
212 212
213 private void installFlow(FibEntry entry, Group group) { 213 private void installFlow(FibEntry entry, Group group) {
214 - TrafficSelector selector = DefaultTrafficSelector.builder() 214 + FlowRule flowRule = generateRibFlowRule(entry.prefix(), group);
215 - .matchEthType(Ethernet.TYPE_IPV4)
216 - .matchIPDst(entry.prefix())
217 - .build();
218 -
219 - TrafficTreatment treatment = DefaultTrafficTreatment.builder()
220 - .group(group.id())
221 - .build();
222 -
223 - FlowRule flowRule = new DefaultFlowRule(deviceId, selector, treatment,
224 - PRIORITY, appId, 0, true,
225 - FlowRule.Type.IP);
226 215
227 flowService.applyFlowRules(flowRule); 216 flowService.applyFlowRules(flowRule);
228 } 217 }
...@@ -231,21 +220,33 @@ public class BgpRouter { ...@@ -231,21 +220,33 @@ public class BgpRouter {
231 for (FibUpdate update : withdraws) { 220 for (FibUpdate update : withdraws) {
232 FibEntry entry = update.entry(); 221 FibEntry entry = update.entry();
233 222
234 - deleteNextHop(entry.prefix()); 223 + Group group = deleteNextHop(entry.prefix());
235 - 224 + if (group == null) {
236 - TrafficSelector selector = DefaultTrafficSelector.builder() 225 + log.warn("Group not found when deleting {}", entry);
237 - .matchEthType(Ethernet.TYPE_IPV4) 226 + return;
238 - .matchIPDst(update.entry().prefix()) 227 + }
239 - .build();
240 228
241 - FlowRule flowRule = new DefaultFlowRule(deviceId, selector, null, 229 + FlowRule flowRule = generateRibFlowRule(entry.prefix(), group);
242 - PRIORITY, appId, 0, true,
243 - FlowRule.Type.IP);
244 230
245 flowService.removeFlowRules(flowRule); 231 flowService.removeFlowRules(flowRule);
246 } 232 }
247 } 233 }
248 234
235 + private FlowRule generateRibFlowRule(IpPrefix prefix, Group group) {
236 + TrafficSelector selector = DefaultTrafficSelector.builder()
237 + .matchEthType(Ethernet.TYPE_IPV4)
238 + .matchIPDst(prefix)
239 + .build();
240 +
241 + TrafficTreatment treatment = DefaultTrafficTreatment.builder()
242 + .group(group.id())
243 + .build();
244 +
245 + return new DefaultFlowRule(deviceId, selector, treatment,
246 + PRIORITY, appId, 0, true,
247 + FlowRule.Type.IP);
248 + }
249 +
249 private synchronized void addNextHop(FibEntry entry) { 250 private synchronized void addNextHop(FibEntry entry) {
250 prefixToNextHop.put(entry.prefix(), entry.nextHopIp()); 251 prefixToNextHop.put(entry.prefix(), entry.nextHopIp());
251 if (nextHopsCount.count(entry.nextHopIp()) == 0) { 252 if (nextHopsCount.count(entry.nextHopIp()) == 0) {
...@@ -288,14 +289,16 @@ public class BgpRouter { ...@@ -288,14 +289,16 @@ public class BgpRouter {
288 nextHopsCount.add(entry.nextHopIp()); 289 nextHopsCount.add(entry.nextHopIp());
289 } 290 }
290 291
291 - private synchronized void deleteNextHop(IpPrefix prefix) { 292 + private synchronized Group deleteNextHop(IpPrefix prefix) {
292 IpAddress nextHopIp = prefixToNextHop.remove(prefix); 293 IpAddress nextHopIp = prefixToNextHop.remove(prefix);
293 NextHop nextHop = nextHops.get(nextHopIp); 294 NextHop nextHop = nextHops.get(nextHopIp);
294 if (nextHop == null) { 295 if (nextHop == null) {
295 log.warn("No next hop found when removing prefix {}", prefix); 296 log.warn("No next hop found when removing prefix {}", prefix);
296 - return; 297 + return null;
297 } 298 }
298 299
300 + Group group = groupService.getGroup(deviceId, nextHop.group());
301 +
299 if (nextHopsCount.remove(nextHopIp, 1) <= 1) { 302 if (nextHopsCount.remove(nextHopIp, 1) <= 1) {
300 // There was one or less next hops, so there are now none 303 // There was one or less next hops, so there are now none
301 304
...@@ -305,6 +308,8 @@ public class BgpRouter { ...@@ -305,6 +308,8 @@ public class BgpRouter {
305 308
306 groupService.removeGroup(deviceId, nextHop.group(), appId); 309 groupService.removeGroup(deviceId, nextHop.group(), appId);
307 } 310 }
311 +
312 + return group;
308 } 313 }
309 314
310 private class InternalFibListener implements FibListener { 315 private class InternalFibListener implements FibListener {
......
...@@ -180,6 +180,7 @@ public class FlowModBuilderVer13 extends FlowModBuilder { ...@@ -180,6 +180,7 @@ public class FlowModBuilderVer13 extends FlowModBuilder {
180 .setMatch(match) 180 .setMatch(match)
181 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) 181 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
182 .setPriority(flowRule().priority()) 182 .setPriority(flowRule().priority())
183 + .setTableId(TableId.of(flowRule().type().ordinal()))
183 .build(); 184 .build();
184 185
185 return fm; 186 return fm;
......