blob: d97b5ac9be1ed5185a1b034538cdf2fa05adb720 [file] [log] [blame]
Charles Chan03a73e02016-10-24 14:52:01 -07001/*
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
17package org.onosproject.segmentrouting;
18
19import com.google.common.collect.ImmutableSet;
20import org.onlab.packet.IpPrefix;
21import org.onlab.packet.MacAddress;
Charles Chan7ffd81f2017-02-08 15:52:08 -080022import org.onlab.packet.VlanId;
Charles Chan03a73e02016-10-24 14:52:01 -070023import org.onosproject.incubator.net.routing.ResolvedRoute;
24import org.onosproject.incubator.net.routing.RouteEvent;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DeviceId;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30/**
31 * Handles RouteEvent and manages routing entries.
32 */
33public 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 Chana9f8ae42017-03-17 18:36:26 -070042 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 Chan03a73e02016-10-24 14:52:01 -070049 });
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 Chana9f8ae42017-03-17 18:36:26 -070060 VlanId nextHopVlan = route.nextHopVlan();
Charles Chan03a73e02016-10-24 14:52:01 -070061 ConnectPoint location = route.location();
62
Pier Ventre968da122016-12-09 17:26:04 -080063 srManager.deviceConfiguration.addSubnet(location, prefix);
64 srManager.defaultRoutingHandler.populateSubnet(location, ImmutableSet.of(prefix));
Charles Chan03a73e02016-10-24 14:52:01 -070065 srManager.routingRulePopulator.populateRoute(location.deviceId(), prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -080066 nextHopMac, nextHopVlan, location.port());
Charles Chan03a73e02016-10-24 14:52:01 -070067 }
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 Chana9f8ae42017-03-17 18:36:26 -070083 VlanId nextHopVlan = route.nextHopVlan();
Charles Chan03a73e02016-10-24 14:52:01 -070084 ConnectPoint location = route.location();
85
Pier Luigi721b6622017-02-03 13:34:21 -080086 srManager.deviceConfiguration.removeSubnet(location, prefix);
87 srManager.defaultRoutingHandler.revokeSubnet(ImmutableSet.of(prefix));
Charles Chan03a73e02016-10-24 14:52:01 -070088 srManager.routingRulePopulator.revokeRoute(
Charles Chan7ffd81f2017-02-08 15:52:08 -080089 location.deviceId(), prefix, nextHopMac, nextHopVlan, location.port());
Charles Chan03a73e02016-10-24 14:52:01 -070090 }
91}