Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame^] | 16 | |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 17 | package org.onosproject.routing.config.impl; |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 18 | |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 20 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 21 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 22 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Deactivate; |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.apache.felix.scr.annotations.Service; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 29 | import org.onlab.packet.Ip4Address; |
| 30 | import org.onlab.packet.Ip6Address; |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 31 | import org.onlab.packet.IpAddress; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 32 | import org.onlab.packet.IpPrefix; |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 33 | import org.onlab.packet.MacAddress; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
| 36 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 37 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 38 | import org.onosproject.net.config.ConfigFactory; |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigListener; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 41 | import org.onosproject.net.config.NetworkConfigRegistry; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 42 | import org.onosproject.net.config.NetworkConfigService; |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 43 | import org.onosproject.net.config.basics.SubjectFactories; |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 44 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 45 | import org.onosproject.routing.config.BgpConfig; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 46 | import org.onosproject.routing.config.LocalIpPrefixEntry; |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 47 | import org.onosproject.routing.config.ReactiveRoutingConfig; |
| 48 | import org.onosproject.routing.config.RouterConfig; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 49 | import org.onosproject.routing.config.RoutingConfigurationService; |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | import org.slf4j.LoggerFactory; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 52 | |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 53 | import java.util.HashSet; |
| 54 | import java.util.Objects; |
| 55 | import java.util.Set; |
| 56 | import java.util.stream.Collectors; |
| 57 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 58 | /** |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 59 | * Implementation of RoutingConfigurationService which reads routing |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 60 | * configuration from the network configuration service. |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 61 | */ |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 62 | @Component(immediate = true) |
| 63 | @Service |
| 64 | public class RoutingConfigurationImpl implements RoutingConfigurationService { |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 65 | |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 66 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 67 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 69 | protected NetworkConfigRegistry registry; |
| 70 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected NetworkConfigService configService; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | protected CoreService coreService; |
| 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected InterfaceService interfaceService; |
| 79 | |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 80 | private Set<IpAddress> gatewayIpAddresses = new HashSet<>(); |
Pingping Lin | 8a52471 | 2015-06-24 14:58:24 -0700 | [diff] [blame] | 81 | private Set<ConnectPoint> bgpPeerConnectPoints = new HashSet<>(); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 82 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 83 | private InvertedRadixTree<LocalIpPrefixEntry> |
| 84 | localPrefixTable4 = new ConcurrentInvertedRadixTree<>( |
| 85 | new DefaultByteArrayNodeFactory()); |
| 86 | private InvertedRadixTree<LocalIpPrefixEntry> |
| 87 | localPrefixTable6 = new ConcurrentInvertedRadixTree<>( |
| 88 | new DefaultByteArrayNodeFactory()); |
| 89 | |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 90 | private MacAddress virtualGatewayMacAddress; |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 91 | private final InternalNetworkConfigListener configListener = |
| 92 | new InternalNetworkConfigListener(); |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 93 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 94 | private ConfigFactory<ApplicationId, BgpConfig> bgpConfigFactory = |
| 95 | new ConfigFactory<ApplicationId, BgpConfig>( |
| 96 | SubjectFactories.APP_SUBJECT_FACTORY, BgpConfig.class, "bgp") { |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 97 | @Override |
| 98 | public BgpConfig createConfig() { |
| 99 | return new BgpConfig(); |
| 100 | } |
| 101 | }; |
| 102 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 103 | private ConfigFactory<ApplicationId, RouterConfig> routerConfigFactory = |
| 104 | new ConfigFactory<ApplicationId, RouterConfig>( |
| 105 | SubjectFactories.APP_SUBJECT_FACTORY, RouterConfig.class, "router") { |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 106 | @Override |
| 107 | public RouterConfig createConfig() { |
| 108 | return new RouterConfig(); |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | private ConfigFactory<ApplicationId, ReactiveRoutingConfig> |
| 113 | reactiveRoutingConfigFactory = |
| 114 | new ConfigFactory<ApplicationId, ReactiveRoutingConfig>( |
| 115 | SubjectFactories.APP_SUBJECT_FACTORY, |
| 116 | ReactiveRoutingConfig.class, "reactiveRouting") { |
| 117 | @Override |
| 118 | public ReactiveRoutingConfig createConfig() { |
| 119 | return new ReactiveRoutingConfig(); |
| 120 | } |
| 121 | }; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 122 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 123 | @Activate |
| 124 | public void activate() { |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 125 | configService.addListener(configListener); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 126 | registry.registerConfigFactory(bgpConfigFactory); |
| 127 | registry.registerConfigFactory(routerConfigFactory); |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 128 | registry.registerConfigFactory(reactiveRoutingConfigFactory); |
| 129 | setUpConfiguration(); |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 130 | log.info("Routing configuration service started"); |
| 131 | } |
| 132 | |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 133 | @Deactivate |
| 134 | public void deactivate() { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 135 | registry.unregisterConfigFactory(bgpConfigFactory); |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 136 | registry.unregisterConfigFactory(routerConfigFactory); |
| 137 | registry.unregisterConfigFactory(reactiveRoutingConfigFactory); |
| 138 | configService.removeListener(configListener); |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 139 | log.info("Routing configuration service stopped"); |
| 140 | } |
| 141 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 142 | /** |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 143 | * Set up reactive routing information from configuration. |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 144 | */ |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 145 | private void setUpConfiguration() { |
| 146 | ReactiveRoutingConfig config = configService.getConfig( |
| 147 | coreService.registerApplication(RoutingConfigurationService |
| 148 | .REACTIVE_ROUTING_APP_ID), |
| 149 | RoutingConfigurationService.CONFIG_CLASS); |
| 150 | if (config == null) { |
| 151 | log.warn("No reactive routing config available!"); |
| 152 | return; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 153 | } |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 154 | for (LocalIpPrefixEntry entry : config.localIp4PrefixEntries()) { |
| 155 | localPrefixTable4.put(createBinaryString(entry.ipPrefix()), entry); |
| 156 | gatewayIpAddresses.add(entry.getGatewayIpAddress()); |
| 157 | } |
| 158 | for (LocalIpPrefixEntry entry : config.localIp6PrefixEntries()) { |
| 159 | localPrefixTable6.put(createBinaryString(entry.ipPrefix()), entry); |
| 160 | gatewayIpAddresses.add(entry.getGatewayIpAddress()); |
| 161 | } |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 162 | |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 163 | virtualGatewayMacAddress = config.virtualGatewayMacAddress(); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 164 | |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 165 | // Setup BGP peer connect points |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 166 | ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 167 | if (routerAppId == null) { |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 168 | log.info("Router application ID is null!"); |
| 169 | return; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | BgpConfig bgpConfig = configService.getConfig(routerAppId, BgpConfig.class); |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 173 | |
Pingping Lin | 9b85c03 | 2015-10-05 18:16:27 -0700 | [diff] [blame] | 174 | if (bgpConfig == null) { |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 175 | log.info("BGP config is null!"); |
| 176 | return; |
Pingping Lin | 9b85c03 | 2015-10-05 18:16:27 -0700 | [diff] [blame] | 177 | } else { |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 178 | bgpPeerConnectPoints = |
| 179 | bgpConfig.bgpSpeakers().stream() |
Pingping Lin | 9b85c03 | 2015-10-05 18:16:27 -0700 | [diff] [blame] | 180 | .flatMap(speaker -> speaker.peers().stream()) |
| 181 | .map(peer -> interfaceService.getMatchingInterface(peer)) |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 182 | .filter(Objects::nonNull) |
Pingping Lin | 9b85c03 | 2015-10-05 18:16:27 -0700 | [diff] [blame] | 183 | .map(intf -> intf.connectPoint()) |
| 184 | .collect(Collectors.toSet()); |
| 185 | } |
Pingping Lin | 8a52471 | 2015-06-24 14:58:24 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 189 | public boolean isIpAddressLocal(IpAddress ipAddress) { |
| 190 | if (ipAddress.isIp4()) { |
| 191 | return localPrefixTable4.getValuesForKeysPrefixing( |
| 192 | createBinaryString( |
| 193 | IpPrefix.valueOf(ipAddress, Ip4Address.BIT_LENGTH))) |
| 194 | .iterator().hasNext(); |
| 195 | } else { |
| 196 | return localPrefixTable6.getValuesForKeysPrefixing( |
| 197 | createBinaryString( |
| 198 | IpPrefix.valueOf(ipAddress, Ip6Address.BIT_LENGTH))) |
| 199 | .iterator().hasNext(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public boolean isIpPrefixLocal(IpPrefix ipPrefix) { |
| 205 | return (localPrefixTable4.getValueForExactKey( |
| 206 | createBinaryString(ipPrefix)) != null || |
| 207 | localPrefixTable6.getValueForExactKey( |
| 208 | createBinaryString(ipPrefix)) != null); |
| 209 | } |
| 210 | |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 211 | @Override |
| 212 | public boolean isVirtualGatewayIpAddress(IpAddress ipAddress) { |
| 213 | return gatewayIpAddresses.contains(ipAddress); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public MacAddress getVirtualGatewayMacAddress() { |
| 218 | return virtualGatewayMacAddress; |
| 219 | } |
| 220 | |
Jonathan Hart | c9e36c5 | 2017-01-05 09:53:33 +1300 | [diff] [blame] | 221 | @Override |
| 222 | public Set<ConnectPoint> getBgpPeerConnectPoints() { |
| 223 | return ImmutableSet.copyOf(bgpPeerConnectPoints); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Creates the binary string representation of an IP prefix. |
| 228 | * The prefix can be either IPv4 or IPv6. |
| 229 | * The string length is equal to the prefix length + 1. |
| 230 | * |
| 231 | * For each string, we put a extra "0" in the front. The purpose of |
| 232 | * doing this is to store the default route inside InvertedRadixTree. |
| 233 | * |
| 234 | * @param ipPrefix the IP prefix to use |
| 235 | * @return the binary string representation |
| 236 | */ |
| 237 | private static String createBinaryString(IpPrefix ipPrefix) { |
| 238 | if (ipPrefix.prefixLength() == 0) { |
| 239 | return "0"; |
| 240 | } |
| 241 | |
| 242 | byte[] octets = ipPrefix.address().toOctets(); |
| 243 | StringBuilder result = new StringBuilder(ipPrefix.prefixLength()); |
| 244 | for (int i = 0; i < ipPrefix.prefixLength(); i++) { |
| 245 | int byteOffset = i / Byte.SIZE; |
| 246 | int bitOffset = i % Byte.SIZE; |
| 247 | int mask = 1 << (Byte.SIZE - 1 - bitOffset); |
| 248 | byte value = octets[byteOffset]; |
| 249 | boolean isSet = ((value & mask) != 0); |
| 250 | result.append(isSet ? "1" : "0"); |
| 251 | } |
| 252 | |
| 253 | return "0" + result.toString(); |
| 254 | } |
| 255 | |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 256 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 257 | |
| 258 | @Override |
| 259 | public void event(NetworkConfigEvent event) { |
| 260 | switch (event.type()) { |
| 261 | case CONFIG_REGISTERED: |
| 262 | break; |
| 263 | case CONFIG_UNREGISTERED: |
| 264 | break; |
| 265 | case CONFIG_ADDED: |
| 266 | case CONFIG_UPDATED: |
| 267 | case CONFIG_REMOVED: |
| 268 | if (event.configClass() == RoutingConfigurationService.CONFIG_CLASS) { |
| 269 | setUpConfiguration(); |
| 270 | } |
| 271 | break; |
| 272 | default: |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | } |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 277 | } |