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