blob: 49485bb49c354e4518a007a25ca8fe2080d7351a [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;
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
Charles Chanb8664b82017-06-22 14:15:05 -070030import java.util.Objects;
31
Charles Chan03a73e02016-10-24 14:52:01 -070032/**
33 * Handles RouteEvent and manages routing entries.
34 */
35public 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 Chana9f8ae42017-03-17 18:36:26 -070044 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 Chan03a73e02016-10-24 14:52:01 -070051 });
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 Chanb8664b82017-06-22 14:15:05 -070060 if (!isReady()) {
61 log.info("System is not ready. Skip adding route for {}", route.prefix());
62 return;
63 }
64
Charles Chan03a73e02016-10-24 14:52:01 -070065 IpPrefix prefix = route.prefix();
66 MacAddress nextHopMac = route.nextHopMac();
Charles Chana9f8ae42017-03-17 18:36:26 -070067 VlanId nextHopVlan = route.nextHopVlan();
Charles Chan03a73e02016-10-24 14:52:01 -070068 ConnectPoint location = route.location();
69
Pier Ventre968da122016-12-09 17:26:04 -080070 srManager.deviceConfiguration.addSubnet(location, prefix);
71 srManager.defaultRoutingHandler.populateSubnet(location, ImmutableSet.of(prefix));
Charles Chan03a73e02016-10-24 14:52:01 -070072 srManager.routingRulePopulator.populateRoute(location.deviceId(), prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -080073 nextHopMac, nextHopVlan, location.port());
Charles Chan03a73e02016-10-24 14:52:01 -070074 }
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 Chanb8664b82017-06-22 14:15:05 -070088 if (!isReady()) {
89 log.info("System is not ready. Skip removing route for {}", route.prefix());
90 return;
91 }
92
Charles Chan03a73e02016-10-24 14:52:01 -070093 IpPrefix prefix = route.prefix();
94 MacAddress nextHopMac = route.nextHopMac();
Charles Chana9f8ae42017-03-17 18:36:26 -070095 VlanId nextHopVlan = route.nextHopVlan();
Charles Chan03a73e02016-10-24 14:52:01 -070096 ConnectPoint location = route.location();
97
Pier Luigi721b6622017-02-03 13:34:21 -080098 srManager.deviceConfiguration.removeSubnet(location, prefix);
99 srManager.defaultRoutingHandler.revokeSubnet(ImmutableSet.of(prefix));
Charles Chan03a73e02016-10-24 14:52:01 -0700100 srManager.routingRulePopulator.revokeRoute(
Charles Chan7ffd81f2017-02-08 15:52:08 -0800101 location.deviceId(), prefix, nextHopMac, nextHopVlan, location.port());
Charles Chan03a73e02016-10-24 14:52:01 -0700102 }
Charles Chanb8664b82017-06-22 14:15:05 -0700103
104 private boolean isReady() {
105 return Objects.nonNull(srManager.deviceConfiguration) &&
106 Objects.nonNull(srManager.defaultRoutingHandler) &&
107 Objects.nonNull(srManager.routingRulePopulator);
108 }
Charles Chan03a73e02016-10-24 14:52:01 -0700109}