blob: c82b39b1e7f8a788e57b97d8f000a5e015bd24de [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;
Ray Milkey69ec8712017-08-08 13:00:43 -070025import org.onosproject.routeservice.ResolvedRoute;
26import org.onosproject.routeservice.RouteEvent;
Charles Chan03a73e02016-10-24 14:52:01 -070027import org.onosproject.net.ConnectPoint;
28import 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
41 public RouteHandler(SegmentRoutingManager srManager) {
42 this.srManager = srManager;
43 }
44
45 protected void init(DeviceId deviceId) {
Charles Chana9f8ae42017-03-17 18:36:26 -070046 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 Chan03a73e02016-10-24 14:52:01 -070053 });
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 Chanb8664b82017-06-22 14:15:05 -070062 if (!isReady()) {
63 log.info("System is not ready. Skip adding route for {}", route.prefix());
64 return;
65 }
66
Charles Chan03a73e02016-10-24 14:52:01 -070067 IpPrefix prefix = route.prefix();
68 MacAddress nextHopMac = route.nextHopMac();
Charles Chana9f8ae42017-03-17 18:36:26 -070069 VlanId nextHopVlan = route.nextHopVlan();
Charles Chan03a73e02016-10-24 14:52:01 -070070 ConnectPoint location = route.location();
71
Pier Ventre968da122016-12-09 17:26:04 -080072 srManager.deviceConfiguration.addSubnet(location, prefix);
Saurav Das7bcbe702017-06-13 15:35:54 -070073 // XXX need to handle the case where there are two connectpoints
74 srManager.defaultRoutingHandler.populateSubnet(Sets.newHashSet(location),
75 Sets.newHashSet(prefix));
Charles Chan03a73e02016-10-24 14:52:01 -070076 srManager.routingRulePopulator.populateRoute(location.deviceId(), prefix,
Charles Chan7ffd81f2017-02-08 15:52:08 -080077 nextHopMac, nextHopVlan, location.port());
Charles Chan03a73e02016-10-24 14:52:01 -070078 }
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 Chanb8664b82017-06-22 14:15:05 -070092 if (!isReady()) {
93 log.info("System is not ready. Skip removing route for {}", route.prefix());
94 return;
95 }
96
Charles Chan03a73e02016-10-24 14:52:01 -070097 IpPrefix prefix = route.prefix();
98 MacAddress nextHopMac = route.nextHopMac();
Charles Chana9f8ae42017-03-17 18:36:26 -070099 VlanId nextHopVlan = route.nextHopVlan();
Charles Chan03a73e02016-10-24 14:52:01 -0700100 ConnectPoint location = route.location();
101
Pier Luigi721b6622017-02-03 13:34:21 -0800102 srManager.deviceConfiguration.removeSubnet(location, prefix);
103 srManager.defaultRoutingHandler.revokeSubnet(ImmutableSet.of(prefix));
Charles Chan03a73e02016-10-24 14:52:01 -0700104 srManager.routingRulePopulator.revokeRoute(
Charles Chan7ffd81f2017-02-08 15:52:08 -0800105 location.deviceId(), prefix, nextHopMac, nextHopVlan, location.port());
Charles Chan03a73e02016-10-24 14:52:01 -0700106 }
Charles Chanb8664b82017-06-22 14:15:05 -0700107
108 private boolean isReady() {
109 return Objects.nonNull(srManager.deviceConfiguration) &&
110 Objects.nonNull(srManager.defaultRoutingHandler) &&
111 Objects.nonNull(srManager.routingRulePopulator);
112 }
Charles Chan03a73e02016-10-24 14:52:01 -0700113}