Thomas Vachuska | 8fd2505 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 8fd2505 | 2015-09-10 16:15:33 -0700 | [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 | */ |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 16 | package org.onosproject.segmentrouting.config; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 17 | |
Charles Chan | 6191aca | 2017-09-12 12:09:22 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 21 | import com.google.common.collect.HashMultimap; |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
Charles Chan | 19b7003 | 2019-04-17 14:20:26 -0700 | [diff] [blame^] | 23 | import com.google.common.collect.Lists; |
Charles Chan | dc7d6f9 | 2018-03-20 13:30:38 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Multimaps; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 25 | import com.google.common.collect.SetMultimap; |
Charles Chan | 19b7003 | 2019-04-17 14:20:26 -0700 | [diff] [blame^] | 26 | import com.google.common.collect.Sets; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 27 | import org.onlab.packet.Ip4Address; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 28 | import org.onlab.packet.Ip6Address; |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 29 | import org.onlab.packet.IpAddress; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 30 | import org.onlab.packet.IpPrefix; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 31 | import org.onlab.packet.MacAddress; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 32 | import org.onlab.packet.VlanId; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 33 | import org.onosproject.net.ConnectPoint; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 34 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 35 | import org.onosproject.net.PortNumber; |
Jonathan Hart | 61e24e1 | 2017-11-30 18:23:42 -0800 | [diff] [blame] | 36 | import org.onosproject.net.config.ConfigException; |
| 37 | import org.onosproject.net.config.basics.InterfaceConfig; |
| 38 | import org.onosproject.net.host.InterfaceIpAddress; |
| 39 | import org.onosproject.net.intf.Interface; |
Charles Chan | 19b7003 | 2019-04-17 14:20:26 -0700 | [diff] [blame^] | 40 | import org.onosproject.routeservice.Route; |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 41 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 45 | import java.util.ArrayList; |
Jonathan Hart | 61e24e1 | 2017-11-30 18:23:42 -0800 | [diff] [blame] | 46 | import java.util.Collection; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 47 | import java.util.Collections; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 48 | import java.util.HashMap; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 49 | import java.util.HashSet; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 50 | import java.util.List; |
| 51 | import java.util.Map; |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 52 | import java.util.Objects; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 53 | import java.util.Set; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 54 | import java.util.concurrent.ConcurrentHashMap; |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 55 | import java.util.stream.Collectors; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 56 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 57 | import static com.google.common.base.Preconditions.checkNotNull; |
| 58 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Segment Routing configuration component that reads the |
| 61 | * segment routing related configuration from Network Configuration Manager |
| 62 | * component and organizes in more accessible formats. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 63 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 64 | public class DeviceConfiguration implements DeviceProperties { |
| 65 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 66 | private static final String NO_SUBNET = "No subnet configured on {}"; |
| 67 | |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 68 | private static final Logger log = LoggerFactory.getLogger(DeviceConfiguration.class); |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 69 | private final List<Integer> allSegmentIds = new ArrayList<>(); |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 70 | private final Map<DeviceId, SegmentRouterInfo> deviceConfigMap = new ConcurrentHashMap<>(); |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 71 | private SegmentRoutingManager srManager; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 72 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 73 | private class SegmentRouterInfo { |
Charles Chan | 3d35a54 | 2018-10-02 12:52:29 -0700 | [diff] [blame] | 74 | int ipv4NodeSid = -1; |
| 75 | int ipv6NodeSid = -1; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 76 | DeviceId deviceId; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 77 | Ip4Address ipv4Loopback; |
| 78 | Ip6Address ipv6Loopback; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 79 | MacAddress mac; |
| 80 | boolean isEdge; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 81 | SetMultimap<PortNumber, IpAddress> gatewayIps; |
| 82 | SetMultimap<PortNumber, IpPrefix> subnets; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 83 | Map<Integer, Set<Integer>> adjacencySids; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 84 | DeviceId pairDeviceId; |
| 85 | PortNumber pairLocalPort; |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 86 | int pwRoutingLabel; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 87 | |
| 88 | public SegmentRouterInfo() { |
Charles Chan | dc7d6f9 | 2018-03-20 13:30:38 -0700 | [diff] [blame] | 89 | gatewayIps = Multimaps.synchronizedSetMultimap(HashMultimap.create()); |
| 90 | subnets = Multimaps.synchronizedSetMultimap(HashMultimap.create()); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 91 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 92 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 93 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 94 | /** |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 95 | * Constructs device configuration for all Segment Router devices, |
| 96 | * organizing the data into various maps for easier access. |
Brian O'Connor | 65eeb57 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 97 | * |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 98 | * @param srManager Segment Routing Manager |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 99 | */ |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 100 | public DeviceConfiguration(SegmentRoutingManager srManager) { |
| 101 | this.srManager = srManager; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 102 | updateConfig(); |
| 103 | } |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 104 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 105 | public void updateConfig() { |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 106 | // Read config from device subject, excluding gatewayIps and subnets. |
| 107 | Set<DeviceId> deviceSubjects = |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 108 | srManager.cfgService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 109 | deviceSubjects.forEach(subject -> { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 110 | SegmentRoutingDeviceConfig config = |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 111 | srManager.cfgService.getConfig(subject, SegmentRoutingDeviceConfig.class); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 112 | SegmentRouterInfo info = new SegmentRouterInfo(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 113 | info.deviceId = subject; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 114 | info.ipv4NodeSid = config.nodeSidIPv4(); |
| 115 | info.ipv6NodeSid = config.nodeSidIPv6(); |
| 116 | info.ipv4Loopback = config.routerIpv4(); |
| 117 | info.ipv6Loopback = config.routerIpv6(); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 118 | info.mac = config.routerMac(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 119 | info.isEdge = config.isEdgeRouter(); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 120 | info.adjacencySids = config.adjacencySids(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 121 | info.pairDeviceId = config.pairDeviceId(); |
| 122 | info.pairLocalPort = config.pairLocalPort(); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 123 | info.pwRoutingLabel = info.ipv4NodeSid + 1000; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 124 | deviceConfigMap.put(info.deviceId, info); |
Charles Chan | 68363b1 | 2017-06-26 15:25:09 -0700 | [diff] [blame] | 125 | log.debug("Read device config for device: {}", info.deviceId); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 126 | /* |
| 127 | * IPv6 sid is not inserted. this part of the code is not used for now. |
| 128 | */ |
| 129 | allSegmentIds.add(info.ipv4NodeSid); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 130 | }); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 131 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 132 | // Read gatewayIps and subnets from port subject. Ignore suppressed ports. |
| 133 | Set<ConnectPoint> portSubjects = srManager.cfgService |
| 134 | .getSubjects(ConnectPoint.class, InterfaceConfig.class); |
Jonathan Hart | 61e24e1 | 2017-11-30 18:23:42 -0800 | [diff] [blame] | 135 | portSubjects.stream() |
| 136 | .filter(subject -> deviceConfigMap.containsKey(subject.deviceId())) |
| 137 | .filter(subject -> !isSuppressedPort(subject)).forEach(subject -> { |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 138 | InterfaceConfig config = |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 139 | srManager.cfgService.getConfig(subject, InterfaceConfig.class); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 140 | Set<Interface> networkInterfaces; |
| 141 | try { |
| 142 | networkInterfaces = config.getInterfaces(); |
| 143 | } catch (ConfigException e) { |
| 144 | log.error("Error loading port configuration"); |
| 145 | return; |
| 146 | } |
| 147 | networkInterfaces.forEach(networkInterface -> { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 148 | VlanId vlanId = networkInterface.vlan(); |
| 149 | ConnectPoint connectPoint = networkInterface.connectPoint(); |
| 150 | DeviceId dpid = connectPoint.deviceId(); |
| 151 | PortNumber port = connectPoint.port(); |
Charles Chan | 6191aca | 2017-09-12 12:09:22 -0700 | [diff] [blame] | 152 | MacAddress mac = networkInterface.mac(); |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 153 | SegmentRouterInfo info = deviceConfigMap.get(dpid); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 154 | |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 155 | // skip if there is no corresponding device for this ConenctPoint |
| 156 | if (info != null) { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 157 | // Extract subnet information |
Jonathan Hart | 8fa9ec5 | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 158 | List<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddressesList(); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 159 | interfaceAddresses.forEach(interfaceAddress -> { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 160 | // Do not add /0, /32 and /128 to gateway IP list |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 161 | int prefixLength = interfaceAddress.subnetAddress().prefixLength(); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 162 | IpPrefix ipPrefix = interfaceAddress.subnetAddress(); |
| 163 | if (ipPrefix.isIp4()) { |
| 164 | if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET_MASK_LENGTH) { |
| 165 | info.gatewayIps.put(port, interfaceAddress.ipAddress()); |
| 166 | } |
| 167 | info.subnets.put(port, interfaceAddress.subnetAddress()); |
| 168 | } else { |
| 169 | if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET6_MASK_LENGTH) { |
| 170 | info.gatewayIps.put(port, interfaceAddress.ipAddress()); |
| 171 | } |
| 172 | info.subnets.put(port, interfaceAddress.subnetAddress()); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 173 | } |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 174 | }); |
Charles Chan | 6191aca | 2017-09-12 12:09:22 -0700 | [diff] [blame] | 175 | |
| 176 | // Override interface mac with router mac |
| 177 | if (!mac.equals(info.mac)) { |
| 178 | ArrayNode array = (ArrayNode) config.node(); |
| 179 | for (JsonNode intfNode : array) { |
| 180 | ObjectNode objNode = (ObjectNode) intfNode; |
| 181 | objNode.put(InterfaceConfig.MAC, info.mac.toString()); |
| 182 | } |
| 183 | srManager.cfgService.applyConfig(connectPoint, InterfaceConfig.class, array); |
| 184 | } |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 185 | } |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 186 | }); |
Pier Luigi | 6a2643a | 2017-01-31 09:35:05 -0800 | [diff] [blame] | 187 | // We register the connect point with the NRS. |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 188 | srManager.registerConnectPoint(subject); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 189 | }); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Jonathan Hart | 61e24e1 | 2017-11-30 18:23:42 -0800 | [diff] [blame] | 192 | public Collection<DeviceId> getRouters() { |
| 193 | return deviceConfigMap.keySet(); |
| 194 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 195 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 196 | @Override |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 197 | public boolean isConfigured(DeviceId deviceId) { |
| 198 | return deviceConfigMap.get(deviceId) != null; |
| 199 | } |
| 200 | |
| 201 | @Override |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 202 | public int getIPv4SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 203 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 204 | if (srinfo != null) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 205 | log.trace("getIPv4SegmentId for device{} is {}", deviceId, srinfo.ipv4NodeSid); |
| 206 | return srinfo.ipv4NodeSid; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 207 | } else { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 208 | String message = "getIPv4SegmentId fails for device: " + deviceId + "."; |
| 209 | throw new DeviceConfigNotFoundException(message); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public int getIPv6SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException { |
| 215 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 216 | if (srinfo != null) { |
| 217 | log.trace("getIPv6SegmentId for device{} is {}", deviceId, srinfo.ipv6NodeSid); |
| 218 | return srinfo.ipv6NodeSid; |
| 219 | } else { |
| 220 | String message = "getIPv6SegmentId fails for device: " + deviceId + "."; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 221 | throw new DeviceConfigNotFoundException(message); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 225 | @Override |
| 226 | public int getPWRoutingLabel(DeviceId deviceId) throws DeviceConfigNotFoundException { |
| 227 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 228 | if (srinfo != null) { |
| 229 | log.trace("pwRoutingLabel for device{} is {}", deviceId, srinfo.pwRoutingLabel); |
| 230 | return srinfo.pwRoutingLabel; |
| 231 | } else { |
| 232 | String message = "getPWRoutingLabel fails for device: " + deviceId + "."; |
| 233 | throw new DeviceConfigNotFoundException(message); |
| 234 | } |
| 235 | } |
| 236 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 237 | /** |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 238 | * Returns the IPv4 Node segment id of a segment router given its Router mac address. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 239 | * |
| 240 | * @param routerMac router mac address |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 241 | * @return node segment id, or -1 if not found in config |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 242 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 243 | public int getIPv4SegmentId(MacAddress routerMac) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 244 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 245 | deviceConfigMap.entrySet()) { |
| 246 | if (entry.getValue().mac.equals(routerMac)) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 247 | return entry.getValue().ipv4NodeSid; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 250 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | /** |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 255 | * Returns the IPv6 Node segment id of a segment router given its Router mac address. |
| 256 | * |
| 257 | * @param routerMac router mac address |
| 258 | * @return node segment id, or -1 if not found in config |
| 259 | */ |
| 260 | public int getIPv6SegmentId(MacAddress routerMac) { |
| 261 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 262 | deviceConfigMap.entrySet()) { |
| 263 | if (entry.getValue().mac.equals(routerMac)) { |
| 264 | return entry.getValue().ipv6NodeSid; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return -1; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Returns the IPv4 Node segment id of a segment router given its Router ip address. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 273 | * |
| 274 | * @param routerAddress router ip address |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 275 | * @return node segment id, or -1 if not found in config |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 276 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 277 | public int getIPv4SegmentId(Ip4Address routerAddress) { |
Charles Chan | 3d35a54 | 2018-10-02 12:52:29 -0700 | [diff] [blame] | 278 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: deviceConfigMap.entrySet()) { |
| 279 | Ip4Address ipv4Loopback = entry.getValue().ipv4Loopback; |
| 280 | if (ipv4Loopback == null) { |
| 281 | continue; |
| 282 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 283 | if (entry.getValue().ipv4Loopback.equals(routerAddress)) { |
Charles Chan | 3d35a54 | 2018-10-02 12:52:29 -0700 | [diff] [blame] | 284 | if (entry.getValue().ipv4NodeSid == -1) { |
| 285 | continue; |
| 286 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 287 | return entry.getValue().ipv4NodeSid; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Returns the IPv6 Node segment id of a segment router given its Router ip address. |
| 296 | * |
| 297 | * @param routerAddress router ip address |
| 298 | * @return node segment id, or -1 if not found in config |
| 299 | */ |
| 300 | public int getIPv6SegmentId(Ip6Address routerAddress) { |
Charles Chan | 3d35a54 | 2018-10-02 12:52:29 -0700 | [diff] [blame] | 301 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: deviceConfigMap.entrySet()) { |
| 302 | Ip6Address ipv6Loopback = entry.getValue().ipv6Loopback; |
| 303 | if (ipv6Loopback == null) { |
| 304 | continue; |
| 305 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 306 | if (entry.getValue().ipv6Loopback.equals(routerAddress)) { |
Charles Chan | 3d35a54 | 2018-10-02 12:52:29 -0700 | [diff] [blame] | 307 | if (entry.getValue().ipv6NodeSid == -1) { |
| 308 | continue; |
| 309 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 310 | return entry.getValue().ipv6NodeSid; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | return -1; |
| 315 | } |
| 316 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 317 | @Override |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 318 | public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 319 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 320 | if (srinfo != null) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 321 | return srinfo.mac; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 322 | } else { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 323 | String message = "getDeviceMac fails for device: " + deviceId + "."; |
| 324 | throw new DeviceConfigNotFoundException(message); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 328 | @Override |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 329 | public Ip4Address getRouterIpv4(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 330 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 331 | if (srinfo != null) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 332 | log.trace("getRouterIpv4 for device{} is {}", deviceId, srinfo.ipv4Loopback); |
| 333 | return srinfo.ipv4Loopback; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 334 | } else { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 335 | String message = "getRouterIpv4 fails for device: " + deviceId + "."; |
| 336 | throw new DeviceConfigNotFoundException(message); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public Ip6Address getRouterIpv6(DeviceId deviceId) throws DeviceConfigNotFoundException { |
| 342 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 343 | if (srinfo != null) { |
| 344 | log.trace("getRouterIpv6 for device{} is {}", deviceId, srinfo.ipv6Loopback); |
| 345 | return srinfo.ipv6Loopback; |
| 346 | } else { |
| 347 | String message = "getRouterIpv6 fails for device: " + deviceId + "."; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 348 | throw new DeviceConfigNotFoundException(message); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 349 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 352 | @Override |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 353 | public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 354 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 355 | if (srinfo != null) { |
| 356 | log.trace("isEdgeDevice for device{} is {}", deviceId, srinfo.isEdge); |
| 357 | return srinfo.isEdge; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 358 | } else { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 359 | String message = "isEdgeDevice fails for device: " + deviceId + "."; |
| 360 | throw new DeviceConfigNotFoundException(message); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 364 | @Override |
| 365 | public List<Integer> getAllDeviceSegmentIds() { |
| 366 | return allSegmentIds; |
| 367 | } |
| 368 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 369 | @Override |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 370 | public Map<IpPrefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId) |
Saurav Das | 52d4ed7 | 2016-03-28 19:00:18 -0700 | [diff] [blame] | 371 | throws DeviceConfigNotFoundException { |
| 372 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 373 | if (srinfo == null) { |
| 374 | String message = "getSubnetPortsMap fails for device: " + deviceId + "."; |
| 375 | throw new DeviceConfigNotFoundException(message); |
| 376 | } |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 377 | // Construct subnet-port mapping from port-subnet mapping |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 378 | SetMultimap<PortNumber, IpPrefix> portSubnetMap = srinfo.subnets; |
| 379 | Map<IpPrefix, List<PortNumber>> subnetPortMap = new HashMap<>(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 380 | |
| 381 | portSubnetMap.entries().forEach(entry -> { |
| 382 | PortNumber port = entry.getKey(); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 383 | IpPrefix subnet = entry.getValue(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 384 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 385 | if (subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH || |
| 386 | subnet.prefixLength() == IpPrefix.MAX_INET6_MASK_LENGTH) { |
Charles Chan | bbd004c | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 387 | return; |
| 388 | } |
| 389 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 390 | if (subnetPortMap.containsKey(subnet)) { |
| 391 | subnetPortMap.get(subnet).add(port); |
| 392 | } else { |
| 393 | ArrayList<PortNumber> ports = new ArrayList<>(); |
| 394 | ports.add(port); |
| 395 | subnetPortMap.put(subnet, ports); |
| 396 | } |
| 397 | }); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 398 | return subnetPortMap; |
| 399 | } |
| 400 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 401 | /** |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 402 | * Returns the device identifier or data plane identifier (dpid) |
| 403 | * of a segment router given its segment id. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 404 | * |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 405 | * @param sid segment id |
| 406 | * @return deviceId device identifier |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 407 | */ |
| 408 | public DeviceId getDeviceId(int sid) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 409 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 410 | deviceConfigMap.entrySet()) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 411 | if (entry.getValue().ipv4NodeSid == sid || |
| 412 | entry.getValue().ipv6NodeSid == sid) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 413 | return entry.getValue().deviceId; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | return null; |
| 418 | } |
| 419 | |
| 420 | /** |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 421 | * Returns the device identifier or data plane identifier (dpid) |
| 422 | * of a segment router given its router ip address. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 423 | * |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 424 | * @param ipAddress router ip address |
| 425 | * @return deviceId device identifier |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 426 | */ |
| 427 | public DeviceId getDeviceId(Ip4Address ipAddress) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 428 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 429 | deviceConfigMap.entrySet()) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 430 | if (entry.getValue().ipv4Loopback.equals(ipAddress)) { |
| 431 | return entry.getValue().deviceId; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | return null; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Returns the device identifier or data plane identifier (dpid) |
| 440 | * of a segment router given its router ipv6 address. |
| 441 | * |
| 442 | * @param ipAddress router ipv6 address |
| 443 | * @return deviceId device identifier |
| 444 | */ |
| 445 | public DeviceId getDeviceId(Ip6Address ipAddress) { |
| 446 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 447 | deviceConfigMap.entrySet()) { |
| 448 | if (entry.getValue().ipv6Loopback.equals(ipAddress)) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 449 | return entry.getValue().deviceId; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | return null; |
| 454 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 455 | |
| 456 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 457 | * Returns the configured port ip addresses for a segment router. |
| 458 | * These addresses serve as gateway IP addresses for the subnets configured |
| 459 | * on those ports. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 460 | * |
| 461 | * @param deviceId device identifier |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 462 | * @return immutable set of ip addresses configured on the ports or null if not found |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 463 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 464 | public Set<IpAddress> getPortIPs(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 465 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 466 | if (srinfo != null) { |
| 467 | log.trace("getSubnetGatewayIps for device{} is {}", deviceId, |
| 468 | srinfo.gatewayIps.values()); |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 469 | return ImmutableSet.copyOf(srinfo.gatewayIps.values()); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 470 | } |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 471 | return null; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /** |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 475 | * Returns configured subnets for a segment router. |
| 476 | * |
| 477 | * @param deviceId device identifier |
| 478 | * @return list of ip prefixes or null if not found |
| 479 | */ |
| 480 | public Set<IpPrefix> getConfiguredSubnets(DeviceId deviceId) { |
| 481 | Set<IpPrefix> subnets = srManager.interfaceService.getInterfaces().stream() |
| 482 | .filter(intf -> Objects.equals(deviceId, intf.connectPoint().deviceId())) |
| 483 | .flatMap(intf -> intf.ipAddressesList().stream()) |
| 484 | .map(InterfaceIpAddress::subnetAddress) |
| 485 | .collect(Collectors.toSet()); |
| 486 | |
| 487 | if (subnets.isEmpty()) { |
| 488 | log.debug(NO_SUBNET, deviceId); |
| 489 | return Collections.emptySet(); |
| 490 | } |
| 491 | return subnets; |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Returns all subnets for a segment router, including subnets learnt from route service. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 496 | * |
| 497 | * @param deviceId device identifier |
Charles Chan | 19b7003 | 2019-04-17 14:20:26 -0700 | [diff] [blame^] | 498 | * @return set of ip prefixes or null if not found |
| 499 | * @deprecated use getBatchedSubnets(DeviceId deviceId) instead |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 500 | */ |
Charles Chan | 19b7003 | 2019-04-17 14:20:26 -0700 | [diff] [blame^] | 501 | @Deprecated |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 502 | public Set<IpPrefix> getSubnets(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 503 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Charles Chan | 5eb6604 | 2018-06-18 14:42:17 -0700 | [diff] [blame] | 504 | if (srinfo != null && srinfo.subnets != null) { |
| 505 | // Note: ImmutableSet.Builder.addAll calls the iterator of parameter internally, |
| 506 | // which is not protected by SynchronizedCollection mutex. |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 507 | ImmutableSet.Builder<IpPrefix> builder = ImmutableSet.builder(); |
Charles Chan | 5eb6604 | 2018-06-18 14:42:17 -0700 | [diff] [blame] | 508 | srinfo.subnets.forEach((k, v) -> builder.add(v)); |
| 509 | return builder.build(); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 510 | } |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 511 | return null; |
| 512 | } |
| 513 | |
Charles Chan | 19b7003 | 2019-04-17 14:20:26 -0700 | [diff] [blame^] | 514 | /** |
| 515 | * Returns batches of all subnets reachable on the given device. |
| 516 | * <p> |
| 517 | * First batch includes configured subnets, FPM and STATIC routes |
| 518 | * Second batch includes all other type of routes obtained from routeService, including DHCP routes. |
| 519 | * |
| 520 | * @param deviceId device identifier |
| 521 | * @return list of subnet batches, each batch includes a set of prefixes. |
| 522 | */ |
| 523 | // TODO Querying routeService directly may be expensive. Some kind of reverse lookup cache should be developed. |
| 524 | public List<Set<IpPrefix>> getBatchedSubnets(DeviceId deviceId) { |
| 525 | Set<IpPrefix> high = Sets.newHashSet(); |
| 526 | Set<IpPrefix> low = Sets.newHashSet(); |
| 527 | |
| 528 | high.addAll(getConfiguredSubnets(deviceId)); |
| 529 | srManager.routeService.getRouteTables().stream() |
| 530 | .map(tableId -> srManager.routeService.getResolvedRoutes(tableId)) |
| 531 | .flatMap(Collection::stream) |
| 532 | .forEach(resolvedRoute -> { |
| 533 | // Continue to next resolved route if none of the next hop attaches to given device |
| 534 | if (srManager.nextHopLocations(resolvedRoute).stream() |
| 535 | .noneMatch(cp -> Objects.equals(deviceId, cp.deviceId()))) { |
| 536 | return; |
| 537 | } |
| 538 | // Prioritize STATIC and FPM among others |
| 539 | if (resolvedRoute.route().source() == Route.Source.STATIC || |
| 540 | resolvedRoute.route().source() == Route.Source.FPM) { |
| 541 | high.add(resolvedRoute.prefix()); |
| 542 | } else { |
| 543 | low.add(resolvedRoute.prefix()); |
| 544 | } |
| 545 | }); |
| 546 | return Lists.newArrayList(high, low); |
| 547 | } |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 548 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 549 | /** |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 550 | * Returns the subnet configuration of given device and port. |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 551 | * |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 552 | * @param deviceId Device ID |
| 553 | * @param port Port number |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 554 | * @return The subnets configured on given port or empty set if |
Charles Chan | 2f4d2bc | 2017-04-24 16:21:01 -0700 | [diff] [blame] | 555 | * the port is unconfigured or suppressed. |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 556 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 557 | public Set<IpPrefix> getPortSubnets(DeviceId deviceId, PortNumber port) { |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 558 | ConnectPoint connectPoint = new ConnectPoint(deviceId, port); |
| 559 | |
| 560 | if (isSuppressedPort(connectPoint)) { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 561 | return Collections.emptySet(); |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 562 | } |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 563 | |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 564 | Set<IpPrefix> subnets = srManager.interfaceService.getInterfacesByPort(connectPoint).stream() |
| 565 | .flatMap(intf -> intf.ipAddressesList().stream()) |
| 566 | .map(InterfaceIpAddress::subnetAddress) |
| 567 | .collect(Collectors.toSet()); |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 568 | |
Jon Hall | 31d8478 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 569 | if (subnets.isEmpty()) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 570 | log.debug(NO_SUBNET, connectPoint); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 571 | return Collections.emptySet(); |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 572 | } |
Charles Chan | 2f4d2bc | 2017-04-24 16:21:01 -0700 | [diff] [blame] | 573 | return subnets; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /** |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 577 | * Returns all ports that has a subnet that contains any of the given IP addresses. |
| 578 | * |
| 579 | * @param ips a set of IP addresses |
| 580 | * @return a set of connect point that has a subnet that contains any of the given IP addresses |
| 581 | */ |
| 582 | public Set<ConnectPoint> getPortByIps(Set<IpAddress> ips) { |
| 583 | return srManager.interfaceService.getInterfaces().stream() |
| 584 | .filter(intf -> intf.ipAddressesList().stream().anyMatch(intfAddress -> |
| 585 | ips.stream().anyMatch(ip -> intfAddress.subnetAddress().contains(ip)))) |
| 586 | .map(Interface::connectPoint) |
| 587 | .collect(Collectors.toSet()); |
| 588 | } |
| 589 | |
| 590 | /** |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 591 | * Returns the router ip address of segment router that has the |
| 592 | * specified ip address in its subnets. |
| 593 | * |
| 594 | * @param destIpAddress target ip address |
| 595 | * @return router ip address |
| 596 | */ |
| 597 | public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) { |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 598 | Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress); |
| 599 | |
| 600 | if (matchIntf == null) { |
| 601 | log.debug("No router was found for {}", destIpAddress); |
| 602 | return null; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 605 | DeviceId routerDeviceId = matchIntf.connectPoint().deviceId(); |
| 606 | SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId); |
| 607 | if (srInfo == null) { |
| 608 | log.debug("No device config was found for {}", routerDeviceId); |
| 609 | return null; |
| 610 | } |
| 611 | |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 612 | return srInfo.ipv4Loopback; |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Returns the router ipv6 address of segment router that has the |
| 617 | * specified ip address in its subnets. |
| 618 | * |
| 619 | * @param destIpAddress target ip address |
| 620 | * @return router ip address |
| 621 | */ |
| 622 | public Ip6Address getRouterIpAddressForASubnetHost(Ip6Address destIpAddress) { |
| 623 | Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress); |
| 624 | |
| 625 | if (matchIntf == null) { |
| 626 | log.debug("No router was found for {}", destIpAddress); |
| 627 | return null; |
| 628 | } |
| 629 | |
| 630 | DeviceId routerDeviceId = matchIntf.connectPoint().deviceId(); |
| 631 | SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId); |
| 632 | if (srInfo == null) { |
| 633 | log.debug("No device config was found for {}", routerDeviceId); |
| 634 | return null; |
| 635 | } |
| 636 | |
| 637 | return srInfo.ipv6Loopback; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Returns the router mac address of segment router that has the |
| 642 | * specified ip address as one of its subnet gateway ip address. |
| 643 | * |
| 644 | * @param gatewayIpAddress router gateway ip address |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 645 | * @return router mac address or null if not found |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 646 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 647 | public MacAddress getRouterMacForAGatewayIp(IpAddress gatewayIpAddress) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 648 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 649 | deviceConfigMap.entrySet()) { |
| 650 | if (entry.getValue().gatewayIps. |
| 651 | values().contains(gatewayIpAddress)) { |
| 652 | return entry.getValue().mac; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | log.debug("Cannot find a router for {}", gatewayIpAddress); |
| 657 | return null; |
| 658 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 659 | |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 660 | /** |
Charles Chan | 35f21e4 | 2017-06-26 18:30:18 -0700 | [diff] [blame] | 661 | * Checks if the host IP is in any of the subnet defined in the router with the |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 662 | * device ID given. |
| 663 | * |
| 664 | * @param deviceId device identification of the router |
| 665 | * @param hostIp host IP address to check |
Charles Chan | 35f21e4 | 2017-06-26 18:30:18 -0700 | [diff] [blame] | 666 | * @return true if the given IP is within any of the subnet defined in the router, |
| 667 | * false if no subnet is defined in the router or if the host is not |
| 668 | * within any subnet defined in the router |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 669 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 670 | public boolean inSameSubnet(DeviceId deviceId, IpAddress hostIp) { |
Charles Chan | cf78982 | 2019-03-22 10:04:27 -0700 | [diff] [blame] | 671 | Set<IpPrefix> subnets = getConfiguredSubnets(deviceId); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 672 | if (subnets == null) { |
| 673 | return false; |
| 674 | } |
| 675 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 676 | for (IpPrefix subnet: subnets) { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 677 | // Exclude /0 since it is a special case used for default route |
| 678 | if (subnet.prefixLength() != 0 && subnet.contains(hostIp)) { |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 679 | return true; |
| 680 | } |
| 681 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 682 | return false; |
| 683 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 684 | |
| 685 | /** |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 686 | * Checks if the IP is in the subnet defined on given connect point. |
| 687 | * |
| 688 | * @param connectPoint Connect point |
| 689 | * @param ip The IP address to check |
| 690 | * @return True if the IP belongs to the subnet. |
| 691 | * False if the IP does not belong to the subnet, or |
| 692 | * there is no subnet configuration on given connect point. |
| 693 | */ |
| 694 | public boolean inSameSubnet(ConnectPoint connectPoint, IpAddress ip) { |
Charles Chan | 35f21e4 | 2017-06-26 18:30:18 -0700 | [diff] [blame] | 695 | return getPortSubnets(connectPoint.deviceId(), connectPoint.port()).stream() |
| 696 | .anyMatch(ipPrefix -> ipPrefix.contains(ip)); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /** |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 700 | * Returns the ports corresponding to the adjacency Sid given. |
| 701 | * |
| 702 | * @param deviceId device identification of the router |
| 703 | * @param sid adjacency Sid |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 704 | * @return set of port numbers |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 705 | */ |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 706 | public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 707 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 708 | return srinfo != null ? |
| 709 | ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) : |
| 710 | ImmutableSet.copyOf(new HashSet<>()); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Check if the Sid given is whether adjacency Sid of the router device or not. |
| 715 | * |
| 716 | * @param deviceId device identification of the router |
| 717 | * @param sid Sid to check |
| 718 | * @return true if the Sid given is the adjacency Sid of the device, |
| 719 | * otherwise false |
| 720 | */ |
| 721 | public boolean isAdjacencySid(DeviceId deviceId, int sid) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 722 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 723 | return srinfo != null && srinfo.adjacencySids.containsKey(sid); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 724 | } |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 725 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 726 | /** |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 727 | * Add subnet to specific connect point. |
| 728 | * |
| 729 | * @param cp connect point |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 730 | * @param ipPrefix subnet being added to the device |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 731 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 732 | public void addSubnet(ConnectPoint cp, IpPrefix ipPrefix) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 733 | checkNotNull(cp); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 734 | checkNotNull(ipPrefix); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 735 | SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId()); |
| 736 | if (srinfo == null) { |
| 737 | log.warn("Device {} is not configured. Abort.", cp.deviceId()); |
| 738 | return; |
| 739 | } |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 740 | srinfo.subnets.put(cp.port(), ipPrefix); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /** |
| 744 | * Remove subnet from specific connect point. |
| 745 | * |
| 746 | * @param cp connect point |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 747 | * @param ipPrefix subnet being removed to the device |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 748 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 749 | public void removeSubnet(ConnectPoint cp, IpPrefix ipPrefix) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 750 | checkNotNull(cp); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 751 | checkNotNull(ipPrefix); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 752 | SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId()); |
| 753 | if (srinfo == null) { |
| 754 | log.warn("Device {} is not configured. Abort.", cp.deviceId()); |
| 755 | return; |
| 756 | } |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 757 | srinfo.subnets.remove(cp.port(), ipPrefix); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 758 | } |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 759 | |
| 760 | private boolean isSuppressedPort(ConnectPoint connectPoint) { |
| 761 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 762 | .getConfig(srManager.appId(), SegmentRoutingAppConfig.class); |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 763 | if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) { |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 764 | log.info("Interface configuration on port {} is ignored", connectPoint); |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 765 | return true; |
| 766 | } |
| 767 | return false; |
| 768 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 769 | |
| 770 | public boolean isPairedEdge(DeviceId deviceId) throws DeviceConfigNotFoundException { |
| 771 | if (!isEdgeDevice(deviceId)) { |
| 772 | return false; |
| 773 | } |
| 774 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 775 | return (srinfo.pairDeviceId == null) ? false : true; |
| 776 | } |
| 777 | |
| 778 | public DeviceId getPairDeviceId(DeviceId deviceId) throws DeviceConfigNotFoundException { |
| 779 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 780 | if (srinfo != null) { |
| 781 | return srinfo.pairDeviceId; |
| 782 | } else { |
| 783 | String message = "getPairDeviceId fails for device: " + deviceId + "."; |
| 784 | throw new DeviceConfigNotFoundException(message); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | public PortNumber getPairLocalPort(DeviceId deviceId) |
| 789 | throws DeviceConfigNotFoundException { |
| 790 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 791 | if (srinfo != null) { |
| 792 | return srinfo.pairLocalPort; |
| 793 | } else { |
| 794 | String message = "getPairLocalPort fails for device: " + deviceId + "."; |
| 795 | throw new DeviceConfigNotFoundException(message); |
| 796 | } |
| 797 | } |
| 798 | |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 799 | public boolean isPairLocalPort(DeviceId devId, PortNumber pnum) { |
| 800 | return pnum.equals(srManager.getPairLocalPort(devId).orElse(null)); |
| 801 | } |
Jonathan Hart | 8fa9ec5 | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 802 | } |