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