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