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; |
| 22 | import org.onosproject.incubator.net.routing.ResolvedRoute; |
| 23 | import org.onosproject.incubator.net.routing.RouteEvent; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | /** |
| 30 | * Handles RouteEvent and manages routing entries. |
| 31 | */ |
| 32 | public class RouteHandler { |
| 33 | private static final Logger log = LoggerFactory.getLogger(RouteHandler.class); |
| 34 | private final SegmentRoutingManager srManager; |
| 35 | |
| 36 | public RouteHandler(SegmentRoutingManager srManager) { |
| 37 | this.srManager = srManager; |
| 38 | } |
| 39 | |
| 40 | protected void init(DeviceId deviceId) { |
| 41 | srManager.routeService.getNextHops().forEach(nextHop -> { |
| 42 | if (nextHop.location().deviceId().equals(deviceId)) { |
| 43 | srManager.routeService.getRoutesForNextHop(nextHop.ip()).forEach(route -> { |
| 44 | ResolvedRoute resolvedRoute = |
| 45 | new ResolvedRoute(route, nextHop.mac(), nextHop.location()); |
| 46 | processRouteAddedInternal(resolvedRoute); |
| 47 | }); |
| 48 | } |
| 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(); |
| 60 | ConnectPoint location = route.location(); |
| 61 | |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 62 | srManager.deviceConfiguration.addSubnet(location, prefix); |
| 63 | srManager.defaultRoutingHandler.populateSubnet(location, ImmutableSet.of(prefix)); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 64 | srManager.routingRulePopulator.populateRoute(location.deviceId(), prefix, |
| 65 | nextHopMac, location.port()); |
| 66 | } |
| 67 | |
| 68 | protected void processRouteUpdated(RouteEvent event) { |
| 69 | log.info("processRouteUpdated {}", event); |
| 70 | processRouteRemovedInternal(event.prevSubject()); |
| 71 | processRouteAddedInternal(event.subject()); |
| 72 | } |
| 73 | |
| 74 | protected void processRouteRemoved(RouteEvent event) { |
| 75 | log.info("processRouteRemoved {}", event); |
| 76 | processRouteRemovedInternal(event.subject()); |
| 77 | } |
| 78 | |
| 79 | private void processRouteRemovedInternal(ResolvedRoute route) { |
| 80 | IpPrefix prefix = route.prefix(); |
| 81 | MacAddress nextHopMac = route.nextHopMac(); |
| 82 | ConnectPoint location = route.location(); |
| 83 | |
Pier Luigi | ad0a67f | 2017-02-03 13:34:21 -0800 | [diff] [blame] | 84 | srManager.deviceConfiguration.removeSubnet(location, prefix); |
| 85 | srManager.defaultRoutingHandler.revokeSubnet(ImmutableSet.of(prefix)); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 86 | srManager.routingRulePopulator.revokeRoute( |
| 87 | location.deviceId(), prefix, nextHopMac, location.port()); |
| 88 | } |
| 89 | } |