blob: 694e1b0e2f1150d5f50f42f27da5728b4410beae [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;
22import org.onosproject.incubator.net.routing.ResolvedRoute;
23import org.onosproject.incubator.net.routing.RouteEvent;
24import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.DeviceId;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29/**
30 * Handles RouteEvent and manages routing entries.
31 */
32public 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
62 srManager.deviceConfiguration.addSubnet(location, prefix.getIp4Prefix());
63 srManager.defaultRoutingHandler.populateSubnet(location, ImmutableSet.of(prefix.getIp4Prefix()));
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
84 srManager.deviceConfiguration.removeSubnet(location, prefix.getIp4Prefix());
85 srManager.defaultRoutingHandler.revokeSubnet(ImmutableSet.of(prefix.getIp4Prefix()));
86 srManager.routingRulePopulator.revokeRoute(
87 location.deviceId(), prefix, nextHopMac, location.port());
88 }
89}