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 | |
| 54 | /** |
| 55 | * Reactive routing configuration manager. |
| 56 | */ |
| 57 | @Component(immediate = true) |
| 58 | @Service |
| 59 | public class ReactiveRoutingConfiguration implements |
| 60 | ReactiveRoutingConfigurationService { |
| 61 | |
| 62 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected NetworkConfigRegistry registry; |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected NetworkConfigService configService; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected CoreService coreService; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected InterfaceService interfaceService; |
| 75 | |
| 76 | private Set<IpAddress> gatewayIpAddresses = new HashSet<>(); |
| 77 | private Set<ConnectPoint> bgpPeerConnectPoints = new HashSet<>(); |
| 78 | |
| 79 | private InvertedRadixTree<LocalIpPrefixEntry> |
| 80 | localPrefixTable4 = new ConcurrentInvertedRadixTree<>( |
| 81 | new DefaultByteArrayNodeFactory()); |
| 82 | private InvertedRadixTree<LocalIpPrefixEntry> |
| 83 | localPrefixTable6 = new ConcurrentInvertedRadixTree<>( |
| 84 | new DefaultByteArrayNodeFactory()); |
| 85 | |
| 86 | private MacAddress virtualGatewayMacAddress; |
| 87 | private final InternalNetworkConfigListener configListener = |
| 88 | new InternalNetworkConfigListener(); |
| 89 | |
| 90 | private ConfigFactory<ApplicationId, ReactiveRoutingConfig> |
| 91 | reactiveRoutingConfigFactory = |
| 92 | new ConfigFactory<ApplicationId, ReactiveRoutingConfig>( |
| 93 | SubjectFactories.APP_SUBJECT_FACTORY, |
| 94 | ReactiveRoutingConfig.class, "reactiveRouting") { |
| 95 | @Override |
| 96 | public ReactiveRoutingConfig createConfig() { |
| 97 | return new ReactiveRoutingConfig(); |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | @Activate |
| 102 | public void activate() { |
| 103 | configService.addListener(configListener); |
| 104 | registry.registerConfigFactory(reactiveRoutingConfigFactory); |
| 105 | setUpConfiguration(); |
| 106 | log.info("Reactive routing configuration service started"); |
| 107 | } |
| 108 | |
| 109 | @Deactivate |
| 110 | public void deactivate() { |
| 111 | registry.unregisterConfigFactory(reactiveRoutingConfigFactory); |
| 112 | configService.removeListener(configListener); |
| 113 | log.info("Reactive routing configuration service stopped"); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Set up reactive routing information from configuration. |
| 118 | */ |
| 119 | private void setUpConfiguration() { |
| 120 | ReactiveRoutingConfig config = configService.getConfig( |
| 121 | coreService.registerApplication(ReactiveRoutingConfigurationService |
| 122 | .REACTIVE_ROUTING_APP_ID), |
| 123 | ReactiveRoutingConfigurationService.CONFIG_CLASS); |
| 124 | if (config == null) { |
| 125 | log.warn("No reactive routing config available!"); |
| 126 | return; |
| 127 | } |
| 128 | for (LocalIpPrefixEntry entry : config.localIp4PrefixEntries()) { |
| 129 | localPrefixTable4.put(createBinaryString(entry.ipPrefix()), entry); |
| 130 | gatewayIpAddresses.add(entry.getGatewayIpAddress()); |
| 131 | } |
| 132 | for (LocalIpPrefixEntry entry : config.localIp6PrefixEntries()) { |
| 133 | localPrefixTable6.put(createBinaryString(entry.ipPrefix()), entry); |
| 134 | gatewayIpAddresses.add(entry.getGatewayIpAddress()); |
| 135 | } |
| 136 | |
| 137 | virtualGatewayMacAddress = config.virtualGatewayMacAddress(); |
| 138 | |
| 139 | // Setup BGP peer connect points |
| 140 | ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID); |
| 141 | if (routerAppId == null) { |
| 142 | log.info("Router application ID is null!"); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | BgpConfig bgpConfig = configService.getConfig(routerAppId, BgpConfig.class); |
| 147 | |
| 148 | if (bgpConfig == null) { |
| 149 | log.info("BGP config is null!"); |
| 150 | return; |
| 151 | } else { |
| 152 | bgpPeerConnectPoints = |
| 153 | bgpConfig.bgpSpeakers().stream() |
| 154 | .flatMap(speaker -> speaker.peers().stream()) |
| 155 | .map(peer -> interfaceService.getMatchingInterface(peer)) |
| 156 | .filter(Objects::nonNull) |
| 157 | .map(intf -> intf.connectPoint()) |
| 158 | .collect(Collectors.toSet()); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public boolean isIpAddressLocal(IpAddress ipAddress) { |
| 164 | if (ipAddress.isIp4()) { |
| 165 | return localPrefixTable4.getValuesForKeysPrefixing( |
| 166 | createBinaryString( |
| 167 | IpPrefix.valueOf(ipAddress, Ip4Address.BIT_LENGTH))) |
| 168 | .iterator().hasNext(); |
| 169 | } else { |
| 170 | return localPrefixTable6.getValuesForKeysPrefixing( |
| 171 | createBinaryString( |
| 172 | IpPrefix.valueOf(ipAddress, Ip6Address.BIT_LENGTH))) |
| 173 | .iterator().hasNext(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public boolean isIpPrefixLocal(IpPrefix ipPrefix) { |
| 179 | return (localPrefixTable4.getValueForExactKey( |
| 180 | createBinaryString(ipPrefix)) != null || |
| 181 | localPrefixTable6.getValueForExactKey( |
| 182 | createBinaryString(ipPrefix)) != null); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public boolean isVirtualGatewayIpAddress(IpAddress ipAddress) { |
| 187 | return gatewayIpAddresses.contains(ipAddress); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public MacAddress getVirtualGatewayMacAddress() { |
| 192 | return virtualGatewayMacAddress; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public Set<ConnectPoint> getBgpPeerConnectPoints() { |
| 197 | return ImmutableSet.copyOf(bgpPeerConnectPoints); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Creates the binary string representation of an IP prefix. |
| 202 | * The prefix can be either IPv4 or IPv6. |
| 203 | * The string length is equal to the prefix length + 1. |
| 204 | * |
| 205 | * For each string, we put a extra "0" in the front. The purpose of |
| 206 | * doing this is to store the default route inside InvertedRadixTree. |
| 207 | * |
| 208 | * @param ipPrefix the IP prefix to use |
| 209 | * @return the binary string representation |
| 210 | */ |
| 211 | private static String createBinaryString(IpPrefix ipPrefix) { |
| 212 | if (ipPrefix.prefixLength() == 0) { |
| 213 | return "0"; |
| 214 | } |
| 215 | |
| 216 | byte[] octets = ipPrefix.address().toOctets(); |
| 217 | StringBuilder result = new StringBuilder(ipPrefix.prefixLength()); |
| 218 | for (int i = 0; i < ipPrefix.prefixLength(); i++) { |
| 219 | int byteOffset = i / Byte.SIZE; |
| 220 | int bitOffset = i % Byte.SIZE; |
| 221 | int mask = 1 << (Byte.SIZE - 1 - bitOffset); |
| 222 | byte value = octets[byteOffset]; |
| 223 | boolean isSet = ((value & mask) != 0); |
| 224 | result.append(isSet ? "1" : "0"); |
| 225 | } |
| 226 | |
| 227 | return "0" + result.toString(); |
| 228 | } |
| 229 | |
| 230 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 231 | |
| 232 | @Override |
| 233 | public void event(NetworkConfigEvent event) { |
| 234 | switch (event.type()) { |
| 235 | case CONFIG_REGISTERED: |
| 236 | break; |
| 237 | case CONFIG_UNREGISTERED: |
| 238 | break; |
| 239 | case CONFIG_ADDED: |
| 240 | case CONFIG_UPDATED: |
| 241 | case CONFIG_REMOVED: |
| 242 | if (event.configClass() == ReactiveRoutingConfigurationService.CONFIG_CLASS) { |
| 243 | setUpConfiguration(); |
| 244 | } |
| 245 | break; |
| 246 | default: |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |