blob: 5d48b6de1b7b4b34f37b53ea71d86a10f2033e55 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Charles Chand6832882015-10-05 17:50:33 -07002 * Copyright 2014-2015 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 Chand6832882015-10-05 17:50:33 -0700104
Charles Chane849c192016-01-11 18:28:54 -0800105 deviceConfigMap.put(info.deviceId, info);
106 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
267 public Map<Ip4Prefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId) {
268 Map<Ip4Prefix, List<PortNumber>> subnetPortMap = new HashMap<>();
269
270 // Construct subnet-port mapping from port-subnet mapping
Charles Chan5270ed02016-01-30 23:22:37 -0800271 SetMultimap<PortNumber, Ip4Prefix> portSubnetMap =
Charles Chanc42e84e2015-10-20 16:24:19 -0700272 this.deviceConfigMap.get(deviceId).subnets;
Charles Chan5270ed02016-01-30 23:22:37 -0800273
274 portSubnetMap.entries().forEach(entry -> {
275 PortNumber port = entry.getKey();
276 Ip4Prefix subnet = entry.getValue();
277
Charles Chand0fd5dc2016-02-16 23:14:49 -0800278 if (subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) {
279 return;
280 }
281
Charles Chanc42e84e2015-10-20 16:24:19 -0700282 if (subnetPortMap.containsKey(subnet)) {
283 subnetPortMap.get(subnet).add(port);
284 } else {
285 ArrayList<PortNumber> ports = new ArrayList<>();
286 ports.add(port);
287 subnetPortMap.put(subnet, ports);
288 }
289 });
Charles Chanc42e84e2015-10-20 16:24:19 -0700290 return subnetPortMap;
291 }
292
Charles Chane849c192016-01-11 18:28:54 -0800293 @Override
294 public Map<VlanId, List<ConnectPoint>> getXConnects() {
295 return xConnects;
296 }
297
sanghob35a6192015-04-01 13:05:26 -0700298 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700299 * Returns the device identifier or data plane identifier (dpid)
300 * of a segment router given its segment id.
sanghob35a6192015-04-01 13:05:26 -0700301 *
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700302 * @param sid segment id
303 * @return deviceId device identifier
sanghob35a6192015-04-01 13:05:26 -0700304 */
305 public DeviceId getDeviceId(int sid) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700306 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
307 deviceConfigMap.entrySet()) {
308 if (entry.getValue().nodeSid == sid) {
309 return entry.getValue().deviceId;
sanghob35a6192015-04-01 13:05:26 -0700310 }
311 }
312
313 return null;
314 }
315
316 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700317 * Returns the device identifier or data plane identifier (dpid)
318 * of a segment router given its router ip address.
sanghob35a6192015-04-01 13:05:26 -0700319 *
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700320 * @param ipAddress router ip address
321 * @return deviceId device identifier
sanghob35a6192015-04-01 13:05:26 -0700322 */
323 public DeviceId getDeviceId(Ip4Address ipAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700324 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
325 deviceConfigMap.entrySet()) {
326 if (entry.getValue().ip.equals(ipAddress)) {
327 return entry.getValue().deviceId;
sanghob35a6192015-04-01 13:05:26 -0700328 }
329 }
330
331 return null;
332 }
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700333
334 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700335 * Returns the configured port ip addresses for a segment router.
336 * These addresses serve as gateway IP addresses for the subnets configured
337 * on those ports.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700338 *
339 * @param deviceId device identifier
Saurav Das837e0bb2015-10-30 17:45:38 -0700340 * @return immutable set of ip addresses configured on the ports or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700341 */
Saurav Das837e0bb2015-10-30 17:45:38 -0700342 public Set<Ip4Address> getPortIPs(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700343 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
344 if (srinfo != null) {
345 log.trace("getSubnetGatewayIps for device{} is {}", deviceId,
346 srinfo.gatewayIps.values());
Saurav Das837e0bb2015-10-30 17:45:38 -0700347 return ImmutableSet.copyOf(srinfo.gatewayIps.values());
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700348 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700349 return null;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700350 }
351
352 /**
353 * Returns the configured subnet prefixes for a segment router.
354 *
355 * @param deviceId device identifier
Saurav Das0e99e2b2015-10-28 12:39:42 -0700356 * @return list of ip prefixes or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700357 */
Charles Chan9f676b62015-10-29 14:58:10 -0700358 public Set<Ip4Prefix> getSubnets(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700359 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
360 if (srinfo != null) {
361 log.trace("getSubnets for device{} is {}", deviceId,
362 srinfo.subnets.values());
Charles Chanf2565a92016-02-10 20:46:58 -0800363
364 ImmutableSet.Builder<Ip4Prefix> builder = ImmutableSet.builder();
365 builder.addAll(srinfo.subnets.values());
Charles Chan2196a922016-03-07 00:49:33 -0800366 SegmentRoutingAppConfig appConfig =
367 cfgService.getConfig(appId, SegmentRoutingAppConfig.class);
368 if (appConfig != null) {
369 if (deviceId.equals(appConfig.vRouterId().orElse(null))) {
370 builder.add(Ip4Prefix.valueOf("0.0.0.0/0"));
371 }
Charles Chanf2565a92016-02-10 20:46:58 -0800372 }
373 return builder.build();
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700374 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700375 return null;
376 }
377
378 /**
Charles Chan5270ed02016-01-30 23:22:37 -0800379 * Returns the configured non-/32 and non-/0 subnet on the given port,
380 * or null if no subnet has been configured on the port.
Saurav Das0e99e2b2015-10-28 12:39:42 -0700381 *
382 * @param deviceId device identifier
383 * @param pnum port identifier
384 * @return configured subnet on port, or null
385 */
386 public Ip4Prefix getPortSubnet(DeviceId deviceId, PortNumber pnum) {
387 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
388 if (srinfo != null) {
Charles Chan5270ed02016-01-30 23:22:37 -0800389 Optional<Ip4Prefix> result = srinfo.subnets.get(pnum).stream()
390 .filter(subnet ->
391 subnet.getIp4Prefix().prefixLength() != IpPrefix.MAX_INET_MASK_LENGTH &&
392 subnet.getIp4Prefix().prefixLength() != 0)
393 .findFirst();
394 return (result.isPresent()) ? result.get() : null;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700395 }
396 return null;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700397 }
398
399 /**
400 * Returns the router ip address of segment router that has the
401 * specified ip address in its subnets.
402 *
403 * @param destIpAddress target ip address
404 * @return router ip address
405 */
406 public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) {
407 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
408 deviceConfigMap.entrySet()) {
Charles Chan5270ed02016-01-30 23:22:37 -0800409 for (Ip4Prefix prefix : entry.getValue().subnets.values()) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700410 if (prefix.contains(destIpAddress)) {
411 return entry.getValue().ip;
412 }
413 }
414 }
415
416 log.debug("No router was found for {}", destIpAddress);
417 return null;
418 }
419
420 /**
421 * Returns the router mac address of segment router that has the
422 * specified ip address as one of its subnet gateway ip address.
423 *
424 * @param gatewayIpAddress router gateway ip address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700425 * @return router mac address or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700426 */
427 public MacAddress getRouterMacForAGatewayIp(Ip4Address gatewayIpAddress) {
428 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
429 deviceConfigMap.entrySet()) {
430 if (entry.getValue().gatewayIps.
431 values().contains(gatewayIpAddress)) {
432 return entry.getValue().mac;
433 }
434 }
435
436 log.debug("Cannot find a router for {}", gatewayIpAddress);
437 return null;
438 }
sangho666cd6d2015-04-14 16:27:13 -0700439
440
441 /**
442 * Checks if the host is in the subnet defined in the router with the
443 * device ID given.
444 *
445 * @param deviceId device identification of the router
446 * @param hostIp host IP address to check
447 * @return true if the host is within the subnet of the router,
448 * false if no subnet is defined under the router or if the host is not
449 * within the subnet defined in the router
450 */
451 public boolean inSameSubnet(DeviceId deviceId, Ip4Address hostIp) {
452
Charles Chan9f676b62015-10-29 14:58:10 -0700453 Set<Ip4Prefix> subnets = getSubnets(deviceId);
sangho666cd6d2015-04-14 16:27:13 -0700454 if (subnets == null) {
455 return false;
456 }
457
458 for (Ip4Prefix subnet: subnets) {
Charles Chan5270ed02016-01-30 23:22:37 -0800459 // Exclude /0 since it is a special case used for default route
460 if (subnet.prefixLength() != 0 && subnet.contains(hostIp)) {
sangho666cd6d2015-04-14 16:27:13 -0700461 return true;
462 }
463 }
464
465 return false;
466 }
sangho1e575652015-05-14 00:39:53 -0700467
468 /**
469 * Returns the ports corresponding to the adjacency Sid given.
470 *
471 * @param deviceId device identification of the router
472 * @param sid adjacency Sid
Charles Chan531a78b2015-12-01 10:00:51 -0800473 * @return set of port numbers
sangho1e575652015-05-14 00:39:53 -0700474 */
Charles Chan531a78b2015-12-01 10:00:51 -0800475 public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700476 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan531a78b2015-12-01 10:00:51 -0800477 return srinfo != null ?
478 ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) :
479 ImmutableSet.copyOf(new HashSet<>());
sangho1e575652015-05-14 00:39:53 -0700480 }
481
482 /**
483 * Check if the Sid given is whether adjacency Sid of the router device or not.
484 *
485 * @param deviceId device identification of the router
486 * @param sid Sid to check
487 * @return true if the Sid given is the adjacency Sid of the device,
488 * otherwise false
489 */
490 public boolean isAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700491 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan531a78b2015-12-01 10:00:51 -0800492 return srinfo != null && srinfo.adjacencySids.containsKey(sid);
sangho1e575652015-05-14 00:39:53 -0700493 }
Charles Chanf2565a92016-02-10 20:46:58 -0800494
Charles Chand9681e72016-02-22 19:27:29 -0800495 public Set<ConnectPoint> suppressSubnet() {
Charles Chan2196a922016-03-07 00:49:33 -0800496 SegmentRoutingAppConfig appConfig =
497 cfgService.getConfig(appId, SegmentRoutingAppConfig.class);
Charles Chand9681e72016-02-22 19:27:29 -0800498 return (appConfig != null) ? appConfig.suppressSubnet() : ImmutableSet.of();
499 }
500
501 public Set<ConnectPoint> suppressHost() {
Charles Chan2196a922016-03-07 00:49:33 -0800502 SegmentRoutingAppConfig appConfig =
503 cfgService.getConfig(appId, SegmentRoutingAppConfig.class);
Charles Chand9681e72016-02-22 19:27:29 -0800504 return (appConfig != null) ? appConfig.suppressHost() : ImmutableSet.of();
Charles Chanf2565a92016-02-10 20:46:58 -0800505 }
Jonathan Hart00cddda2016-02-16 10:30:37 -0800506}