Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
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; |
| 22 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 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 | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.host.HostService; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 35 | import org.onosproject.routing.config.BgpPeer; |
| 36 | import org.onosproject.routing.config.BgpSpeaker; |
| 37 | import org.onosproject.routing.config.Interface; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 38 | import org.onosproject.routing.config.LocalIpPrefixEntry; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 39 | import org.onosproject.routing.config.RoutingConfigurationService; |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 42 | |
| 43 | import java.io.File; |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 44 | import java.io.FileNotFoundException; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 45 | import java.io.IOException; |
| 46 | import java.util.Collections; |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame^] | 47 | import java.util.HashSet; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 48 | import java.util.Map; |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 49 | import java.util.Set; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 50 | import java.util.concurrent.ConcurrentHashMap; |
| 51 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 52 | import static org.onosproject.routing.RouteEntry.createBinaryString; |
| 53 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 54 | /** |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 55 | * Implementation of RoutingConfigurationService which reads routing |
| 56 | * configuration from a file. |
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 | @Component(immediate = true) |
| 59 | @Service |
| 60 | public class RoutingConfigurationImpl implements RoutingConfigurationService { |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 61 | |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 62 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 63 | |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 64 | private static final String CONFIG_DIR = "../config"; |
| 65 | private static final String DEFAULT_CONFIG_FILE = "sdnip.json"; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 66 | private String configFileName = DEFAULT_CONFIG_FILE; |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 67 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected HostService hostService; |
| 70 | |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 71 | private Map<String, BgpSpeaker> bgpSpeakers = new ConcurrentHashMap<>(); |
| 72 | private Map<IpAddress, BgpPeer> bgpPeers = new ConcurrentHashMap<>(); |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame^] | 73 | private Set<IpAddress> gatewayIpAddresses = new HashSet<>(); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 74 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 75 | private InvertedRadixTree<LocalIpPrefixEntry> |
| 76 | localPrefixTable4 = new ConcurrentInvertedRadixTree<>( |
| 77 | new DefaultByteArrayNodeFactory()); |
| 78 | private InvertedRadixTree<LocalIpPrefixEntry> |
| 79 | localPrefixTable6 = new ConcurrentInvertedRadixTree<>( |
| 80 | new DefaultByteArrayNodeFactory()); |
| 81 | |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame^] | 82 | private MacAddress virtualGatewayMacAddress; |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 83 | private HostToInterfaceAdaptor hostAdaptor; |
| 84 | |
| 85 | @Activate |
| 86 | public void activate() { |
| 87 | readConfiguration(); |
| 88 | hostAdaptor = new HostToInterfaceAdaptor(hostService); |
| 89 | log.info("Routing configuration service started"); |
| 90 | } |
| 91 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 92 | /** |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 93 | * Reads SDN-IP related information contained in the configuration file. |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 94 | * |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 95 | * @param configFilename the name of the configuration file for the SDN-IP |
| 96 | * application |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 97 | */ |
| 98 | private void readConfiguration(String configFilename) { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 99 | File configFile = new File(CONFIG_DIR, configFilename); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 100 | ObjectMapper mapper = new ObjectMapper(); |
| 101 | |
| 102 | try { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 103 | log.info("Loading config: {}", configFile.getAbsolutePath()); |
| 104 | Configuration config = mapper.readValue(configFile, |
| 105 | Configuration.class); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 106 | for (BgpSpeaker speaker : config.getBgpSpeakers()) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 107 | bgpSpeakers.put(speaker.name(), speaker); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 108 | } |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 109 | for (BgpPeer peer : config.getPeers()) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 110 | bgpPeers.put(peer.ipAddress(), peer); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 111 | } |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 112 | |
| 113 | for (LocalIpPrefixEntry entry : config.getLocalIp4PrefixEntries()) { |
| 114 | localPrefixTable4.put(createBinaryString(entry.ipPrefix()), |
| 115 | entry); |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame^] | 116 | gatewayIpAddresses.add(entry.getGatewayIpAddress()); |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 117 | } |
| 118 | for (LocalIpPrefixEntry entry : config.getLocalIp6PrefixEntries()) { |
| 119 | localPrefixTable6.put(createBinaryString(entry.ipPrefix()), |
| 120 | entry); |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame^] | 121 | gatewayIpAddresses.add(entry.getGatewayIpAddress()); |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame^] | 124 | virtualGatewayMacAddress = config.getVirtualGatewayMacAddress(); |
| 125 | |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 126 | } catch (FileNotFoundException e) { |
| 127 | log.warn("Configuration file not found: {}", configFileName); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 128 | } catch (IOException e) { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 129 | log.error("Error loading configuration", e); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 130 | } |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame] | 133 | /** |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 134 | * Instructs the configuration reader to read the configuration from the |
| 135 | * file. |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame] | 136 | */ |
| 137 | public void readConfiguration() { |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 138 | readConfiguration(configFileName); |
| 139 | } |
| 140 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 141 | @Override |
| 142 | public Map<String, BgpSpeaker> getBgpSpeakers() { |
| 143 | return Collections.unmodifiableMap(bgpSpeakers); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Map<IpAddress, BgpPeer> getBgpPeers() { |
| 148 | return Collections.unmodifiableMap(bgpPeers); |
| 149 | } |
| 150 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 151 | @Override |
| 152 | public Set<Interface> getInterfaces() { |
| 153 | return hostAdaptor.getInterfaces(); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 154 | } |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 155 | |
| 156 | @Override |
| 157 | public Interface getInterface(ConnectPoint connectPoint) { |
| 158 | return hostAdaptor.getInterface(connectPoint); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public Interface getMatchingInterface(IpAddress ipAddress) { |
| 163 | return hostAdaptor.getMatchingInterface(ipAddress); |
| 164 | } |
| 165 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 166 | @Override |
| 167 | public boolean isIpAddressLocal(IpAddress ipAddress) { |
| 168 | if (ipAddress.isIp4()) { |
| 169 | return localPrefixTable4.getValuesForKeysPrefixing( |
| 170 | createBinaryString( |
| 171 | IpPrefix.valueOf(ipAddress, Ip4Address.BIT_LENGTH))) |
| 172 | .iterator().hasNext(); |
| 173 | } else { |
| 174 | return localPrefixTable6.getValuesForKeysPrefixing( |
| 175 | createBinaryString( |
| 176 | IpPrefix.valueOf(ipAddress, Ip6Address.BIT_LENGTH))) |
| 177 | .iterator().hasNext(); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public boolean isIpPrefixLocal(IpPrefix ipPrefix) { |
| 183 | return (localPrefixTable4.getValueForExactKey( |
| 184 | createBinaryString(ipPrefix)) != null || |
| 185 | localPrefixTable6.getValueForExactKey( |
| 186 | createBinaryString(ipPrefix)) != null); |
| 187 | } |
| 188 | |
Pingping Lin | c9e16bf | 2015-04-10 14:42:41 -0700 | [diff] [blame^] | 189 | @Override |
| 190 | public boolean isVirtualGatewayIpAddress(IpAddress ipAddress) { |
| 191 | return gatewayIpAddresses.contains(ipAddress); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public MacAddress getVirtualGatewayMacAddress() { |
| 196 | return virtualGatewayMacAddress; |
| 197 | } |
| 198 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 199 | } |