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