blob: 214ba6bde5935d74f297c402413cae6e5173a84b [file] [log] [blame]
Charles Chan03a73e02016-10-24 14:52:01 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chan03a73e02016-10-24 14:52:01 -07003 *
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
17package org.onosproject.segmentrouting;
18
19import com.google.common.collect.ImmutableSet;
Saurav Das7bcbe702017-06-13 15:35:54 -070020import com.google.common.collect.Sets;
21
Charles Chan03a73e02016-10-24 14:52:01 -070022import org.onlab.packet.IpPrefix;
23import org.onlab.packet.MacAddress;
Charles Chan7ffd81f2017-02-08 15:52:08 -080024import org.onlab.packet.VlanId;
Charles Chanb15d0102017-08-23 13:55:39 -070025import org.onosproject.net.ConnectPoint;
Ray Milkey69ec8712017-08-08 13:00:43 -070026import org.onosproject.routeservice.ResolvedRoute;
27import org.onosproject.routeservice.RouteEvent;
Charles Chan03a73e02016-10-24 14:52:01 -070028import org.onosproject.net.DeviceId;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
Charles Chanb8664b82017-06-22 14:15:05 -070032import java.util.Objects;
33
Charles Chan03a73e02016-10-24 14:52:01 -070034/**
35 * Handles RouteEvent and manages routing entries.
36 */
37public class RouteHandler {
38 private static final Logger log = LoggerFactory.getLogger(RouteHandler.class);
39 private final SegmentRoutingManager srManager;
40
Charles Chanb15d0102017-08-23 13:55:39 -070041 RouteHandler(SegmentRoutingManager srManager) {
Charles Chan03a73e02016-10-24 14:52:01 -070042 this.srManager = srManager;
43 }
44
45 protected void init(DeviceId deviceId) {
Charles Chanb15d0102017-08-23 13:55:39 -070046 srManager.routeService.getRouteTables().forEach(routeTableId ->
47 srManager.routeService.getRoutes(routeTableId).forEach(routeInfo ->
48 routeInfo.allRoutes().forEach(resolvedRoute ->
49 srManager.nextHopLocations(resolvedRoute).stream()
50 .filter(location -> deviceId.equals(location.deviceId()))
51 .forEach(location -> processRouteAddedInternal(resolvedRoute)
52 )
53 )
54 )
55 );
Charles Chan03a73e02016-10-24 14:52:01 -070056 }
57
Charles Chanb15d0102017-08-23 13:55:39 -070058 void processRouteAdded(RouteEvent event) {
Charles Chan03a73e02016-10-24 14:52:01 -070059 log.info("processRouteAdded {}", event);
60 processRouteAddedInternal(event.subject());
61 }
62
63 private void processRouteAddedInternal(ResolvedRoute route) {
Charles Chanb8664b82017-06-22 14:15:05 -070064 if (!isReady()) {
65 log.info("System is not ready. Skip adding route for {}", route.prefix());
66 return;
67 }
68
Charles Chan03a73e02016-10-24 14:52:01 -070069 IpPrefix prefix = route.prefix();
70 MacAddress nextHopMac = route.nextHopMac();
Charles Chana9f8ae42017-03-17 18:36:26 -070071 VlanId nextHopVlan = route.nextHopVlan();
Charles Chanb15d0102017-08-23 13:55:39 -070072 ConnectPoint location = srManager.nextHopLocations(route).stream().findFirst().orElse(null);
73
74 if (location == null) {
75 log.info("{} ignored. Cannot find nexthop location", prefix);
76 return;
77 }
Charles Chan03a73e02016-10-24 14:52:01 -070078
Pier Ventre968da122016-12-09 17:26:04 -080079 srManager.deviceConfiguration.addSubnet(location, prefix);
Saurav Das7bcbe702017-06-13 15:35:54 -070080 // XXX need to handle the case where there are two connectpoints
81 srManager.defaultRoutingHandler.populateSubnet(Sets.newHashSet(location),
82 Sets.newHashSet(prefix));
Charles Chan03a73e02016-10-24 14:52:01 -070083 srManager.routingRulePopulator.populateRoute(location.deviceId(), prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -080084 nextHopMac, nextHopVlan, location.port());
Charles Chan03a73e02016-10-24 14:52:01 -070085 }
86
Charles Chanb15d0102017-08-23 13:55:39 -070087 void processRouteUpdated(RouteEvent event) {
Charles Chan03a73e02016-10-24 14:52:01 -070088 log.info("processRouteUpdated {}", event);
89 processRouteRemovedInternal(event.prevSubject());
90 processRouteAddedInternal(event.subject());
91 }
92
Charles Chanb15d0102017-08-23 13:55:39 -070093 void processRouteRemoved(RouteEvent event) {
Charles Chan03a73e02016-10-24 14:52:01 -070094 log.info("processRouteRemoved {}", event);
95 processRouteRemovedInternal(event.subject());
96 }
97
98 private void processRouteRemovedInternal(ResolvedRoute route) {
Charles Chanb8664b82017-06-22 14:15:05 -070099 if (!isReady()) {
100 log.info("System is not ready. Skip removing route for {}", route.prefix());
101 return;
102 }
103
Charles Chan03a73e02016-10-24 14:52:01 -0700104 IpPrefix prefix = route.prefix();
105 MacAddress nextHopMac = route.nextHopMac();
Charles Chana9f8ae42017-03-17 18:36:26 -0700106 VlanId nextHopVlan = route.nextHopVlan();
Charles Chanb15d0102017-08-23 13:55:39 -0700107 ConnectPoint location = srManager.nextHopLocations(route).stream().findFirst().orElse(null);
108
109 if (location == null) {
110 log.info("{} ignored. Cannot find nexthop location", prefix);
111 return;
112 }
Charles Chan03a73e02016-10-24 14:52:01 -0700113
Pier Luigi721b6622017-02-03 13:34:21 -0800114 srManager.deviceConfiguration.removeSubnet(location, prefix);
115 srManager.defaultRoutingHandler.revokeSubnet(ImmutableSet.of(prefix));
Charles Chan03a73e02016-10-24 14:52:01 -0700116 srManager.routingRulePopulator.revokeRoute(
Charles Chan7ffd81f2017-02-08 15:52:08 -0800117 location.deviceId(), prefix, nextHopMac, nextHopVlan, location.port());
Charles Chan03a73e02016-10-24 14:52:01 -0700118 }
Charles Chanb8664b82017-06-22 14:15:05 -0700119
120 private boolean isReady() {
121 return Objects.nonNull(srManager.deviceConfiguration) &&
122 Objects.nonNull(srManager.defaultRoutingHandler) &&
123 Objects.nonNull(srManager.routingRulePopulator);
124 }
Charles Chan03a73e02016-10-24 14:52:01 -0700125}