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