Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.bgprouter; |
| 17 | |
| 18 | import com.google.common.collect.ConcurrentHashMultiset; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 19 | import com.google.common.collect.HashMultimap; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import com.google.common.collect.Multimap; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Multiset; |
| 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.onlab.packet.Ethernet; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 29 | import org.onlab.packet.MacAddress; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 30 | import org.onlab.packet.IpAddress; |
| 31 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.flow.DefaultFlowRule; |
| 36 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onosproject.net.flow.FlowRule; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 39 | import org.onosproject.net.flow.FlowRuleOperations; |
| 40 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 41 | import org.onosproject.net.flow.FlowRuleService; |
| 42 | import org.onosproject.net.flow.TrafficSelector; |
| 43 | import org.onosproject.net.flow.TrafficTreatment; |
| 44 | import org.onosproject.net.group.DefaultGroupBucket; |
| 45 | import org.onosproject.net.group.DefaultGroupDescription; |
| 46 | import org.onosproject.net.group.Group; |
| 47 | import org.onosproject.net.group.GroupBucket; |
| 48 | import org.onosproject.net.group.GroupBuckets; |
| 49 | import org.onosproject.net.group.GroupDescription; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 50 | import org.onosproject.net.group.GroupEvent; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 51 | import org.onosproject.net.group.GroupKey; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 52 | import org.onosproject.net.group.GroupListener; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 53 | import org.onosproject.net.group.GroupService; |
| 54 | import org.onosproject.net.packet.PacketService; |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 55 | import org.onosproject.routing.FibEntry; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 56 | import org.onosproject.routing.FibListener; |
| 57 | import org.onosproject.routing.FibUpdate; |
| 58 | import org.onosproject.routing.RoutingService; |
| 59 | import org.onosproject.routing.config.Interface; |
| 60 | import org.onosproject.routing.config.RoutingConfigurationService; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 61 | import org.slf4j.Logger; |
| 62 | import org.slf4j.LoggerFactory; |
| 63 | |
| 64 | import java.util.Collection; |
| 65 | import java.util.Collections; |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 66 | import java.util.Map; |
| 67 | |
| 68 | /** |
| 69 | * BgpRouter component. |
| 70 | */ |
| 71 | @Component(immediate = true) |
| 72 | public class BgpRouter { |
| 73 | |
| 74 | private static final Logger log = LoggerFactory.getLogger(BgpRouter.class); |
| 75 | |
| 76 | private static final String BGP_ROUTER_APP = "org.onosproject.bgprouter"; |
| 77 | |
| 78 | private static final int PRIORITY = 1; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected CoreService coreService; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected FlowRuleService flowService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected GroupService groupService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected RoutingService routingService; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected RoutingConfigurationService configService; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected PacketService packetService; |
| 97 | |
| 98 | private ApplicationId appId; |
| 99 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 100 | // Reference count for how many times a next hop is used by a route |
| 101 | private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create(); |
| 102 | |
| 103 | // Mapping from prefix to its current next hop |
| 104 | private final Map<IpPrefix, IpAddress> prefixToNextHop = Maps.newHashMap(); |
| 105 | |
| 106 | // Mapping from next hop IP to next hop object containing group info |
| 107 | private final Map<IpAddress, NextHop> nextHops = Maps.newHashMap(); |
| 108 | |
| 109 | // Stores FIB updates that are waiting for groups to be set up |
| 110 | private final Multimap<GroupKey, FibEntry> pendingUpdates = HashMultimap.create(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 111 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 112 | private DeviceId deviceId = DeviceId.deviceId("of:0000000000000001"); // TODO config |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 113 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 114 | private final GroupListener groupListener = new InternalGroupListener(); |
| 115 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 116 | private TunnellingConnectivityManager connectivityManager; |
| 117 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 118 | private InternalTableHandler provisionStaticTables = new InternalTableHandler(); |
| 119 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 120 | @Activate |
| 121 | protected void activate() { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 122 | appId = coreService.registerApplication(BGP_ROUTER_APP); |
| 123 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 124 | groupService.addListener(groupListener); |
| 125 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 126 | provisionStaticTables.provision(true); |
| 127 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 128 | connectivityManager = new TunnellingConnectivityManager(appId, |
| 129 | configService, |
| 130 | packetService); |
| 131 | |
| 132 | routingService.start(new InternalFibListener()); |
| 133 | |
| 134 | connectivityManager.start(); |
| 135 | |
| 136 | log.info("BgpRouter started"); |
| 137 | } |
| 138 | |
| 139 | @Deactivate |
| 140 | protected void deactivate() { |
| 141 | routingService.stop(); |
| 142 | connectivityManager.stop(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 143 | provisionStaticTables.provision(false); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 144 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 145 | groupService.removeListener(groupListener); |
| 146 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 147 | log.info("BgpRouter stopped"); |
| 148 | } |
| 149 | |
| 150 | private void updateFibEntry(Collection<FibUpdate> updates) { |
| 151 | for (FibUpdate update : updates) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 152 | FibEntry entry = update.entry(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 153 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 154 | addNextHop(entry); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 155 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 156 | Group group; |
| 157 | synchronized (pendingUpdates) { |
| 158 | NextHop nextHop = nextHops.get(entry.nextHopIp()); |
| 159 | group = groupService.getGroup(deviceId, nextHop.group()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 160 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 161 | if (group == null) { |
| 162 | log.debug("Adding pending flow {}", update.entry()); |
| 163 | pendingUpdates.put(nextHop.group(), update.entry()); |
| 164 | continue; |
| 165 | } |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 168 | installFlow(update.entry(), group); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 172 | private void installFlow(FibEntry entry, Group group) { |
| 173 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 174 | .matchEthType(Ethernet.TYPE_IPV4) |
| 175 | .matchIPDst(entry.prefix()) |
| 176 | .build(); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 177 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 178 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 179 | .group(group.id()) |
| 180 | .build(); |
| 181 | |
| 182 | FlowRule flowRule = new DefaultFlowRule(deviceId, selector, treatment, |
| 183 | PRIORITY, appId, 0, true, |
| 184 | FlowRule.Type.IP); |
| 185 | |
| 186 | flowService.applyFlowRules(flowRule); |
| 187 | } |
| 188 | |
| 189 | private synchronized void deleteFibEntry(Collection<FibUpdate> withdraws) { |
| 190 | for (FibUpdate update : withdraws) { |
| 191 | FibEntry entry = update.entry(); |
| 192 | |
| 193 | deleteNextHop(entry.prefix()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 194 | |
| 195 | TrafficSelector selector = DefaultTrafficSelector.builder() |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 196 | .matchEthType(Ethernet.TYPE_IPV4) |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 197 | .matchIPDst(update.entry().prefix()) |
| 198 | .build(); |
| 199 | |
| 200 | FlowRule flowRule = new DefaultFlowRule(deviceId, selector, null, |
| 201 | PRIORITY, appId, 0, true, |
| 202 | FlowRule.Type.IP); |
| 203 | |
| 204 | flowService.removeFlowRules(flowRule); |
| 205 | } |
| 206 | } |
| 207 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 208 | private synchronized void addNextHop(FibEntry entry) { |
| 209 | prefixToNextHop.put(entry.prefix(), entry.nextHopIp()); |
| 210 | if (nextHopsCount.count(entry.nextHopIp()) == 0) { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 211 | // There was no next hop in the multiset |
| 212 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 213 | Interface egressIntf = configService.getMatchingInterface(entry.nextHopIp()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 214 | if (egressIntf == null) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 215 | log.warn("no egress interface found for {}", entry); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 216 | return; |
| 217 | } |
| 218 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 219 | NextHopGroupKey groupKey = new NextHopGroupKey(entry.nextHopIp()); |
| 220 | |
| 221 | NextHop nextHop = new NextHop(entry.nextHopIp(), entry.nextHopMac(), groupKey); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 222 | |
| 223 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 224 | .setEthSrc(egressIntf.mac()) |
| 225 | .setEthDst(nextHop.mac()) |
| 226 | .setVlanId(egressIntf.vlan()) |
| 227 | .setOutput(egressIntf.connectPoint().port()) |
| 228 | .build(); |
| 229 | |
| 230 | GroupBucket bucket = DefaultGroupBucket.createIndirectGroupBucket(treatment); |
| 231 | |
| 232 | GroupDescription groupDescription |
| 233 | = new DefaultGroupDescription(deviceId, |
| 234 | GroupDescription.Type.INDIRECT, |
| 235 | new GroupBuckets(Collections |
| 236 | .singletonList(bucket)), |
| 237 | groupKey, |
| 238 | appId); |
| 239 | |
| 240 | groupService.addGroup(groupDescription); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 241 | |
| 242 | nextHops.put(nextHop.ip(), nextHop); |
| 243 | |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 244 | } |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 245 | |
| 246 | nextHopsCount.add(entry.nextHopIp()); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 249 | private synchronized void deleteNextHop(IpPrefix prefix) { |
| 250 | IpAddress nextHopIp = prefixToNextHop.remove(prefix); |
| 251 | NextHop nextHop = nextHops.get(nextHopIp); |
| 252 | if (nextHop == null) { |
| 253 | log.warn("No next hop found when removing prefix {}", prefix); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | if (nextHopsCount.remove(nextHopIp, 1) <= 1) { |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 258 | // There was one or less next hops, so there are now none |
| 259 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 260 | log.debug("removing group for next hop {}", nextHop); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 261 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 262 | nextHops.remove(nextHopIp); |
| 263 | |
| 264 | groupService.removeGroup(deviceId, nextHop.group(), appId); |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | private class InternalFibListener implements FibListener { |
| 269 | |
| 270 | @Override |
| 271 | public void update(Collection<FibUpdate> updates, |
| 272 | Collection<FibUpdate> withdraws) { |
| 273 | BgpRouter.this.deleteFibEntry(withdraws); |
| 274 | BgpRouter.this.updateFibEntry(updates); |
| 275 | } |
| 276 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 277 | |
| 278 | private class InternalTableHandler { |
| 279 | |
| 280 | private static final int CONTROLLER_PRIORITY = 255; |
| 281 | private static final int DROP_PRIORITY = 0; |
| 282 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 283 | public void provision(boolean install) { |
| 284 | |
| 285 | processTableZero(install); |
| 286 | processTableOne(install); |
| 287 | processTableTwo(install); |
| 288 | processTableThree(install); |
| 289 | processTableFive(install); |
| 290 | processTableSix(install); |
| 291 | processTableNine(install); |
| 292 | |
| 293 | } |
| 294 | |
| 295 | private void processTableZero(boolean install) { |
| 296 | TrafficSelector.Builder selector; |
| 297 | TrafficTreatment.Builder treatment; |
| 298 | |
| 299 | selector = DefaultTrafficSelector.builder(); |
| 300 | treatment = DefaultTrafficTreatment.builder(); |
| 301 | |
| 302 | selector.matchEthDst(MacAddress.BROADCAST); |
| 303 | treatment.transition(FlowRule.Type.VLAN_MPLS); |
| 304 | |
| 305 | FlowRule rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 306 | treatment.build(), |
| 307 | CONTROLLER_PRIORITY, appId, 0, |
| 308 | true); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 309 | |
| 310 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 311 | |
| 312 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 313 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 314 | //Drop rule |
| 315 | selector = DefaultTrafficSelector.builder(); |
| 316 | treatment = DefaultTrafficTreatment.builder(); |
| 317 | |
| 318 | treatment.drop(); |
| 319 | |
| 320 | rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 321 | treatment.build(), DROP_PRIORITY, appId, |
| 322 | 0, true, FlowRule.Type.VLAN_MPLS); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 323 | |
| 324 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 325 | |
| 326 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 327 | @Override |
| 328 | public void onSuccess(FlowRuleOperations ops) { |
| 329 | log.info("Provisioned default table for bgp router"); |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public void onError(FlowRuleOperations ops) { |
| 334 | log.info("Failed to provision default table for bgp router"); |
| 335 | } |
| 336 | })); |
| 337 | |
| 338 | } |
| 339 | |
| 340 | private void processTableOne(boolean install) { |
| 341 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 342 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 343 | .builder(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 344 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 345 | FlowRule rule; |
| 346 | |
| 347 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 348 | treatment.transition(FlowRule.Type.VLAN); |
| 349 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 350 | rule = new DefaultFlowRule(deviceId, selector.build(), treatment.build(), CONTROLLER_PRIORITY, |
| 351 | appId, 0, true, FlowRule.Type.VLAN_MPLS); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 352 | |
| 353 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 354 | |
| 355 | selector = DefaultTrafficSelector.builder(); |
| 356 | treatment = DefaultTrafficTreatment.builder(); |
| 357 | |
| 358 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 359 | treatment.transition(FlowRule.Type.VLAN); |
| 360 | |
| 361 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 362 | treatment.build(), CONTROLLER_PRIORITY, |
| 363 | appId, 0, true, FlowRule.Type.VLAN_MPLS); |
| 364 | |
| 365 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 366 | |
| 367 | selector = DefaultTrafficSelector.builder(); |
| 368 | treatment = DefaultTrafficTreatment.builder(); |
| 369 | |
| 370 | selector.matchEthType(Ethernet.TYPE_VLAN); |
| 371 | treatment.transition(FlowRule.Type.VLAN); |
| 372 | |
| 373 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 374 | treatment.build(), CONTROLLER_PRIORITY, |
| 375 | appId, 0, true, FlowRule.Type.VLAN_MPLS); |
| 376 | |
| 377 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 378 | |
| 379 | //Drop rule |
| 380 | selector = DefaultTrafficSelector.builder(); |
| 381 | treatment = DefaultTrafficTreatment.builder(); |
| 382 | |
| 383 | treatment.drop(); |
| 384 | |
| 385 | rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 386 | treatment.build(), DROP_PRIORITY, appId, |
| 387 | 0, true, FlowRule.Type.VLAN_MPLS); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 388 | |
| 389 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 390 | |
| 391 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 392 | @Override |
| 393 | public void onSuccess(FlowRuleOperations ops) { |
| 394 | log.info("Provisioned vlan/mpls table for bgp router"); |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public void onError(FlowRuleOperations ops) { |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 399 | log.info( |
| 400 | "Failed to provision vlan/mpls table for bgp router"); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 401 | } |
| 402 | })); |
| 403 | |
| 404 | } |
| 405 | |
| 406 | private void processTableTwo(boolean install) { |
| 407 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 408 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 409 | .builder(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 410 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 411 | FlowRule rule; |
| 412 | |
| 413 | //Drop rule |
| 414 | |
| 415 | treatment.drop(); |
| 416 | |
| 417 | rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 418 | treatment.build(), DROP_PRIORITY, appId, |
| 419 | 0, true, FlowRule.Type.VLAN); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 420 | |
| 421 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 422 | |
| 423 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 424 | @Override |
| 425 | public void onSuccess(FlowRuleOperations ops) { |
| 426 | log.info("Provisioned vlan table for bgp router"); |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public void onError(FlowRuleOperations ops) { |
| 431 | log.info("Failed to provision vlan table for bgp router"); |
| 432 | } |
| 433 | })); |
| 434 | } |
| 435 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 436 | private void processTableThree(boolean install) { |
| 437 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 438 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 439 | .builder(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 440 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 441 | FlowRule rule; |
| 442 | |
| 443 | selector.matchEthType(Ethernet.TYPE_ARP); |
| 444 | treatment.punt(); |
| 445 | |
| 446 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 447 | treatment.build(), CONTROLLER_PRIORITY, |
| 448 | appId, 0, true, FlowRule.Type.ETHER); |
| 449 | |
| 450 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 451 | |
| 452 | selector = DefaultTrafficSelector.builder(); |
| 453 | treatment = DefaultTrafficTreatment.builder(); |
| 454 | |
| 455 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 456 | treatment.transition(FlowRule.Type.COS); |
| 457 | |
| 458 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 459 | treatment.build(), CONTROLLER_PRIORITY, |
| 460 | appId, 0, true, FlowRule.Type.ETHER); |
| 461 | |
| 462 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 463 | |
| 464 | //Drop rule |
| 465 | selector = DefaultTrafficSelector.builder(); |
| 466 | treatment = DefaultTrafficTreatment.builder(); |
| 467 | |
| 468 | treatment.drop(); |
| 469 | |
| 470 | rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 471 | treatment.build(), DROP_PRIORITY, appId, |
| 472 | 0, true, FlowRule.Type.VLAN_MPLS); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 473 | |
| 474 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 475 | |
| 476 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 477 | @Override |
| 478 | public void onSuccess(FlowRuleOperations ops) { |
| 479 | log.info("Provisioned ether table for bgp router"); |
| 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public void onError(FlowRuleOperations ops) { |
| 484 | log.info("Failed to provision ether table for bgp router"); |
| 485 | } |
| 486 | })); |
| 487 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | private void processTableFive(boolean install) { |
| 491 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 492 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 493 | .builder(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 494 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 495 | FlowRule rule; |
| 496 | |
| 497 | treatment.transition(FlowRule.Type.IP); |
| 498 | |
| 499 | rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 500 | treatment.build(), DROP_PRIORITY, appId, |
| 501 | 0, true, FlowRule.Type.COS); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 502 | |
| 503 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 504 | |
| 505 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 506 | @Override |
| 507 | public void onSuccess(FlowRuleOperations ops) { |
| 508 | log.info("Provisioned cos table for bgp router"); |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public void onError(FlowRuleOperations ops) { |
| 513 | log.info("Failed to provision cos table for bgp router"); |
| 514 | } |
| 515 | })); |
| 516 | |
| 517 | } |
| 518 | |
| 519 | private void processTableSix(boolean install) { |
| 520 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 521 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 522 | .builder(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 523 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 524 | FlowRule rule; |
| 525 | |
| 526 | //Drop rule |
| 527 | |
| 528 | treatment.drop(); |
| 529 | |
| 530 | rule = new DefaultFlowRule(deviceId, selector.build(), |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 531 | treatment.build(), DROP_PRIORITY, appId, |
| 532 | 0, true, FlowRule.Type.IP); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 533 | |
| 534 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 535 | |
| 536 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 537 | @Override |
| 538 | public void onSuccess(FlowRuleOperations ops) { |
| 539 | log.info("Provisioned FIB table for bgp router"); |
| 540 | } |
| 541 | |
| 542 | @Override |
| 543 | public void onError(FlowRuleOperations ops) { |
| 544 | log.info("Failed to provision FIB table for bgp router"); |
| 545 | } |
| 546 | })); |
| 547 | } |
| 548 | |
| 549 | private void processTableNine(boolean install) { |
| 550 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 551 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 552 | .builder(); |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 553 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 554 | FlowRule rule; |
| 555 | |
| 556 | treatment.punt(); |
| 557 | |
| 558 | rule = new DefaultFlowRule(deviceId, selector.build(), |
| 559 | treatment.build(), CONTROLLER_PRIORITY, |
| 560 | appId, 0, true, FlowRule.Type.ACL); |
| 561 | |
| 562 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 563 | |
| 564 | flowService.apply(ops.build(new FlowRuleOperationsContext() { |
| 565 | @Override |
| 566 | public void onSuccess(FlowRuleOperations ops) { |
| 567 | log.info("Provisioned Local table for bgp router"); |
| 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public void onError(FlowRuleOperations ops) { |
| 572 | log.info("Failed to provision Local table for bgp router"); |
| 573 | } |
| 574 | })); |
| 575 | } |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 576 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 577 | |
Jonathan Hart | 7baba07 | 2015-02-23 14:27:59 -0800 | [diff] [blame^] | 578 | private class InternalGroupListener implements GroupListener { |
| 579 | |
| 580 | @Override |
| 581 | public void event(GroupEvent event) { |
| 582 | Group group = event.subject(); |
| 583 | |
| 584 | if (event.type() == GroupEvent.Type.GROUP_ADDED || |
| 585 | event.type() == GroupEvent.Type.GROUP_UPDATED) { |
| 586 | synchronized (pendingUpdates) { |
| 587 | pendingUpdates.removeAll(group.appCookie()) |
| 588 | .forEach((entry) -> installFlow(entry, group)); |
| 589 | } |
| 590 | } |
| 591 | } |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 592 | } |
Jonathan Hart | f582920 | 2015-02-12 09:37:02 -0800 | [diff] [blame] | 593 | } |