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