blob: 8d010369ad12eabd518943f407f7d77e9d0a4649 [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;
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
28import org.apache.felix.scr.annotations.Service;
29import org.onlab.packet.Ip4Address;
30import org.onlab.packet.Ip6Address;
31import org.onlab.packet.IpAddress;
32import org.onlab.packet.IpPrefix;
33import org.onlab.packet.MacAddress;
34import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
Yuta HIGUCHI1a1ba652018-03-29 08:25:29 -070036import org.onosproject.net.intf.Interface;
Ray Milkeyfacf2862017-08-03 11:58:29 -070037import org.onosproject.net.intf.InterfaceService;
Jonathan Hartbb782be2017-02-09 17:45:49 -080038import org.onosproject.net.ConnectPoint;
39import org.onosproject.net.config.ConfigFactory;
40import org.onosproject.net.config.NetworkConfigEvent;
41import org.onosproject.net.config.NetworkConfigListener;
42import org.onosproject.net.config.NetworkConfigRegistry;
43import org.onosproject.net.config.NetworkConfigService;
44import org.onosproject.net.config.basics.SubjectFactories;
45import org.onosproject.routing.RoutingService;
46import org.onosproject.routing.config.BgpConfig;
47import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
50import java.util.HashSet;
51import java.util.Objects;
52import java.util.Set;
53import java.util.stream.Collectors;
54
Ray Milkey69ec8712017-08-08 13:00:43 -070055import static org.onosproject.routeservice.RouteTools.createBinaryString;
Jonathan Hartc4c2d622017-02-10 14:13:57 -080056
Jonathan Hartbb782be2017-02-09 17:45:49 -080057/**
58 * Reactive routing configuration manager.
59 */
60@Component(immediate = true)
61@Service
62public class ReactiveRoutingConfiguration implements
63 ReactiveRoutingConfigurationService {
64
65 private final Logger log = LoggerFactory.getLogger(getClass());
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected NetworkConfigRegistry registry;
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected NetworkConfigService configService;
72
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected CoreService coreService;
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected InterfaceService interfaceService;
78
79 private Set<IpAddress> gatewayIpAddresses = new HashSet<>();
80 private Set<ConnectPoint> bgpPeerConnectPoints = new HashSet<>();
81
82 private InvertedRadixTree<LocalIpPrefixEntry>
83 localPrefixTable4 = new ConcurrentInvertedRadixTree<>(
84 new DefaultByteArrayNodeFactory());
85 private InvertedRadixTree<LocalIpPrefixEntry>
86 localPrefixTable6 = new ConcurrentInvertedRadixTree<>(
87 new DefaultByteArrayNodeFactory());
88
89 private MacAddress virtualGatewayMacAddress;
90 private final InternalNetworkConfigListener configListener =
91 new InternalNetworkConfigListener();
92
93 private ConfigFactory<ApplicationId, ReactiveRoutingConfig>
94 reactiveRoutingConfigFactory =
95 new ConfigFactory<ApplicationId, ReactiveRoutingConfig>(
96 SubjectFactories.APP_SUBJECT_FACTORY,
97 ReactiveRoutingConfig.class, "reactiveRouting") {
98 @Override
99 public ReactiveRoutingConfig createConfig() {
100 return new ReactiveRoutingConfig();
101 }
102 };
103
104 @Activate
105 public void activate() {
106 configService.addListener(configListener);
107 registry.registerConfigFactory(reactiveRoutingConfigFactory);
108 setUpConfiguration();
109 log.info("Reactive routing configuration service started");
110 }
111
112 @Deactivate
113 public void deactivate() {
114 registry.unregisterConfigFactory(reactiveRoutingConfigFactory);
115 configService.removeListener(configListener);
116 log.info("Reactive routing configuration service stopped");
117 }
118
119 /**
120 * Set up reactive routing information from configuration.
121 */
122 private void setUpConfiguration() {
123 ReactiveRoutingConfig config = configService.getConfig(
124 coreService.registerApplication(ReactiveRoutingConfigurationService
125 .REACTIVE_ROUTING_APP_ID),
126 ReactiveRoutingConfigurationService.CONFIG_CLASS);
127 if (config == null) {
128 log.warn("No reactive routing config available!");
129 return;
130 }
131 for (LocalIpPrefixEntry entry : config.localIp4PrefixEntries()) {
132 localPrefixTable4.put(createBinaryString(entry.ipPrefix()), entry);
133 gatewayIpAddresses.add(entry.getGatewayIpAddress());
Yuta HIGUCHI1a1ba652018-03-29 08:25:29 -0700134 log.info("adding local IPv4 entry: {} {}", entry.ipPrefix(), entry.getGatewayIpAddress());
Jonathan Hartbb782be2017-02-09 17:45:49 -0800135 }
136 for (LocalIpPrefixEntry entry : config.localIp6PrefixEntries()) {
137 localPrefixTable6.put(createBinaryString(entry.ipPrefix()), entry);
138 gatewayIpAddresses.add(entry.getGatewayIpAddress());
Yuta HIGUCHI1a1ba652018-03-29 08:25:29 -0700139 log.info("adding local IPv6 entry: {} {}", entry.ipPrefix(), entry.getGatewayIpAddress());
Jonathan Hartbb782be2017-02-09 17:45:49 -0800140 }
141
142 virtualGatewayMacAddress = config.virtualGatewayMacAddress();
Yuta HIGUCHI1a1ba652018-03-29 08:25:29 -0700143 log.info("virtual gateway MAC: {}", virtualGatewayMacAddress);
Jonathan Hartbb782be2017-02-09 17:45:49 -0800144
145 // Setup BGP peer connect points
146 ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
147 if (routerAppId == null) {
148 log.info("Router application ID is null!");
149 return;
150 }
151
152 BgpConfig bgpConfig = configService.getConfig(routerAppId, BgpConfig.class);
153
154 if (bgpConfig == null) {
155 log.info("BGP config is null!");
156 return;
157 } else {
158 bgpPeerConnectPoints =
159 bgpConfig.bgpSpeakers().stream()
160 .flatMap(speaker -> speaker.peers().stream())
161 .map(peer -> interfaceService.getMatchingInterface(peer))
162 .filter(Objects::nonNull)
Yuta HIGUCHI1a1ba652018-03-29 08:25:29 -0700163 .map(Interface::connectPoint)
Jonathan Hartbb782be2017-02-09 17:45:49 -0800164 .collect(Collectors.toSet());
165 }
166 }
167
168 @Override
169 public boolean isIpAddressLocal(IpAddress ipAddress) {
170 if (ipAddress.isIp4()) {
171 return localPrefixTable4.getValuesForKeysPrefixing(
172 createBinaryString(
173 IpPrefix.valueOf(ipAddress, Ip4Address.BIT_LENGTH)))
174 .iterator().hasNext();
175 } else {
176 return localPrefixTable6.getValuesForKeysPrefixing(
177 createBinaryString(
178 IpPrefix.valueOf(ipAddress, Ip6Address.BIT_LENGTH)))
179 .iterator().hasNext();
180 }
181 }
182
183 @Override
184 public boolean isIpPrefixLocal(IpPrefix ipPrefix) {
185 return (localPrefixTable4.getValueForExactKey(
186 createBinaryString(ipPrefix)) != null ||
187 localPrefixTable6.getValueForExactKey(
188 createBinaryString(ipPrefix)) != null);
189 }
190
191 @Override
192 public boolean isVirtualGatewayIpAddress(IpAddress ipAddress) {
193 return gatewayIpAddresses.contains(ipAddress);
194 }
195
196 @Override
197 public MacAddress getVirtualGatewayMacAddress() {
198 return virtualGatewayMacAddress;
199 }
200
201 @Override
202 public Set<ConnectPoint> getBgpPeerConnectPoints() {
203 return ImmutableSet.copyOf(bgpPeerConnectPoints);
204 }
205
Jonathan Hartbb782be2017-02-09 17:45:49 -0800206 private class InternalNetworkConfigListener implements NetworkConfigListener {
207
208 @Override
209 public void event(NetworkConfigEvent event) {
210 switch (event.type()) {
211 case CONFIG_REGISTERED:
212 break;
213 case CONFIG_UNREGISTERED:
214 break;
215 case CONFIG_ADDED:
216 case CONFIG_UPDATED:
217 case CONFIG_REMOVED:
218 if (event.configClass() == ReactiveRoutingConfigurationService.CONFIG_CLASS) {
219 setUpConfiguration();
220 }
221 break;
222 default:
223 break;
224 }
225 }
226 }
227}