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 | |
Charles Chan | ee8dbf8 | 2017-06-22 14:15:05 -0700 | [diff] [blame] | 30 | import java.util.Objects; |
| 31 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Handles RouteEvent and manages routing entries. |
| 34 | */ |
| 35 | public class RouteHandler { |
| 36 | private static final Logger log = LoggerFactory.getLogger(RouteHandler.class); |
| 37 | private final SegmentRoutingManager srManager; |
| 38 | |
| 39 | public RouteHandler(SegmentRoutingManager srManager) { |
| 40 | this.srManager = srManager; |
| 41 | } |
| 42 | |
| 43 | protected void init(DeviceId deviceId) { |
Charles Chan | f924c65 | 2017-03-17 18:36:26 -0700 | [diff] [blame] | 44 | srManager.routeService.getRouteTables().forEach(routeTableId -> { |
| 45 | srManager.routeService.getRoutes(routeTableId).forEach(routeInfo -> { |
| 46 | routeInfo.allRoutes().stream() |
| 47 | .filter(resolvedRoute -> resolvedRoute.location() != null && |
| 48 | resolvedRoute.location().deviceId().equals(deviceId)) |
| 49 | .forEach(this::processRouteAddedInternal); |
| 50 | }); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 51 | }); |
| 52 | } |
| 53 | |
| 54 | protected void processRouteAdded(RouteEvent event) { |
| 55 | log.info("processRouteAdded {}", event); |
| 56 | processRouteAddedInternal(event.subject()); |
| 57 | } |
| 58 | |
| 59 | private void processRouteAddedInternal(ResolvedRoute route) { |
Charles Chan | ee8dbf8 | 2017-06-22 14:15:05 -0700 | [diff] [blame] | 60 | if (!isReady()) { |
| 61 | log.info("System is not ready. Skip adding route for {}", route.prefix()); |
| 62 | return; |
| 63 | } |
| 64 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 65 | IpPrefix prefix = route.prefix(); |
| 66 | MacAddress nextHopMac = route.nextHopMac(); |
Charles Chan | f924c65 | 2017-03-17 18:36:26 -0700 | [diff] [blame] | 67 | VlanId nextHopVlan = route.nextHopVlan(); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 68 | ConnectPoint location = route.location(); |
| 69 | |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 70 | srManager.deviceConfiguration.addSubnet(location, prefix); |
| 71 | srManager.defaultRoutingHandler.populateSubnet(location, ImmutableSet.of(prefix)); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 72 | srManager.routingRulePopulator.populateRoute(location.deviceId(), prefix, |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 73 | nextHopMac, nextHopVlan, location.port()); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | protected void processRouteUpdated(RouteEvent event) { |
| 77 | log.info("processRouteUpdated {}", event); |
| 78 | processRouteRemovedInternal(event.prevSubject()); |
| 79 | processRouteAddedInternal(event.subject()); |
| 80 | } |
| 81 | |
| 82 | protected void processRouteRemoved(RouteEvent event) { |
| 83 | log.info("processRouteRemoved {}", event); |
| 84 | processRouteRemovedInternal(event.subject()); |
| 85 | } |
| 86 | |
| 87 | private void processRouteRemovedInternal(ResolvedRoute route) { |
Charles Chan | ee8dbf8 | 2017-06-22 14:15:05 -0700 | [diff] [blame] | 88 | if (!isReady()) { |
| 89 | log.info("System is not ready. Skip removing route for {}", route.prefix()); |
| 90 | return; |
| 91 | } |
| 92 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 93 | IpPrefix prefix = route.prefix(); |
| 94 | MacAddress nextHopMac = route.nextHopMac(); |
Charles Chan | f924c65 | 2017-03-17 18:36:26 -0700 | [diff] [blame] | 95 | VlanId nextHopVlan = route.nextHopVlan(); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 96 | ConnectPoint location = route.location(); |
| 97 | |
Pier Luigi | ad0a67f | 2017-02-03 13:34:21 -0800 | [diff] [blame] | 98 | srManager.deviceConfiguration.removeSubnet(location, prefix); |
| 99 | srManager.defaultRoutingHandler.revokeSubnet(ImmutableSet.of(prefix)); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 100 | srManager.routingRulePopulator.revokeRoute( |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 101 | location.deviceId(), prefix, nextHopMac, nextHopVlan, location.port()); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 102 | } |
Charles Chan | ee8dbf8 | 2017-06-22 14:15:05 -0700 | [diff] [blame] | 103 | |
| 104 | private boolean isReady() { |
| 105 | return Objects.nonNull(srManager.deviceConfiguration) && |
| 106 | Objects.nonNull(srManager.defaultRoutingHandler) && |
| 107 | Objects.nonNull(srManager.routingRulePopulator); |
| 108 | } |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 109 | } |