Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | import org.onlab.packet.MacAddress; |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 22 | import org.onlab.packet.VlanId; |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 23 | import org.onosproject.incubator.net.routing.ResolvedRoute; |
| 24 | import org.onosproject.incubator.net.routing.RouteEvent; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | /** |
| 31 | * Handles RouteEvent and manages routing entries. |
| 32 | */ |
| 33 | public class RouteHandler { |
| 34 | private static final Logger log = LoggerFactory.getLogger(RouteHandler.class); |
| 35 | private final SegmentRoutingManager srManager; |
| 36 | |
| 37 | public RouteHandler(SegmentRoutingManager srManager) { |
| 38 | this.srManager = srManager; |
| 39 | } |
| 40 | |
| 41 | protected void init(DeviceId deviceId) { |
Charles Chan | f924c65 | 2017-03-17 18:36:26 -0700 | [diff] [blame] | 42 | srManager.routeService.getRouteTables().forEach(routeTableId -> { |
| 43 | srManager.routeService.getRoutes(routeTableId).forEach(routeInfo -> { |
| 44 | routeInfo.allRoutes().stream() |
| 45 | .filter(resolvedRoute -> resolvedRoute.location() != null && |
| 46 | resolvedRoute.location().deviceId().equals(deviceId)) |
| 47 | .forEach(this::processRouteAddedInternal); |
| 48 | }); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 49 | }); |
| 50 | } |
| 51 | |
| 52 | protected void processRouteAdded(RouteEvent event) { |
| 53 | log.info("processRouteAdded {}", event); |
| 54 | processRouteAddedInternal(event.subject()); |
| 55 | } |
| 56 | |
| 57 | private void processRouteAddedInternal(ResolvedRoute route) { |
| 58 | IpPrefix prefix = route.prefix(); |
| 59 | MacAddress nextHopMac = route.nextHopMac(); |
Charles Chan | f924c65 | 2017-03-17 18:36:26 -0700 | [diff] [blame] | 60 | VlanId nextHopVlan = route.nextHopVlan(); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 61 | ConnectPoint location = route.location(); |
| 62 | |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 63 | srManager.deviceConfiguration.addSubnet(location, prefix); |
| 64 | srManager.defaultRoutingHandler.populateSubnet(location, ImmutableSet.of(prefix)); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 65 | srManager.routingRulePopulator.populateRoute(location.deviceId(), prefix, |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 66 | nextHopMac, nextHopVlan, location.port()); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | protected void processRouteUpdated(RouteEvent event) { |
| 70 | log.info("processRouteUpdated {}", event); |
| 71 | processRouteRemovedInternal(event.prevSubject()); |
| 72 | processRouteAddedInternal(event.subject()); |
| 73 | } |
| 74 | |
| 75 | protected void processRouteRemoved(RouteEvent event) { |
| 76 | log.info("processRouteRemoved {}", event); |
| 77 | processRouteRemovedInternal(event.subject()); |
| 78 | } |
| 79 | |
| 80 | private void processRouteRemovedInternal(ResolvedRoute route) { |
| 81 | IpPrefix prefix = route.prefix(); |
| 82 | MacAddress nextHopMac = route.nextHopMac(); |
Charles Chan | f924c65 | 2017-03-17 18:36:26 -0700 | [diff] [blame] | 83 | VlanId nextHopVlan = route.nextHopVlan(); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 84 | ConnectPoint location = route.location(); |
| 85 | |
Pier Luigi | ad0a67f | 2017-02-03 13:34:21 -0800 | [diff] [blame] | 86 | srManager.deviceConfiguration.removeSubnet(location, prefix); |
| 87 | srManager.defaultRoutingHandler.revokeSubnet(ImmutableSet.of(prefix)); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 88 | srManager.routingRulePopulator.revokeRoute( |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 89 | location.deviceId(), prefix, nextHopMac, nextHopVlan, location.port()); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 90 | } |
| 91 | } |