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