blob: 3288d7d46646e977c8d7b01dda67e3db46d239e9 [file] [log] [blame]
Jonathan Hartbb782be2017-02-09 17:45:49 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jonathan Hartbb782be2017-02-09 17:45:49 -08003 *
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.reactive.routing;
18
19import com.google.common.collect.ImmutableSet;
20import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
21import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
22import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
Jonathan Hartbb782be2017-02-09 17:45:49 -080023import org.onlab.packet.Ip4Address;
24import org.onlab.packet.Ip6Address;
25import org.onlab.packet.IpAddress;
26import org.onlab.packet.IpPrefix;
27import org.onlab.packet.MacAddress;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
Jonathan Hartbb782be2017-02-09 17:45:49 -080030import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.config.ConfigFactory;
32import org.onosproject.net.config.NetworkConfigEvent;
33import org.onosproject.net.config.NetworkConfigListener;
34import org.onosproject.net.config.NetworkConfigRegistry;
35import org.onosproject.net.config.NetworkConfigService;
36import org.onosproject.net.config.basics.SubjectFactories;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037import org.onosproject.net.intf.Interface;
38import org.onosproject.net.intf.InterfaceService;
Jonathan Hartbb782be2017-02-09 17:45:49 -080039import org.onosproject.routing.RoutingService;
40import org.onosproject.routing.config.BgpConfig;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041import org.osgi.service.component.annotations.Activate;
42import org.osgi.service.component.annotations.Component;
43import org.osgi.service.component.annotations.Deactivate;
44import org.osgi.service.component.annotations.Reference;
45import org.osgi.service.component.annotations.ReferenceCardinality;
Jonathan Hartbb782be2017-02-09 17:45:49 -080046import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
48
49import java.util.HashSet;
50import java.util.Objects;
51import java.util.Set;
52import java.util.stream.Collectors;
53
Ray Milkey69ec8712017-08-08 13:00:43 -070054import static org.onosproject.routeservice.RouteTools.createBinaryString;
Jonathan Hartc4c2d622017-02-10 14:13:57 -080055
Jonathan Hartbb782be2017-02-09 17:45:49 -080056/**
57 * Reactive routing configuration manager.
58 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070059@Component(immediate = true, service = ReactiveRoutingConfigurationService.class)
Jonathan Hartbb782be2017-02-09 17:45:49 -080060public class ReactiveRoutingConfiguration implements
61 ReactiveRoutingConfigurationService {
62
63 private final Logger log = LoggerFactory.getLogger(getClass());
64
Ray Milkeyd84f89b2018-08-17 14:54:17 -070065 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartbb782be2017-02-09 17:45:49 -080066 protected NetworkConfigRegistry registry;
67
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartbb782be2017-02-09 17:45:49 -080069 protected NetworkConfigService configService;
70
Ray Milkeyd84f89b2018-08-17 14:54:17 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartbb782be2017-02-09 17:45:49 -080072 protected CoreService coreService;
73
Ray Milkeyd84f89b2018-08-17 14:54:17 -070074 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartbb782be2017-02-09 17:45:49 -080075 protected InterfaceService interfaceService;
76
77 private Set<IpAddress> gatewayIpAddresses = new HashSet<>();
78 private Set<ConnectPoint> bgpPeerConnectPoints = new HashSet<>();
79
80 private InvertedRadixTree<LocalIpPrefixEntry>
81 localPrefixTable4 = new ConcurrentInvertedRadixTree<>(
82 new DefaultByteArrayNodeFactory());
83 private InvertedRadixTree<LocalIpPrefixEntry>
84 localPrefixTable6 = new ConcurrentInvertedRadixTree<>(
85 new DefaultByteArrayNodeFactory());
86
87 private MacAddress virtualGatewayMacAddress;
88 private final InternalNetworkConfigListener configListener =
89 new InternalNetworkConfigListener();
90
91 private ConfigFactory<ApplicationId, ReactiveRoutingConfig>
92 reactiveRoutingConfigFactory =
93 new ConfigFactory<ApplicationId, ReactiveRoutingConfig>(
94 SubjectFactories.APP_SUBJECT_FACTORY,
95 ReactiveRoutingConfig.class, "reactiveRouting") {
96 @Override
97 public ReactiveRoutingConfig createConfig() {
98 return new ReactiveRoutingConfig();
99 }
100 };
101
102 @Activate
103 public void activate() {
104 configService.addListener(configListener);
105 registry.registerConfigFactory(reactiveRoutingConfigFactory);
106 setUpConfiguration();
107 log.info("Reactive routing configuration service started");
108 }
109
110 @Deactivate
111 public void deactivate() {
112 registry.unregisterConfigFactory(reactiveRoutingConfigFactory);
113 configService.removeListener(configListener);
114 log.info("Reactive routing configuration service stopped");
115 }
116
117 /**
118 * Set up reactive routing information from configuration.
119 */
120 private void setUpConfiguration() {
121 ReactiveRoutingConfig config = configService.getConfig(
122 coreService.registerApplication(ReactiveRoutingConfigurationService
123 .REACTIVE_ROUTING_APP_ID),
124 ReactiveRoutingConfigurationService.CONFIG_CLASS);
125 if (config == null) {
126 log.warn("No reactive routing config available!");
127 return;
128 }
129 for (LocalIpPrefixEntry entry : config.localIp4PrefixEntries()) {
130 localPrefixTable4.put(createBinaryString(entry.ipPrefix()), entry);
131 gatewayIpAddresses.add(entry.getGatewayIpAddress());
Yuta HIGUCHI1a1ba652018-03-29 08:25:29 -0700132 log.info("adding local IPv4 entry: {} {}", entry.ipPrefix(), entry.getGatewayIpAddress());
Jonathan Hartbb782be2017-02-09 17:45:49 -0800133 }
134 for (LocalIpPrefixEntry entry : config.localIp6PrefixEntries()) {
135 localPrefixTable6.put(createBinaryString(entry.ipPrefix()), entry);
136 gatewayIpAddresses.add(entry.getGatewayIpAddress());
Yuta HIGUCHI1a1ba652018-03-29 08:25:29 -0700137 log.info("adding local IPv6 entry: {} {}", entry.ipPrefix(), entry.getGatewayIpAddress());
Jonathan Hartbb782be2017-02-09 17:45:49 -0800138 }
139
140 virtualGatewayMacAddress = config.virtualGatewayMacAddress();
Yuta HIGUCHI1a1ba652018-03-29 08:25:29 -0700141 log.info("virtual gateway MAC: {}", virtualGatewayMacAddress);
Jonathan Hartbb782be2017-02-09 17:45:49 -0800142
143 // Setup BGP peer connect points
144 ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
145 if (routerAppId == null) {
146 log.info("Router application ID is null!");
147 return;
148 }
149
150 BgpConfig bgpConfig = configService.getConfig(routerAppId, BgpConfig.class);
151
152 if (bgpConfig == null) {
153 log.info("BGP config is null!");
154 return;
155 } else {
156 bgpPeerConnectPoints =
157 bgpConfig.bgpSpeakers().stream()
158 .flatMap(speaker -> speaker.peers().stream())
159 .map(peer -> interfaceService.getMatchingInterface(peer))
160 .filter(Objects::nonNull)
Yuta HIGUCHI1a1ba652018-03-29 08:25:29 -0700161 .map(Interface::connectPoint)
Jonathan Hartbb782be2017-02-09 17:45:49 -0800162 .collect(Collectors.toSet());
163 }
164 }
165
166 @Override
167 public boolean isIpAddressLocal(IpAddress ipAddress) {
168 if (ipAddress.isIp4()) {
169 return localPrefixTable4.getValuesForKeysPrefixing(
170 createBinaryString(
171 IpPrefix.valueOf(ipAddress, Ip4Address.BIT_LENGTH)))
172 .iterator().hasNext();
173 } else {
174 return localPrefixTable6.getValuesForKeysPrefixing(
175 createBinaryString(
176 IpPrefix.valueOf(ipAddress, Ip6Address.BIT_LENGTH)))
177 .iterator().hasNext();
178 }
179 }
180
181 @Override
182 public boolean isIpPrefixLocal(IpPrefix ipPrefix) {
183 return (localPrefixTable4.getValueForExactKey(
184 createBinaryString(ipPrefix)) != null ||
185 localPrefixTable6.getValueForExactKey(
186 createBinaryString(ipPrefix)) != null);
187 }
188
189 @Override
190 public boolean isVirtualGatewayIpAddress(IpAddress ipAddress) {
191 return gatewayIpAddresses.contains(ipAddress);
192 }
193
194 @Override
195 public MacAddress getVirtualGatewayMacAddress() {
196 return virtualGatewayMacAddress;
197 }
198
199 @Override
200 public Set<ConnectPoint> getBgpPeerConnectPoints() {
201 return ImmutableSet.copyOf(bgpPeerConnectPoints);
202 }
203
Jonathan Hartbb782be2017-02-09 17:45:49 -0800204 private class InternalNetworkConfigListener implements NetworkConfigListener {
205
206 @Override
207 public void event(NetworkConfigEvent event) {
208 switch (event.type()) {
209 case CONFIG_REGISTERED:
210 break;
211 case CONFIG_UNREGISTERED:
212 break;
213 case CONFIG_ADDED:
214 case CONFIG_UPDATED:
215 case CONFIG_REMOVED:
216 if (event.configClass() == ReactiveRoutingConfigurationService.CONFIG_CLASS) {
217 setUpConfiguration();
218 }
219 break;
220 default:
221 break;
222 }
223 }
224 }
225}