Jonathan Hart | bb782be | 2017-02-09 17:45:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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; |
| 36 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 37 | import org.onosproject.net.ConnectPoint; |
| 38 | import org.onosproject.net.config.ConfigFactory; |
| 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigListener; |
| 41 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 42 | import org.onosproject.net.config.NetworkConfigService; |
| 43 | import org.onosproject.net.config.basics.SubjectFactories; |
| 44 | import org.onosproject.routing.RoutingService; |
| 45 | import org.onosproject.routing.config.BgpConfig; |
| 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
| 49 | import java.util.HashSet; |
| 50 | import java.util.Objects; |
| 51 | import java.util.Set; |
| 52 | import java.util.stream.Collectors; |
| 53 | |
Jonathan Hart | c4c2d62 | 2017-02-10 14:13:57 -0800 | [diff] [blame] | 54 | import static org.onosproject.incubator.net.routing.RouteTools.createBinaryString; |
| 55 | |
Jonathan Hart | bb782be | 2017-02-09 17:45:49 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Reactive routing configuration manager. |
| 58 | */ |
| 59 | @Component(immediate = true) |
| 60 | @Service |
| 61 | public 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 Hart | bb782be | 2017-02-09 17:45:49 -0800 | [diff] [blame] | 202 | 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 | } |