blob: 3251e7a4207042d6a57dab25b4d4c3f1f4b20865 [file] [log] [blame]
Thomas Vachuska8fd25052015-09-10 16:15:33 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska8fd25052015-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 */
pierventre2b102f92020-09-08 16:45:36 +020016package org.onosproject.segmentrouting;
sangho80f11cb2015-04-01 13:05:26 -070017
Charles Chan6191aca2017-09-12 12:09:22 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Charles Chan82ab1932016-01-30 23:22:37 -080021import com.google.common.collect.HashMultimap;
Charles Chanc6ad7752015-10-29 14:58:10 -070022import com.google.common.collect.ImmutableSet;
Charles Chan19b70032019-04-17 14:20:26 -070023import com.google.common.collect.Lists;
Charles Chandc7d6f92018-03-20 13:30:38 -070024import com.google.common.collect.Multimaps;
Charles Chan82ab1932016-01-30 23:22:37 -080025import com.google.common.collect.SetMultimap;
Charles Chan19b70032019-04-17 14:20:26 -070026import com.google.common.collect.Sets;
sangho80f11cb2015-04-01 13:05:26 -070027import org.onlab.packet.Ip4Address;
Pier Ventreadb4ae62016-11-23 09:57:42 -080028import org.onlab.packet.Ip6Address;
Charles Chandebfea32016-10-24 14:52:01 -070029import org.onlab.packet.IpAddress;
Charles Chan82ab1932016-01-30 23:22:37 -080030import org.onlab.packet.IpPrefix;
sangho80f11cb2015-04-01 13:05:26 -070031import org.onlab.packet.MacAddress;
Charles Chanb7f75ac2016-01-11 18:28:54 -080032import org.onlab.packet.VlanId;
Charles Chane7c61022015-10-07 14:21:45 -070033import org.onosproject.net.ConnectPoint;
sangho80f11cb2015-04-01 13:05:26 -070034import org.onosproject.net.DeviceId;
Charles Chan5eec3b12019-04-18 14:30:41 -070035import org.onosproject.net.HostId;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070036import org.onosproject.net.PortNumber;
Jonathan Hart61e24e12017-11-30 18:23:42 -080037import org.onosproject.net.config.ConfigException;
Andrea Campanella09943842020-03-27 12:53:46 +010038import org.onosproject.net.config.basics.BasicDeviceConfig;
Jonathan Hart61e24e12017-11-30 18:23:42 -080039import org.onosproject.net.config.basics.InterfaceConfig;
40import org.onosproject.net.host.InterfaceIpAddress;
41import org.onosproject.net.intf.Interface;
Charles Chan19b70032019-04-17 14:20:26 -070042import org.onosproject.routeservice.Route;
pierventre2b102f92020-09-08 16:45:36 +020043import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
44import org.onosproject.segmentrouting.config.DeviceProperties;
45import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
46import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
sangho80f11cb2015-04-01 13:05:26 -070047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
49
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070050import java.util.ArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -080051import java.util.Collection;
Pier Ventreb6a7f342016-11-26 21:05:22 -080052import java.util.Collections;
sangho80f11cb2015-04-01 13:05:26 -070053import java.util.HashMap;
Charles Chan9bec8a32015-12-01 10:00:51 -080054import java.util.HashSet;
sangho80f11cb2015-04-01 13:05:26 -070055import java.util.List;
56import java.util.Map;
Charles Chancf789822019-03-22 10:04:27 -070057import java.util.Objects;
Charles Chan72f556a2015-10-05 17:50:33 -070058import java.util.Set;
Saurav Das7c305372015-10-28 12:39:42 -070059import java.util.concurrent.ConcurrentHashMap;
Charles Chan46fdfaf2016-11-09 20:51:44 -080060import java.util.stream.Collectors;
Charles Chan5eec3b12019-04-18 14:30:41 -070061import java.util.stream.Stream;
sangho80f11cb2015-04-01 13:05:26 -070062
Charles Chanc22cef32016-04-29 14:38:22 -070063import static com.google.common.base.Preconditions.checkNotNull;
64
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070065/**
66 * Segment Routing configuration component that reads the
67 * segment routing related configuration from Network Configuration Manager
68 * component and organizes in more accessible formats.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070069 */
sangho80f11cb2015-04-01 13:05:26 -070070public class DeviceConfiguration implements DeviceProperties {
71
Charles Chan46fdfaf2016-11-09 20:51:44 -080072 private static final String NO_SUBNET = "No subnet configured on {}";
73
Charles Chan43547ca2016-02-10 20:46:58 -080074 private static final Logger log = LoggerFactory.getLogger(DeviceConfiguration.class);
Sho SHIMIZU47b8aa22015-09-11 11:19:11 -070075 private final List<Integer> allSegmentIds = new ArrayList<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -080076 private final Map<DeviceId, SegmentRouterInfo> deviceConfigMap = new ConcurrentHashMap<>();
Charles Chan46fdfaf2016-11-09 20:51:44 -080077 private SegmentRoutingManager srManager;
sangho80f11cb2015-04-01 13:05:26 -070078
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070079 private class SegmentRouterInfo {
Charles Chan3d35a542018-10-02 12:52:29 -070080 int ipv4NodeSid = -1;
81 int ipv6NodeSid = -1;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070082 DeviceId deviceId;
Pier Ventreadb4ae62016-11-23 09:57:42 -080083 Ip4Address ipv4Loopback;
84 Ip6Address ipv6Loopback;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070085 MacAddress mac;
86 boolean isEdge;
Pier Ventreb6a7f342016-11-26 21:05:22 -080087 SetMultimap<PortNumber, IpAddress> gatewayIps;
88 SetMultimap<PortNumber, IpPrefix> subnets;
Charles Chan9bec8a32015-12-01 10:00:51 -080089 Map<Integer, Set<Integer>> adjacencySids;
Saurav Das261c3002017-06-13 15:35:54 -070090 DeviceId pairDeviceId;
91 PortNumber pairLocalPort;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -070092 int pwRoutingLabel;
Charles Chan2b078ae2015-10-14 11:24:40 -070093
94 public SegmentRouterInfo() {
Charles Chandc7d6f92018-03-20 13:30:38 -070095 gatewayIps = Multimaps.synchronizedSetMultimap(HashMultimap.create());
96 subnets = Multimaps.synchronizedSetMultimap(HashMultimap.create());
Charles Chan2b078ae2015-10-14 11:24:40 -070097 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -070098 }
sangho80f11cb2015-04-01 13:05:26 -070099
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700100 /**
Charles Chanb7f75ac2016-01-11 18:28:54 -0800101 * Constructs device configuration for all Segment Router devices,
102 * organizing the data into various maps for easier access.
Brian O'Connor65eeb572015-10-09 17:03:44 -0700103 *
Charles Chan46fdfaf2016-11-09 20:51:44 -0800104 * @param srManager Segment Routing Manager
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700105 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800106 public DeviceConfiguration(SegmentRoutingManager srManager) {
107 this.srManager = srManager;
Saurav Das261c3002017-06-13 15:35:54 -0700108 updateConfig();
109 }
Charles Chan57bd98c2016-02-22 19:27:29 -0800110
Saurav Das261c3002017-06-13 15:35:54 -0700111 public void updateConfig() {
Charles Chane7c61022015-10-07 14:21:45 -0700112 // Read config from device subject, excluding gatewayIps and subnets.
113 Set<DeviceId> deviceSubjects =
Charles Chan46fdfaf2016-11-09 20:51:44 -0800114 srManager.cfgService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class);
Charles Chane7c61022015-10-07 14:21:45 -0700115 deviceSubjects.forEach(subject -> {
Andrea Campanella09943842020-03-27 12:53:46 +0100116 BasicDeviceConfig basicDeviceConfig = srManager.cfgService.addConfig(subject, BasicDeviceConfig.class);
pierc81188f2020-04-15 22:48:36 +0200117 if (!basicDeviceConfig.purgeOnDisconnection()) {
118 // Setting purge on disconnection flag for the device SR has control over.
119 // addConfig returns a config if it exists or creates a new one.
120 log.info("PurgeOnDisconnection set to true for device {}", subject);
121 basicDeviceConfig.purgeOnDisconnection(true);
122 srManager.cfgService.applyConfig(subject, BasicDeviceConfig.class, basicDeviceConfig.node());
123 }
Charles Chan82ab1932016-01-30 23:22:37 -0800124 SegmentRoutingDeviceConfig config =
Charles Chan46fdfaf2016-11-09 20:51:44 -0800125 srManager.cfgService.getConfig(subject, SegmentRoutingDeviceConfig.class);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700126 SegmentRouterInfo info = new SegmentRouterInfo();
Charles Chan72f556a2015-10-05 17:50:33 -0700127 info.deviceId = subject;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800128 info.ipv4NodeSid = config.nodeSidIPv4();
129 info.ipv6NodeSid = config.nodeSidIPv6();
130 info.ipv4Loopback = config.routerIpv4();
131 info.ipv6Loopback = config.routerIpv6();
Charles Chan9bec8a32015-12-01 10:00:51 -0800132 info.mac = config.routerMac();
Charles Chan72f556a2015-10-05 17:50:33 -0700133 info.isEdge = config.isEdgeRouter();
Charles Chan9bec8a32015-12-01 10:00:51 -0800134 info.adjacencySids = config.adjacencySids();
Saurav Das261c3002017-06-13 15:35:54 -0700135 info.pairDeviceId = config.pairDeviceId();
136 info.pairLocalPort = config.pairLocalPort();
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700137 info.pwRoutingLabel = info.ipv4NodeSid + 1000;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800138 deviceConfigMap.put(info.deviceId, info);
Charles Chan68363b12017-06-26 15:25:09 -0700139 log.debug("Read device config for device: {}", info.deviceId);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800140 /*
141 * IPv6 sid is not inserted. this part of the code is not used for now.
142 */
143 allSegmentIds.add(info.ipv4NodeSid);
Charles Chan72f556a2015-10-05 17:50:33 -0700144 });
Charles Chane7c61022015-10-07 14:21:45 -0700145
Charles Chan46fdfaf2016-11-09 20:51:44 -0800146 // Read gatewayIps and subnets from port subject. Ignore suppressed ports.
147 Set<ConnectPoint> portSubjects = srManager.cfgService
148 .getSubjects(ConnectPoint.class, InterfaceConfig.class);
Jonathan Hart61e24e12017-11-30 18:23:42 -0800149 portSubjects.stream()
150 .filter(subject -> deviceConfigMap.containsKey(subject.deviceId()))
151 .filter(subject -> !isSuppressedPort(subject)).forEach(subject -> {
Charles Chane7c61022015-10-07 14:21:45 -0700152 InterfaceConfig config =
Charles Chan46fdfaf2016-11-09 20:51:44 -0800153 srManager.cfgService.getConfig(subject, InterfaceConfig.class);
Charles Chane7c61022015-10-07 14:21:45 -0700154 Set<Interface> networkInterfaces;
155 try {
156 networkInterfaces = config.getInterfaces();
157 } catch (ConfigException e) {
158 log.error("Error loading port configuration");
159 return;
160 }
161 networkInterfaces.forEach(networkInterface -> {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800162 VlanId vlanId = networkInterface.vlan();
163 ConnectPoint connectPoint = networkInterface.connectPoint();
164 DeviceId dpid = connectPoint.deviceId();
165 PortNumber port = connectPoint.port();
Charles Chan6191aca2017-09-12 12:09:22 -0700166 MacAddress mac = networkInterface.mac();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800167 SegmentRouterInfo info = deviceConfigMap.get(dpid);
Charles Chane7c61022015-10-07 14:21:45 -0700168
Charles Chan2b078ae2015-10-14 11:24:40 -0700169 // skip if there is no corresponding device for this ConenctPoint
170 if (info != null) {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800171 // Extract subnet information
Jonathan Hart8fa9ec52016-02-16 10:30:37 -0800172 List<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddressesList();
Charles Chan2b078ae2015-10-14 11:24:40 -0700173 interfaceAddresses.forEach(interfaceAddress -> {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800174 // Do not add /0, /32 and /128 to gateway IP list
Charles Chan82ab1932016-01-30 23:22:37 -0800175 int prefixLength = interfaceAddress.subnetAddress().prefixLength();
Pier Ventreb6a7f342016-11-26 21:05:22 -0800176 IpPrefix ipPrefix = interfaceAddress.subnetAddress();
177 if (ipPrefix.isIp4()) {
178 if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET_MASK_LENGTH) {
179 info.gatewayIps.put(port, interfaceAddress.ipAddress());
180 }
181 info.subnets.put(port, interfaceAddress.subnetAddress());
182 } else {
183 if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET6_MASK_LENGTH) {
184 info.gatewayIps.put(port, interfaceAddress.ipAddress());
185 }
186 info.subnets.put(port, interfaceAddress.subnetAddress());
Charles Chan82ab1932016-01-30 23:22:37 -0800187 }
Charles Chan2b078ae2015-10-14 11:24:40 -0700188 });
Charles Chan6191aca2017-09-12 12:09:22 -0700189
190 // Override interface mac with router mac
191 if (!mac.equals(info.mac)) {
192 ArrayNode array = (ArrayNode) config.node();
193 for (JsonNode intfNode : array) {
194 ObjectNode objNode = (ObjectNode) intfNode;
195 objNode.put(InterfaceConfig.MAC, info.mac.toString());
196 }
197 srManager.cfgService.applyConfig(connectPoint, InterfaceConfig.class, array);
198 }
Charles Chan2b078ae2015-10-14 11:24:40 -0700199 }
Charles Chane7c61022015-10-07 14:21:45 -0700200 });
Pier Luigi6a2643a2017-01-31 09:35:05 -0800201 // We register the connect point with the NRS.
Pier Ventreb6a7f342016-11-26 21:05:22 -0800202 srManager.registerConnectPoint(subject);
Charles Chane7c61022015-10-07 14:21:45 -0700203 });
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700204 }
205
Jonathan Hart61e24e12017-11-30 18:23:42 -0800206 public Collection<DeviceId> getRouters() {
207 return deviceConfigMap.keySet();
208 }
Saurav Das261c3002017-06-13 15:35:54 -0700209
sangho80f11cb2015-04-01 13:05:26 -0700210 @Override
Charles Chan319d1a22015-11-03 10:42:14 -0800211 public boolean isConfigured(DeviceId deviceId) {
212 return deviceConfigMap.get(deviceId) != null;
213 }
214
215 @Override
Pier Ventreadb4ae62016-11-23 09:57:42 -0800216 public int getIPv4SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das7c305372015-10-28 12:39:42 -0700217 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
218 if (srinfo != null) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800219 log.trace("getIPv4SegmentId for device{} is {}", deviceId, srinfo.ipv4NodeSid);
220 return srinfo.ipv4NodeSid;
sangho80f11cb2015-04-01 13:05:26 -0700221 } else {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800222 String message = "getIPv4SegmentId fails for device: " + deviceId + ".";
223 throw new DeviceConfigNotFoundException(message);
224 }
225 }
226
227 @Override
228 public int getIPv6SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException {
229 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
230 if (srinfo != null) {
231 log.trace("getIPv6SegmentId for device{} is {}", deviceId, srinfo.ipv6NodeSid);
232 return srinfo.ipv6NodeSid;
233 } else {
234 String message = "getIPv6SegmentId fails for device: " + deviceId + ".";
Charles Chan319d1a22015-11-03 10:42:14 -0800235 throw new DeviceConfigNotFoundException(message);
sangho80f11cb2015-04-01 13:05:26 -0700236 }
237 }
238
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700239 @Override
240 public int getPWRoutingLabel(DeviceId deviceId) throws DeviceConfigNotFoundException {
241 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
242 if (srinfo != null) {
243 log.trace("pwRoutingLabel for device{} is {}", deviceId, srinfo.pwRoutingLabel);
244 return srinfo.pwRoutingLabel;
245 } else {
246 String message = "getPWRoutingLabel fails for device: " + deviceId + ".";
247 throw new DeviceConfigNotFoundException(message);
248 }
249 }
250
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700251 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800252 * Returns the IPv4 Node segment id of a segment router given its Router mac address.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700253 *
254 * @param routerMac router mac address
Saurav Das7c305372015-10-28 12:39:42 -0700255 * @return node segment id, or -1 if not found in config
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700256 */
Pier Ventreadb4ae62016-11-23 09:57:42 -0800257 public int getIPv4SegmentId(MacAddress routerMac) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700258 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
259 deviceConfigMap.entrySet()) {
260 if (entry.getValue().mac.equals(routerMac)) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800261 return entry.getValue().ipv4NodeSid;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700262 }
263 }
sangho80f11cb2015-04-01 13:05:26 -0700264
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700265 return -1;
266 }
267
268 /**
Pier Ventreadb4ae62016-11-23 09:57:42 -0800269 * Returns the IPv6 Node segment id of a segment router given its Router mac address.
270 *
271 * @param routerMac router mac address
272 * @return node segment id, or -1 if not found in config
273 */
274 public int getIPv6SegmentId(MacAddress routerMac) {
275 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
276 deviceConfigMap.entrySet()) {
277 if (entry.getValue().mac.equals(routerMac)) {
278 return entry.getValue().ipv6NodeSid;
279 }
280 }
281
282 return -1;
283 }
284
285 /**
286 * Returns the IPv4 Node segment id of a segment router given its Router ip address.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700287 *
288 * @param routerAddress router ip address
Saurav Das7c305372015-10-28 12:39:42 -0700289 * @return node segment id, or -1 if not found in config
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700290 */
Pier Ventreadb4ae62016-11-23 09:57:42 -0800291 public int getIPv4SegmentId(Ip4Address routerAddress) {
Charles Chan3d35a542018-10-02 12:52:29 -0700292 for (Map.Entry<DeviceId, SegmentRouterInfo> entry: deviceConfigMap.entrySet()) {
293 Ip4Address ipv4Loopback = entry.getValue().ipv4Loopback;
294 if (ipv4Loopback == null) {
295 continue;
296 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800297 if (entry.getValue().ipv4Loopback.equals(routerAddress)) {
Charles Chan3d35a542018-10-02 12:52:29 -0700298 if (entry.getValue().ipv4NodeSid == -1) {
299 continue;
300 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800301 return entry.getValue().ipv4NodeSid;
302 }
303 }
304
305 return -1;
306 }
307
308 /**
309 * Returns the IPv6 Node segment id of a segment router given its Router ip address.
310 *
311 * @param routerAddress router ip address
312 * @return node segment id, or -1 if not found in config
313 */
314 public int getIPv6SegmentId(Ip6Address routerAddress) {
Charles Chan3d35a542018-10-02 12:52:29 -0700315 for (Map.Entry<DeviceId, SegmentRouterInfo> entry: deviceConfigMap.entrySet()) {
316 Ip6Address ipv6Loopback = entry.getValue().ipv6Loopback;
317 if (ipv6Loopback == null) {
318 continue;
319 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800320 if (entry.getValue().ipv6Loopback.equals(routerAddress)) {
Charles Chan3d35a542018-10-02 12:52:29 -0700321 if (entry.getValue().ipv6NodeSid == -1) {
322 continue;
323 }
Pier Ventreadb4ae62016-11-23 09:57:42 -0800324 return entry.getValue().ipv6NodeSid;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700325 }
326 }
327
328 return -1;
329 }
330
sangho80f11cb2015-04-01 13:05:26 -0700331 @Override
Charles Chan319d1a22015-11-03 10:42:14 -0800332 public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das7c305372015-10-28 12:39:42 -0700333 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
334 if (srinfo != null) {
Saurav Das7c305372015-10-28 12:39:42 -0700335 return srinfo.mac;
sangho80f11cb2015-04-01 13:05:26 -0700336 } else {
Charles Chan319d1a22015-11-03 10:42:14 -0800337 String message = "getDeviceMac fails for device: " + deviceId + ".";
338 throw new DeviceConfigNotFoundException(message);
sangho80f11cb2015-04-01 13:05:26 -0700339 }
340 }
341
Charles Chan319d1a22015-11-03 10:42:14 -0800342 @Override
Pier Ventreadb4ae62016-11-23 09:57:42 -0800343 public Ip4Address getRouterIpv4(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das7c305372015-10-28 12:39:42 -0700344 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
345 if (srinfo != null) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800346 log.trace("getRouterIpv4 for device{} is {}", deviceId, srinfo.ipv4Loopback);
347 return srinfo.ipv4Loopback;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700348 } else {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800349 String message = "getRouterIpv4 fails for device: " + deviceId + ".";
350 throw new DeviceConfigNotFoundException(message);
351 }
352 }
353
354 @Override
355 public Ip6Address getRouterIpv6(DeviceId deviceId) throws DeviceConfigNotFoundException {
356 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
357 if (srinfo != null) {
358 log.trace("getRouterIpv6 for device{} is {}", deviceId, srinfo.ipv6Loopback);
359 return srinfo.ipv6Loopback;
360 } else {
361 String message = "getRouterIpv6 fails for device: " + deviceId + ".";
Charles Chan319d1a22015-11-03 10:42:14 -0800362 throw new DeviceConfigNotFoundException(message);
sangho80f11cb2015-04-01 13:05:26 -0700363 }
sangho80f11cb2015-04-01 13:05:26 -0700364 }
365
pier6a2052b2019-06-28 22:17:31 +0200366 /**
367 * Gets router ip address based on the destination ip address.
368 *
369 * @param destIpAddress the destination ip address
370 * @param routerDeviceId the device id
371 * @return the ip address of the routes
372 */
373 public IpAddress getRouterIpAddress(IpAddress destIpAddress, DeviceId routerDeviceId) {
374 IpAddress routerIpAddress;
375 try {
376 routerIpAddress = destIpAddress.isIp4() ? getRouterIpv4(routerDeviceId) :
377 getRouterIpv6(routerDeviceId);
378 } catch (DeviceConfigNotFoundException e) {
379 routerIpAddress = null;
380 }
381 return routerIpAddress;
382 }
383
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700384 @Override
Charles Chan319d1a22015-11-03 10:42:14 -0800385 public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das7c305372015-10-28 12:39:42 -0700386 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
387 if (srinfo != null) {
388 log.trace("isEdgeDevice for device{} is {}", deviceId, srinfo.isEdge);
389 return srinfo.isEdge;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700390 } else {
Charles Chan319d1a22015-11-03 10:42:14 -0800391 String message = "isEdgeDevice fails for device: " + deviceId + ".";
392 throw new DeviceConfigNotFoundException(message);
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700393 }
394 }
395
sangho80f11cb2015-04-01 13:05:26 -0700396 @Override
397 public List<Integer> getAllDeviceSegmentIds() {
398 return allSegmentIds;
399 }
400
Charles Chan77277672015-10-20 16:24:19 -0700401 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800402 public Map<IpPrefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId)
Saurav Das52d4ed72016-03-28 19:00:18 -0700403 throws DeviceConfigNotFoundException {
404 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
405 if (srinfo == null) {
406 String message = "getSubnetPortsMap fails for device: " + deviceId + ".";
407 throw new DeviceConfigNotFoundException(message);
408 }
Charles Chan77277672015-10-20 16:24:19 -0700409 // Construct subnet-port mapping from port-subnet mapping
Pier Ventreb6a7f342016-11-26 21:05:22 -0800410 SetMultimap<PortNumber, IpPrefix> portSubnetMap = srinfo.subnets;
411 Map<IpPrefix, List<PortNumber>> subnetPortMap = new HashMap<>();
Charles Chan82ab1932016-01-30 23:22:37 -0800412
413 portSubnetMap.entries().forEach(entry -> {
414 PortNumber port = entry.getKey();
Pier Ventreb6a7f342016-11-26 21:05:22 -0800415 IpPrefix subnet = entry.getValue();
Charles Chan82ab1932016-01-30 23:22:37 -0800416
Pier Ventreb6a7f342016-11-26 21:05:22 -0800417 if (subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH ||
418 subnet.prefixLength() == IpPrefix.MAX_INET6_MASK_LENGTH) {
Charles Chanbbd004c2016-02-16 23:14:49 -0800419 return;
420 }
421
Charles Chan77277672015-10-20 16:24:19 -0700422 if (subnetPortMap.containsKey(subnet)) {
423 subnetPortMap.get(subnet).add(port);
424 } else {
425 ArrayList<PortNumber> ports = new ArrayList<>();
426 ports.add(port);
427 subnetPortMap.put(subnet, ports);
428 }
429 });
Charles Chan77277672015-10-20 16:24:19 -0700430 return subnetPortMap;
431 }
432
sangho80f11cb2015-04-01 13:05:26 -0700433 /**
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700434 * Returns the device identifier or data plane identifier (dpid)
435 * of a segment router given its segment id.
sangho80f11cb2015-04-01 13:05:26 -0700436 *
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700437 * @param sid segment id
438 * @return deviceId device identifier
sangho80f11cb2015-04-01 13:05:26 -0700439 */
440 public DeviceId getDeviceId(int sid) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700441 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
442 deviceConfigMap.entrySet()) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800443 if (entry.getValue().ipv4NodeSid == sid ||
444 entry.getValue().ipv6NodeSid == sid) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700445 return entry.getValue().deviceId;
sangho80f11cb2015-04-01 13:05:26 -0700446 }
447 }
448
449 return null;
450 }
451
452 /**
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700453 * Returns the device identifier or data plane identifier (dpid)
454 * of a segment router given its router ip address.
sangho80f11cb2015-04-01 13:05:26 -0700455 *
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700456 * @param ipAddress router ip address
457 * @return deviceId device identifier
sangho80f11cb2015-04-01 13:05:26 -0700458 */
459 public DeviceId getDeviceId(Ip4Address ipAddress) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700460 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
461 deviceConfigMap.entrySet()) {
Pier Ventreadb4ae62016-11-23 09:57:42 -0800462 if (entry.getValue().ipv4Loopback.equals(ipAddress)) {
463 return entry.getValue().deviceId;
464 }
465 }
466
467 return null;
468 }
469
470 /**
471 * Returns the device identifier or data plane identifier (dpid)
472 * of a segment router given its router ipv6 address.
473 *
474 * @param ipAddress router ipv6 address
475 * @return deviceId device identifier
476 */
477 public DeviceId getDeviceId(Ip6Address ipAddress) {
478 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
479 deviceConfigMap.entrySet()) {
480 if (entry.getValue().ipv6Loopback.equals(ipAddress)) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700481 return entry.getValue().deviceId;
sangho80f11cb2015-04-01 13:05:26 -0700482 }
483 }
484
485 return null;
486 }
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700487
488 /**
Saurav Das9f1c42e2015-10-23 10:51:11 -0700489 * Returns the configured port ip addresses for a segment router.
490 * These addresses serve as gateway IP addresses for the subnets configured
491 * on those ports.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700492 *
493 * @param deviceId device identifier
Saurav Dasc28b3432015-10-30 17:45:38 -0700494 * @return immutable set of ip addresses configured on the ports or null if not found
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700495 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800496 public Set<IpAddress> getPortIPs(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700497 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
498 if (srinfo != null) {
499 log.trace("getSubnetGatewayIps for device{} is {}", deviceId,
500 srinfo.gatewayIps.values());
Saurav Dasc28b3432015-10-30 17:45:38 -0700501 return ImmutableSet.copyOf(srinfo.gatewayIps.values());
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700502 }
Saurav Das7c305372015-10-28 12:39:42 -0700503 return null;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700504 }
505
506 /**
Charles Chancf789822019-03-22 10:04:27 -0700507 * Returns configured subnets for a segment router.
508 *
509 * @param deviceId device identifier
510 * @return list of ip prefixes or null if not found
511 */
512 public Set<IpPrefix> getConfiguredSubnets(DeviceId deviceId) {
513 Set<IpPrefix> subnets = srManager.interfaceService.getInterfaces().stream()
514 .filter(intf -> Objects.equals(deviceId, intf.connectPoint().deviceId()))
515 .flatMap(intf -> intf.ipAddressesList().stream())
516 .map(InterfaceIpAddress::subnetAddress)
517 .collect(Collectors.toSet());
518
519 if (subnets.isEmpty()) {
520 log.debug(NO_SUBNET, deviceId);
521 return Collections.emptySet();
522 }
523 return subnets;
524 }
525
526 /**
527 * Returns all subnets for a segment router, including subnets learnt from route service.
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700528 *
529 * @param deviceId device identifier
Charles Chan19b70032019-04-17 14:20:26 -0700530 * @return set of ip prefixes or null if not found
531 * @deprecated use getBatchedSubnets(DeviceId deviceId) instead
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700532 */
Charles Chan19b70032019-04-17 14:20:26 -0700533 @Deprecated
Pier Ventreb6a7f342016-11-26 21:05:22 -0800534 public Set<IpPrefix> getSubnets(DeviceId deviceId) {
Saurav Das7c305372015-10-28 12:39:42 -0700535 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan5eb66042018-06-18 14:42:17 -0700536 if (srinfo != null && srinfo.subnets != null) {
537 // Note: ImmutableSet.Builder.addAll calls the iterator of parameter internally,
538 // which is not protected by SynchronizedCollection mutex.
Pier Ventreb6a7f342016-11-26 21:05:22 -0800539 ImmutableSet.Builder<IpPrefix> builder = ImmutableSet.builder();
Charles Chan5eb66042018-06-18 14:42:17 -0700540 srinfo.subnets.forEach((k, v) -> builder.add(v));
541 return builder.build();
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700542 }
Saurav Das7c305372015-10-28 12:39:42 -0700543 return null;
544 }
545
Charles Chan19b70032019-04-17 14:20:26 -0700546 /**
Charles Chan5eec3b12019-04-18 14:30:41 -0700547 * Returns batches of all subnets reachable via given next hop
548 * <p>
549 * First batch includes FPM and STATIC routes
550 * Second batch includes all other type of routes obtained from routeService, including DHCP routes.
551 *
552 * @param hostId next hop host id
553 * @return list of subnet batches, each batch includes a set of prefixes.
554 */
555 // TODO Querying routeService directly may be expensive. Some kind of reverse lookup cache should be developed.
556 public List<Set<IpPrefix>> getBatchedSubnets(HostId hostId) {
557 Set<IpPrefix> high = Sets.newHashSet();
558 Set<IpPrefix> low = Sets.newHashSet();
559
560 srManager.routeService.getRouteTables().stream()
561 .map(tableId -> srManager.routeService.getResolvedRoutes(tableId))
562 .flatMap(Collection::stream)
563 .forEach(resolvedRoute -> {
564 // Continue if next hop is not what we are looking for
565 if (!Objects.equals(hostId.mac(), resolvedRoute.nextHopMac()) ||
566 !Objects.equals(hostId.vlanId(), resolvedRoute.nextHopVlan())) {
567 return;
568 }
569 // Prioritize STATIC and FPM among others
570 if (resolvedRoute.route().source() == Route.Source.STATIC ||
571 resolvedRoute.route().source() == Route.Source.FPM) {
572 high.add(resolvedRoute.prefix());
573 } else {
574 low.add(resolvedRoute.prefix());
575 }
576 });
577 return Stream.of(high, low).filter(set -> !set.isEmpty()).collect(Collectors.toList());
578 }
579
580 /**
Charles Chan19b70032019-04-17 14:20:26 -0700581 * Returns batches of all subnets reachable on the given device.
582 * <p>
583 * First batch includes configured subnets, FPM and STATIC routes
584 * Second batch includes all other type of routes obtained from routeService, including DHCP routes.
585 *
586 * @param deviceId device identifier
587 * @return list of subnet batches, each batch includes a set of prefixes.
588 */
589 // TODO Querying routeService directly may be expensive. Some kind of reverse lookup cache should be developed.
590 public List<Set<IpPrefix>> getBatchedSubnets(DeviceId deviceId) {
591 Set<IpPrefix> high = Sets.newHashSet();
592 Set<IpPrefix> low = Sets.newHashSet();
593
594 high.addAll(getConfiguredSubnets(deviceId));
595 srManager.routeService.getRouteTables().stream()
596 .map(tableId -> srManager.routeService.getResolvedRoutes(tableId))
597 .flatMap(Collection::stream)
598 .forEach(resolvedRoute -> {
599 // Continue to next resolved route if none of the next hop attaches to given device
600 if (srManager.nextHopLocations(resolvedRoute).stream()
601 .noneMatch(cp -> Objects.equals(deviceId, cp.deviceId()))) {
602 return;
603 }
604 // Prioritize STATIC and FPM among others
605 if (resolvedRoute.route().source() == Route.Source.STATIC ||
606 resolvedRoute.route().source() == Route.Source.FPM) {
607 high.add(resolvedRoute.prefix());
608 } else {
609 low.add(resolvedRoute.prefix());
610 }
611 });
612 return Lists.newArrayList(high, low);
613 }
Charles Chan46fdfaf2016-11-09 20:51:44 -0800614
Saurav Das7c305372015-10-28 12:39:42 -0700615 /**
Charles Chan46fdfaf2016-11-09 20:51:44 -0800616 * Returns the subnet configuration of given device and port.
Saurav Das7c305372015-10-28 12:39:42 -0700617 *
Charles Chan46fdfaf2016-11-09 20:51:44 -0800618 * @param deviceId Device ID
619 * @param port Port number
Pier Ventreb6a7f342016-11-26 21:05:22 -0800620 * @return The subnets configured on given port or empty set if
Charles Chan2f4d2bc2017-04-24 16:21:01 -0700621 * the port is unconfigured or suppressed.
Saurav Das7c305372015-10-28 12:39:42 -0700622 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800623 public Set<IpPrefix> getPortSubnets(DeviceId deviceId, PortNumber port) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800624 ConnectPoint connectPoint = new ConnectPoint(deviceId, port);
625
626 if (isSuppressedPort(connectPoint)) {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800627 return Collections.emptySet();
Saurav Das7c305372015-10-28 12:39:42 -0700628 }
Charles Chan46fdfaf2016-11-09 20:51:44 -0800629
Charles Chancf789822019-03-22 10:04:27 -0700630 Set<IpPrefix> subnets = srManager.interfaceService.getInterfacesByPort(connectPoint).stream()
631 .flatMap(intf -> intf.ipAddressesList().stream())
632 .map(InterfaceIpAddress::subnetAddress)
633 .collect(Collectors.toSet());
Charles Chan46fdfaf2016-11-09 20:51:44 -0800634
Jon Hall31d84782017-01-18 20:15:44 -0800635 if (subnets.isEmpty()) {
Saurav Dasf9332192017-02-18 14:05:44 -0800636 log.debug(NO_SUBNET, connectPoint);
Pier Ventreb6a7f342016-11-26 21:05:22 -0800637 return Collections.emptySet();
Charles Chan46fdfaf2016-11-09 20:51:44 -0800638 }
Charles Chan2f4d2bc2017-04-24 16:21:01 -0700639 return subnets;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800640 }
641
642 /**
Charles Chan873661e2017-11-30 15:37:50 -0800643 * Returns all ports that has a subnet that contains any of the given IP addresses.
644 *
645 * @param ips a set of IP addresses
646 * @return a set of connect point that has a subnet that contains any of the given IP addresses
647 */
648 public Set<ConnectPoint> getPortByIps(Set<IpAddress> ips) {
649 return srManager.interfaceService.getInterfaces().stream()
650 .filter(intf -> intf.ipAddressesList().stream().anyMatch(intfAddress ->
651 ips.stream().anyMatch(ip -> intfAddress.subnetAddress().contains(ip))))
652 .map(Interface::connectPoint)
653 .collect(Collectors.toSet());
654 }
655
656 /**
pier6a2052b2019-06-28 22:17:31 +0200657 * Returns all the connect points of the segment routers that have the
658 * specified ip address in their subnets.
659 *
660 * @param destIpAddress target ip address
661 * @return connect points of the segment routers
662 */
663 public Set<ConnectPoint> getConnectPointsForASubnetHost(IpAddress destIpAddress) {
664 return srManager.interfaceService.getMatchingInterfaces(destIpAddress).stream()
665 .map(Interface::connectPoint)
666 .collect(Collectors.toSet());
667 }
668
669 /**
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700670 * Returns the router ip address of segment router that has the
671 * specified ip address in its subnets.
672 *
673 * @param destIpAddress target ip address
674 * @return router ip address
675 */
676 public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) {
Charles Chan70661362016-12-09 12:54:49 -0800677 Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress);
678
679 if (matchIntf == null) {
680 log.debug("No router was found for {}", destIpAddress);
681 return null;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700682 }
683
Charles Chan70661362016-12-09 12:54:49 -0800684 DeviceId routerDeviceId = matchIntf.connectPoint().deviceId();
685 SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId);
686 if (srInfo == null) {
687 log.debug("No device config was found for {}", routerDeviceId);
688 return null;
689 }
690
Pier Ventreadb4ae62016-11-23 09:57:42 -0800691 return srInfo.ipv4Loopback;
692 }
693
694 /**
695 * Returns the router ipv6 address of segment router that has the
696 * specified ip address in its subnets.
697 *
698 * @param destIpAddress target ip address
699 * @return router ip address
700 */
701 public Ip6Address getRouterIpAddressForASubnetHost(Ip6Address destIpAddress) {
702 Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress);
703
704 if (matchIntf == null) {
705 log.debug("No router was found for {}", destIpAddress);
706 return null;
707 }
708
709 DeviceId routerDeviceId = matchIntf.connectPoint().deviceId();
710 SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId);
711 if (srInfo == null) {
712 log.debug("No device config was found for {}", routerDeviceId);
713 return null;
714 }
715
716 return srInfo.ipv6Loopback;
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700717 }
718
719 /**
720 * Returns the router mac address of segment router that has the
721 * specified ip address as one of its subnet gateway ip address.
722 *
723 * @param gatewayIpAddress router gateway ip address
Saurav Das7c305372015-10-28 12:39:42 -0700724 * @return router mac address or null if not found
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700725 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800726 public MacAddress getRouterMacForAGatewayIp(IpAddress gatewayIpAddress) {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700727 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
728 deviceConfigMap.entrySet()) {
729 if (entry.getValue().gatewayIps.
730 values().contains(gatewayIpAddress)) {
731 return entry.getValue().mac;
732 }
733 }
734
735 log.debug("Cannot find a router for {}", gatewayIpAddress);
736 return null;
737 }
sangho9b169e32015-04-14 16:27:13 -0700738
sangho9b169e32015-04-14 16:27:13 -0700739 /**
Charles Chan35f21e42017-06-26 18:30:18 -0700740 * Checks if the host IP is in any of the subnet defined in the router with the
sangho9b169e32015-04-14 16:27:13 -0700741 * device ID given.
742 *
743 * @param deviceId device identification of the router
744 * @param hostIp host IP address to check
Charles Chan35f21e42017-06-26 18:30:18 -0700745 * @return true if the given IP is within any of the subnet defined in the router,
746 * false if no subnet is defined in the router or if the host is not
747 * within any subnet defined in the router
sangho9b169e32015-04-14 16:27:13 -0700748 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800749 public boolean inSameSubnet(DeviceId deviceId, IpAddress hostIp) {
Charles Chancf789822019-03-22 10:04:27 -0700750 Set<IpPrefix> subnets = getConfiguredSubnets(deviceId);
sangho9b169e32015-04-14 16:27:13 -0700751 if (subnets == null) {
752 return false;
753 }
754
Pier Ventreb6a7f342016-11-26 21:05:22 -0800755 for (IpPrefix subnet: subnets) {
Charles Chan82ab1932016-01-30 23:22:37 -0800756 // Exclude /0 since it is a special case used for default route
757 if (subnet.prefixLength() != 0 && subnet.contains(hostIp)) {
sangho9b169e32015-04-14 16:27:13 -0700758 return true;
759 }
760 }
sangho9b169e32015-04-14 16:27:13 -0700761 return false;
762 }
sangho27462c62015-05-14 00:39:53 -0700763
764 /**
Charles Chandebfea32016-10-24 14:52:01 -0700765 * Checks if the IP is in the subnet defined on given connect point.
766 *
767 * @param connectPoint Connect point
768 * @param ip The IP address to check
769 * @return True if the IP belongs to the subnet.
770 * False if the IP does not belong to the subnet, or
771 * there is no subnet configuration on given connect point.
772 */
773 public boolean inSameSubnet(ConnectPoint connectPoint, IpAddress ip) {
Charles Chan35f21e42017-06-26 18:30:18 -0700774 return getPortSubnets(connectPoint.deviceId(), connectPoint.port()).stream()
775 .anyMatch(ipPrefix -> ipPrefix.contains(ip));
Charles Chandebfea32016-10-24 14:52:01 -0700776 }
777
778 /**
sangho27462c62015-05-14 00:39:53 -0700779 * Returns the ports corresponding to the adjacency Sid given.
780 *
781 * @param deviceId device identification of the router
782 * @param sid adjacency Sid
Charles Chan9bec8a32015-12-01 10:00:51 -0800783 * @return set of port numbers
sangho27462c62015-05-14 00:39:53 -0700784 */
Charles Chan9bec8a32015-12-01 10:00:51 -0800785 public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das7c305372015-10-28 12:39:42 -0700786 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan9bec8a32015-12-01 10:00:51 -0800787 return srinfo != null ?
788 ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) :
789 ImmutableSet.copyOf(new HashSet<>());
sangho27462c62015-05-14 00:39:53 -0700790 }
791
792 /**
793 * Check if the Sid given is whether adjacency Sid of the router device or not.
794 *
795 * @param deviceId device identification of the router
796 * @param sid Sid to check
797 * @return true if the Sid given is the adjacency Sid of the device,
798 * otherwise false
799 */
800 public boolean isAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das7c305372015-10-28 12:39:42 -0700801 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan9bec8a32015-12-01 10:00:51 -0800802 return srinfo != null && srinfo.adjacencySids.containsKey(sid);
sangho27462c62015-05-14 00:39:53 -0700803 }
Charles Chan43547ca2016-02-10 20:46:58 -0800804
Charles Chanc91c8782016-03-30 17:54:24 -0700805 /**
Charles Chanc22cef32016-04-29 14:38:22 -0700806 * Add subnet to specific connect point.
807 *
808 * @param cp connect point
Pier Ventreb6a7f342016-11-26 21:05:22 -0800809 * @param ipPrefix subnet being added to the device
Charles Chanc22cef32016-04-29 14:38:22 -0700810 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800811 public void addSubnet(ConnectPoint cp, IpPrefix ipPrefix) {
Charles Chanc22cef32016-04-29 14:38:22 -0700812 checkNotNull(cp);
Pier Ventreb6a7f342016-11-26 21:05:22 -0800813 checkNotNull(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700814 SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId());
815 if (srinfo == null) {
816 log.warn("Device {} is not configured. Abort.", cp.deviceId());
817 return;
818 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800819 srinfo.subnets.put(cp.port(), ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700820 }
821
822 /**
823 * Remove subnet from specific connect point.
824 *
825 * @param cp connect point
Pier Ventreb6a7f342016-11-26 21:05:22 -0800826 * @param ipPrefix subnet being removed to the device
Charles Chanc22cef32016-04-29 14:38:22 -0700827 */
Pier Ventreb6a7f342016-11-26 21:05:22 -0800828 public void removeSubnet(ConnectPoint cp, IpPrefix ipPrefix) {
Charles Chanc22cef32016-04-29 14:38:22 -0700829 checkNotNull(cp);
Pier Ventreb6a7f342016-11-26 21:05:22 -0800830 checkNotNull(ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700831 SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId());
832 if (srinfo == null) {
833 log.warn("Device {} is not configured. Abort.", cp.deviceId());
834 return;
835 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800836 srinfo.subnets.remove(cp.port(), ipPrefix);
Charles Chanc22cef32016-04-29 14:38:22 -0700837 }
Charles Chan46fdfaf2016-11-09 20:51:44 -0800838
839 private boolean isSuppressedPort(ConnectPoint connectPoint) {
840 SegmentRoutingAppConfig appConfig = srManager.cfgService
Ray Milkeyb85de082017-04-05 09:42:04 -0700841 .getConfig(srManager.appId(), SegmentRoutingAppConfig.class);
Charles Chan46fdfaf2016-11-09 20:51:44 -0800842 if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
Charles Chandebfea32016-10-24 14:52:01 -0700843 log.info("Interface configuration on port {} is ignored", connectPoint);
Charles Chan46fdfaf2016-11-09 20:51:44 -0800844 return true;
845 }
846 return false;
847 }
Saurav Das261c3002017-06-13 15:35:54 -0700848
849 public boolean isPairedEdge(DeviceId deviceId) throws DeviceConfigNotFoundException {
850 if (!isEdgeDevice(deviceId)) {
851 return false;
852 }
853 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
854 return (srinfo.pairDeviceId == null) ? false : true;
855 }
856
857 public DeviceId getPairDeviceId(DeviceId deviceId) throws DeviceConfigNotFoundException {
858 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
859 if (srinfo != null) {
860 return srinfo.pairDeviceId;
861 } else {
862 String message = "getPairDeviceId fails for device: " + deviceId + ".";
863 throw new DeviceConfigNotFoundException(message);
864 }
865 }
866
867 public PortNumber getPairLocalPort(DeviceId deviceId)
868 throws DeviceConfigNotFoundException {
869 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
870 if (srinfo != null) {
871 return srinfo.pairLocalPort;
872 } else {
873 String message = "getPairLocalPort fails for device: " + deviceId + ".";
874 throw new DeviceConfigNotFoundException(message);
875 }
876 }
877
Saurav Dasec683dc2018-04-27 18:42:30 -0700878 public boolean isPairLocalPort(DeviceId devId, PortNumber pnum) {
879 return pnum.equals(srManager.getPairLocalPort(devId).orElse(null));
880 }
Jonathan Hart8fa9ec52016-02-16 10:30:37 -0800881}