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