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