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