blob: 3d2d337cc4879ff419a6a800fa03e701985faa47 [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 Chan9f676b62015-10-29 14:58:10 -070018import com.google.common.collect.ImmutableSet;
sanghob35a6192015-04-01 13:05:26 -070019import org.onlab.packet.Ip4Address;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070020import org.onlab.packet.Ip4Prefix;
sanghob35a6192015-04-01 13:05:26 -070021import org.onlab.packet.MacAddress;
Charles Chane849c192016-01-11 18:28:54 -080022import org.onlab.packet.VlanId;
Charles Chan4636be02015-10-07 14:21:45 -070023import org.onosproject.incubator.net.config.basics.ConfigException;
24import org.onosproject.incubator.net.config.basics.InterfaceConfig;
25import org.onosproject.incubator.net.intf.Interface;
26import org.onosproject.net.ConnectPoint;
Charles Chand6832882015-10-05 17:50:33 -070027import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chan4636be02015-10-07 14:21:45 -070028import org.onosproject.net.host.InterfaceIpAddress;
sanghob35a6192015-04-01 13:05:26 -070029import org.onosproject.net.DeviceId;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070030import org.onosproject.net.PortNumber;
sanghob35a6192015-04-01 13:05:26 -070031import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070034import java.util.ArrayList;
sanghob35a6192015-04-01 13:05:26 -070035import java.util.HashMap;
Charles Chan531a78b2015-12-01 10:00:51 -080036import java.util.HashSet;
Charles Chane849c192016-01-11 18:28:54 -080037import java.util.LinkedList;
sanghob35a6192015-04-01 13:05:26 -070038import java.util.List;
39import java.util.Map;
Charles Chand6832882015-10-05 17:50:33 -070040import java.util.Set;
Saurav Das0e99e2b2015-10-28 12:39:42 -070041import java.util.concurrent.ConcurrentHashMap;
sanghob35a6192015-04-01 13:05:26 -070042
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070043/**
44 * Segment Routing configuration component that reads the
45 * segment routing related configuration from Network Configuration Manager
46 * component and organizes in more accessible formats.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070047 */
sanghob35a6192015-04-01 13:05:26 -070048public class DeviceConfiguration implements DeviceProperties {
49
50 private static final Logger log = LoggerFactory
51 .getLogger(DeviceConfiguration.class);
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -070052 private final List<Integer> allSegmentIds = new ArrayList<>();
Charles Chane849c192016-01-11 18:28:54 -080053 private final Map<DeviceId, SegmentRouterInfo> deviceConfigMap = new ConcurrentHashMap<>();
54 private final Map<VlanId, List<ConnectPoint>> xConnects = new ConcurrentHashMap<>();
sanghob35a6192015-04-01 13:05:26 -070055
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070056 private class SegmentRouterInfo {
57 int nodeSid;
58 DeviceId deviceId;
59 Ip4Address ip;
60 MacAddress mac;
61 boolean isEdge;
62 HashMap<PortNumber, Ip4Address> gatewayIps;
63 HashMap<PortNumber, Ip4Prefix> subnets;
Charles Chan531a78b2015-12-01 10:00:51 -080064 Map<Integer, Set<Integer>> adjacencySids;
Charles Chanb8e10c82015-10-14 11:24:40 -070065
66 public SegmentRouterInfo() {
Charles Chane849c192016-01-11 18:28:54 -080067 gatewayIps = new HashMap<>();
68 subnets = new HashMap<>();
Charles Chanb8e10c82015-10-14 11:24:40 -070069 }
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070070 }
sanghob35a6192015-04-01 13:05:26 -070071
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070072 /**
Charles Chane849c192016-01-11 18:28:54 -080073 * Constructs device configuration for all Segment Router devices,
74 * organizing the data into various maps for easier access.
Brian O'Connor52515622015-10-09 17:03:44 -070075 *
76 * @param cfgService config service
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070077 */
Charles Chand6832882015-10-05 17:50:33 -070078 public DeviceConfiguration(NetworkConfigRegistry cfgService) {
Charles Chan4636be02015-10-07 14:21:45 -070079 // Read config from device subject, excluding gatewayIps and subnets.
80 Set<DeviceId> deviceSubjects =
Charles Chand6832882015-10-05 17:50:33 -070081 cfgService.getSubjects(DeviceId.class, SegmentRoutingConfig.class);
Charles Chan4636be02015-10-07 14:21:45 -070082 deviceSubjects.forEach(subject -> {
Charles Chand6832882015-10-05 17:50:33 -070083 SegmentRoutingConfig config =
84 cfgService.getConfig(subject, SegmentRoutingConfig.class);
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070085 SegmentRouterInfo info = new SegmentRouterInfo();
Charles Chand6832882015-10-05 17:50:33 -070086 info.deviceId = subject;
Charles Chan531a78b2015-12-01 10:00:51 -080087 info.nodeSid = config.nodeSid();
88 info.ip = config.routerIp();
89 info.mac = config.routerMac();
Charles Chand6832882015-10-05 17:50:33 -070090 info.isEdge = config.isEdgeRouter();
Charles Chan531a78b2015-12-01 10:00:51 -080091 info.adjacencySids = config.adjacencySids();
Charles Chand6832882015-10-05 17:50:33 -070092
Charles Chane849c192016-01-11 18:28:54 -080093 deviceConfigMap.put(info.deviceId, info);
94 allSegmentIds.add(info.nodeSid);
Charles Chand6832882015-10-05 17:50:33 -070095 });
Charles Chan4636be02015-10-07 14:21:45 -070096
97 // Read gatewayIps and subnets from port subject.
98 Set<ConnectPoint> portSubjects =
99 cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class);
100 portSubjects.forEach(subject -> {
101 InterfaceConfig config =
102 cfgService.getConfig(subject, InterfaceConfig.class);
103 Set<Interface> networkInterfaces;
104 try {
105 networkInterfaces = config.getInterfaces();
106 } catch (ConfigException e) {
107 log.error("Error loading port configuration");
108 return;
109 }
110 networkInterfaces.forEach(networkInterface -> {
Charles Chane849c192016-01-11 18:28:54 -0800111 VlanId vlanId = networkInterface.vlan();
112 ConnectPoint connectPoint = networkInterface.connectPoint();
113 DeviceId dpid = connectPoint.deviceId();
114 PortNumber port = connectPoint.port();
115 SegmentRouterInfo info = deviceConfigMap.get(dpid);
Charles Chan4636be02015-10-07 14:21:45 -0700116
Charles Chanb8e10c82015-10-14 11:24:40 -0700117 // skip if there is no corresponding device for this ConenctPoint
118 if (info != null) {
Charles Chane849c192016-01-11 18:28:54 -0800119 // Extract subnet information
Charles Chanb8e10c82015-10-14 11:24:40 -0700120 Set<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddresses();
121 interfaceAddresses.forEach(interfaceAddress -> {
122 info.gatewayIps.put(port, interfaceAddress.ipAddress().getIp4Address());
123 info.subnets.put(port, interfaceAddress.subnetAddress().getIp4Prefix());
124 });
Charles Chane849c192016-01-11 18:28:54 -0800125
126 // Extract VLAN cross-connect information
127 // Do not setup cross-connect if VLAN is NONE
128 if (vlanId.equals(VlanId.NONE)) {
129 return;
130 }
131 List<ConnectPoint> connectPoints = xConnects.get(vlanId);
132 if (connectPoints != null) {
133 if (connectPoints.size() != 1) {
134 log.warn("Cross-connect should only have two endpoints. Aborting.");
135 return;
136 }
137 if (!connectPoints.get(0).deviceId().equals(connectPoint.deviceId())) {
138 log.warn("Cross-connect endpoints must be on the same switch. Aborting.");
139 return;
140 }
141 connectPoints.add(connectPoint);
142 } else {
143 connectPoints = new LinkedList<>();
144 connectPoints.add(connectPoint);
145 xConnects.put(vlanId, connectPoints);
146 }
Charles Chanb8e10c82015-10-14 11:24:40 -0700147 }
Charles Chan4636be02015-10-07 14:21:45 -0700148 });
149
150 });
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700151 }
152
sanghob35a6192015-04-01 13:05:26 -0700153 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800154 public boolean isConfigured(DeviceId deviceId) {
155 return deviceConfigMap.get(deviceId) != null;
156 }
157
158 @Override
159 public int getSegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700160 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
161 if (srinfo != null) {
162 log.trace("getSegmentId for device{} is {}", deviceId, srinfo.nodeSid);
163 return srinfo.nodeSid;
sanghob35a6192015-04-01 13:05:26 -0700164 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800165 String message = "getSegmentId fails for device: " + deviceId + ".";
166 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700167 }
168 }
169
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700170 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700171 * Returns the Node segment id of a segment router given its Router mac address.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700172 *
173 * @param routerMac router mac address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700174 * @return node segment id, or -1 if not found in config
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700175 */
176 public int getSegmentId(MacAddress routerMac) {
177 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
178 deviceConfigMap.entrySet()) {
179 if (entry.getValue().mac.equals(routerMac)) {
180 return entry.getValue().nodeSid;
181 }
182 }
sanghob35a6192015-04-01 13:05:26 -0700183
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700184 return -1;
185 }
186
187 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700188 * Returns the Node segment id of a segment router given its Router ip address.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700189 *
190 * @param routerAddress router ip address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700191 * @return node segment id, or -1 if not found in config
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700192 */
193 public int getSegmentId(Ip4Address routerAddress) {
194 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
195 deviceConfigMap.entrySet()) {
196 if (entry.getValue().ip.equals(routerAddress)) {
197 return entry.getValue().nodeSid;
198 }
199 }
200
201 return -1;
202 }
203
sanghob35a6192015-04-01 13:05:26 -0700204 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800205 public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700206 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
207 if (srinfo != null) {
208 log.trace("getDeviceMac for device{} is {}", deviceId, srinfo.mac);
209 return srinfo.mac;
sanghob35a6192015-04-01 13:05:26 -0700210 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800211 String message = "getDeviceMac fails for device: " + deviceId + ".";
212 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700213 }
214 }
215
Charles Chan0b4e6182015-11-03 10:42:14 -0800216 @Override
217 public Ip4Address getRouterIp(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700218 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
219 if (srinfo != null) {
220 log.trace("getDeviceIp for device{} is {}", deviceId, srinfo.ip);
221 return srinfo.ip;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700222 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800223 String message = "getRouterIp fails for device: " + deviceId + ".";
224 throw new DeviceConfigNotFoundException(message);
sanghob35a6192015-04-01 13:05:26 -0700225 }
sanghob35a6192015-04-01 13:05:26 -0700226 }
227
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700228 @Override
Charles Chan0b4e6182015-11-03 10:42:14 -0800229 public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700230 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
231 if (srinfo != null) {
232 log.trace("isEdgeDevice for device{} is {}", deviceId, srinfo.isEdge);
233 return srinfo.isEdge;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700234 } else {
Charles Chan0b4e6182015-11-03 10:42:14 -0800235 String message = "isEdgeDevice fails for device: " + deviceId + ".";
236 throw new DeviceConfigNotFoundException(message);
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700237 }
238 }
239
sanghob35a6192015-04-01 13:05:26 -0700240 @Override
241 public List<Integer> getAllDeviceSegmentIds() {
242 return allSegmentIds;
243 }
244
Charles Chanc42e84e2015-10-20 16:24:19 -0700245 @Override
246 public Map<Ip4Prefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId) {
247 Map<Ip4Prefix, List<PortNumber>> subnetPortMap = new HashMap<>();
248
249 // Construct subnet-port mapping from port-subnet mapping
250 Map<PortNumber, Ip4Prefix> portSubnetMap =
251 this.deviceConfigMap.get(deviceId).subnets;
252 portSubnetMap.forEach((port, subnet) -> {
253 if (subnetPortMap.containsKey(subnet)) {
254 subnetPortMap.get(subnet).add(port);
255 } else {
256 ArrayList<PortNumber> ports = new ArrayList<>();
257 ports.add(port);
258 subnetPortMap.put(subnet, ports);
259 }
260 });
261
262 return subnetPortMap;
263 }
264
Charles Chane849c192016-01-11 18:28:54 -0800265 @Override
266 public Map<VlanId, List<ConnectPoint>> getXConnects() {
267 return xConnects;
268 }
269
sanghob35a6192015-04-01 13:05:26 -0700270 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700271 * Returns the device identifier or data plane identifier (dpid)
272 * of a segment router given its segment id.
sanghob35a6192015-04-01 13:05:26 -0700273 *
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700274 * @param sid segment id
275 * @return deviceId device identifier
sanghob35a6192015-04-01 13:05:26 -0700276 */
277 public DeviceId getDeviceId(int sid) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700278 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
279 deviceConfigMap.entrySet()) {
280 if (entry.getValue().nodeSid == sid) {
281 return entry.getValue().deviceId;
sanghob35a6192015-04-01 13:05:26 -0700282 }
283 }
284
285 return null;
286 }
287
288 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700289 * Returns the device identifier or data plane identifier (dpid)
290 * of a segment router given its router ip address.
sanghob35a6192015-04-01 13:05:26 -0700291 *
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700292 * @param ipAddress router ip address
293 * @return deviceId device identifier
sanghob35a6192015-04-01 13:05:26 -0700294 */
295 public DeviceId getDeviceId(Ip4Address ipAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700296 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
297 deviceConfigMap.entrySet()) {
298 if (entry.getValue().ip.equals(ipAddress)) {
299 return entry.getValue().deviceId;
sanghob35a6192015-04-01 13:05:26 -0700300 }
301 }
302
303 return null;
304 }
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700305
306 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700307 * Returns the configured port ip addresses for a segment router.
308 * These addresses serve as gateway IP addresses for the subnets configured
309 * on those ports.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700310 *
311 * @param deviceId device identifier
Saurav Das837e0bb2015-10-30 17:45:38 -0700312 * @return immutable set of ip addresses configured on the ports or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700313 */
Saurav Das837e0bb2015-10-30 17:45:38 -0700314 public Set<Ip4Address> getPortIPs(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700315 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
316 if (srinfo != null) {
317 log.trace("getSubnetGatewayIps for device{} is {}", deviceId,
318 srinfo.gatewayIps.values());
Saurav Das837e0bb2015-10-30 17:45:38 -0700319 return ImmutableSet.copyOf(srinfo.gatewayIps.values());
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700320 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700321 return null;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700322 }
323
324 /**
Saurav Das822c4e22015-10-23 10:51:11 -0700325 * Returns the configured IP addresses per port
326 * for a segment router.
327 *
328 * @param deviceId device identifier
Saurav Das0e99e2b2015-10-28 12:39:42 -0700329 * @return map of port to gateway IP addresses or null if not found
Saurav Das822c4e22015-10-23 10:51:11 -0700330 */
331 public Map<PortNumber, Ip4Address> getPortIPMap(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700332 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
333 if (srinfo != null) {
334 return srinfo.gatewayIps;
Saurav Das822c4e22015-10-23 10:51:11 -0700335 }
336 return null;
337 }
338
339 /**
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700340 * Returns the configured subnet prefixes for a segment router.
341 *
342 * @param deviceId device identifier
Saurav Das0e99e2b2015-10-28 12:39:42 -0700343 * @return list of ip prefixes or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700344 */
Charles Chan9f676b62015-10-29 14:58:10 -0700345 public Set<Ip4Prefix> getSubnets(DeviceId deviceId) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700346 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
347 if (srinfo != null) {
348 log.trace("getSubnets for device{} is {}", deviceId,
349 srinfo.subnets.values());
Charles Chan9f676b62015-10-29 14:58:10 -0700350 return ImmutableSet.copyOf(srinfo.subnets.values());
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700351 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700352 return null;
353 }
354
355 /**
356 * Returns the configured subnet on the given port, or null if no
357 * subnet has been configured on the port.
358 *
359 * @param deviceId device identifier
360 * @param pnum port identifier
361 * @return configured subnet on port, or null
362 */
363 public Ip4Prefix getPortSubnet(DeviceId deviceId, PortNumber pnum) {
364 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
365 if (srinfo != null) {
366 return srinfo.subnets.get(pnum);
367 }
368 return null;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700369 }
370
371 /**
372 * Returns the router ip address of segment router that has the
373 * specified ip address in its subnets.
374 *
375 * @param destIpAddress target ip address
376 * @return router ip address
377 */
378 public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) {
379 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
380 deviceConfigMap.entrySet()) {
381 for (Ip4Prefix prefix:entry.getValue().subnets.values()) {
382 if (prefix.contains(destIpAddress)) {
383 return entry.getValue().ip;
384 }
385 }
386 }
387
388 log.debug("No router was found for {}", destIpAddress);
389 return null;
390 }
391
392 /**
393 * Returns the router mac address of segment router that has the
394 * specified ip address as one of its subnet gateway ip address.
395 *
396 * @param gatewayIpAddress router gateway ip address
Saurav Das0e99e2b2015-10-28 12:39:42 -0700397 * @return router mac address or null if not found
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700398 */
399 public MacAddress getRouterMacForAGatewayIp(Ip4Address gatewayIpAddress) {
400 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
401 deviceConfigMap.entrySet()) {
402 if (entry.getValue().gatewayIps.
403 values().contains(gatewayIpAddress)) {
404 return entry.getValue().mac;
405 }
406 }
407
408 log.debug("Cannot find a router for {}", gatewayIpAddress);
409 return null;
410 }
sangho666cd6d2015-04-14 16:27:13 -0700411
412
413 /**
414 * Checks if the host is in the subnet defined in the router with the
415 * device ID given.
416 *
417 * @param deviceId device identification of the router
418 * @param hostIp host IP address to check
419 * @return true if the host is within the subnet of the router,
420 * false if no subnet is defined under the router or if the host is not
421 * within the subnet defined in the router
422 */
423 public boolean inSameSubnet(DeviceId deviceId, Ip4Address hostIp) {
424
Charles Chan9f676b62015-10-29 14:58:10 -0700425 Set<Ip4Prefix> subnets = getSubnets(deviceId);
sangho666cd6d2015-04-14 16:27:13 -0700426 if (subnets == null) {
427 return false;
428 }
429
430 for (Ip4Prefix subnet: subnets) {
431 if (subnet.contains(hostIp)) {
432 return true;
433 }
434 }
435
436 return false;
437 }
sangho1e575652015-05-14 00:39:53 -0700438
439 /**
440 * Returns the ports corresponding to the adjacency Sid given.
441 *
442 * @param deviceId device identification of the router
443 * @param sid adjacency Sid
Charles Chan531a78b2015-12-01 10:00:51 -0800444 * @return set of port numbers
sangho1e575652015-05-14 00:39:53 -0700445 */
Charles Chan531a78b2015-12-01 10:00:51 -0800446 public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700447 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan531a78b2015-12-01 10:00:51 -0800448 return srinfo != null ?
449 ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) :
450 ImmutableSet.copyOf(new HashSet<>());
sangho1e575652015-05-14 00:39:53 -0700451 }
452
453 /**
454 * Check if the Sid given is whether adjacency Sid of the router device or not.
455 *
456 * @param deviceId device identification of the router
457 * @param sid Sid to check
458 * @return true if the Sid given is the adjacency Sid of the device,
459 * otherwise false
460 */
461 public boolean isAdjacencySid(DeviceId deviceId, int sid) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700462 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
Charles Chan531a78b2015-12-01 10:00:51 -0800463 return srinfo != null && srinfo.adjacencySids.containsKey(sid);
sangho1e575652015-05-14 00:39:53 -0700464 }
Charles Chand6832882015-10-05 17:50:33 -0700465}