Jonathan Hart | bb782be | 2017-02-09 17:45:49 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | bb782be | 2017-02-09 17:45:49 -0800 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.reactive.routing; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 21 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 22 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
| 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.apache.felix.scr.annotations.Service; |
| 29 | import org.onlab.packet.Ip4Address; |
| 30 | import org.onlab.packet.Ip6Address; |
| 31 | import org.onlab.packet.IpAddress; |
| 32 | import org.onlab.packet.IpPrefix; |
| 33 | import org.onlab.packet.MacAddress; |
| 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
Yuta HIGUCHI | 1a1ba65 | 2018-03-29 08:25:29 -0700 | [diff] [blame] | 36 | import org.onosproject.net.intf.Interface; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 37 | import org.onosproject.net.intf.InterfaceService; |
Jonathan Hart | bb782be | 2017-02-09 17:45:49 -0800 | [diff] [blame] | 38 | import org.onosproject.net.ConnectPoint; |
| 39 | import org.onosproject.net.config.ConfigFactory; |
| 40 | import org.onosproject.net.config.NetworkConfigEvent; |
| 41 | import org.onosproject.net.config.NetworkConfigListener; |
| 42 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 43 | import org.onosproject.net.config.NetworkConfigService; |
| 44 | import org.onosproject.net.config.basics.SubjectFactories; |
| 45 | import org.onosproject.routing.RoutingService; |
| 46 | import org.onosproject.routing.config.BgpConfig; |
| 47 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; |
| 49 | |
| 50 | import java.util.HashSet; |
| 51 | import java.util.Objects; |
| 52 | import java.util.Set; |
| 53 | import java.util.stream.Collectors; |
| 54 | |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 55 | import static org.onosproject.routeservice.RouteTools.createBinaryString; |
Jonathan Hart | c4c2d62 | 2017-02-10 14:13:57 -0800 | [diff] [blame] | 56 | |
Jonathan Hart | bb782be | 2017-02-09 17:45:49 -0800 | [diff] [blame] | 57 | /** |
| 58 | * Reactive routing configuration manager. |
| 59 | */ |
| 60 | @Component(immediate = true) |
| 61 | @Service |
| 62 | public 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 HIGUCHI | 1a1ba65 | 2018-03-29 08:25:29 -0700 | [diff] [blame] | 134 | log.info("adding local IPv4 entry: {} {}", entry.ipPrefix(), entry.getGatewayIpAddress()); |
Jonathan Hart | bb782be | 2017-02-09 17:45:49 -0800 | [diff] [blame] | 135 | } |
| 136 | for (LocalIpPrefixEntry entry : config.localIp6PrefixEntries()) { |
| 137 | localPrefixTable6.put(createBinaryString(entry.ipPrefix()), entry); |
| 138 | gatewayIpAddresses.add(entry.getGatewayIpAddress()); |
Yuta HIGUCHI | 1a1ba65 | 2018-03-29 08:25:29 -0700 | [diff] [blame] | 139 | log.info("adding local IPv6 entry: {} {}", entry.ipPrefix(), entry.getGatewayIpAddress()); |
Jonathan Hart | bb782be | 2017-02-09 17:45:49 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | virtualGatewayMacAddress = config.virtualGatewayMacAddress(); |
Yuta HIGUCHI | 1a1ba65 | 2018-03-29 08:25:29 -0700 | [diff] [blame] | 143 | log.info("virtual gateway MAC: {}", virtualGatewayMacAddress); |
Jonathan Hart | bb782be | 2017-02-09 17:45:49 -0800 | [diff] [blame] | 144 | |
| 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 HIGUCHI | 1a1ba65 | 2018-03-29 08:25:29 -0700 | [diff] [blame] | 163 | .map(Interface::connectPoint) |
Jonathan Hart | bb782be | 2017-02-09 17:45:49 -0800 | [diff] [blame] | 164 | .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 Hart | bb782be | 2017-02-09 17:45:49 -0800 | [diff] [blame] | 206 | 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 | } |