blob: 8fe00baf9fcdc0fe1e68db5f0b875307f6c4d975 [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;
Charles Chan5270ed02016-01-30 23:22:37 -080023import org.onlab.packet.IpPrefix;
sanghob35a6192015-04-01 13:05:26 -070024import org.onlab.packet.MacAddress;
Charles Chane849c192016-01-11 18:28:54 -080025import org.onlab.packet.VlanId;
Charles Chanf2565a92016-02-10 20:46:58 -080026import org.onosproject.core.ApplicationId;
Charles Chan4636be02015-10-07 14:21:45 -070027import org.onosproject.incubator.net.config.basics.ConfigException;
28import org.onosproject.incubator.net.config.basics.InterfaceConfig;
29import org.onosproject.incubator.net.intf.Interface;
30import org.onosproject.net.ConnectPoint;
Charles Chand6832882015-10-05 17:50:33 -070031import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chand9681e72016-02-22 19:27:29 -080032import org.onosproject.net.config.NetworkConfigService;
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;
sanghob35a6192015-04-01 13:05:26 -070036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070039import java.util.ArrayList;
sanghob35a6192015-04-01 13:05:26 -070040import java.util.HashMap;
Charles Chan531a78b2015-12-01 10:00:51 -080041import java.util.HashSet;
Charles Chane849c192016-01-11 18:28:54 -080042import java.util.LinkedList;
sanghob35a6192015-04-01 13:05:26 -070043import java.util.List;
44import java.util.Map;
Charles Chan5270ed02016-01-30 23:22:37 -080045import java.util.Optional;
Charles Chand6832882015-10-05 17:50:33 -070046import java.util.Set;
Saurav Das0e99e2b2015-10-28 12:39:42 -070047import java.util.concurrent.ConcurrentHashMap;
sanghob35a6192015-04-01 13:05:26 -070048
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070049/**
50 * Segment Routing configuration component that reads the
51 * segment routing related configuration from Network Configuration Manager
52 * component and organizes in more accessible formats.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070053 */
sanghob35a6192015-04-01 13:05:26 -070054public class DeviceConfiguration implements DeviceProperties {
55
Charles Chanf2565a92016-02-10 20:46:58 -080056 private static final Logger log = LoggerFactory.getLogger(DeviceConfiguration.class);
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -070057 private final List<Integer> allSegmentIds = new ArrayList<>();
Charles Chane849c192016-01-11 18:28:54 -080058 private final Map<DeviceId, SegmentRouterInfo> deviceConfigMap = new ConcurrentHashMap<>();
59 private final Map<VlanId, List<ConnectPoint>> xConnects = new ConcurrentHashMap<>();
Charles Chand9681e72016-02-22 19:27:29 -080060 private ApplicationId appId;
61 private NetworkConfigService cfgService;
sanghob35a6192015-04-01 13:05:26 -070062
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070063 private class SegmentRouterInfo {
64 int nodeSid;
65 DeviceId deviceId;
66 Ip4Address ip;
67 MacAddress mac;
68 boolean isEdge;
Charles Chan5270ed02016-01-30 23:22:37 -080069 Map<PortNumber, Ip4Address> gatewayIps;
70 SetMultimap<PortNumber, Ip4Prefix> subnets;
Charles Chan531a78b2015-12-01 10:00:51 -080071 Map<Integer, Set<Integer>> adjacencySids;
Charles Chanb8e10c82015-10-14 11:24:40 -070072
73 public SegmentRouterInfo() {
Charles Chane849c192016-01-11 18:28:54 -080074 gatewayIps = new HashMap<>();
Charles Chan5270ed02016-01-30 23:22:37 -080075 subnets = HashMultimap.create();
Charles Chanb8e10c82015-10-14 11:24:40 -070076 }
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070077 }
sanghob35a6192015-04-01 13:05:26 -070078
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070079 /**
Charles Chane849c192016-01-11 18:28:54 -080080 * Constructs device configuration for all Segment Router devices,
81 * organizing the data into various maps for easier access.
Brian O'Connor52515622015-10-09 17:03:44 -070082 *
Charles Chanf2565a92016-02-10 20:46:58 -080083 * @param appId application id
Brian O'Connor52515622015-10-09 17:03:44 -070084 * @param cfgService config service
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070085 */
Charles Chanf2565a92016-02-10 20:46:58 -080086 public DeviceConfiguration(ApplicationId appId,
87 NetworkConfigRegistry cfgService) {
Charles Chand9681e72016-02-22 19:27:29 -080088 this.appId = appId;
89 this.cfgService = cfgService;
90
Charles Chan4636be02015-10-07 14:21:45 -070091 // Read config from device subject, excluding gatewayIps and subnets.
92 Set<DeviceId> deviceSubjects =
Charles Chan5270ed02016-01-30 23:22:37 -080093 cfgService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class);
Charles Chan4636be02015-10-07 14:21:45 -070094 deviceSubjects.forEach(subject -> {
Charles Chan5270ed02016-01-30 23:22:37 -080095 SegmentRoutingDeviceConfig config =
96 cfgService.getConfig(subject, SegmentRoutingDeviceConfig.class);
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070097 SegmentRouterInfo info = new SegmentRouterInfo();
Charles Chand6832882015-10-05 17:50:33 -070098 info.deviceId = subject;
Charles Chan531a78b2015-12-01 10:00:51 -080099 info.nodeSid = config.nodeSid();
100 info.ip = config.routerIp();
101 info.mac = config.routerMac();
Charles Chand6832882015-10-05 17:50:33 -0700102 info.isEdge = config.isEdgeRouter();
Charles Chan531a78b2015-12-01 10:00:51 -0800103 info.adjacencySids = config.adjacencySids();
Charles Chane849c192016-01-11 18:28:54 -0800104 deviceConfigMap.put(info.deviceId, info);
Saurav Das59232cf2016-04-27 18:35:50 -0700105 log.info("Read device config for device: {}", info.deviceId);
Charles Chane849c192016-01-11 18:28:54 -0800106 allSegmentIds.add(info.nodeSid);
Charles Chand6832882015-10-05 17:50:33 -0700107 });
Charles Chan4636be02015-10-07 14:21:45 -0700108
109 // Read gatewayIps and subnets from port subject.
110 Set<ConnectPoint> portSubjects =
111 cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class);
112 portSubjects.forEach(subject -> {
Charles Chand9681e72016-02-22 19:27:29 -0800113 // Do not process excluded ports
114 if (suppressSubnet().contains(subject)) {
115 return;
116 }
117
Charles Chan4636be02015-10-07 14:21:45 -0700118 InterfaceConfig config =
119 cfgService.getConfig(subject, InterfaceConfig.class);
120 Set<Interface> networkInterfaces;
121 try {
122 networkInterfaces = config.getInterfaces();
123 } catch (ConfigException e) {
124 log.error("Error loading port configuration");
125 return;
126 }
127 networkInterfaces.forEach(networkInterface -> {
Charles Chane849c192016-01-11 18:28:54 -0800128 VlanId vlanId = networkInterface.vlan();
129 ConnectPoint connectPoint = networkInterface.connectPoint();
130 DeviceId dpid = connectPoint.deviceId();
131 PortNumber port = connectPoint.port();
132 SegmentRouterInfo info = deviceConfigMap.get(dpid);
Charles Chan4636be02015-10-07 14:21:45 -0700133
Charles Chanb8e10c82015-10-14 11:24:40 -0700134 // skip if there is no corresponding device for this ConenctPoint
135 if (info != null) {
Charles Chane849c192016-01-11 18:28:54 -0800136 // Extract subnet information
Jonathan Hart00cddda2016-02-16 10:30:37 -0800137 List<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddressesList();
Charles Chanb8e10c82015-10-14 11:24:40 -0700138 interfaceAddresses.forEach(interfaceAddress -> {
Charles Chan5270ed02016-01-30 23:22:37 -0800139 // Do not add /0 and /32 to gateway IP list
140 int prefixLength = interfaceAddress.subnetAddress().prefixLength();
141 if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET_MASK_LENGTH) {
142 info.gatewayIps.put(port, interfaceAddress.ipAddress().getIp4Address());
143 }
Charles Chanb8e10c82015-10-14 11:24:40 -0700144 info.subnets.put(port, interfaceAddress.subnetAddress().getIp4Prefix());
145 });
Charles Chane849c192016-01-11 18:28:54 -0800146
147 // Extract VLAN cross-connect information
148 // Do not setup cross-connect if VLAN is NONE
149 if (vlanId.equals(VlanId.NONE)) {
150 return;
151 }
152 List<ConnectPoint> connectPoints = xConnects.get(vlanId);
153 if (connectPoints != null) {
154 if (connectPoints.size() != 1) {
155 log.warn("Cross-connect should only have two endpoints. Aborting.");
156 return;
157 }
158 if (!connectPoints.get(0).deviceId().equals(connectPoint.deviceId())) {
159 log.warn("Cross-connect endpoints must be on the same switch. Aborting.");
160 return;
161 }
162 connectPoints.add(connectPoint);
163 } else {
164 connectPoints = new LinkedList<>();
165 connectPoints.add(connectPoint);
166 xConnects.put(vlanId, connectPoints);
167 }
Charles Chanb8e10c82015-10-14 11:24:40 -0700168 }
Charles Chan4636be02015-10-07 14:21:45 -0700169 });
170
171 });
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700172 }
173
sanghob35a6192015-04-01 13:05:26 -0700174 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800175 public boolean isConfigured(DeviceId deviceId) {
176 return deviceConfigMap.get(deviceId) != null;
177 }
178
179 @Override
180 public int getSegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700181 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
182 if (srinfo != null) {
183 log.trace("getSegmentId for device{} is {}", deviceId, srinfo.nodeSid);
184 return srinfo.nodeSid;
sanghob35a6192015-04-01 13:05:26 -0700185 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800186 String message = "getSegmentId fails for device: " + deviceId + ".";
187 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700188 }
189 }
190
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700191 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700192 * Returns the Node segment id of a segment router given its Router mac address.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700193 *
194 * @param routerMac router mac address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700195 * @return node segment id, or -1 if not found in config
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700196 */
197 public int getSegmentId(MacAddress routerMac) {
198 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
199 deviceConfigMap.entrySet()) {
200 if (entry.getValue().mac.equals(routerMac)) {
201 return entry.getValue().nodeSid;
202 }
203 }
sanghob35a6192015-04-01 13:05:26 -0700204
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700205 return -1;
206 }
207
208 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700209 * Returns the Node segment id of a segment router given its Router ip address.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700210 *
211 * @param routerAddress router ip address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700212 * @return node segment id, or -1 if not found in config
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700213 */
214 public int getSegmentId(Ip4Address routerAddress) {
215 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
216 deviceConfigMap.entrySet()) {
217 if (entry.getValue().ip.equals(routerAddress)) {
218 return entry.getValue().nodeSid;
219 }
220 }
221
222 return -1;
223 }
224
sanghob35a6192015-04-01 13:05:26 -0700225 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800226 public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700227 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
228 if (srinfo != null) {
229 log.trace("getDeviceMac for device{} is {}", deviceId, srinfo.mac);
230 return srinfo.mac;
sanghob35a6192015-04-01 13:05:26 -0700231 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800232 String message = "getDeviceMac fails for device: " + deviceId + ".";
233 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700234 }
235 }
236
Charles Chan0b4e6182015-11-03 10:42:14 -0800237 @Override
238 public Ip4Address getRouterIp(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700239 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
240 if (srinfo != null) {
241 log.trace("getDeviceIp for device{} is {}", deviceId, srinfo.ip);
242 return srinfo.ip;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700243 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800244 String message = "getRouterIp fails for device: " + deviceId + ".";
245 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700246 }
sanghob35a6192015-04-01 13:05:26 -0700247 }
248
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700249 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800250 public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700251 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
252 if (srinfo != null) {
253 log.trace("isEdgeDevice for device{} is {}", deviceId, srinfo.isEdge);
254 return srinfo.isEdge;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700255 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800256 String message = "isEdgeDevice fails for device: " + deviceId + ".";
257 throw new DeviceConfigNotFoundException(message);
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700258 }
259 }
260
sanghob35a6192015-04-01 13:05:26 -0700261 @Override
262 public List<Integer> getAllDeviceSegmentIds() {
263 return allSegmentIds;
264 }
265
Charles Chanc42e84e2015-10-20 16:24:19 -0700266 @Override
Saurav Das7a1ffca2016-03-28 19:00:18 -0700267 public Map<Ip4Prefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId)
268 throws DeviceConfigNotFoundException {
269 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
270 if (srinfo == null) {
271 String message = "getSubnetPortsMap fails for device: " + deviceId + ".";
272 throw new DeviceConfigNotFoundException(message);
273 }
Charles Chanc42e84e2015-10-20 16:24:19 -0700274 // Construct subnet-port mapping from port-subnet mapping
Saurav Das7a1ffca2016-03-28 19:00:18 -0700275 SetMultimap<PortNumber, Ip4Prefix> portSubnetMap = srinfo.subnets;
276 Map<Ip4Prefix, List<PortNumber>> subnetPortMap = new HashMap<>();
Charles Chan5270ed02016-01-30 23:22:37 -0800277
278 portSubnetMap.entries().forEach(entry -> {
279 PortNumber port = entry.getKey();
280 Ip4Prefix subnet = entry.getValue();
281
Charles Chand0fd5dc2016-02-16 23:14:49 -0800282 if (subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
283 return;
284 }
285
Charles Chanc42e84e2015-10-20 16:24:19 -0700286 if (subnetPortMap.containsKey(subnet)) {
287 subnetPortMap.get(subnet).add(port);
288 } else {
289 ArrayList<PortNumber> ports = new ArrayList<>();
290 ports.add(port);
291 subnetPortMap.put(subnet, ports);
292 }
293 });
Charles Chanc42e84e2015-10-20 16:24:19 -0700294 return subnetPortMap;
295 }
296
Charles Chane849c192016-01-11 18:28:54 -0800297 @Override
298 public Map<VlanId, List<ConnectPoint>> getXConnects() {
299 return xConnects;
300 }
301
sanghob35a6192015-04-01 13:05:26 -0700302 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700303 * Returns the device identifier or data plane identifier (dpid)
304 * of a segment router given its segment id.
sanghob35a6192015-04-01 13:05:26 -0700305 *
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700306 * @param sid segment id
307 * @return deviceId device identifier
sanghob35a6192015-04-01 13:05:26 -0700308 */
309 public DeviceId getDeviceId(int sid) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700310 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
311 deviceConfigMap.entrySet()) {
312 if (entry.getValue().nodeSid == sid) {
313 return entry.getValue().deviceId;
sanghob35a6192015-04-01 13:05:26 -0700314 }
315 }
316
317 return null;
318 }
319
320 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700321 * Returns the device identifier or data plane identifier (dpid)
322 * of a segment router given its router ip address.
sanghob35a6192015-04-01 13:05:26 -0700323 *
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700324 * @param ipAddress router ip address
325 * @return deviceId device identifier
sanghob35a6192015-04-01 13:05:26 -0700326 */
327 public DeviceId getDeviceId(Ip4Address ipAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700328 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
329 deviceConfigMap.entrySet()) {
330 if (entry.getValue().ip.equals(ipAddress)) {
331 return entry.getValue().deviceId;
sanghob35a6192015-04-01 13:05:26 -0700332 }
333 }
334
335 return null;
336 }
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700337
338 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700339 * Returns the configured port ip addresses for a segment router.
340 * These addresses serve as gateway IP addresses for the subnets configured
341 * on those ports.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700342 *
343 * @param deviceId device identifier
Saurav Das837e0bb2015-10-30 17:45:38 -0700344 * @return immutable set of ip addresses configured on the ports or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700345 */
Saurav Das837e0bb2015-10-30 17:45:38 -0700346 public Set<Ip4Address> getPortIPs(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700347 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
348 if (srinfo != null) {
349 log.trace("getSubnetGatewayIps for device{} is {}", deviceId,
350 srinfo.gatewayIps.values());
Saurav Das837e0bb2015-10-30 17:45:38 -0700351 return ImmutableSet.copyOf(srinfo.gatewayIps.values());
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700352 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700353 return null;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700354 }
355
356 /**
357 * Returns the configured subnet prefixes for a segment router.
358 *
359 * @param deviceId device identifier
Saurav Das0e99e2b2015-10-28 12:39:42 -0700360 * @return list of ip prefixes or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700361 */
Charles Chan9f676b62015-10-29 14:58:10 -0700362 public Set<Ip4Prefix> getSubnets(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700363 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
364 if (srinfo != null) {
365 log.trace("getSubnets for device{} is {}", deviceId,
366 srinfo.subnets.values());
Charles Chanf2565a92016-02-10 20:46:58 -0800367
368 ImmutableSet.Builder<Ip4Prefix> builder = ImmutableSet.builder();
369 builder.addAll(srinfo.subnets.values());
Charles Chan2196a922016-03-07 00:49:33 -0800370 SegmentRoutingAppConfig appConfig =
371 cfgService.getConfig(appId, SegmentRoutingAppConfig.class);
372 if (appConfig != null) {
373 if (deviceId.equals(appConfig.vRouterId().orElse(null))) {
374 builder.add(Ip4Prefix.valueOf("0.0.0.0/0"));
375 }
Charles Chanf2565a92016-02-10 20:46:58 -0800376 }
377 return builder.build();
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700378 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700379 return null;
380 }
381
382 /**
Charles Chan5270ed02016-01-30 23:22:37 -0800383 * Returns the configured non-/32 and non-/0 subnet on the given port,
384 * or null if no subnet has been configured on the port.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700385 *
386 * @param deviceId device identifier
387 * @param pnum port identifier
388 * @return configured subnet on port, or null
389 */
390 public Ip4Prefix getPortSubnet(DeviceId deviceId, PortNumber pnum) {
391 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
392 if (srinfo != null) {
Charles Chan5270ed02016-01-30 23:22:37 -0800393 Optional<Ip4Prefix> result = srinfo.subnets.get(pnum).stream()
394 .filter(subnet ->
395 subnet.getIp4Prefix().prefixLength() != IpPrefix.MAX_INET_MASK_LENGTH &&
396 subnet.getIp4Prefix().prefixLength() != 0)
397 .findFirst();
398 return (result.isPresent()) ? result.get() : null;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700399 }
400 return null;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700401 }
402
403 /**
404 * Returns the router ip address of segment router that has the
405 * specified ip address in its subnets.
406 *
407 * @param destIpAddress target ip address
408 * @return router ip address
409 */
410 public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) {
411 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
412 deviceConfigMap.entrySet()) {
Charles Chan5270ed02016-01-30 23:22:37 -0800413 for (Ip4Prefix prefix : entry.getValue().subnets.values()) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700414 if (prefix.contains(destIpAddress)) {
415 return entry.getValue().ip;
416 }
417 }
418 }
419
420 log.debug("No router was found for {}", destIpAddress);
421 return null;
422 }
423
424 /**
425 * Returns the router mac address of segment router that has the
426 * specified ip address as one of its subnet gateway ip address.
427 *
428 * @param gatewayIpAddress router gateway ip address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700429 * @return router mac address or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700430 */
431 public MacAddress getRouterMacForAGatewayIp(Ip4Address gatewayIpAddress) {
432 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
433 deviceConfigMap.entrySet()) {
434 if (entry.getValue().gatewayIps.
435 values().contains(gatewayIpAddress)) {
436 return entry.getValue().mac;
437 }
438 }
439
440 log.debug("Cannot find a router for {}", gatewayIpAddress);
441 return null;
442 }
sangho666cd6d2015-04-14 16:27:13 -0700443
444
445 /**
446 * Checks if the host is in the subnet defined in the router with the
447 * device ID given.
448 *
449 * @param deviceId device identification of the router
450 * @param hostIp host IP address to check
451 * @return true if the host is within the subnet of the router,
452 * false if no subnet is defined under the router or if the host is not
453 * within the subnet defined in the router
454 */
455 public boolean inSameSubnet(DeviceId deviceId, Ip4Address hostIp) {
456
Charles Chan9f676b62015-10-29 14:58:10 -0700457 Set<Ip4Prefix> subnets = getSubnets(deviceId);
sangho666cd6d2015-04-14 16:27:13 -0700458 if (subnets == null) {
459 return false;
460 }
461
462 for (Ip4Prefix subnet: subnets) {
Charles Chan5270ed02016-01-30 23:22:37 -0800463 // Exclude /0 since it is a special case used for default route
464 if (subnet.prefixLength() != 0 && subnet.contains(hostIp)) {
sangho666cd6d2015-04-14 16:27:13 -0700465 return true;
466 }
467 }
468
469 return false;
470 }
sangho1e575652015-05-14 00:39:53 -0700471
472 /**
473 * Returns the ports corresponding to the adjacency Sid given.
474 *
475 * @param deviceId device identification of the router
476 * @param sid adjacency Sid
Charles Chan531a78b2015-12-01 10:00:51 -0800477 * @return set of port numbers
sangho1e575652015-05-14 00:39:53 -0700478 */
Charles Chan531a78b2015-12-01 10:00:51 -0800479 public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700480 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan531a78b2015-12-01 10:00:51 -0800481 return srinfo != null ?
482 ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) :
483 ImmutableSet.copyOf(new HashSet<>());
sangho1e575652015-05-14 00:39:53 -0700484 }
485
486 /**
487 * Check if the Sid given is whether adjacency Sid of the router device or not.
488 *
489 * @param deviceId device identification of the router
490 * @param sid Sid to check
491 * @return true if the Sid given is the adjacency Sid of the device,
492 * otherwise false
493 */
494 public boolean isAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700495 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan531a78b2015-12-01 10:00:51 -0800496 return srinfo != null && srinfo.adjacencySids.containsKey(sid);
sangho1e575652015-05-14 00:39:53 -0700497 }
Charles Chanf2565a92016-02-10 20:46:58 -0800498
Charles Chand55e84d2016-03-30 17:54:24 -0700499 /**
500 * Gets connect points for which segment routing does not install subnet rules.
501 *
502 * @return set of connect points
503 */
Charles Chand9681e72016-02-22 19:27:29 -0800504 public Set<ConnectPoint> suppressSubnet() {
Charles Chan2196a922016-03-07 00:49:33 -0800505 SegmentRoutingAppConfig appConfig =
506 cfgService.getConfig(appId, SegmentRoutingAppConfig.class);
Charles Chand9681e72016-02-22 19:27:29 -0800507 return (appConfig != null) ? appConfig.suppressSubnet() : ImmutableSet.of();
508 }
509
Charles Chand55e84d2016-03-30 17:54:24 -0700510 /**
511 * Gets connect points for which segment routing does not install host rules.
512 *
513 * @return set of connect points
514 */
Charles Chand9681e72016-02-22 19:27:29 -0800515 public Set<ConnectPoint> suppressHost() {
Charles Chan2196a922016-03-07 00:49:33 -0800516 SegmentRoutingAppConfig appConfig =
517 cfgService.getConfig(appId, SegmentRoutingAppConfig.class);
Charles Chand9681e72016-02-22 19:27:29 -0800518 return (appConfig != null) ? appConfig.suppressHost() : ImmutableSet.of();
Charles Chanf2565a92016-02-10 20:46:58 -0800519 }
Jonathan Hart00cddda2016-02-16 10:30:37 -0800520}