Thomas Vachuska | 8fd2505 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 8fd2505 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 16 | package org.onosproject.segmentrouting.config; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 17 | |
Charles Chan | 6191aca | 2017-09-12 12:09:22 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 21 | import com.google.common.collect.HashMultimap; |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 23 | import com.google.common.collect.SetMultimap; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 24 | import org.onlab.packet.Ip4Address; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 25 | import org.onlab.packet.Ip6Address; |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 27 | import org.onlab.packet.IpPrefix; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 28 | import org.onlab.packet.MacAddress; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 29 | import org.onlab.packet.VlanId; |
Ray Milkey | c256783 | 2017-08-15 10:16:43 -0700 | [diff] [blame] | 30 | import org.onosproject.net.config.ConfigException; |
| 31 | import org.onosproject.net.config.basics.InterfaceConfig; |
Ray Milkey | c2d43be | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 32 | import org.onosproject.net.intf.Interface; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 33 | import org.onosproject.net.ConnectPoint; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 34 | import org.onosproject.net.host.InterfaceIpAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 36 | import org.onosproject.net.PortNumber; |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 37 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 41 | import java.util.ArrayList; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 42 | import java.util.Collections; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 43 | import java.util.HashMap; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 44 | import java.util.HashSet; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 45 | import java.util.List; |
| 46 | import java.util.Map; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 47 | import java.util.Set; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 48 | import java.util.concurrent.ConcurrentHashMap; |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 49 | import java.util.stream.Collectors; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 50 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 51 | import static com.google.common.base.Preconditions.checkNotNull; |
| 52 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Segment Routing configuration component that reads the |
| 55 | * segment routing related configuration from Network Configuration Manager |
| 56 | * component and organizes in more accessible formats. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 57 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 58 | public class DeviceConfiguration implements DeviceProperties { |
| 59 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 60 | private static final String NO_SUBNET = "No subnet configured on {}"; |
| 61 | |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 62 | private static final Logger log = LoggerFactory.getLogger(DeviceConfiguration.class); |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 63 | private final List<Integer> allSegmentIds = new ArrayList<>(); |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 64 | private final Map<DeviceId, SegmentRouterInfo> deviceConfigMap = new ConcurrentHashMap<>(); |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 65 | private SegmentRoutingManager srManager; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 66 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 67 | private class SegmentRouterInfo { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 68 | int ipv4NodeSid; |
| 69 | int ipv6NodeSid; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 70 | DeviceId deviceId; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 71 | Ip4Address ipv4Loopback; |
| 72 | Ip6Address ipv6Loopback; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 73 | MacAddress mac; |
| 74 | boolean isEdge; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 75 | SetMultimap<PortNumber, IpAddress> gatewayIps; |
| 76 | SetMultimap<PortNumber, IpPrefix> subnets; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 77 | Map<Integer, Set<Integer>> adjacencySids; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 78 | DeviceId pairDeviceId; |
| 79 | PortNumber pairLocalPort; |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame^] | 80 | int pwRoutingLabel; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 81 | |
| 82 | public SegmentRouterInfo() { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 83 | gatewayIps = HashMultimap.create(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 84 | subnets = HashMultimap.create(); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 85 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 86 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 87 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 88 | /** |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 89 | * Constructs device configuration for all Segment Router devices, |
| 90 | * organizing the data into various maps for easier access. |
Brian O'Connor | 65eeb57 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 91 | * |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 92 | * @param srManager Segment Routing Manager |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 93 | */ |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 94 | public DeviceConfiguration(SegmentRoutingManager srManager) { |
| 95 | this.srManager = srManager; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 96 | updateConfig(); |
| 97 | } |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 98 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 99 | public void updateConfig() { |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 100 | // Read config from device subject, excluding gatewayIps and subnets. |
| 101 | Set<DeviceId> deviceSubjects = |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 102 | srManager.cfgService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 103 | deviceSubjects.forEach(subject -> { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 104 | SegmentRoutingDeviceConfig config = |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 105 | srManager.cfgService.getConfig(subject, SegmentRoutingDeviceConfig.class); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 106 | SegmentRouterInfo info = new SegmentRouterInfo(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 107 | info.deviceId = subject; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 108 | info.ipv4NodeSid = config.nodeSidIPv4(); |
| 109 | info.ipv6NodeSid = config.nodeSidIPv6(); |
| 110 | info.ipv4Loopback = config.routerIpv4(); |
| 111 | info.ipv6Loopback = config.routerIpv6(); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 112 | info.mac = config.routerMac(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 113 | info.isEdge = config.isEdgeRouter(); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 114 | info.adjacencySids = config.adjacencySids(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 115 | info.pairDeviceId = config.pairDeviceId(); |
| 116 | info.pairLocalPort = config.pairLocalPort(); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame^] | 117 | info.pwRoutingLabel = info.ipv4NodeSid + 1000; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 118 | deviceConfigMap.put(info.deviceId, info); |
Charles Chan | 68363b1 | 2017-06-26 15:25:09 -0700 | [diff] [blame] | 119 | log.debug("Read device config for device: {}", info.deviceId); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 120 | /* |
| 121 | * IPv6 sid is not inserted. this part of the code is not used for now. |
| 122 | */ |
| 123 | allSegmentIds.add(info.ipv4NodeSid); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 124 | }); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 125 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 126 | // Read gatewayIps and subnets from port subject. Ignore suppressed ports. |
| 127 | Set<ConnectPoint> portSubjects = srManager.cfgService |
| 128 | .getSubjects(ConnectPoint.class, InterfaceConfig.class); |
| 129 | portSubjects.stream().filter(subject -> !isSuppressedPort(subject)).forEach(subject -> { |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 130 | InterfaceConfig config = |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 131 | srManager.cfgService.getConfig(subject, InterfaceConfig.class); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 132 | Set<Interface> networkInterfaces; |
| 133 | try { |
| 134 | networkInterfaces = config.getInterfaces(); |
| 135 | } catch (ConfigException e) { |
| 136 | log.error("Error loading port configuration"); |
| 137 | return; |
| 138 | } |
| 139 | networkInterfaces.forEach(networkInterface -> { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 140 | VlanId vlanId = networkInterface.vlan(); |
| 141 | ConnectPoint connectPoint = networkInterface.connectPoint(); |
| 142 | DeviceId dpid = connectPoint.deviceId(); |
| 143 | PortNumber port = connectPoint.port(); |
Charles Chan | 6191aca | 2017-09-12 12:09:22 -0700 | [diff] [blame] | 144 | MacAddress mac = networkInterface.mac(); |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 145 | SegmentRouterInfo info = deviceConfigMap.get(dpid); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 146 | |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 147 | // skip if there is no corresponding device for this ConenctPoint |
| 148 | if (info != null) { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 149 | // Extract subnet information |
Jonathan Hart | 8fa9ec5 | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 150 | List<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddressesList(); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 151 | interfaceAddresses.forEach(interfaceAddress -> { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 152 | // Do not add /0, /32 and /128 to gateway IP list |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 153 | int prefixLength = interfaceAddress.subnetAddress().prefixLength(); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 154 | IpPrefix ipPrefix = interfaceAddress.subnetAddress(); |
| 155 | if (ipPrefix.isIp4()) { |
| 156 | if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET_MASK_LENGTH) { |
| 157 | info.gatewayIps.put(port, interfaceAddress.ipAddress()); |
| 158 | } |
| 159 | info.subnets.put(port, interfaceAddress.subnetAddress()); |
| 160 | } else { |
| 161 | if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET6_MASK_LENGTH) { |
| 162 | info.gatewayIps.put(port, interfaceAddress.ipAddress()); |
| 163 | } |
| 164 | info.subnets.put(port, interfaceAddress.subnetAddress()); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 165 | } |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 166 | }); |
Charles Chan | 6191aca | 2017-09-12 12:09:22 -0700 | [diff] [blame] | 167 | |
| 168 | // Override interface mac with router mac |
| 169 | if (!mac.equals(info.mac)) { |
| 170 | ArrayNode array = (ArrayNode) config.node(); |
| 171 | for (JsonNode intfNode : array) { |
| 172 | ObjectNode objNode = (ObjectNode) intfNode; |
| 173 | objNode.put(InterfaceConfig.MAC, info.mac.toString()); |
| 174 | } |
| 175 | srManager.cfgService.applyConfig(connectPoint, InterfaceConfig.class, array); |
| 176 | } |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 177 | } |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 178 | }); |
Pier Luigi | 6a2643a | 2017-01-31 09:35:05 -0800 | [diff] [blame] | 179 | // We register the connect point with the NRS. |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 180 | srManager.registerConnectPoint(subject); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 181 | }); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 184 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 185 | @Override |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 186 | public boolean isConfigured(DeviceId deviceId) { |
| 187 | return deviceConfigMap.get(deviceId) != null; |
| 188 | } |
| 189 | |
| 190 | @Override |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 191 | public int getIPv4SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 192 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame^] | 193 | log.info("DEVICE MAP IS {}", deviceConfigMap); |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 194 | if (srinfo != null) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 195 | log.trace("getIPv4SegmentId for device{} is {}", deviceId, srinfo.ipv4NodeSid); |
| 196 | return srinfo.ipv4NodeSid; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 197 | } else { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 198 | String message = "getIPv4SegmentId fails for device: " + deviceId + "."; |
| 199 | throw new DeviceConfigNotFoundException(message); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public int getIPv6SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException { |
| 205 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 206 | if (srinfo != null) { |
| 207 | log.trace("getIPv6SegmentId for device{} is {}", deviceId, srinfo.ipv6NodeSid); |
| 208 | return srinfo.ipv6NodeSid; |
| 209 | } else { |
| 210 | String message = "getIPv6SegmentId fails for device: " + deviceId + "."; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 211 | throw new DeviceConfigNotFoundException(message); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame^] | 215 | @Override |
| 216 | public int getPWRoutingLabel(DeviceId deviceId) throws DeviceConfigNotFoundException { |
| 217 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 218 | if (srinfo != null) { |
| 219 | log.trace("pwRoutingLabel for device{} is {}", deviceId, srinfo.pwRoutingLabel); |
| 220 | return srinfo.pwRoutingLabel; |
| 221 | } else { |
| 222 | String message = "getPWRoutingLabel fails for device: " + deviceId + "."; |
| 223 | throw new DeviceConfigNotFoundException(message); |
| 224 | } |
| 225 | } |
| 226 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 227 | /** |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 228 | * 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] | 229 | * |
| 230 | * @param routerMac router mac address |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 231 | * @return node segment id, or -1 if not found in config |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 232 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 233 | public int getIPv4SegmentId(MacAddress routerMac) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 234 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 235 | deviceConfigMap.entrySet()) { |
| 236 | if (entry.getValue().mac.equals(routerMac)) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 237 | return entry.getValue().ipv4NodeSid; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 240 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | /** |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 245 | * Returns the IPv6 Node segment id of a segment router given its Router mac address. |
| 246 | * |
| 247 | * @param routerMac router mac address |
| 248 | * @return node segment id, or -1 if not found in config |
| 249 | */ |
| 250 | public int getIPv6SegmentId(MacAddress routerMac) { |
| 251 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 252 | deviceConfigMap.entrySet()) { |
| 253 | if (entry.getValue().mac.equals(routerMac)) { |
| 254 | return entry.getValue().ipv6NodeSid; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * 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] | 263 | * |
| 264 | * @param routerAddress router ip address |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 265 | * @return node segment id, or -1 if not found in config |
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 | public int getIPv4SegmentId(Ip4Address routerAddress) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 268 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 269 | deviceConfigMap.entrySet()) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 270 | if (entry.getValue().ipv4Loopback.equals(routerAddress)) { |
| 271 | return entry.getValue().ipv4NodeSid; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | return -1; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Returns the IPv6 Node segment id of a segment router given its Router ip address. |
| 280 | * |
| 281 | * @param routerAddress router ip address |
| 282 | * @return node segment id, or -1 if not found in config |
| 283 | */ |
| 284 | public int getIPv6SegmentId(Ip6Address routerAddress) { |
| 285 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 286 | deviceConfigMap.entrySet()) { |
| 287 | if (entry.getValue().ipv6Loopback.equals(routerAddress)) { |
| 288 | return entry.getValue().ipv6NodeSid; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | return -1; |
| 293 | } |
| 294 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 295 | @Override |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 296 | public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 297 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 298 | if (srinfo != null) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 299 | return srinfo.mac; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 300 | } else { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 301 | String message = "getDeviceMac fails for device: " + deviceId + "."; |
| 302 | throw new DeviceConfigNotFoundException(message); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 306 | @Override |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 307 | public Ip4Address getRouterIpv4(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 308 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 309 | if (srinfo != null) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 310 | log.trace("getRouterIpv4 for device{} is {}", deviceId, srinfo.ipv4Loopback); |
| 311 | return srinfo.ipv4Loopback; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 312 | } else { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 313 | String message = "getRouterIpv4 fails for device: " + deviceId + "."; |
| 314 | throw new DeviceConfigNotFoundException(message); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public Ip6Address getRouterIpv6(DeviceId deviceId) throws DeviceConfigNotFoundException { |
| 320 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 321 | if (srinfo != null) { |
| 322 | log.trace("getRouterIpv6 for device{} is {}", deviceId, srinfo.ipv6Loopback); |
| 323 | return srinfo.ipv6Loopback; |
| 324 | } else { |
| 325 | String message = "getRouterIpv6 fails for device: " + deviceId + "."; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 326 | throw new DeviceConfigNotFoundException(message); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 327 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 330 | @Override |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 331 | public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 332 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 333 | if (srinfo != null) { |
| 334 | log.trace("isEdgeDevice for device{} is {}", deviceId, srinfo.isEdge); |
| 335 | return srinfo.isEdge; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 336 | } else { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 337 | String message = "isEdgeDevice fails for device: " + deviceId + "."; |
| 338 | throw new DeviceConfigNotFoundException(message); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 342 | @Override |
| 343 | public List<Integer> getAllDeviceSegmentIds() { |
| 344 | return allSegmentIds; |
| 345 | } |
| 346 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 347 | @Override |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 348 | public Map<IpPrefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId) |
Saurav Das | 52d4ed7 | 2016-03-28 19:00:18 -0700 | [diff] [blame] | 349 | throws DeviceConfigNotFoundException { |
| 350 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 351 | if (srinfo == null) { |
| 352 | String message = "getSubnetPortsMap fails for device: " + deviceId + "."; |
| 353 | throw new DeviceConfigNotFoundException(message); |
| 354 | } |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 355 | // Construct subnet-port mapping from port-subnet mapping |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 356 | SetMultimap<PortNumber, IpPrefix> portSubnetMap = srinfo.subnets; |
| 357 | Map<IpPrefix, List<PortNumber>> subnetPortMap = new HashMap<>(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 358 | |
| 359 | portSubnetMap.entries().forEach(entry -> { |
| 360 | PortNumber port = entry.getKey(); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 361 | IpPrefix subnet = entry.getValue(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 362 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 363 | if (subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH || |
| 364 | subnet.prefixLength() == IpPrefix.MAX_INET6_MASK_LENGTH) { |
Charles Chan | bbd004c | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 365 | return; |
| 366 | } |
| 367 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 368 | if (subnetPortMap.containsKey(subnet)) { |
| 369 | subnetPortMap.get(subnet).add(port); |
| 370 | } else { |
| 371 | ArrayList<PortNumber> ports = new ArrayList<>(); |
| 372 | ports.add(port); |
| 373 | subnetPortMap.put(subnet, ports); |
| 374 | } |
| 375 | }); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 376 | return subnetPortMap; |
| 377 | } |
| 378 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 379 | /** |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 380 | * Returns the device identifier or data plane identifier (dpid) |
| 381 | * of a segment router given its segment id. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 382 | * |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 383 | * @param sid segment id |
| 384 | * @return deviceId device identifier |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 385 | */ |
| 386 | public DeviceId getDeviceId(int sid) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 387 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 388 | deviceConfigMap.entrySet()) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 389 | if (entry.getValue().ipv4NodeSid == sid || |
| 390 | entry.getValue().ipv6NodeSid == sid) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 391 | return entry.getValue().deviceId; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | return null; |
| 396 | } |
| 397 | |
| 398 | /** |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 399 | * Returns the device identifier or data plane identifier (dpid) |
| 400 | * of a segment router given its router ip address. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 401 | * |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 402 | * @param ipAddress router ip address |
| 403 | * @return deviceId device identifier |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 404 | */ |
| 405 | public DeviceId getDeviceId(Ip4Address ipAddress) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 406 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 407 | deviceConfigMap.entrySet()) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 408 | if (entry.getValue().ipv4Loopback.equals(ipAddress)) { |
| 409 | return entry.getValue().deviceId; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | return null; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Returns the device identifier or data plane identifier (dpid) |
| 418 | * of a segment router given its router ipv6 address. |
| 419 | * |
| 420 | * @param ipAddress router ipv6 address |
| 421 | * @return deviceId device identifier |
| 422 | */ |
| 423 | public DeviceId getDeviceId(Ip6Address ipAddress) { |
| 424 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 425 | deviceConfigMap.entrySet()) { |
| 426 | if (entry.getValue().ipv6Loopback.equals(ipAddress)) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 427 | return entry.getValue().deviceId; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | return null; |
| 432 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 433 | |
| 434 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 435 | * Returns the configured port ip addresses for a segment router. |
| 436 | * These addresses serve as gateway IP addresses for the subnets configured |
| 437 | * on those ports. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 438 | * |
| 439 | * @param deviceId device identifier |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 440 | * @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] | 441 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 442 | public Set<IpAddress> getPortIPs(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 443 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 444 | if (srinfo != null) { |
| 445 | log.trace("getSubnetGatewayIps for device{} is {}", deviceId, |
| 446 | srinfo.gatewayIps.values()); |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 447 | return ImmutableSet.copyOf(srinfo.gatewayIps.values()); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 448 | } |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 449 | return null; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Returns the configured subnet prefixes for a segment router. |
| 454 | * |
| 455 | * @param deviceId device identifier |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 456 | * @return list of ip prefixes or null if not found |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 457 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 458 | public Set<IpPrefix> getSubnets(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 459 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 460 | if (srinfo != null) { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 461 | ImmutableSet.Builder<IpPrefix> builder = ImmutableSet.builder(); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 462 | return builder.addAll(srinfo.subnets.values()).build(); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 463 | } |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 464 | return null; |
| 465 | } |
| 466 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 467 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 468 | /** |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 469 | * Returns the subnet configuration of given device and port. |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 470 | * |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 471 | * @param deviceId Device ID |
| 472 | * @param port Port number |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 473 | * @return The subnets configured on given port or empty set if |
Charles Chan | 2f4d2bc | 2017-04-24 16:21:01 -0700 | [diff] [blame] | 474 | * the port is unconfigured or suppressed. |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 475 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 476 | public Set<IpPrefix> getPortSubnets(DeviceId deviceId, PortNumber port) { |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 477 | ConnectPoint connectPoint = new ConnectPoint(deviceId, port); |
| 478 | |
| 479 | if (isSuppressedPort(connectPoint)) { |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 480 | return Collections.emptySet(); |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 481 | } |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 482 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 483 | Set<IpPrefix> subnets = |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 484 | srManager.interfaceService.getInterfacesByPort(connectPoint).stream() |
| 485 | .flatMap(intf -> intf.ipAddressesList().stream()) |
| 486 | .map(InterfaceIpAddress::subnetAddress) |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 487 | .collect(Collectors.toSet()); |
| 488 | |
Jon Hall | 31d8478 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 489 | if (subnets.isEmpty()) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 490 | log.debug(NO_SUBNET, connectPoint); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 491 | return Collections.emptySet(); |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 492 | } |
Charles Chan | 2f4d2bc | 2017-04-24 16:21:01 -0700 | [diff] [blame] | 493 | |
| 494 | return subnets; |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /** |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 498 | * Returns the router ip address of segment router that has the |
| 499 | * specified ip address in its subnets. |
| 500 | * |
| 501 | * @param destIpAddress target ip address |
| 502 | * @return router ip address |
| 503 | */ |
| 504 | public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) { |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 505 | Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress); |
| 506 | |
| 507 | if (matchIntf == null) { |
| 508 | log.debug("No router was found for {}", destIpAddress); |
| 509 | return null; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 512 | DeviceId routerDeviceId = matchIntf.connectPoint().deviceId(); |
| 513 | SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId); |
| 514 | if (srInfo == null) { |
| 515 | log.debug("No device config was found for {}", routerDeviceId); |
| 516 | return null; |
| 517 | } |
| 518 | |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 519 | return srInfo.ipv4Loopback; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Returns the router ipv6 address of segment router that has the |
| 524 | * specified ip address in its subnets. |
| 525 | * |
| 526 | * @param destIpAddress target ip address |
| 527 | * @return router ip address |
| 528 | */ |
| 529 | public Ip6Address getRouterIpAddressForASubnetHost(Ip6Address destIpAddress) { |
| 530 | Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress); |
| 531 | |
| 532 | if (matchIntf == null) { |
| 533 | log.debug("No router was found for {}", destIpAddress); |
| 534 | return null; |
| 535 | } |
| 536 | |
| 537 | DeviceId routerDeviceId = matchIntf.connectPoint().deviceId(); |
| 538 | SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId); |
| 539 | if (srInfo == null) { |
| 540 | log.debug("No device config was found for {}", routerDeviceId); |
| 541 | return null; |
| 542 | } |
| 543 | |
| 544 | return srInfo.ipv6Loopback; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Returns the router mac address of segment router that has the |
| 549 | * specified ip address as one of its subnet gateway ip address. |
| 550 | * |
| 551 | * @param gatewayIpAddress router gateway ip address |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 552 | * @return router mac address or null if not found |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 553 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 554 | public MacAddress getRouterMacForAGatewayIp(IpAddress gatewayIpAddress) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 555 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 556 | deviceConfigMap.entrySet()) { |
| 557 | if (entry.getValue().gatewayIps. |
| 558 | values().contains(gatewayIpAddress)) { |
| 559 | return entry.getValue().mac; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | log.debug("Cannot find a router for {}", gatewayIpAddress); |
| 564 | return null; |
| 565 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 566 | |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 567 | /** |
Charles Chan | 35f21e4 | 2017-06-26 18:30:18 -0700 | [diff] [blame] | 568 | * 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] | 569 | * device ID given. |
| 570 | * |
| 571 | * @param deviceId device identification of the router |
| 572 | * @param hostIp host IP address to check |
Charles Chan | 35f21e4 | 2017-06-26 18:30:18 -0700 | [diff] [blame] | 573 | * @return true if the given IP is within any of the subnet defined in the router, |
| 574 | * false if no subnet is defined in the router or if the host is not |
| 575 | * within any subnet defined in the router |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 576 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 577 | public boolean inSameSubnet(DeviceId deviceId, IpAddress hostIp) { |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 578 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 579 | Set<IpPrefix> subnets = getSubnets(deviceId); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 580 | if (subnets == null) { |
| 581 | return false; |
| 582 | } |
| 583 | |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 584 | for (IpPrefix subnet: subnets) { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 585 | // Exclude /0 since it is a special case used for default route |
| 586 | if (subnet.prefixLength() != 0 && subnet.contains(hostIp)) { |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 587 | return true; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | return false; |
| 592 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 593 | |
| 594 | /** |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 595 | * Checks if the IP is in the subnet defined on given connect point. |
| 596 | * |
| 597 | * @param connectPoint Connect point |
| 598 | * @param ip The IP address to check |
| 599 | * @return True if the IP belongs to the subnet. |
| 600 | * False if the IP does not belong to the subnet, or |
| 601 | * there is no subnet configuration on given connect point. |
| 602 | */ |
| 603 | public boolean inSameSubnet(ConnectPoint connectPoint, IpAddress ip) { |
Charles Chan | 35f21e4 | 2017-06-26 18:30:18 -0700 | [diff] [blame] | 604 | return getPortSubnets(connectPoint.deviceId(), connectPoint.port()).stream() |
| 605 | .anyMatch(ipPrefix -> ipPrefix.contains(ip)); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /** |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 609 | * Returns the ports corresponding to the adjacency Sid given. |
| 610 | * |
| 611 | * @param deviceId device identification of the router |
| 612 | * @param sid adjacency Sid |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 613 | * @return set of port numbers |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 614 | */ |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 615 | public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 616 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 617 | return srinfo != null ? |
| 618 | ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) : |
| 619 | ImmutableSet.copyOf(new HashSet<>()); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Check if the Sid given is whether adjacency Sid of the router device or not. |
| 624 | * |
| 625 | * @param deviceId device identification of the router |
| 626 | * @param sid Sid to check |
| 627 | * @return true if the Sid given is the adjacency Sid of the device, |
| 628 | * otherwise false |
| 629 | */ |
| 630 | public boolean isAdjacencySid(DeviceId deviceId, int sid) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 631 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 632 | return srinfo != null && srinfo.adjacencySids.containsKey(sid); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 633 | } |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 634 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 635 | /** |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 636 | * Add subnet to specific connect point. |
| 637 | * |
| 638 | * @param cp connect point |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 639 | * @param ipPrefix subnet being added to the device |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 640 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 641 | public void addSubnet(ConnectPoint cp, IpPrefix ipPrefix) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 642 | checkNotNull(cp); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 643 | checkNotNull(ipPrefix); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 644 | SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId()); |
| 645 | if (srinfo == null) { |
| 646 | log.warn("Device {} is not configured. Abort.", cp.deviceId()); |
| 647 | return; |
| 648 | } |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 649 | srinfo.subnets.put(cp.port(), ipPrefix); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Remove subnet from specific connect point. |
| 654 | * |
| 655 | * @param cp connect point |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 656 | * @param ipPrefix subnet being removed to the device |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 657 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 658 | public void removeSubnet(ConnectPoint cp, IpPrefix ipPrefix) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 659 | checkNotNull(cp); |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 660 | checkNotNull(ipPrefix); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 661 | SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId()); |
| 662 | if (srinfo == null) { |
| 663 | log.warn("Device {} is not configured. Abort.", cp.deviceId()); |
| 664 | return; |
| 665 | } |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 666 | srinfo.subnets.remove(cp.port(), ipPrefix); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 667 | } |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 668 | |
| 669 | private boolean isSuppressedPort(ConnectPoint connectPoint) { |
| 670 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 671 | .getConfig(srManager.appId(), SegmentRoutingAppConfig.class); |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 672 | if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) { |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 673 | log.info("Interface configuration on port {} is ignored", connectPoint); |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 674 | return true; |
| 675 | } |
| 676 | return false; |
| 677 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 678 | |
| 679 | public boolean isPairedEdge(DeviceId deviceId) throws DeviceConfigNotFoundException { |
| 680 | if (!isEdgeDevice(deviceId)) { |
| 681 | return false; |
| 682 | } |
| 683 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 684 | return (srinfo.pairDeviceId == null) ? false : true; |
| 685 | } |
| 686 | |
| 687 | public DeviceId getPairDeviceId(DeviceId deviceId) throws DeviceConfigNotFoundException { |
| 688 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 689 | if (srinfo != null) { |
| 690 | return srinfo.pairDeviceId; |
| 691 | } else { |
| 692 | String message = "getPairDeviceId fails for device: " + deviceId + "."; |
| 693 | throw new DeviceConfigNotFoundException(message); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | public PortNumber getPairLocalPort(DeviceId deviceId) |
| 698 | throws DeviceConfigNotFoundException { |
| 699 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 700 | if (srinfo != null) { |
| 701 | return srinfo.pairLocalPort; |
| 702 | } else { |
| 703 | String message = "getPairLocalPort fails for device: " + deviceId + "."; |
| 704 | throw new DeviceConfigNotFoundException(message); |
| 705 | } |
| 706 | } |
| 707 | |
Jonathan Hart | 8fa9ec5 | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 708 | } |