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