blob: 626109a7cb4348181e904d1b9530179a0c95e242 [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 Chan82fac582017-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 Chan5270ed02016-01-30 23:22:37 -080021import com.google.common.collect.HashMultimap;
Charles Chan9f676b62015-10-29 14:58:10 -070022import com.google.common.collect.ImmutableSet;
Charles Chan9c44ec72018-03-20 13:30:38 -070023import com.google.common.collect.Multimaps;
Charles Chan5270ed02016-01-30 23:22:37 -080024import com.google.common.collect.SetMultimap;
sanghob35a6192015-04-01 13:05:26 -070025import org.onlab.packet.Ip4Address;
Pier Ventree0ae7a32016-11-23 09:57:42 -080026import org.onlab.packet.Ip6Address;
Charles Chan03a73e02016-10-24 14:52:01 -070027import org.onlab.packet.IpAddress;
Charles Chan5270ed02016-01-30 23:22:37 -080028import org.onlab.packet.IpPrefix;
sanghob35a6192015-04-01 13:05:26 -070029import org.onlab.packet.MacAddress;
Charles Chane849c192016-01-11 18:28:54 -080030import org.onlab.packet.VlanId;
Charles Chan4636be02015-10-07 14:21:45 -070031import org.onosproject.net.ConnectPoint;
sanghob35a6192015-04-01 13:05:26 -070032import org.onosproject.net.DeviceId;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070033import org.onosproject.net.PortNumber;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -080034import org.onosproject.net.config.ConfigException;
35import org.onosproject.net.config.basics.InterfaceConfig;
36import org.onosproject.net.host.InterfaceIpAddress;
37import org.onosproject.net.intf.Interface;
Charles Chan2c15aca2016-11-09 20:51:44 -080038import org.onosproject.segmentrouting.SegmentRoutingManager;
sanghob35a6192015-04-01 13:05:26 -070039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070042import java.util.ArrayList;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -080043import java.util.Collection;
Pier Ventre10bd8d12016-11-26 21:05:22 -080044import java.util.Collections;
sanghob35a6192015-04-01 13:05:26 -070045import java.util.HashMap;
Charles Chan531a78b2015-12-01 10:00:51 -080046import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070047import java.util.List;
48import java.util.Map;
Charles Chand6832882015-10-05 17:50:33 -070049import java.util.Set;
Saurav Das0e99e2b2015-10-28 12:39:42 -070050import java.util.concurrent.ConcurrentHashMap;
Charles Chan2c15aca2016-11-09 20:51:44 -080051import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -070052
Charles Chan93e71ba2016-04-29 14:38:22 -070053import static com.google.common.base.Preconditions.checkNotNull;
54
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070055/**
56 * Segment Routing configuration component that reads the
57 * segment routing related configuration from Network Configuration Manager
58 * component and organizes in more accessible formats.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070059 */
sanghob35a6192015-04-01 13:05:26 -070060public class DeviceConfiguration implements DeviceProperties {
61
Charles Chan2c15aca2016-11-09 20:51:44 -080062 private static final String NO_SUBNET = "No subnet configured on {}";
63
Charles Chanf2565a92016-02-10 20:46:58 -080064 private static final Logger log = LoggerFactory.getLogger(DeviceConfiguration.class);
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -070065 private final List<Integer> allSegmentIds = new ArrayList<>();
Charles Chane849c192016-01-11 18:28:54 -080066 private final Map<DeviceId, SegmentRouterInfo> deviceConfigMap = new ConcurrentHashMap<>();
Charles Chan2c15aca2016-11-09 20:51:44 -080067 private SegmentRoutingManager srManager;
sanghob35a6192015-04-01 13:05:26 -070068
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070069 private class SegmentRouterInfo {
Pier Ventree0ae7a32016-11-23 09:57:42 -080070 int ipv4NodeSid;
71 int ipv6NodeSid;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070072 DeviceId deviceId;
Pier Ventree0ae7a32016-11-23 09:57:42 -080073 Ip4Address ipv4Loopback;
74 Ip6Address ipv6Loopback;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070075 MacAddress mac;
76 boolean isEdge;
Pier Ventre10bd8d12016-11-26 21:05:22 -080077 SetMultimap<PortNumber, IpAddress> gatewayIps;
78 SetMultimap<PortNumber, IpPrefix> subnets;
Charles Chan531a78b2015-12-01 10:00:51 -080079 Map<Integer, Set<Integer>> adjacencySids;
Saurav Das7bcbe702017-06-13 15:35:54 -070080 DeviceId pairDeviceId;
81 PortNumber pairLocalPort;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070082 int pwRoutingLabel;
Charles Chanb8e10c82015-10-14 11:24:40 -070083
84 public SegmentRouterInfo() {
Charles Chan9c44ec72018-03-20 13:30:38 -070085 gatewayIps = Multimaps.synchronizedSetMultimap(HashMultimap.create());
86 subnets = Multimaps.synchronizedSetMultimap(HashMultimap.create());
Charles Chanb8e10c82015-10-14 11:24:40 -070087 }
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070088 }
sanghob35a6192015-04-01 13:05:26 -070089
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070090 /**
Charles Chane849c192016-01-11 18:28:54 -080091 * Constructs device configuration for all Segment Router devices,
92 * organizing the data into various maps for easier access.
Brian O'Connor52515622015-10-09 17:03:44 -070093 *
Charles Chan2c15aca2016-11-09 20:51:44 -080094 * @param srManager Segment Routing Manager
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070095 */
Charles Chan2c15aca2016-11-09 20:51:44 -080096 public DeviceConfiguration(SegmentRoutingManager srManager) {
97 this.srManager = srManager;
Saurav Das7bcbe702017-06-13 15:35:54 -070098 updateConfig();
99 }
Charles Chand9681e72016-02-22 19:27:29 -0800100
Saurav Das7bcbe702017-06-13 15:35:54 -0700101 public void updateConfig() {
Charles Chan4636be02015-10-07 14:21:45 -0700102 // Read config from device subject, excluding gatewayIps and subnets.
103 Set<DeviceId> deviceSubjects =
Charles Chan2c15aca2016-11-09 20:51:44 -0800104 srManager.cfgService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class);
Charles Chan4636be02015-10-07 14:21:45 -0700105 deviceSubjects.forEach(subject -> {
Charles Chan5270ed02016-01-30 23:22:37 -0800106 SegmentRoutingDeviceConfig config =
Charles Chan2c15aca2016-11-09 20:51:44 -0800107 srManager.cfgService.getConfig(subject, SegmentRoutingDeviceConfig.class);
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700108 SegmentRouterInfo info = new SegmentRouterInfo();
Charles Chand6832882015-10-05 17:50:33 -0700109 info.deviceId = subject;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800110 info.ipv4NodeSid = config.nodeSidIPv4();
111 info.ipv6NodeSid = config.nodeSidIPv6();
112 info.ipv4Loopback = config.routerIpv4();
113 info.ipv6Loopback = config.routerIpv6();
Charles Chan531a78b2015-12-01 10:00:51 -0800114 info.mac = config.routerMac();
Charles Chand6832882015-10-05 17:50:33 -0700115 info.isEdge = config.isEdgeRouter();
Charles Chan531a78b2015-12-01 10:00:51 -0800116 info.adjacencySids = config.adjacencySids();
Saurav Das7bcbe702017-06-13 15:35:54 -0700117 info.pairDeviceId = config.pairDeviceId();
118 info.pairLocalPort = config.pairLocalPort();
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700119 info.pwRoutingLabel = info.ipv4NodeSid + 1000;
Charles Chane849c192016-01-11 18:28:54 -0800120 deviceConfigMap.put(info.deviceId, info);
Charles Chan278ce832017-06-26 15:25:09 -0700121 log.debug("Read device config for device: {}", info.deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800122 /*
123 * IPv6 sid is not inserted. this part of the code is not used for now.
124 */
125 allSegmentIds.add(info.ipv4NodeSid);
Charles Chand6832882015-10-05 17:50:33 -0700126 });
Charles Chan4636be02015-10-07 14:21:45 -0700127
Charles Chan2c15aca2016-11-09 20:51:44 -0800128 // Read gatewayIps and subnets from port subject. Ignore suppressed ports.
129 Set<ConnectPoint> portSubjects = srManager.cfgService
130 .getSubjects(ConnectPoint.class, InterfaceConfig.class);
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800131 portSubjects.stream()
132 .filter(subject -> deviceConfigMap.containsKey(subject.deviceId()))
133 .filter(subject -> !isSuppressedPort(subject)).forEach(subject -> {
Charles Chan4636be02015-10-07 14:21:45 -0700134 InterfaceConfig config =
Charles Chan2c15aca2016-11-09 20:51:44 -0800135 srManager.cfgService.getConfig(subject, InterfaceConfig.class);
Charles Chan4636be02015-10-07 14:21:45 -0700136 Set<Interface> networkInterfaces;
137 try {
138 networkInterfaces = config.getInterfaces();
139 } catch (ConfigException e) {
140 log.error("Error loading port configuration");
141 return;
142 }
143 networkInterfaces.forEach(networkInterface -> {
Charles Chane849c192016-01-11 18:28:54 -0800144 VlanId vlanId = networkInterface.vlan();
145 ConnectPoint connectPoint = networkInterface.connectPoint();
146 DeviceId dpid = connectPoint.deviceId();
147 PortNumber port = connectPoint.port();
Charles Chan82fac582017-09-12 12:09:22 -0700148 MacAddress mac = networkInterface.mac();
Charles Chane849c192016-01-11 18:28:54 -0800149 SegmentRouterInfo info = deviceConfigMap.get(dpid);
Charles Chan4636be02015-10-07 14:21:45 -0700150
Charles Chanb8e10c82015-10-14 11:24:40 -0700151 // skip if there is no corresponding device for this ConenctPoint
152 if (info != null) {
Charles Chane849c192016-01-11 18:28:54 -0800153 // Extract subnet information
Jonathan Hart00cddda2016-02-16 10:30:37 -0800154 List<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddressesList();
Charles Chanb8e10c82015-10-14 11:24:40 -0700155 interfaceAddresses.forEach(interfaceAddress -> {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800156 // Do not add /0, /32 and /128 to gateway IP list
Charles Chan5270ed02016-01-30 23:22:37 -0800157 int prefixLength = interfaceAddress.subnetAddress().prefixLength();
Pier Ventre10bd8d12016-11-26 21:05:22 -0800158 IpPrefix ipPrefix = interfaceAddress.subnetAddress();
159 if (ipPrefix.isIp4()) {
160 if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET_MASK_LENGTH) {
161 info.gatewayIps.put(port, interfaceAddress.ipAddress());
162 }
163 info.subnets.put(port, interfaceAddress.subnetAddress());
164 } else {
165 if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET6_MASK_LENGTH) {
166 info.gatewayIps.put(port, interfaceAddress.ipAddress());
167 }
168 info.subnets.put(port, interfaceAddress.subnetAddress());
Charles Chan5270ed02016-01-30 23:22:37 -0800169 }
Charles Chanb8e10c82015-10-14 11:24:40 -0700170 });
Charles Chan82fac582017-09-12 12:09:22 -0700171
172 // Override interface mac with router mac
173 if (!mac.equals(info.mac)) {
174 ArrayNode array = (ArrayNode) config.node();
175 for (JsonNode intfNode : array) {
176 ObjectNode objNode = (ObjectNode) intfNode;
177 objNode.put(InterfaceConfig.MAC, info.mac.toString());
178 }
179 srManager.cfgService.applyConfig(connectPoint, InterfaceConfig.class, array);
180 }
Charles Chanb8e10c82015-10-14 11:24:40 -0700181 }
Charles Chan4636be02015-10-07 14:21:45 -0700182 });
Pier Luigi0e358632017-01-31 09:35:05 -0800183 // We register the connect point with the NRS.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800184 srManager.registerConnectPoint(subject);
Charles Chan4636be02015-10-07 14:21:45 -0700185 });
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700186 }
187
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800188 public Collection<DeviceId> getRouters() {
189 return deviceConfigMap.keySet();
190 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700191
sanghob35a6192015-04-01 13:05:26 -0700192 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800193 public boolean isConfigured(DeviceId deviceId) {
194 return deviceConfigMap.get(deviceId) != null;
195 }
196
197 @Override
Pier Ventree0ae7a32016-11-23 09:57:42 -0800198 public int getIPv4SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700199 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
200 if (srinfo != null) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800201 log.trace("getIPv4SegmentId for device{} is {}", deviceId, srinfo.ipv4NodeSid);
202 return srinfo.ipv4NodeSid;
sanghob35a6192015-04-01 13:05:26 -0700203 } else {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800204 String message = "getIPv4SegmentId fails for device: " + deviceId + ".";
205 throw new DeviceConfigNotFoundException(message);
206 }
207 }
208
209 @Override
210 public int getIPv6SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException {
211 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
212 if (srinfo != null) {
213 log.trace("getIPv6SegmentId for device{} is {}", deviceId, srinfo.ipv6NodeSid);
214 return srinfo.ipv6NodeSid;
215 } else {
216 String message = "getIPv6SegmentId fails for device: " + deviceId + ".";
Charles Chan0b4e6182015-11-03 10:42:14 -0800217 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700218 }
219 }
220
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700221 @Override
222 public int getPWRoutingLabel(DeviceId deviceId) throws DeviceConfigNotFoundException {
223 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
224 if (srinfo != null) {
225 log.trace("pwRoutingLabel for device{} is {}", deviceId, srinfo.pwRoutingLabel);
226 return srinfo.pwRoutingLabel;
227 } else {
228 String message = "getPWRoutingLabel fails for device: " + deviceId + ".";
229 throw new DeviceConfigNotFoundException(message);
230 }
231 }
232
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700233 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800234 * Returns the IPv4 Node segment id of a segment router given its Router mac address.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700235 *
236 * @param routerMac router mac address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700237 * @return node segment id, or -1 if not found in config
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700238 */
Pier Ventree0ae7a32016-11-23 09:57:42 -0800239 public int getIPv4SegmentId(MacAddress routerMac) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700240 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
241 deviceConfigMap.entrySet()) {
242 if (entry.getValue().mac.equals(routerMac)) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800243 return entry.getValue().ipv4NodeSid;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700244 }
245 }
sanghob35a6192015-04-01 13:05:26 -0700246
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700247 return -1;
248 }
249
250 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800251 * Returns the IPv6 Node segment id of a segment router given its Router mac address.
252 *
253 * @param routerMac router mac address
254 * @return node segment id, or -1 if not found in config
255 */
256 public int getIPv6SegmentId(MacAddress routerMac) {
257 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
258 deviceConfigMap.entrySet()) {
259 if (entry.getValue().mac.equals(routerMac)) {
260 return entry.getValue().ipv6NodeSid;
261 }
262 }
263
264 return -1;
265 }
266
267 /**
268 * Returns the IPv4 Node segment id of a segment router given its Router ip address.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700269 *
270 * @param routerAddress router ip address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700271 * @return node segment id, or -1 if not found in config
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700272 */
Pier Ventree0ae7a32016-11-23 09:57:42 -0800273 public int getIPv4SegmentId(Ip4Address routerAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700274 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
275 deviceConfigMap.entrySet()) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800276 if (entry.getValue().ipv4Loopback.equals(routerAddress)) {
277 return entry.getValue().ipv4NodeSid;
278 }
279 }
280
281 return -1;
282 }
283
284 /**
285 * Returns the IPv6 Node segment id of a segment router given its Router ip address.
286 *
287 * @param routerAddress router ip address
288 * @return node segment id, or -1 if not found in config
289 */
290 public int getIPv6SegmentId(Ip6Address routerAddress) {
291 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
292 deviceConfigMap.entrySet()) {
293 if (entry.getValue().ipv6Loopback.equals(routerAddress)) {
294 return entry.getValue().ipv6NodeSid;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700295 }
296 }
297
298 return -1;
299 }
300
sanghob35a6192015-04-01 13:05:26 -0700301 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800302 public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700303 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
304 if (srinfo != null) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700305 return srinfo.mac;
sanghob35a6192015-04-01 13:05:26 -0700306 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800307 String message = "getDeviceMac fails for device: " + deviceId + ".";
308 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700309 }
310 }
311
Charles Chan0b4e6182015-11-03 10:42:14 -0800312 @Override
Pier Ventree0ae7a32016-11-23 09:57:42 -0800313 public Ip4Address getRouterIpv4(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700314 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
315 if (srinfo != null) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800316 log.trace("getRouterIpv4 for device{} is {}", deviceId, srinfo.ipv4Loopback);
317 return srinfo.ipv4Loopback;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700318 } else {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800319 String message = "getRouterIpv4 fails for device: " + deviceId + ".";
320 throw new DeviceConfigNotFoundException(message);
321 }
322 }
323
324 @Override
325 public Ip6Address getRouterIpv6(DeviceId deviceId) throws DeviceConfigNotFoundException {
326 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
327 if (srinfo != null) {
328 log.trace("getRouterIpv6 for device{} is {}", deviceId, srinfo.ipv6Loopback);
329 return srinfo.ipv6Loopback;
330 } else {
331 String message = "getRouterIpv6 fails for device: " + deviceId + ".";
Charles Chan0b4e6182015-11-03 10:42:14 -0800332 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700333 }
sanghob35a6192015-04-01 13:05:26 -0700334 }
335
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700336 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800337 public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700338 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
339 if (srinfo != null) {
340 log.trace("isEdgeDevice for device{} is {}", deviceId, srinfo.isEdge);
341 return srinfo.isEdge;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700342 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800343 String message = "isEdgeDevice fails for device: " + deviceId + ".";
344 throw new DeviceConfigNotFoundException(message);
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700345 }
346 }
347
sanghob35a6192015-04-01 13:05:26 -0700348 @Override
349 public List<Integer> getAllDeviceSegmentIds() {
350 return allSegmentIds;
351 }
352
Charles Chanc42e84e2015-10-20 16:24:19 -0700353 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800354 public Map<IpPrefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId)
Saurav Das7a1ffca2016-03-28 19:00:18 -0700355 throws DeviceConfigNotFoundException {
356 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
357 if (srinfo == null) {
358 String message = "getSubnetPortsMap fails for device: " + deviceId + ".";
359 throw new DeviceConfigNotFoundException(message);
360 }
Charles Chanc42e84e2015-10-20 16:24:19 -0700361 // Construct subnet-port mapping from port-subnet mapping
Pier Ventre10bd8d12016-11-26 21:05:22 -0800362 SetMultimap<PortNumber, IpPrefix> portSubnetMap = srinfo.subnets;
363 Map<IpPrefix, List<PortNumber>> subnetPortMap = new HashMap<>();
Charles Chan5270ed02016-01-30 23:22:37 -0800364
365 portSubnetMap.entries().forEach(entry -> {
366 PortNumber port = entry.getKey();
Pier Ventre10bd8d12016-11-26 21:05:22 -0800367 IpPrefix subnet = entry.getValue();
Charles Chan5270ed02016-01-30 23:22:37 -0800368
Pier Ventre10bd8d12016-11-26 21:05:22 -0800369 if (subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH ||
370 subnet.prefixLength() == IpPrefix.MAX_INET6_MASK_LENGTH) {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800371 return;
372 }
373
Charles Chanc42e84e2015-10-20 16:24:19 -0700374 if (subnetPortMap.containsKey(subnet)) {
375 subnetPortMap.get(subnet).add(port);
376 } else {
377 ArrayList<PortNumber> ports = new ArrayList<>();
378 ports.add(port);
379 subnetPortMap.put(subnet, ports);
380 }
381 });
Charles Chanc42e84e2015-10-20 16:24:19 -0700382 return subnetPortMap;
383 }
384
sanghob35a6192015-04-01 13:05:26 -0700385 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700386 * Returns the device identifier or data plane identifier (dpid)
387 * of a segment router given its segment id.
sanghob35a6192015-04-01 13:05:26 -0700388 *
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700389 * @param sid segment id
390 * @return deviceId device identifier
sanghob35a6192015-04-01 13:05:26 -0700391 */
392 public DeviceId getDeviceId(int sid) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700393 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
394 deviceConfigMap.entrySet()) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800395 if (entry.getValue().ipv4NodeSid == sid ||
396 entry.getValue().ipv6NodeSid == sid) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700397 return entry.getValue().deviceId;
sanghob35a6192015-04-01 13:05:26 -0700398 }
399 }
400
401 return null;
402 }
403
404 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700405 * Returns the device identifier or data plane identifier (dpid)
406 * of a segment router given its router ip address.
sanghob35a6192015-04-01 13:05:26 -0700407 *
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700408 * @param ipAddress router ip address
409 * @return deviceId device identifier
sanghob35a6192015-04-01 13:05:26 -0700410 */
411 public DeviceId getDeviceId(Ip4Address ipAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700412 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
413 deviceConfigMap.entrySet()) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800414 if (entry.getValue().ipv4Loopback.equals(ipAddress)) {
415 return entry.getValue().deviceId;
416 }
417 }
418
419 return null;
420 }
421
422 /**
423 * Returns the device identifier or data plane identifier (dpid)
424 * of a segment router given its router ipv6 address.
425 *
426 * @param ipAddress router ipv6 address
427 * @return deviceId device identifier
428 */
429 public DeviceId getDeviceId(Ip6Address ipAddress) {
430 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
431 deviceConfigMap.entrySet()) {
432 if (entry.getValue().ipv6Loopback.equals(ipAddress)) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700433 return entry.getValue().deviceId;
sanghob35a6192015-04-01 13:05:26 -0700434 }
435 }
436
437 return null;
438 }
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700439
440 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700441 * Returns the configured port ip addresses for a segment router.
442 * These addresses serve as gateway IP addresses for the subnets configured
443 * on those ports.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700444 *
445 * @param deviceId device identifier
Saurav Das837e0bb2015-10-30 17:45:38 -0700446 * @return immutable set of ip addresses configured on the ports or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700447 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800448 public Set<IpAddress> getPortIPs(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700449 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
450 if (srinfo != null) {
451 log.trace("getSubnetGatewayIps for device{} is {}", deviceId,
452 srinfo.gatewayIps.values());
Saurav Das837e0bb2015-10-30 17:45:38 -0700453 return ImmutableSet.copyOf(srinfo.gatewayIps.values());
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700454 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700455 return null;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700456 }
457
458 /**
459 * Returns the configured subnet prefixes for a segment router.
460 *
461 * @param deviceId device identifier
Saurav Das0e99e2b2015-10-28 12:39:42 -0700462 * @return list of ip prefixes or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700463 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800464 public Set<IpPrefix> getSubnets(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700465 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
466 if (srinfo != null) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800467 ImmutableSet.Builder<IpPrefix> builder = ImmutableSet.builder();
Charles Chan03a73e02016-10-24 14:52:01 -0700468 return builder.addAll(srinfo.subnets.values()).build();
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700469 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700470 return null;
471 }
472
Charles Chan2c15aca2016-11-09 20:51:44 -0800473
Saurav Das0e99e2b2015-10-28 12:39:42 -0700474 /**
Charles Chan2c15aca2016-11-09 20:51:44 -0800475 * Returns the subnet configuration of given device and port.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700476 *
Charles Chan2c15aca2016-11-09 20:51:44 -0800477 * @param deviceId Device ID
478 * @param port Port number
Pier Ventre10bd8d12016-11-26 21:05:22 -0800479 * @return The subnets configured on given port or empty set if
Charles Chanc53d6172017-04-24 16:21:01 -0700480 * the port is unconfigured or suppressed.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700481 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800482 public Set<IpPrefix> getPortSubnets(DeviceId deviceId, PortNumber port) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800483 ConnectPoint connectPoint = new ConnectPoint(deviceId, port);
484
485 if (isSuppressedPort(connectPoint)) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800486 return Collections.emptySet();
Saurav Das0e99e2b2015-10-28 12:39:42 -0700487 }
Charles Chan2c15aca2016-11-09 20:51:44 -0800488
Pier Ventre10bd8d12016-11-26 21:05:22 -0800489 Set<IpPrefix> subnets =
Charles Chan2c15aca2016-11-09 20:51:44 -0800490 srManager.interfaceService.getInterfacesByPort(connectPoint).stream()
491 .flatMap(intf -> intf.ipAddressesList().stream())
492 .map(InterfaceIpAddress::subnetAddress)
Charles Chan2c15aca2016-11-09 20:51:44 -0800493 .collect(Collectors.toSet());
494
Jon Hallcbd1b392017-01-18 20:15:44 -0800495 if (subnets.isEmpty()) {
Saurav Das018605f2017-02-18 14:05:44 -0800496 log.debug(NO_SUBNET, connectPoint);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800497 return Collections.emptySet();
Charles Chan2c15aca2016-11-09 20:51:44 -0800498 }
Charles Chanc53d6172017-04-24 16:21:01 -0700499
500 return subnets;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800501 }
502
503 /**
Charles Chan47933752017-11-30 15:37:50 -0800504 * Returns all ports that has a subnet that contains any of the given IP addresses.
505 *
506 * @param ips a set of IP addresses
507 * @return a set of connect point that has a subnet that contains any of the given IP addresses
508 */
509 public Set<ConnectPoint> getPortByIps(Set<IpAddress> ips) {
510 return srManager.interfaceService.getInterfaces().stream()
511 .filter(intf -> intf.ipAddressesList().stream().anyMatch(intfAddress ->
512 ips.stream().anyMatch(ip -> intfAddress.subnetAddress().contains(ip))))
513 .map(Interface::connectPoint)
514 .collect(Collectors.toSet());
515 }
516
517 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700518 * Returns the router ip address of segment router that has the
519 * specified ip address in its subnets.
520 *
521 * @param destIpAddress target ip address
522 * @return router ip address
523 */
524 public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) {
Charles Chan68aaad52016-12-09 12:54:49 -0800525 Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress);
526
527 if (matchIntf == null) {
528 log.debug("No router was found for {}", destIpAddress);
529 return null;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700530 }
531
Charles Chan68aaad52016-12-09 12:54:49 -0800532 DeviceId routerDeviceId = matchIntf.connectPoint().deviceId();
533 SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId);
534 if (srInfo == null) {
535 log.debug("No device config was found for {}", routerDeviceId);
536 return null;
537 }
538
Pier Ventree0ae7a32016-11-23 09:57:42 -0800539 return srInfo.ipv4Loopback;
540 }
541
542 /**
543 * Returns the router ipv6 address of segment router that has the
544 * specified ip address in its subnets.
545 *
546 * @param destIpAddress target ip address
547 * @return router ip address
548 */
549 public Ip6Address getRouterIpAddressForASubnetHost(Ip6Address destIpAddress) {
550 Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress);
551
552 if (matchIntf == null) {
553 log.debug("No router was found for {}", destIpAddress);
554 return null;
555 }
556
557 DeviceId routerDeviceId = matchIntf.connectPoint().deviceId();
558 SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId);
559 if (srInfo == null) {
560 log.debug("No device config was found for {}", routerDeviceId);
561 return null;
562 }
563
564 return srInfo.ipv6Loopback;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700565 }
566
567 /**
568 * Returns the router mac address of segment router that has the
569 * specified ip address as one of its subnet gateway ip address.
570 *
571 * @param gatewayIpAddress router gateway ip address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700572 * @return router mac address or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700573 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800574 public MacAddress getRouterMacForAGatewayIp(IpAddress gatewayIpAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700575 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
576 deviceConfigMap.entrySet()) {
577 if (entry.getValue().gatewayIps.
578 values().contains(gatewayIpAddress)) {
579 return entry.getValue().mac;
580 }
581 }
582
583 log.debug("Cannot find a router for {}", gatewayIpAddress);
584 return null;
585 }
sangho666cd6d2015-04-14 16:27:13 -0700586
sangho666cd6d2015-04-14 16:27:13 -0700587 /**
Charles Chana9519af2017-06-26 18:30:18 -0700588 * Checks if the host IP is in any of the subnet defined in the router with the
sangho666cd6d2015-04-14 16:27:13 -0700589 * device ID given.
590 *
591 * @param deviceId device identification of the router
592 * @param hostIp host IP address to check
Charles Chana9519af2017-06-26 18:30:18 -0700593 * @return true if the given IP is within any of the subnet defined in the router,
594 * false if no subnet is defined in the router or if the host is not
595 * within any subnet defined in the router
sangho666cd6d2015-04-14 16:27:13 -0700596 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800597 public boolean inSameSubnet(DeviceId deviceId, IpAddress hostIp) {
sangho666cd6d2015-04-14 16:27:13 -0700598
Pier Ventre10bd8d12016-11-26 21:05:22 -0800599 Set<IpPrefix> subnets = getSubnets(deviceId);
sangho666cd6d2015-04-14 16:27:13 -0700600 if (subnets == null) {
601 return false;
602 }
603
Pier Ventre10bd8d12016-11-26 21:05:22 -0800604 for (IpPrefix subnet: subnets) {
Charles Chan5270ed02016-01-30 23:22:37 -0800605 // Exclude /0 since it is a special case used for default route
606 if (subnet.prefixLength() != 0 && subnet.contains(hostIp)) {
sangho666cd6d2015-04-14 16:27:13 -0700607 return true;
608 }
609 }
610
611 return false;
612 }
sangho1e575652015-05-14 00:39:53 -0700613
614 /**
Charles Chan03a73e02016-10-24 14:52:01 -0700615 * Checks if the IP is in the subnet defined on given connect point.
616 *
617 * @param connectPoint Connect point
618 * @param ip The IP address to check
619 * @return True if the IP belongs to the subnet.
620 * False if the IP does not belong to the subnet, or
621 * there is no subnet configuration on given connect point.
622 */
623 public boolean inSameSubnet(ConnectPoint connectPoint, IpAddress ip) {
Charles Chana9519af2017-06-26 18:30:18 -0700624 return getPortSubnets(connectPoint.deviceId(), connectPoint.port()).stream()
625 .anyMatch(ipPrefix -> ipPrefix.contains(ip));
Charles Chan03a73e02016-10-24 14:52:01 -0700626 }
627
628 /**
sangho1e575652015-05-14 00:39:53 -0700629 * Returns the ports corresponding to the adjacency Sid given.
630 *
631 * @param deviceId device identification of the router
632 * @param sid adjacency Sid
Charles Chan531a78b2015-12-01 10:00:51 -0800633 * @return set of port numbers
sangho1e575652015-05-14 00:39:53 -0700634 */
Charles Chan531a78b2015-12-01 10:00:51 -0800635 public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700636 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan531a78b2015-12-01 10:00:51 -0800637 return srinfo != null ?
638 ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) :
639 ImmutableSet.copyOf(new HashSet<>());
sangho1e575652015-05-14 00:39:53 -0700640 }
641
642 /**
643 * Check if the Sid given is whether adjacency Sid of the router device or not.
644 *
645 * @param deviceId device identification of the router
646 * @param sid Sid to check
647 * @return true if the Sid given is the adjacency Sid of the device,
648 * otherwise false
649 */
650 public boolean isAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700651 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan531a78b2015-12-01 10:00:51 -0800652 return srinfo != null && srinfo.adjacencySids.containsKey(sid);
sangho1e575652015-05-14 00:39:53 -0700653 }
Charles Chanf2565a92016-02-10 20:46:58 -0800654
Charles Chand55e84d2016-03-30 17:54:24 -0700655 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700656 * Add subnet to specific connect point.
657 *
658 * @param cp connect point
Pier Ventre10bd8d12016-11-26 21:05:22 -0800659 * @param ipPrefix subnet being added to the device
Charles Chan93e71ba2016-04-29 14:38:22 -0700660 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800661 public void addSubnet(ConnectPoint cp, IpPrefix ipPrefix) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700662 checkNotNull(cp);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800663 checkNotNull(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700664 SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId());
665 if (srinfo == null) {
666 log.warn("Device {} is not configured. Abort.", cp.deviceId());
667 return;
668 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800669 srinfo.subnets.put(cp.port(), ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700670 }
671
672 /**
673 * Remove subnet from specific connect point.
674 *
675 * @param cp connect point
Pier Ventre10bd8d12016-11-26 21:05:22 -0800676 * @param ipPrefix subnet being removed to the device
Charles Chan93e71ba2016-04-29 14:38:22 -0700677 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800678 public void removeSubnet(ConnectPoint cp, IpPrefix ipPrefix) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700679 checkNotNull(cp);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800680 checkNotNull(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700681 SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId());
682 if (srinfo == null) {
683 log.warn("Device {} is not configured. Abort.", cp.deviceId());
684 return;
685 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800686 srinfo.subnets.remove(cp.port(), ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700687 }
Charles Chan2c15aca2016-11-09 20:51:44 -0800688
689 private boolean isSuppressedPort(ConnectPoint connectPoint) {
690 SegmentRoutingAppConfig appConfig = srManager.cfgService
Ray Milkeye4afdb52017-04-05 09:42:04 -0700691 .getConfig(srManager.appId(), SegmentRoutingAppConfig.class);
Charles Chan2c15aca2016-11-09 20:51:44 -0800692 if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700693 log.info("Interface configuration on port {} is ignored", connectPoint);
Charles Chan2c15aca2016-11-09 20:51:44 -0800694 return true;
695 }
696 return false;
697 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700698
699 public boolean isPairedEdge(DeviceId deviceId) throws DeviceConfigNotFoundException {
700 if (!isEdgeDevice(deviceId)) {
701 return false;
702 }
703 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
704 return (srinfo.pairDeviceId == null) ? false : true;
705 }
706
707 public DeviceId getPairDeviceId(DeviceId deviceId) throws DeviceConfigNotFoundException {
708 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
709 if (srinfo != null) {
710 return srinfo.pairDeviceId;
711 } else {
712 String message = "getPairDeviceId fails for device: " + deviceId + ".";
713 throw new DeviceConfigNotFoundException(message);
714 }
715 }
716
717 public PortNumber getPairLocalPort(DeviceId deviceId)
718 throws DeviceConfigNotFoundException {
719 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
720 if (srinfo != null) {
721 return srinfo.pairLocalPort;
722 } else {
723 String message = "getPairLocalPort fails for device: " + deviceId + ".";
724 throw new DeviceConfigNotFoundException(message);
725 }
726 }
727
Saurav Das9a554292018-04-27 18:42:30 -0700728 public boolean isPairLocalPort(DeviceId devId, PortNumber pnum) {
729 return pnum.equals(srManager.getPairLocalPort(devId).orElse(null));
730 }
Jonathan Hart00cddda2016-02-16 10:30:37 -0800731}