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