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