blob: 55562e0e792430000e0e439768777a12da084a02 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070022import org.onlab.packet.Ip4Prefix;
Pier Ventree0ae7a32016-11-23 09:57:42 -080023import org.onlab.packet.Ip6Address;
Pier Ventre10bd8d12016-11-26 21:05:22 -080024import org.onlab.packet.Ip6Prefix;
Charles Chan03a73e02016-10-24 14:52:01 -070025import org.onlab.packet.IpAddress;
Charles Chan5270ed02016-01-30 23:22:37 -080026import org.onlab.packet.IpPrefix;
sanghob35a6192015-04-01 13:05:26 -070027import org.onlab.packet.MacAddress;
Charles Chane849c192016-01-11 18:28:54 -080028import org.onlab.packet.VlanId;
Charles Chan4636be02015-10-07 14:21:45 -070029import org.onosproject.incubator.net.config.basics.ConfigException;
30import org.onosproject.incubator.net.config.basics.InterfaceConfig;
31import org.onosproject.incubator.net.intf.Interface;
32import org.onosproject.net.ConnectPoint;
Charles Chan4636be02015-10-07 14:21:45 -070033import org.onosproject.net.host.InterfaceIpAddress;
sanghob35a6192015-04-01 13:05:26 -070034import org.onosproject.net.DeviceId;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070035import org.onosproject.net.PortNumber;
Charles Chan2c15aca2016-11-09 20:51:44 -080036import org.onosproject.segmentrouting.SegmentRoutingManager;
sanghob35a6192015-04-01 13:05:26 -070037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070040import java.util.ArrayList;
Pier Ventre10bd8d12016-11-26 21:05:22 -080041import java.util.Collections;
sanghob35a6192015-04-01 13:05:26 -070042import java.util.HashMap;
Charles Chan531a78b2015-12-01 10:00:51 -080043import java.util.HashSet;
sanghob35a6192015-04-01 13:05:26 -070044import java.util.List;
45import java.util.Map;
Charles Chand6832882015-10-05 17:50:33 -070046import java.util.Set;
Saurav Das0e99e2b2015-10-28 12:39:42 -070047import java.util.concurrent.ConcurrentHashMap;
Charles Chan2c15aca2016-11-09 20:51:44 -080048import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -070049
Charles Chan93e71ba2016-04-29 14:38:22 -070050import static com.google.common.base.Preconditions.checkNotNull;
51
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070052/**
53 * Segment Routing configuration component that reads the
54 * segment routing related configuration from Network Configuration Manager
55 * component and organizes in more accessible formats.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070056 */
sanghob35a6192015-04-01 13:05:26 -070057public class DeviceConfiguration implements DeviceProperties {
58
Charles Chan2c15aca2016-11-09 20:51:44 -080059 private static final String ERROR_CONFIG = "Configuration error.";
60 private static final String TOO_MANY_SUBNET = ERROR_CONFIG + " Too many subnets configured on {}";
61 private static final String NO_SUBNET = "No subnet configured on {}";
Pier Ventre10bd8d12016-11-26 21:05:22 -080062 private static final String MISCONFIGURED = "Subnets are not configured correctly for {}";
Charles Chan2c15aca2016-11-09 20:51:44 -080063
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;
Charles Chanb8e10c82015-10-14 11:24:40 -070080
81 public SegmentRouterInfo() {
Pier Ventre10bd8d12016-11-26 21:05:22 -080082 gatewayIps = HashMultimap.create();
Charles Chan5270ed02016-01-30 23:22:37 -080083 subnets = HashMultimap.create();
Charles Chanb8e10c82015-10-14 11:24:40 -070084 }
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070085 }
sanghob35a6192015-04-01 13:05:26 -070086
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070087 /**
Charles Chane849c192016-01-11 18:28:54 -080088 * Constructs device configuration for all Segment Router devices,
89 * organizing the data into various maps for easier access.
Brian O'Connor52515622015-10-09 17:03:44 -070090 *
Charles Chan2c15aca2016-11-09 20:51:44 -080091 * @param srManager Segment Routing Manager
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070092 */
Charles Chan2c15aca2016-11-09 20:51:44 -080093 public DeviceConfiguration(SegmentRoutingManager srManager) {
94 this.srManager = srManager;
Charles Chand9681e72016-02-22 19:27:29 -080095
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();
Charles Chane849c192016-01-11 18:28:54 -0800111 deviceConfigMap.put(info.deviceId, info);
Saurav Das59232cf2016-04-27 18:35:50 -0700112 log.info("Read device config for device: {}", info.deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800113 /*
114 * IPv6 sid is not inserted. this part of the code is not used for now.
115 */
116 allSegmentIds.add(info.ipv4NodeSid);
Charles Chand6832882015-10-05 17:50:33 -0700117 });
Charles Chan4636be02015-10-07 14:21:45 -0700118
Charles Chan2c15aca2016-11-09 20:51:44 -0800119 // Read gatewayIps and subnets from port subject. Ignore suppressed ports.
120 Set<ConnectPoint> portSubjects = srManager.cfgService
121 .getSubjects(ConnectPoint.class, InterfaceConfig.class);
122 portSubjects.stream().filter(subject -> !isSuppressedPort(subject)).forEach(subject -> {
Charles Chan4636be02015-10-07 14:21:45 -0700123 InterfaceConfig config =
Charles Chan2c15aca2016-11-09 20:51:44 -0800124 srManager.cfgService.getConfig(subject, InterfaceConfig.class);
Charles Chan4636be02015-10-07 14:21:45 -0700125 Set<Interface> networkInterfaces;
126 try {
127 networkInterfaces = config.getInterfaces();
128 } catch (ConfigException e) {
129 log.error("Error loading port configuration");
130 return;
131 }
132 networkInterfaces.forEach(networkInterface -> {
Charles Chane849c192016-01-11 18:28:54 -0800133 VlanId vlanId = networkInterface.vlan();
134 ConnectPoint connectPoint = networkInterface.connectPoint();
135 DeviceId dpid = connectPoint.deviceId();
136 PortNumber port = connectPoint.port();
137 SegmentRouterInfo info = deviceConfigMap.get(dpid);
Charles Chan4636be02015-10-07 14:21:45 -0700138
Charles Chanb8e10c82015-10-14 11:24:40 -0700139 // skip if there is no corresponding device for this ConenctPoint
140 if (info != null) {
Charles Chane849c192016-01-11 18:28:54 -0800141 // Extract subnet information
Jonathan Hart00cddda2016-02-16 10:30:37 -0800142 List<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddressesList();
Charles Chanb8e10c82015-10-14 11:24:40 -0700143 interfaceAddresses.forEach(interfaceAddress -> {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800144 // Do not add /0, /32 and /128 to gateway IP list
Charles Chan5270ed02016-01-30 23:22:37 -0800145 int prefixLength = interfaceAddress.subnetAddress().prefixLength();
Pier Ventre10bd8d12016-11-26 21:05:22 -0800146 IpPrefix ipPrefix = interfaceAddress.subnetAddress();
147 if (ipPrefix.isIp4()) {
148 if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET_MASK_LENGTH) {
149 info.gatewayIps.put(port, interfaceAddress.ipAddress());
150 }
151 info.subnets.put(port, interfaceAddress.subnetAddress());
152 } else {
153 if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET6_MASK_LENGTH) {
154 info.gatewayIps.put(port, interfaceAddress.ipAddress());
155 }
156 info.subnets.put(port, interfaceAddress.subnetAddress());
Charles Chan5270ed02016-01-30 23:22:37 -0800157 }
Charles Chanb8e10c82015-10-14 11:24:40 -0700158 });
159 }
Charles Chan4636be02015-10-07 14:21:45 -0700160 });
Pier Ventre10bd8d12016-11-26 21:05:22 -0800161 /*
162 * We register the connect point with the NRS.
163 */
164 srManager.registerConnectPoint(subject);
Charles Chan4636be02015-10-07 14:21:45 -0700165 });
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700166 }
167
sanghob35a6192015-04-01 13:05:26 -0700168 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800169 public boolean isConfigured(DeviceId deviceId) {
170 return deviceConfigMap.get(deviceId) != null;
171 }
172
173 @Override
Pier Ventree0ae7a32016-11-23 09:57:42 -0800174 public int getIPv4SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700175 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
176 if (srinfo != null) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800177 log.trace("getIPv4SegmentId for device{} is {}", deviceId, srinfo.ipv4NodeSid);
178 return srinfo.ipv4NodeSid;
sanghob35a6192015-04-01 13:05:26 -0700179 } else {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800180 String message = "getIPv4SegmentId fails for device: " + deviceId + ".";
181 throw new DeviceConfigNotFoundException(message);
182 }
183 }
184
185 @Override
186 public int getIPv6SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException {
187 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
188 if (srinfo != null) {
189 log.trace("getIPv6SegmentId for device{} is {}", deviceId, srinfo.ipv6NodeSid);
190 return srinfo.ipv6NodeSid;
191 } else {
192 String message = "getIPv6SegmentId fails for device: " + deviceId + ".";
Charles Chan0b4e6182015-11-03 10:42:14 -0800193 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700194 }
195 }
196
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700197 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800198 * Returns the IPv4 Node segment id of a segment router given its Router mac address.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700199 *
200 * @param routerMac router mac address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700201 * @return node segment id, or -1 if not found in config
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700202 */
Pier Ventree0ae7a32016-11-23 09:57:42 -0800203 public int getIPv4SegmentId(MacAddress routerMac) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700204 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
205 deviceConfigMap.entrySet()) {
206 if (entry.getValue().mac.equals(routerMac)) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800207 return entry.getValue().ipv4NodeSid;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700208 }
209 }
sanghob35a6192015-04-01 13:05:26 -0700210
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700211 return -1;
212 }
213
214 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800215 * Returns the IPv6 Node segment id of a segment router given its Router mac address.
216 *
217 * @param routerMac router mac address
218 * @return node segment id, or -1 if not found in config
219 */
220 public int getIPv6SegmentId(MacAddress routerMac) {
221 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
222 deviceConfigMap.entrySet()) {
223 if (entry.getValue().mac.equals(routerMac)) {
224 return entry.getValue().ipv6NodeSid;
225 }
226 }
227
228 return -1;
229 }
230
231 /**
232 * Returns the IPv4 Node segment id of a segment router given its Router ip address.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700233 *
234 * @param routerAddress router ip address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700235 * @return node segment id, or -1 if not found in config
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700236 */
Pier Ventree0ae7a32016-11-23 09:57:42 -0800237 public int getIPv4SegmentId(Ip4Address routerAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700238 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
239 deviceConfigMap.entrySet()) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800240 if (entry.getValue().ipv4Loopback.equals(routerAddress)) {
241 return entry.getValue().ipv4NodeSid;
242 }
243 }
244
245 return -1;
246 }
247
248 /**
249 * Returns the IPv6 Node segment id of a segment router given its Router ip address.
250 *
251 * @param routerAddress router ip address
252 * @return node segment id, or -1 if not found in config
253 */
254 public int getIPv6SegmentId(Ip6Address routerAddress) {
255 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
256 deviceConfigMap.entrySet()) {
257 if (entry.getValue().ipv6Loopback.equals(routerAddress)) {
258 return entry.getValue().ipv6NodeSid;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700259 }
260 }
261
262 return -1;
263 }
264
sanghob35a6192015-04-01 13:05:26 -0700265 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800266 public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700267 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
268 if (srinfo != null) {
269 log.trace("getDeviceMac for device{} is {}", deviceId, srinfo.mac);
270 return srinfo.mac;
sanghob35a6192015-04-01 13:05:26 -0700271 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800272 String message = "getDeviceMac fails for device: " + deviceId + ".";
273 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700274 }
275 }
276
Charles Chan0b4e6182015-11-03 10:42:14 -0800277 @Override
Pier Ventree0ae7a32016-11-23 09:57:42 -0800278 public Ip4Address getRouterIpv4(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700279 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
280 if (srinfo != null) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800281 log.trace("getRouterIpv4 for device{} is {}", deviceId, srinfo.ipv4Loopback);
282 return srinfo.ipv4Loopback;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700283 } else {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800284 String message = "getRouterIpv4 fails for device: " + deviceId + ".";
285 throw new DeviceConfigNotFoundException(message);
286 }
287 }
288
289 @Override
290 public Ip6Address getRouterIpv6(DeviceId deviceId) throws DeviceConfigNotFoundException {
291 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
292 if (srinfo != null) {
293 log.trace("getRouterIpv6 for device{} is {}", deviceId, srinfo.ipv6Loopback);
294 return srinfo.ipv6Loopback;
295 } else {
296 String message = "getRouterIpv6 fails for device: " + deviceId + ".";
Charles Chan0b4e6182015-11-03 10:42:14 -0800297 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700298 }
sanghob35a6192015-04-01 13:05:26 -0700299 }
300
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700301 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800302 public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700303 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
304 if (srinfo != null) {
305 log.trace("isEdgeDevice for device{} is {}", deviceId, srinfo.isEdge);
306 return srinfo.isEdge;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700307 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800308 String message = "isEdgeDevice fails for device: " + deviceId + ".";
309 throw new DeviceConfigNotFoundException(message);
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700310 }
311 }
312
sanghob35a6192015-04-01 13:05:26 -0700313 @Override
314 public List<Integer> getAllDeviceSegmentIds() {
315 return allSegmentIds;
316 }
317
Charles Chanc42e84e2015-10-20 16:24:19 -0700318 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800319 public Map<IpPrefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId)
Saurav Das7a1ffca2016-03-28 19:00:18 -0700320 throws DeviceConfigNotFoundException {
321 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
322 if (srinfo == null) {
323 String message = "getSubnetPortsMap fails for device: " + deviceId + ".";
324 throw new DeviceConfigNotFoundException(message);
325 }
Charles Chanc42e84e2015-10-20 16:24:19 -0700326 // Construct subnet-port mapping from port-subnet mapping
Pier Ventre10bd8d12016-11-26 21:05:22 -0800327 SetMultimap<PortNumber, IpPrefix> portSubnetMap = srinfo.subnets;
328 Map<IpPrefix, List<PortNumber>> subnetPortMap = new HashMap<>();
Charles Chan5270ed02016-01-30 23:22:37 -0800329
330 portSubnetMap.entries().forEach(entry -> {
331 PortNumber port = entry.getKey();
Pier Ventre10bd8d12016-11-26 21:05:22 -0800332 IpPrefix subnet = entry.getValue();
Charles Chan5270ed02016-01-30 23:22:37 -0800333
Pier Ventre10bd8d12016-11-26 21:05:22 -0800334 if (subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH ||
335 subnet.prefixLength() == IpPrefix.MAX_INET6_MASK_LENGTH) {
Charles Chand0fd5dc2016-02-16 23:14:49 -0800336 return;
337 }
338
Charles Chanc42e84e2015-10-20 16:24:19 -0700339 if (subnetPortMap.containsKey(subnet)) {
340 subnetPortMap.get(subnet).add(port);
341 } else {
342 ArrayList<PortNumber> ports = new ArrayList<>();
343 ports.add(port);
344 subnetPortMap.put(subnet, ports);
345 }
346 });
Charles Chanc42e84e2015-10-20 16:24:19 -0700347 return subnetPortMap;
348 }
349
sanghob35a6192015-04-01 13:05:26 -0700350 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700351 * Returns the device identifier or data plane identifier (dpid)
352 * of a segment router given its segment id.
sanghob35a6192015-04-01 13:05:26 -0700353 *
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700354 * @param sid segment id
355 * @return deviceId device identifier
sanghob35a6192015-04-01 13:05:26 -0700356 */
357 public DeviceId getDeviceId(int sid) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700358 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
359 deviceConfigMap.entrySet()) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800360 if (entry.getValue().ipv4NodeSid == sid ||
361 entry.getValue().ipv6NodeSid == sid) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700362 return entry.getValue().deviceId;
sanghob35a6192015-04-01 13:05:26 -0700363 }
364 }
365
366 return null;
367 }
368
369 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700370 * Returns the device identifier or data plane identifier (dpid)
371 * of a segment router given its router ip address.
sanghob35a6192015-04-01 13:05:26 -0700372 *
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700373 * @param ipAddress router ip address
374 * @return deviceId device identifier
sanghob35a6192015-04-01 13:05:26 -0700375 */
376 public DeviceId getDeviceId(Ip4Address ipAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700377 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
378 deviceConfigMap.entrySet()) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800379 if (entry.getValue().ipv4Loopback.equals(ipAddress)) {
380 return entry.getValue().deviceId;
381 }
382 }
383
384 return null;
385 }
386
387 /**
388 * Returns the device identifier or data plane identifier (dpid)
389 * of a segment router given its router ipv6 address.
390 *
391 * @param ipAddress router ipv6 address
392 * @return deviceId device identifier
393 */
394 public DeviceId getDeviceId(Ip6Address ipAddress) {
395 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
396 deviceConfigMap.entrySet()) {
397 if (entry.getValue().ipv6Loopback.equals(ipAddress)) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700398 return entry.getValue().deviceId;
sanghob35a6192015-04-01 13:05:26 -0700399 }
400 }
401
402 return null;
403 }
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700404
405 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700406 * Returns the configured port ip addresses for a segment router.
407 * These addresses serve as gateway IP addresses for the subnets configured
408 * on those ports.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700409 *
410 * @param deviceId device identifier
Saurav Das837e0bb2015-10-30 17:45:38 -0700411 * @return immutable set of ip addresses configured on the ports or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700412 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800413 public Set<IpAddress> getPortIPs(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700414 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
415 if (srinfo != null) {
416 log.trace("getSubnetGatewayIps for device{} is {}", deviceId,
417 srinfo.gatewayIps.values());
Saurav Das837e0bb2015-10-30 17:45:38 -0700418 return ImmutableSet.copyOf(srinfo.gatewayIps.values());
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700419 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700420 return null;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700421 }
422
423 /**
424 * Returns the configured subnet prefixes for a segment router.
425 *
426 * @param deviceId device identifier
Saurav Das0e99e2b2015-10-28 12:39:42 -0700427 * @return list of ip prefixes or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700428 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800429 public Set<IpPrefix> getSubnets(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700430 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
431 if (srinfo != null) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800432 ImmutableSet.Builder<IpPrefix> builder = ImmutableSet.builder();
Charles Chan03a73e02016-10-24 14:52:01 -0700433 return builder.addAll(srinfo.subnets.values()).build();
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700434 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700435 return null;
436 }
437
Charles Chan2c15aca2016-11-09 20:51:44 -0800438
Saurav Das0e99e2b2015-10-28 12:39:42 -0700439 /**
Charles Chan2c15aca2016-11-09 20:51:44 -0800440 * Returns the subnet configuration of given device and port.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700441 *
Charles Chan2c15aca2016-11-09 20:51:44 -0800442 * @param deviceId Device ID
443 * @param port Port number
Pier Ventre10bd8d12016-11-26 21:05:22 -0800444 * @return The subnets configured on given port or empty set if
Charles Chan2c15aca2016-11-09 20:51:44 -0800445 * the port is unconfigured, misconfigured or suppressed.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700446 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800447 public Set<IpPrefix> getPortSubnets(DeviceId deviceId, PortNumber port) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800448 ConnectPoint connectPoint = new ConnectPoint(deviceId, port);
449
450 if (isSuppressedPort(connectPoint)) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800451 return Collections.emptySet();
Saurav Das0e99e2b2015-10-28 12:39:42 -0700452 }
Charles Chan2c15aca2016-11-09 20:51:44 -0800453
Pier Ventre10bd8d12016-11-26 21:05:22 -0800454 Set<IpPrefix> subnets =
Charles Chan2c15aca2016-11-09 20:51:44 -0800455 srManager.interfaceService.getInterfacesByPort(connectPoint).stream()
456 .flatMap(intf -> intf.ipAddressesList().stream())
457 .map(InterfaceIpAddress::subnetAddress)
Charles Chan2c15aca2016-11-09 20:51:44 -0800458 .collect(Collectors.toSet());
459
Jon Hallcbd1b392017-01-18 20:15:44 -0800460 if (subnets.isEmpty()) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800461 log.info(NO_SUBNET, connectPoint);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800462 return Collections.emptySet();
463 } else if (subnets.size() > 2) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800464 log.warn(TOO_MANY_SUBNET, connectPoint);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800465 return Collections.emptySet();
466 } else if (verifySubnets(subnets)) {
467 return subnets;
Charles Chan2c15aca2016-11-09 20:51:44 -0800468 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800469 log.warn(MISCONFIGURED, connectPoint);
470 return Collections.emptySet();
471 }
472
473 /**
474 * Returns the IPv4 subnet configured of given device and port.
475 *
476 * @param deviceId Device ID
477 * @param port Port number
478 * @return The IPv4 subnet configured on given port or null if
479 * the port is unconfigured, misconfigured or suppressed.
480 */
481 public Ip4Prefix getPortIPv4Subnet(DeviceId deviceId, PortNumber port) {
482 return getPortSubnets(deviceId, port).stream()
483 .filter(IpPrefix::isIp4)
484 .map(IpPrefix::getIp4Prefix)
485 .findFirst().orElse(null);
486 }
487
488 /**
489 * Returns the IPv6 subnet configured of given device and port.
490 *
491 * @param deviceId Device ID
492 * @param port Port number
493 * @return The IPV6 subnet configured on given port or null if
494 * the port is unconfigured, misconfigured or suppressed.
495 */
496 public Ip6Prefix getPortIPv6Subnet(DeviceId deviceId, PortNumber port) {
497 return getPortSubnets(deviceId, port).stream()
498 .filter(IpPrefix::isIp6)
499 .map(IpPrefix::getIp6Prefix)
500 .findFirst().orElse(null);
501 }
502
503 /**
504 * Utility to verify the configuration of a given port.
505 *
506 * @param subnets the subnets set to verify
507 * @return true if the configured subnets are ok. False otherwise.
508 */
509 private boolean verifySubnets(Set<IpPrefix> subnets) {
510 Set<Ip4Prefix> ip4Prefices = subnets.stream()
511 .filter(IpPrefix::isIp4)
512 .map(IpPrefix::getIp4Prefix)
513 .collect(Collectors.toSet());
514 if (ip4Prefices.size() > 1) {
515 return false;
516 }
517 Set<Ip6Prefix> ip6Prefices = subnets.stream()
518 .filter(IpPrefix::isIp6)
519 .map(IpPrefix::getIp6Prefix)
520 .collect(Collectors.toSet());
521 if (ip6Prefices.size() > 1) {
522 return false;
523 }
524 return !(ip4Prefices.isEmpty() && ip6Prefices.isEmpty());
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700525 }
526
527 /**
528 * Returns the router ip address of segment router that has the
529 * specified ip address in its subnets.
530 *
531 * @param destIpAddress target ip address
532 * @return router ip address
533 */
534 public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) {
Charles Chan68aaad52016-12-09 12:54:49 -0800535 Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress);
536
537 if (matchIntf == null) {
538 log.debug("No router was found for {}", destIpAddress);
539 return null;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700540 }
541
Charles Chan68aaad52016-12-09 12:54:49 -0800542 DeviceId routerDeviceId = matchIntf.connectPoint().deviceId();
543 SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId);
544 if (srInfo == null) {
545 log.debug("No device config was found for {}", routerDeviceId);
546 return null;
547 }
548
Pier Ventree0ae7a32016-11-23 09:57:42 -0800549 return srInfo.ipv4Loopback;
550 }
551
552 /**
553 * Returns the router ipv6 address of segment router that has the
554 * specified ip address in its subnets.
555 *
556 * @param destIpAddress target ip address
557 * @return router ip address
558 */
559 public Ip6Address getRouterIpAddressForASubnetHost(Ip6Address destIpAddress) {
560 Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress);
561
562 if (matchIntf == null) {
563 log.debug("No router was found for {}", destIpAddress);
564 return null;
565 }
566
567 DeviceId routerDeviceId = matchIntf.connectPoint().deviceId();
568 SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId);
569 if (srInfo == null) {
570 log.debug("No device config was found for {}", routerDeviceId);
571 return null;
572 }
573
574 return srInfo.ipv6Loopback;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700575 }
576
577 /**
578 * Returns the router mac address of segment router that has the
579 * specified ip address as one of its subnet gateway ip address.
580 *
581 * @param gatewayIpAddress router gateway ip address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700582 * @return router mac address or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700583 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800584 public MacAddress getRouterMacForAGatewayIp(IpAddress gatewayIpAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700585 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
586 deviceConfigMap.entrySet()) {
587 if (entry.getValue().gatewayIps.
588 values().contains(gatewayIpAddress)) {
589 return entry.getValue().mac;
590 }
591 }
592
593 log.debug("Cannot find a router for {}", gatewayIpAddress);
594 return null;
595 }
sangho666cd6d2015-04-14 16:27:13 -0700596
sangho666cd6d2015-04-14 16:27:13 -0700597 /**
598 * Checks if the host is in the subnet defined in the router with the
599 * device ID given.
600 *
601 * @param deviceId device identification of the router
602 * @param hostIp host IP address to check
603 * @return true if the host is within the subnet of the router,
604 * false if no subnet is defined under the router or if the host is not
605 * within the subnet defined in the router
606 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800607 public boolean inSameSubnet(DeviceId deviceId, IpAddress hostIp) {
sangho666cd6d2015-04-14 16:27:13 -0700608
Pier Ventre10bd8d12016-11-26 21:05:22 -0800609 Set<IpPrefix> subnets = getSubnets(deviceId);
sangho666cd6d2015-04-14 16:27:13 -0700610 if (subnets == null) {
611 return false;
612 }
613
Pier Ventre10bd8d12016-11-26 21:05:22 -0800614 for (IpPrefix subnet: subnets) {
Charles Chan5270ed02016-01-30 23:22:37 -0800615 // Exclude /0 since it is a special case used for default route
616 if (subnet.prefixLength() != 0 && subnet.contains(hostIp)) {
sangho666cd6d2015-04-14 16:27:13 -0700617 return true;
618 }
619 }
620
621 return false;
622 }
sangho1e575652015-05-14 00:39:53 -0700623
624 /**
Charles Chan03a73e02016-10-24 14:52:01 -0700625 * Checks if the IP is in the subnet defined on given connect point.
626 *
627 * @param connectPoint Connect point
628 * @param ip The IP address to check
629 * @return True if the IP belongs to the subnet.
630 * False if the IP does not belong to the subnet, or
631 * there is no subnet configuration on given connect point.
632 */
633 public boolean inSameSubnet(ConnectPoint connectPoint, IpAddress ip) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800634 Ip4Prefix ipv4Subnet = getPortIPv4Subnet(connectPoint.deviceId(), connectPoint.port());
635 Ip6Prefix ipv6Subnet = getPortIPv6Subnet(connectPoint.deviceId(), connectPoint.port());
636 return (ipv4Subnet != null && ipv4Subnet.contains(ip)) ||
637 (ipv6Subnet != null && ipv6Subnet.contains(ip));
Charles Chan03a73e02016-10-24 14:52:01 -0700638 }
639
640 /**
sangho1e575652015-05-14 00:39:53 -0700641 * Returns the ports corresponding to the adjacency Sid given.
642 *
643 * @param deviceId device identification of the router
644 * @param sid adjacency Sid
Charles Chan531a78b2015-12-01 10:00:51 -0800645 * @return set of port numbers
sangho1e575652015-05-14 00:39:53 -0700646 */
Charles Chan531a78b2015-12-01 10:00:51 -0800647 public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700648 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan531a78b2015-12-01 10:00:51 -0800649 return srinfo != null ?
650 ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) :
651 ImmutableSet.copyOf(new HashSet<>());
sangho1e575652015-05-14 00:39:53 -0700652 }
653
654 /**
655 * Check if the Sid given is whether adjacency Sid of the router device or not.
656 *
657 * @param deviceId device identification of the router
658 * @param sid Sid to check
659 * @return true if the Sid given is the adjacency Sid of the device,
660 * otherwise false
661 */
662 public boolean isAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700663 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan531a78b2015-12-01 10:00:51 -0800664 return srinfo != null && srinfo.adjacencySids.containsKey(sid);
sangho1e575652015-05-14 00:39:53 -0700665 }
Charles Chanf2565a92016-02-10 20:46:58 -0800666
Charles Chand55e84d2016-03-30 17:54:24 -0700667 /**
Charles Chan93e71ba2016-04-29 14:38:22 -0700668 * Add subnet to specific connect point.
669 *
670 * @param cp connect point
Pier Ventre10bd8d12016-11-26 21:05:22 -0800671 * @param ipPrefix subnet being added to the device
Charles Chan93e71ba2016-04-29 14:38:22 -0700672 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800673 public void addSubnet(ConnectPoint cp, IpPrefix ipPrefix) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700674 checkNotNull(cp);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800675 checkNotNull(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700676 SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId());
677 if (srinfo == null) {
678 log.warn("Device {} is not configured. Abort.", cp.deviceId());
679 return;
680 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800681 srinfo.subnets.put(cp.port(), ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700682 }
683
684 /**
685 * Remove subnet from specific connect point.
686 *
687 * @param cp connect point
Pier Ventre10bd8d12016-11-26 21:05:22 -0800688 * @param ipPrefix subnet being removed to the device
Charles Chan93e71ba2016-04-29 14:38:22 -0700689 */
Pier Ventre10bd8d12016-11-26 21:05:22 -0800690 public void removeSubnet(ConnectPoint cp, IpPrefix ipPrefix) {
Charles Chan93e71ba2016-04-29 14:38:22 -0700691 checkNotNull(cp);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800692 checkNotNull(ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700693 SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId());
694 if (srinfo == null) {
695 log.warn("Device {} is not configured. Abort.", cp.deviceId());
696 return;
697 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800698 srinfo.subnets.remove(cp.port(), ipPrefix);
Charles Chan93e71ba2016-04-29 14:38:22 -0700699 }
Charles Chan2c15aca2016-11-09 20:51:44 -0800700
701 private boolean isSuppressedPort(ConnectPoint connectPoint) {
702 SegmentRoutingAppConfig appConfig = srManager.cfgService
703 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
704 if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700705 log.info("Interface configuration on port {} is ignored", connectPoint);
Charles Chan2c15aca2016-11-09 20:51:44 -0800706 return true;
707 }
708 return false;
709 }
Jonathan Hart00cddda2016-02-16 10:30:37 -0800710}