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 | |
| 57 | import static org.onosproject.routing.RouteEntry.createBinaryString; |
| 58 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 59 | /** |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 60 | * Implementation of RoutingConfigurationService which reads routing |
| 61 | * configuration from a file. |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 62 | */ |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 63 | @Component(immediate = true) |
| 64 | @Service |
| 65 | public class RoutingConfigurationImpl implements RoutingConfigurationService { |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 66 | |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 67 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 68 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 70 | protected NetworkConfigRegistry registry; |
| 71 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected NetworkConfigService configService; |
| 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected CoreService coreService; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 79 | protected InterfaceService interfaceService; |
| 80 | |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 81 | private Set<IpAddress> gatewayIpAddresses = new HashSet<>(); |
Pingping Lin | 8a52471 | 2015-06-24 14:58:24 -0700 | [diff] [blame] | 82 | private Set<ConnectPoint> bgpPeerConnectPoints = new HashSet<>(); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 83 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 84 | private InvertedRadixTree<LocalIpPrefixEntry> |
| 85 | localPrefixTable4 = new ConcurrentInvertedRadixTree<>( |
| 86 | new DefaultByteArrayNodeFactory()); |
| 87 | private InvertedRadixTree<LocalIpPrefixEntry> |
| 88 | localPrefixTable6 = new ConcurrentInvertedRadixTree<>( |
| 89 | new DefaultByteArrayNodeFactory()); |
| 90 | |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 91 | private MacAddress virtualGatewayMacAddress; |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 92 | private final InternalNetworkConfigListener configListener = |
| 93 | new InternalNetworkConfigListener(); |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 94 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 95 | private ConfigFactory<ApplicationId, BgpConfig> bgpConfigFactory = |
| 96 | new ConfigFactory<ApplicationId, BgpConfig>( |
| 97 | SubjectFactories.APP_SUBJECT_FACTORY, BgpConfig.class, "bgp") { |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 98 | @Override |
| 99 | public BgpConfig createConfig() { |
| 100 | return new BgpConfig(); |
| 101 | } |
| 102 | }; |
| 103 | |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 104 | private ConfigFactory<ApplicationId, RouterConfig> routerConfigFactory = |
| 105 | new ConfigFactory<ApplicationId, RouterConfig>( |
| 106 | SubjectFactories.APP_SUBJECT_FACTORY, RouterConfig.class, "router") { |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public RouterConfig createConfig() { |
| 109 | return new RouterConfig(); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | private ConfigFactory<ApplicationId, ReactiveRoutingConfig> |
| 114 | reactiveRoutingConfigFactory = |
| 115 | new ConfigFactory<ApplicationId, ReactiveRoutingConfig>( |
| 116 | SubjectFactories.APP_SUBJECT_FACTORY, |
| 117 | ReactiveRoutingConfig.class, "reactiveRouting") { |
| 118 | @Override |
| 119 | public ReactiveRoutingConfig createConfig() { |
| 120 | return new ReactiveRoutingConfig(); |
| 121 | } |
| 122 | }; |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 123 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 124 | @Activate |
| 125 | public void activate() { |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 126 | configService.addListener(configListener); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 127 | registry.registerConfigFactory(bgpConfigFactory); |
| 128 | registry.registerConfigFactory(routerConfigFactory); |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 129 | registry.registerConfigFactory(reactiveRoutingConfigFactory); |
| 130 | setUpConfiguration(); |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 131 | log.info("Routing configuration service started"); |
| 132 | } |
| 133 | |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 134 | @Deactivate |
| 135 | public void deactivate() { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 136 | registry.unregisterConfigFactory(bgpConfigFactory); |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 137 | registry.unregisterConfigFactory(routerConfigFactory); |
| 138 | registry.unregisterConfigFactory(reactiveRoutingConfigFactory); |
| 139 | configService.removeListener(configListener); |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 140 | log.info("Routing configuration service stopped"); |
| 141 | } |
| 142 | |
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 | * Set up reactive routing information from configuration. |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 145 | */ |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 146 | private void setUpConfiguration() { |
| 147 | ReactiveRoutingConfig config = configService.getConfig( |
| 148 | coreService.registerApplication(RoutingConfigurationService |
| 149 | .REACTIVE_ROUTING_APP_ID), |
| 150 | RoutingConfigurationService.CONFIG_CLASS); |
| 151 | if (config == null) { |
| 152 | log.warn("No reactive routing config available!"); |
| 153 | return; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 154 | } |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 155 | for (LocalIpPrefixEntry entry : config.localIp4PrefixEntries()) { |
| 156 | localPrefixTable4.put(createBinaryString(entry.ipPrefix()), entry); |
| 157 | gatewayIpAddresses.add(entry.getGatewayIpAddress()); |
| 158 | } |
| 159 | for (LocalIpPrefixEntry entry : config.localIp6PrefixEntries()) { |
| 160 | localPrefixTable6.put(createBinaryString(entry.ipPrefix()), entry); |
| 161 | gatewayIpAddresses.add(entry.getGatewayIpAddress()); |
| 162 | } |
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 | virtualGatewayMacAddress = config.virtualGatewayMacAddress(); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 165 | |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 166 | // Setup BGP peer connect points |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 167 | ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 168 | if (routerAppId == null) { |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 169 | log.info("Router application ID is null!"); |
| 170 | return; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | BgpConfig bgpConfig = configService.getConfig(routerAppId, BgpConfig.class); |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 174 | |
Pingping Lin | 9b85c03 | 2015-10-05 18:16:27 -0700 | [diff] [blame] | 175 | if (bgpConfig == null) { |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 176 | log.info("BGP config is null!"); |
| 177 | return; |
Pingping Lin | 9b85c03 | 2015-10-05 18:16:27 -0700 | [diff] [blame] | 178 | } else { |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 179 | bgpPeerConnectPoints = |
| 180 | bgpConfig.bgpSpeakers().stream() |
Pingping Lin | 9b85c03 | 2015-10-05 18:16:27 -0700 | [diff] [blame] | 181 | .flatMap(speaker -> speaker.peers().stream()) |
| 182 | .map(peer -> interfaceService.getMatchingInterface(peer)) |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 183 | .filter(Objects::nonNull) |
Pingping Lin | 9b85c03 | 2015-10-05 18:16:27 -0700 | [diff] [blame] | 184 | .map(intf -> intf.connectPoint()) |
| 185 | .collect(Collectors.toSet()); |
| 186 | } |
Pingping Lin | 8a52471 | 2015-06-24 14:58:24 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 190 | public boolean isIpAddressLocal(IpAddress ipAddress) { |
| 191 | if (ipAddress.isIp4()) { |
| 192 | return localPrefixTable4.getValuesForKeysPrefixing( |
| 193 | createBinaryString( |
| 194 | IpPrefix.valueOf(ipAddress, Ip4Address.BIT_LENGTH))) |
| 195 | .iterator().hasNext(); |
| 196 | } else { |
| 197 | return localPrefixTable6.getValuesForKeysPrefixing( |
| 198 | createBinaryString( |
| 199 | IpPrefix.valueOf(ipAddress, Ip6Address.BIT_LENGTH))) |
| 200 | .iterator().hasNext(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public boolean isIpPrefixLocal(IpPrefix ipPrefix) { |
| 206 | return (localPrefixTable4.getValueForExactKey( |
| 207 | createBinaryString(ipPrefix)) != null || |
| 208 | localPrefixTable6.getValueForExactKey( |
| 209 | createBinaryString(ipPrefix)) != null); |
| 210 | } |
| 211 | |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame] | 212 | @Override |
| 213 | public boolean isVirtualGatewayIpAddress(IpAddress ipAddress) { |
| 214 | return gatewayIpAddresses.contains(ipAddress); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public MacAddress getVirtualGatewayMacAddress() { |
| 219 | return virtualGatewayMacAddress; |
| 220 | } |
| 221 | |
Pingping Lin | 9a445c8 | 2016-04-07 11:40:29 -0700 | [diff] [blame] | 222 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 223 | |
| 224 | @Override |
| 225 | public void event(NetworkConfigEvent event) { |
| 226 | switch (event.type()) { |
| 227 | case CONFIG_REGISTERED: |
| 228 | break; |
| 229 | case CONFIG_UNREGISTERED: |
| 230 | break; |
| 231 | case CONFIG_ADDED: |
| 232 | case CONFIG_UPDATED: |
| 233 | case CONFIG_REMOVED: |
| 234 | if (event.configClass() == RoutingConfigurationService.CONFIG_CLASS) { |
| 235 | setUpConfiguration(); |
| 236 | } |
| 237 | break; |
| 238 | default: |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public Set<ConnectPoint> getBgpPeerConnectPoints() { |
| 246 | return ImmutableSet.copyOf(bgpPeerConnectPoints); |
| 247 | } |
| 248 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 249 | } |