blob: 67b7ad0d351bf68139bfa115e9774edfa5e89e5a [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska58de4162015-09-10 16:15:33 -07003 *
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 Chan0b4e6182015-11-03 10:42:14 -080016package org.onosproject.segmentrouting.config;
sanghob35a6192015-04-01 13:05:26 -070017
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.HashMultimap;
Charles Chan9f676b62015-10-29 14:58:10 -070019import com.google.common.collect.ImmutableSet;
Charles Chan5270ed02016-01-30 23:22:37 -080020import com.google.common.collect.SetMultimap;
sanghob35a6192015-04-01 13:05:26 -070021import org.onlab.packet.Ip4Address;
Pier Ventree0ae7a32016-11-23 09:57:42 -080022import org.onlab.packet.Ip6Address;
Charles Chan03a73e02016-10-24 14:52:01 -070023import org.onlab.packet.IpAddress;
Charles Chan5270ed02016-01-30 23:22:37 -080024import org.onlab.packet.IpPrefix;
sanghob35a6192015-04-01 13:05:26 -070025import org.onlab.packet.MacAddress;
Charles Chane849c192016-01-11 18:28:54 -080026import org.onlab.packet.VlanId;
Charles Chan4636be02015-10-07 14:21:45 -070027import org.onosproject.incubator.net.config.basics.ConfigException;
28import org.onosproject.incubator.net.config.basics.InterfaceConfig;
Ray Milkeyfacf2862017-08-03 11:58:29 -070029import org.onosproject.net.intf.Interface;
Charles Chan4636be02015-10-07 14:21:45 -070030import org.onosproject.net.ConnectPoint;
Charles Chan4636be02015-10-07 14:21:45 -070031import org.onosproject.net.host.InterfaceIpAddress;
sanghob35a6192015-04-01 13:05:26 -070032import org.onosproject.net.DeviceId;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070033import org.onosproject.net.PortNumber;
Charles Chan2c15aca2016-11-09 20:51:44 -080034import org.onosproject.segmentrouting.SegmentRoutingManager;
sanghob35a6192015-04-01 13:05:26 -070035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070038import java.util.ArrayList;
Pier Ventre10bd8d12016-11-26 21:05:22 -080039import java.util.Collections;
sanghob35a6192015-04-01 13:05:26 -070040import java.util.HashMap;
Charles Chan531a78b2015-12-01 10:00:51 -080041import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070042import java.util.List;
43import java.util.Map;
Charles Chand6832882015-10-05 17:50:33 -070044import java.util.Set;
Saurav Das0e99e2b2015-10-28 12:39:42 -070045import java.util.concurrent.ConcurrentHashMap;
Charles Chan2c15aca2016-11-09 20:51:44 -080046import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -070047
Charles Chan93e71ba2016-04-29 14:38:22 -070048import static com.google.common.base.Preconditions.checkNotNull;
49
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070050/**
51 * Segment Routing configuration component that reads the
52 * segment routing related configuration from Network Configuration Manager
53 * component and organizes in more accessible formats.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070054 */
sanghob35a6192015-04-01 13:05:26 -070055public class DeviceConfiguration implements DeviceProperties {
56
Charles Chan2c15aca2016-11-09 20:51:44 -080057 private static final String NO_SUBNET = "No subnet configured on {}";
58
Charles Chanf2565a92016-02-10 20:46:58 -080059 private static final Logger log = LoggerFactory.getLogger(DeviceConfiguration.class);
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -070060 private final List<Integer> allSegmentIds = new ArrayList<>();
Charles Chane849c192016-01-11 18:28:54 -080061 private final Map<DeviceId, SegmentRouterInfo> deviceConfigMap = new ConcurrentHashMap<>();
Charles Chan2c15aca2016-11-09 20:51:44 -080062 private SegmentRoutingManager srManager;
sanghob35a6192015-04-01 13:05:26 -070063
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070064 private class SegmentRouterInfo {
Pier Ventree0ae7a32016-11-23 09:57:42 -080065 int ipv4NodeSid;
66 int ipv6NodeSid;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070067 DeviceId deviceId;
Pier Ventree0ae7a32016-11-23 09:57:42 -080068 Ip4Address ipv4Loopback;
69 Ip6Address ipv6Loopback;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070070 MacAddress mac;
71 boolean isEdge;
Pier Ventre10bd8d12016-11-26 21:05:22 -080072 SetMultimap<PortNumber, IpAddress> gatewayIps;
73 SetMultimap<PortNumber, IpPrefix> subnets;
Charles Chan531a78b2015-12-01 10:00:51 -080074 Map<Integer, Set<Integer>> adjacencySids;
Saurav Das7bcbe702017-06-13 15:35:54 -070075 DeviceId pairDeviceId;
76 PortNumber pairLocalPort;
Charles Chanb8e10c82015-10-14 11:24:40 -070077
78 public SegmentRouterInfo() {
Pier Ventre10bd8d12016-11-26 21:05:22 -080079 gatewayIps = HashMultimap.create();
Charles Chan5270ed02016-01-30 23:22:37 -080080 subnets = HashMultimap.create();
Charles Chanb8e10c82015-10-14 11:24:40 -070081 }
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070082 }
sanghob35a6192015-04-01 13:05:26 -070083
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070084 /**
Charles Chane849c192016-01-11 18:28:54 -080085 * Constructs device configuration for all Segment Router devices,
86 * organizing the data into various maps for easier access.
Brian O'Connor52515622015-10-09 17:03:44 -070087 *
Charles Chan2c15aca2016-11-09 20:51:44 -080088 * @param srManager Segment Routing Manager
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070089 */
Charles Chan2c15aca2016-11-09 20:51:44 -080090 public DeviceConfiguration(SegmentRoutingManager srManager) {
91 this.srManager = srManager;
Saurav Das7bcbe702017-06-13 15:35:54 -070092 updateConfig();
93 }
Charles Chand9681e72016-02-22 19:27:29 -080094
Saurav Das7bcbe702017-06-13 15:35:54 -070095 public void updateConfig() {
Charles Chan4636be02015-10-07 14:21:45 -070096 // Read config from device subject, excluding gatewayIps and subnets.
97 Set<DeviceId> deviceSubjects =
Charles Chan2c15aca2016-11-09 20:51:44 -080098 srManager.cfgService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class);
Charles Chan4636be02015-10-07 14:21:45 -070099 deviceSubjects.forEach(subject -> {
Charles Chan5270ed02016-01-30 23:22:37 -0800100 SegmentRoutingDeviceConfig config =
Charles Chan2c15aca2016-11-09 20:51:44 -0800101 srManager.cfgService.getConfig(subject, SegmentRoutingDeviceConfig.class);
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700102 SegmentRouterInfo info = new SegmentRouterInfo();
Charles Chand6832882015-10-05 17:50:33 -0700103 info.deviceId = subject;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800104 info.ipv4NodeSid = config.nodeSidIPv4();
105 info.ipv6NodeSid = config.nodeSidIPv6();
106 info.ipv4Loopback = config.routerIpv4();
107 info.ipv6Loopback = config.routerIpv6();
Charles Chan531a78b2015-12-01 10:00:51 -0800108 info.mac = config.routerMac();
Charles Chand6832882015-10-05 17:50:33 -0700109 info.isEdge = config.isEdgeRouter();
Charles Chan531a78b2015-12-01 10:00:51 -0800110 info.adjacencySids = config.adjacencySids();
Saurav Das7bcbe702017-06-13 15:35:54 -0700111 info.pairDeviceId = config.pairDeviceId();
112 info.pairLocalPort = config.pairLocalPort();
Charles Chane849c192016-01-11 18:28:54 -0800113 deviceConfigMap.put(info.deviceId, info);
Charles Chan278ce832017-06-26 15:25:09 -0700114 log.debug("Read device config for device: {}", info.deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800115 /*
116 * IPv6 sid is not inserted. this part of the code is not used for now.
117 */
118 allSegmentIds.add(info.ipv4NodeSid);
Charles Chand6832882015-10-05 17:50:33 -0700119 });
Charles Chan4636be02015-10-07 14:21:45 -0700120
Charles Chan2c15aca2016-11-09 20:51:44 -0800121 // Read gatewayIps and subnets from port subject. Ignore suppressed ports.
122 Set<ConnectPoint> portSubjects = srManager.cfgService
123 .getSubjects(ConnectPoint.class, InterfaceConfig.class);
124 portSubjects.stream().filter(subject -> !isSuppressedPort(subject)).forEach(subject -> {
Charles Chan4636be02015-10-07 14:21:45 -0700125 InterfaceConfig config =
Charles Chan2c15aca2016-11-09 20:51:44 -0800126 srManager.cfgService.getConfig(subject, InterfaceConfig.class);
Charles Chan4636be02015-10-07 14:21:45 -0700127 Set<Interface> networkInterfaces;
128 try {
129 networkInterfaces = config.getInterfaces();
130 } catch (ConfigException e) {
131 log.error("Error loading port configuration");
132 return;
133 }
134 networkInterfaces.forEach(networkInterface -> {
Charles Chane849c192016-01-11 18:28:54 -0800135 VlanId vlanId = networkInterface.vlan();
136 ConnectPoint connectPoint = networkInterface.connectPoint();
137 DeviceId dpid = connectPoint.deviceId();
138 PortNumber port = connectPoint.port();
139 SegmentRouterInfo info = deviceConfigMap.get(dpid);
Charles Chan4636be02015-10-07 14:21:45 -0700140
Charles Chanb8e10c82015-10-14 11:24:40 -0700141 // skip if there is no corresponding device for this ConenctPoint
142 if (info != null) {
Charles Chane849c192016-01-11 18:28:54 -0800143 // Extract subnet information
Jonathan Hart00cddda2016-02-16 10:30:37 -0800144 List<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddressesList();
Charles Chanb8e10c82015-10-14 11:24:40 -0700145 interfaceAddresses.forEach(interfaceAddress -> {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800146 // Do not add /0, /32 and /128 to gateway IP list
Charles Chan5270ed02016-01-30 23:22:37 -0800147 int prefixLength = interfaceAddress.subnetAddress().prefixLength();
Pier Ventre10bd8d12016-11-26 21:05:22 -0800148 IpPrefix ipPrefix = interfaceAddress.subnetAddress();
149 if (ipPrefix.isIp4()) {
150 if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET_MASK_LENGTH) {
151 info.gatewayIps.put(port, interfaceAddress.ipAddress());
152 }
153 info.subnets.put(port, interfaceAddress.subnetAddress());
154 } else {
155 if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET6_MASK_LENGTH) {
156 info.gatewayIps.put(port, interfaceAddress.ipAddress());
157 }
158 info.subnets.put(port, interfaceAddress.subnetAddress());
Charles Chan5270ed02016-01-30 23:22:37 -0800159 }
Charles Chanb8e10c82015-10-14 11:24:40 -0700160 });
161 }
Charles Chan4636be02015-10-07 14:21:45 -0700162 });
Pier Luigi0e358632017-01-31 09:35:05 -0800163 // We register the connect point with the NRS.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800164 srManager.registerConnectPoint(subject);
Charles Chan4636be02015-10-07 14:21:45 -0700165 });
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700166 }
167
Saurav Das7bcbe702017-06-13 15:35:54 -0700168
sanghob35a6192015-04-01 13:05:26 -0700169 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800170 public boolean isConfigured(DeviceId deviceId) {
171 return deviceConfigMap.get(deviceId) != null;
172 }
173
174 @Override
Pier Ventree0ae7a32016-11-23 09:57:42 -0800175 public int getIPv4SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700176 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
177 if (srinfo != null) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800178 log.trace("getIPv4SegmentId for device{} is {}", deviceId, srinfo.ipv4NodeSid);
179 return srinfo.ipv4NodeSid;
sanghob35a6192015-04-01 13:05:26 -0700180 } else {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800181 String message = "getIPv4SegmentId fails for device: " + deviceId + ".";
182 throw new DeviceConfigNotFoundException(message);
183 }
184 }
185
186 @Override
187 public int getIPv6SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException {
188 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
189 if (srinfo != null) {
190 log.trace("getIPv6SegmentId for device{} is {}", deviceId, srinfo.ipv6NodeSid);
191 return srinfo.ipv6NodeSid;
192 } else {
193 String message = "getIPv6SegmentId fails for device: " + deviceId + ".";
Charles Chan0b4e6182015-11-03 10:42:14 -0800194 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700195 }
196 }
197
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700198 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800199 * Returns the IPv4 Node segment id of a segment router given its Router mac address.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700200 *
201 * @param routerMac router mac address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700202 * @return node segment id, or -1 if not found in config
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700203 */
Pier Ventree0ae7a32016-11-23 09:57:42 -0800204 public int getIPv4SegmentId(MacAddress routerMac) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700205 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
206 deviceConfigMap.entrySet()) {
207 if (entry.getValue().mac.equals(routerMac)) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800208 return entry.getValue().ipv4NodeSid;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700209 }
210 }
sanghob35a6192015-04-01 13:05:26 -0700211
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700212 return -1;
213 }
214
215 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800216 * Returns the IPv6 Node segment id of a segment router given its Router mac address.
217 *
218 * @param routerMac router mac address
219 * @return node segment id, or -1 if not found in config
220 */
221 public int getIPv6SegmentId(MacAddress routerMac) {
222 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
223 deviceConfigMap.entrySet()) {
224 if (entry.getValue().mac.equals(routerMac)) {
225 return entry.getValue().ipv6NodeSid;
226 }
227 }
228
229 return -1;
230 }
231
232 /**
233 * Returns the IPv4 Node segment id of a segment router given its Router ip address.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700234 *
235 * @param routerAddress router ip address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700236 * @return node segment id, or -1 if not found in config
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700237 */
Pier Ventree0ae7a32016-11-23 09:57:42 -0800238 public int getIPv4SegmentId(Ip4Address routerAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700239 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
240 deviceConfigMap.entrySet()) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800241 if (entry.getValue().ipv4Loopback.equals(routerAddress)) {
242 return entry.getValue().ipv4NodeSid;
243 }
244 }
245
246 return -1;
247 }
248
249 /**
250 * Returns the IPv6 Node segment id of a segment router given its Router ip address.
251 *
252 * @param routerAddress router ip address
253 * @return node segment id, or -1 if not found in config
254 */
255 public int getIPv6SegmentId(Ip6Address routerAddress) {
256 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
257 deviceConfigMap.entrySet()) {
258 if (entry.getValue().ipv6Loopback.equals(routerAddress)) {
259 return entry.getValue().ipv6NodeSid;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700260 }
261 }
262
263 return -1;
264 }
265
sanghob35a6192015-04-01 13:05:26 -0700266 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800267 public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700268 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
269 if (srinfo != null) {
270 log.trace("getDeviceMac for device{} is {}", deviceId, srinfo.mac);
271 return srinfo.mac;
sanghob35a6192015-04-01 13:05:26 -0700272 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800273 String message = "getDeviceMac fails for device: " + deviceId + ".";
274 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700275 }
276 }
277
Charles Chan0b4e6182015-11-03 10:42:14 -0800278 @Override
Pier Ventree0ae7a32016-11-23 09:57:42 -0800279 public Ip4Address getRouterIpv4(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700280 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
281 if (srinfo != null) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800282 log.trace("getRouterIpv4 for device{} is {}", deviceId, srinfo.ipv4Loopback);
283 return srinfo.ipv4Loopback;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700284 } else {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800285 String message = "getRouterIpv4 fails for device: " + deviceId + ".";
286 throw new DeviceConfigNotFoundException(message);
287 }
288 }
289
290 @Override
291 public Ip6Address getRouterIpv6(DeviceId deviceId) throws DeviceConfigNotFoundException {
292 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
293 if (srinfo != null) {
294 log.trace("getRouterIpv6 for device{} is {}", deviceId, srinfo.ipv6Loopback);
295 return srinfo.ipv6Loopback;
296 } else {
297 String message = "getRouterIpv6 fails for device: " + deviceId + ".";
Charles Chan0b4e6182015-11-03 10:42:14 -0800298 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700299 }
sanghob35a6192015-04-01 13:05:26 -0700300 }
301
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700302 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800303 public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700304 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
305 if (srinfo != null) {
306 log.trace("isEdgeDevice for device{} is {}", deviceId, srinfo.isEdge);
307 return srinfo.isEdge;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700308 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800309 String message = "isEdgeDevice fails for device: " + deviceId + ".";
310 throw new DeviceConfigNotFoundException(message);
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700311 }
312 }
313
sanghob35a6192015-04-01 13:05:26 -0700314 @Override
315 public List<Integer> getAllDeviceSegmentIds() {
316 return allSegmentIds;
317 }
318
Charles Chanc42e84e2015-10-20 16:24:19 -0700319 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800320 public Map<IpPrefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId)
Saurav Das7a1ffca2016-03-28 19:00:18 -0700321 throws DeviceConfigNotFoundException {
322 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
323 if (srinfo == null) {
324 String message = "getSubnetPortsMap fails for device: " + deviceId + ".";
325 throw new DeviceConfigNotFoundException(message);
326 }
Charles Chanc42e84e2015-10-20 16:24:19 -0700327 // Construct subnet-port mapping from port-subnet mapping
Pier Ventre10bd8d12016-11-26 21:05:22 -0800328 SetMultimap<PortNumber, IpPrefix> portSubnetMap = srinfo.subnets;
329 Map<IpPrefix, List<PortNumber>> subnetPortMap = new HashMap<>();
Charles Chan5270ed02016-01-30 23:22:37 -0800330
331 portSubnetMap.entries().forEach(entry -> {
332 PortNumber port = entry.getKey();
Pier Ventre10bd8d12016-11-26 21:05:22 -0800333 IpPrefix subnet = entry.getValue();
Charles Chan5270ed02016-01-30 23:22:37 -0800334
Pier Ventre10bd8d12016-11-26 21:05:22 -0800335 if (subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH ||
336 subnet.prefixLength() == IpPrefix.MAX_INET6_MASK_LENGTH) {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800337 return;
338 }
339
Charles Chanc42e84e2015-10-20 16:24:19 -0700340 if (subnetPortMap.containsKey(subnet)) {
341 subnetPortMap.get(subnet).add(port);
342 } else {
343 ArrayList<PortNumber> ports = new ArrayList<>();
344 ports.add(port);
345 subnetPortMap.put(subnet, ports);
346 }
347 });
Charles Chanc42e84e2015-10-20 16:24:19 -0700348 return subnetPortMap;
349 }
350
sanghob35a6192015-04-01 13:05:26 -0700351 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700352 * Returns the device identifier or data plane identifier (dpid)
353 * of a segment router given its segment id.
sanghob35a6192015-04-01 13:05:26 -0700354 *
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700355 * @param sid segment id
356 * @return deviceId device identifier
sanghob35a6192015-04-01 13:05:26 -0700357 */
358 public DeviceId getDeviceId(int sid) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700359 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
360 deviceConfigMap.entrySet()) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800361 if (entry.getValue().ipv4NodeSid == sid ||
362 entry.getValue().ipv6NodeSid == sid) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700363 return entry.getValue().deviceId;
sanghob35a6192015-04-01 13:05:26 -0700364 }
365 }
366
367 return null;
368 }
369
370 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700371 * Returns the device identifier or data plane identifier (dpid)
372 * of a segment router given its router ip address.
sanghob35a6192015-04-01 13:05:26 -0700373 *
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700374 * @param ipAddress router ip address
375 * @return deviceId device identifier
sanghob35a6192015-04-01 13:05:26 -0700376 */
377 public DeviceId getDeviceId(Ip4Address ipAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700378 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
379 deviceConfigMap.entrySet()) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800380 if (entry.getValue().ipv4Loopback.equals(ipAddress)) {
381 return entry.getValue().deviceId;
382 }
383 }
384
385 return null;
386 }
387
388 /**
389 * Returns the device identifier or data plane identifier (dpid)
390 * of a segment router given its router ipv6 address.
391 *
392 * @param ipAddress router ipv6 address
393 * @return deviceId device identifier
394 */
395 public DeviceId getDeviceId(Ip6Address ipAddress) {
396 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
397 deviceConfigMap.entrySet()) {
398 if (entry.getValue().ipv6Loopback.equals(ipAddress)) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700399 return entry.getValue().deviceId;
sanghob35a6192015-04-01 13:05:26 -0700400 }
401 }
402
403 return null;
404 }
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700405
406 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700407 * Returns the configured port ip addresses for a segment router.
408 * These addresses serve as gateway IP addresses for the subnets configured
409 * on those ports.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700410 *
411 * @param deviceId device identifier
Saurav Das837e0bb2015-10-30 17:45:38 -0700412 * @return immutable set of ip addresses configured on the ports or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700413 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800414 public Set<IpAddress> getPortIPs(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700415 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
416 if (srinfo != null) {
417 log.trace("getSubnetGatewayIps for device{} is {}", deviceId,
418 srinfo.gatewayIps.values());
Saurav Das837e0bb2015-10-30 17:45:38 -0700419 return ImmutableSet.copyOf(srinfo.gatewayIps.values());
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700420 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700421 return null;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700422 }
423
424 /**
425 * Returns the configured subnet prefixes for a segment router.
426 *
427 * @param deviceId device identifier
Saurav Das0e99e2b2015-10-28 12:39:42 -0700428 * @return list of ip prefixes or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700429 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800430 public Set<IpPrefix> getSubnets(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700431 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
432 if (srinfo != null) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800433 ImmutableSet.Builder<IpPrefix> builder = ImmutableSet.builder();
Charles Chan03a73e02016-10-24 14:52:01 -0700434 return builder.addAll(srinfo.subnets.values()).build();
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700435 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700436 return null;
437 }
438
Charles Chan2c15aca2016-11-09 20:51:44 -0800439
Saurav Das0e99e2b2015-10-28 12:39:42 -0700440 /**
Charles Chan2c15aca2016-11-09 20:51:44 -0800441 * Returns the subnet configuration of given device and port.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700442 *
Charles Chan2c15aca2016-11-09 20:51:44 -0800443 * @param deviceId Device ID
444 * @param port Port number
Pier Ventre10bd8d12016-11-26 21:05:22 -0800445 * @return The subnets configured on given port or empty set if
Charles Chanc53d6172017-04-24 16:21:01 -0700446 * the port is unconfigured or suppressed.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700447 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800448 public Set<IpPrefix> getPortSubnets(DeviceId deviceId, PortNumber port) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800449 ConnectPoint connectPoint = new ConnectPoint(deviceId, port);
450
451 if (isSuppressedPort(connectPoint)) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800452 return Collections.emptySet();
Saurav Das0e99e2b2015-10-28 12:39:42 -0700453 }
Charles Chan2c15aca2016-11-09 20:51:44 -0800454
Pier Ventre10bd8d12016-11-26 21:05:22 -0800455 Set<IpPrefix> subnets =
Charles Chan2c15aca2016-11-09 20:51:44 -0800456 srManager.interfaceService.getInterfacesByPort(connectPoint).stream()
457 .flatMap(intf -> intf.ipAddressesList().stream())
458 .map(InterfaceIpAddress::subnetAddress)
Charles Chan2c15aca2016-11-09 20:51:44 -0800459 .collect(Collectors.toSet());
460
Jon Hallcbd1b392017-01-18 20:15:44 -0800461 if (subnets.isEmpty()) {
Saurav Das018605f2017-02-18 14:05:44 -0800462 log.debug(NO_SUBNET, connectPoint);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800463 return Collections.emptySet();
Charles Chan2c15aca2016-11-09 20:51:44 -0800464 }
Charles Chanc53d6172017-04-24 16:21:01 -0700465
466 return subnets;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800467 }
468
469 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700470 * Returns the router ip address of segment router that has the
471 * specified ip address in its subnets.
472 *
473 * @param destIpAddress target ip address
474 * @return router ip address
475 */
476 public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) {
Charles Chan68aaad52016-12-09 12:54:49 -0800477 Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress);
478
479 if (matchIntf == null) {
480 log.debug("No router was found for {}", destIpAddress);
481 return null;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700482 }
483
Charles Chan68aaad52016-12-09 12:54:49 -0800484 DeviceId routerDeviceId = matchIntf.connectPoint().deviceId();
485 SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId);
486 if (srInfo == null) {
487 log.debug("No device config was found for {}", routerDeviceId);
488 return null;
489 }
490
Pier Ventree0ae7a32016-11-23 09:57:42 -0800491 return srInfo.ipv4Loopback;
492 }
493
494 /**
495 * Returns the router ipv6 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 Ip6Address getRouterIpAddressForASubnetHost(Ip6Address destIpAddress) {
502 Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress);
503
504 if (matchIntf == null) {
505 log.debug("No router was found for {}", destIpAddress);
506 return null;
507 }
508
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
516 return srInfo.ipv6Loopback;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700517 }
518
519 /**
520 * Returns the router mac address of segment router that has the
521 * specified ip address as one of its subnet gateway ip address.
522 *
523 * @param gatewayIpAddress router gateway ip address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700524 * @return router mac address or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700525 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800526 public MacAddress getRouterMacForAGatewayIp(IpAddress gatewayIpAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700527 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
528 deviceConfigMap.entrySet()) {
529 if (entry.getValue().gatewayIps.
530 values().contains(gatewayIpAddress)) {
531 return entry.getValue().mac;
532 }
533 }
534
535 log.debug("Cannot find a router for {}", gatewayIpAddress);
536 return null;
537 }
sangho666cd6d2015-04-14 16:27:13 -0700538
sangho666cd6d2015-04-14 16:27:13 -0700539 /**
Charles Chana9519af2017-06-26 18:30:18 -0700540 * Checks if the host IP is in any of the subnet defined in the router with the
sangho666cd6d2015-04-14 16:27:13 -0700541 * device ID given.
542 *
543 * @param deviceId device identification of the router
544 * @param hostIp host IP address to check
Charles Chana9519af2017-06-26 18:30:18 -0700545 * @return true if the given IP is within any of the subnet defined in the router,
546 * false if no subnet is defined in the router or if the host is not
547 * within any subnet defined in the router
sangho666cd6d2015-04-14 16:27:13 -0700548 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800549 public boolean inSameSubnet(DeviceId deviceId, IpAddress hostIp) {
sangho666cd6d2015-04-14 16:27:13 -0700550
Pier Ventre10bd8d12016-11-26 21:05:22 -0800551 Set<IpPrefix> subnets = getSubnets(deviceId);
sangho666cd6d2015-04-14 16:27:13 -0700552 if (subnets == null) {
553 return false;
554 }
555
Pier Ventre10bd8d12016-11-26 21:05:22 -0800556 for (IpPrefix subnet: subnets) {
Charles Chan5270ed02016-01-30 23:22:37 -0800557 // Exclude /0 since it is a special case used for default route
558 if (subnet.prefixLength() != 0 && subnet.contains(hostIp)) {
sangho666cd6d2015-04-14 16:27:13 -0700559 return true;
560 }
561 }
562
563 return false;
564 }
sangho1e575652015-05-14 00:39:53 -0700565
566 /**
Charles Chan03a73e02016-10-24 14:52:01 -0700567 * Checks if the IP is in the subnet defined on given connect point.
568 *
569 * @param connectPoint Connect point
570 * @param ip The IP address to check
571 * @return True if the IP belongs to the subnet.
572 * False if the IP does not belong to the subnet, or
573 * there is no subnet configuration on given connect point.
574 */
575 public boolean inSameSubnet(ConnectPoint connectPoint, IpAddress ip) {
Charles Chana9519af2017-06-26 18:30:18 -0700576 return getPortSubnets(connectPoint.deviceId(), connectPoint.port()).stream()
577 .anyMatch(ipPrefix -> ipPrefix.contains(ip));
Charles Chan03a73e02016-10-24 14:52:01 -0700578 }
579
580 /**
sangho1e575652015-05-14 00:39:53 -0700581 * Returns the ports corresponding to the adjacency Sid given.
582 *
583 * @param deviceId device identification of the router
584 * @param sid adjacency Sid
Charles Chan531a78b2015-12-01 10:00:51 -0800585 * @return set of port numbers
sangho1e575652015-05-14 00:39:53 -0700586 */
Charles Chan531a78b2015-12-01 10:00:51 -0800587 public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700588 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan531a78b2015-12-01 10:00:51 -0800589 return srinfo != null ?
590 ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) :
591 ImmutableSet.copyOf(new HashSet<>());
sangho1e575652015-05-14 00:39:53 -0700592 }
593
594 /**
595 * Check if the Sid given is whether adjacency Sid of the router device or not.
596 *
597 * @param deviceId device identification of the router
598 * @param sid Sid to check
599 * @return true if the Sid given is the adjacency Sid of the device,
600 * otherwise false
601 */
602 public boolean isAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700603 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan531a78b2015-12-01 10:00:51 -0800604 return srinfo != null && srinfo.adjacencySids.containsKey(sid);
sangho1e575652015-05-14 00:39:53 -0700605 }
Charles Chanf2565a92016-02-10 20:46:58 -0800606
Charles Chand55e84d2016-03-30 17:54:24 -0700607 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700608 * Add subnet to specific connect point.
609 *
610 * @param cp connect point
Pier Ventre10bd8d12016-11-26 21:05:22 -0800611 * @param ipPrefix subnet being added to the device
Charles Chan93e71ba2016-04-29 14:38:22 -0700612 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800613 public void addSubnet(ConnectPoint cp, IpPrefix ipPrefix) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700614 checkNotNull(cp);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800615 checkNotNull(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700616 SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId());
617 if (srinfo == null) {
618 log.warn("Device {} is not configured. Abort.", cp.deviceId());
619 return;
620 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800621 srinfo.subnets.put(cp.port(), ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700622 }
623
624 /**
625 * Remove subnet from specific connect point.
626 *
627 * @param cp connect point
Pier Ventre10bd8d12016-11-26 21:05:22 -0800628 * @param ipPrefix subnet being removed to the device
Charles Chan93e71ba2016-04-29 14:38:22 -0700629 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800630 public void removeSubnet(ConnectPoint cp, IpPrefix ipPrefix) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700631 checkNotNull(cp);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800632 checkNotNull(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700633 SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId());
634 if (srinfo == null) {
635 log.warn("Device {} is not configured. Abort.", cp.deviceId());
636 return;
637 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800638 srinfo.subnets.remove(cp.port(), ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700639 }
Charles Chan2c15aca2016-11-09 20:51:44 -0800640
641 private boolean isSuppressedPort(ConnectPoint connectPoint) {
642 SegmentRoutingAppConfig appConfig = srManager.cfgService
Ray Milkeye4afdb52017-04-05 09:42:04 -0700643 .getConfig(srManager.appId(), SegmentRoutingAppConfig.class);
Charles Chan2c15aca2016-11-09 20:51:44 -0800644 if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700645 log.info("Interface configuration on port {} is ignored", connectPoint);
Charles Chan2c15aca2016-11-09 20:51:44 -0800646 return true;
647 }
648 return false;
649 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700650
651 public boolean isPairedEdge(DeviceId deviceId) throws DeviceConfigNotFoundException {
652 if (!isEdgeDevice(deviceId)) {
653 return false;
654 }
655 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
656 return (srinfo.pairDeviceId == null) ? false : true;
657 }
658
659 public DeviceId getPairDeviceId(DeviceId deviceId) throws DeviceConfigNotFoundException {
660 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
661 if (srinfo != null) {
662 return srinfo.pairDeviceId;
663 } else {
664 String message = "getPairDeviceId fails for device: " + deviceId + ".";
665 throw new DeviceConfigNotFoundException(message);
666 }
667 }
668
669 public PortNumber getPairLocalPort(DeviceId deviceId)
670 throws DeviceConfigNotFoundException {
671 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
672 if (srinfo != null) {
673 return srinfo.pairLocalPort;
674 } else {
675 String message = "getPairLocalPort fails for device: " + deviceId + ".";
676 throw new DeviceConfigNotFoundException(message);
677 }
678 }
679
Jonathan Hart00cddda2016-02-16 10:30:37 -0800680}