Thomas Vachuska | 8fd2505 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 8fd2505 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 3 | * |
| 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 Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 16 | package org.onosproject.segmentrouting.config; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 17 | |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 20 | import com.google.common.collect.SetMultimap; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 21 | import org.onlab.packet.Ip4Address; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 22 | import org.onlab.packet.Ip4Prefix; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 23 | import org.onlab.packet.Ip6Address; |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 25 | import org.onlab.packet.IpPrefix; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 27 | import org.onlab.packet.VlanId; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 28 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 29 | import org.onosproject.incubator.net.config.basics.InterfaceConfig; |
| 30 | import org.onosproject.incubator.net.intf.Interface; |
| 31 | import org.onosproject.net.ConnectPoint; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 32 | import org.onosproject.net.host.InterfaceIpAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 34 | import org.onosproject.net.PortNumber; |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 35 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 39 | import java.util.ArrayList; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 40 | import java.util.HashMap; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 41 | import java.util.HashSet; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | import java.util.List; |
| 43 | import java.util.Map; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 44 | import java.util.Set; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 45 | import java.util.concurrent.ConcurrentHashMap; |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 46 | import java.util.stream.Collectors; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 47 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 48 | import static com.google.common.base.Preconditions.checkNotNull; |
| 49 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Segment Routing configuration component that reads the |
| 52 | * segment routing related configuration from Network Configuration Manager |
| 53 | * component and organizes in more accessible formats. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 54 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 55 | public class DeviceConfiguration implements DeviceProperties { |
| 56 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 57 | private static final String ERROR_CONFIG = "Configuration error."; |
| 58 | private static final String TOO_MANY_SUBNET = ERROR_CONFIG + " Too many subnets configured on {}"; |
| 59 | private static final String NO_SUBNET = "No subnet configured on {}"; |
| 60 | |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 61 | private static final Logger log = LoggerFactory.getLogger(DeviceConfiguration.class); |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 62 | private final List<Integer> allSegmentIds = new ArrayList<>(); |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 63 | private final Map<DeviceId, SegmentRouterInfo> deviceConfigMap = new ConcurrentHashMap<>(); |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 64 | private SegmentRoutingManager srManager; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 65 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 66 | private class SegmentRouterInfo { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 67 | int ipv4NodeSid; |
| 68 | int ipv6NodeSid; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 69 | DeviceId deviceId; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 70 | Ip4Address ipv4Loopback; |
| 71 | Ip6Address ipv6Loopback; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 72 | MacAddress mac; |
| 73 | boolean isEdge; |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 74 | Map<PortNumber, Ip4Address> gatewayIps; |
| 75 | SetMultimap<PortNumber, Ip4Prefix> subnets; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 76 | Map<Integer, Set<Integer>> adjacencySids; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 77 | |
| 78 | public SegmentRouterInfo() { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 79 | gatewayIps = new HashMap<>(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 80 | subnets = HashMultimap.create(); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 81 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 82 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 83 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 84 | /** |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 85 | * Constructs device configuration for all Segment Router devices, |
| 86 | * organizing the data into various maps for easier access. |
Brian O'Connor | 65eeb57 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 87 | * |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 88 | * @param srManager Segment Routing Manager |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 89 | */ |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 90 | public DeviceConfiguration(SegmentRoutingManager srManager) { |
| 91 | this.srManager = srManager; |
Charles Chan | 57bd98c | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 92 | |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 93 | // Read config from device subject, excluding gatewayIps and subnets. |
| 94 | Set<DeviceId> deviceSubjects = |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 95 | srManager.cfgService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 96 | deviceSubjects.forEach(subject -> { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 97 | SegmentRoutingDeviceConfig config = |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 98 | srManager.cfgService.getConfig(subject, SegmentRoutingDeviceConfig.class); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 99 | SegmentRouterInfo info = new SegmentRouterInfo(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 100 | info.deviceId = subject; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 101 | info.ipv4NodeSid = config.nodeSidIPv4(); |
| 102 | info.ipv6NodeSid = config.nodeSidIPv6(); |
| 103 | info.ipv4Loopback = config.routerIpv4(); |
| 104 | info.ipv6Loopback = config.routerIpv6(); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 105 | info.mac = config.routerMac(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 106 | info.isEdge = config.isEdgeRouter(); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 107 | info.adjacencySids = config.adjacencySids(); |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 108 | deviceConfigMap.put(info.deviceId, info); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 109 | log.info("Read device config for device: {}", info.deviceId); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 110 | /* |
| 111 | * IPv6 sid is not inserted. this part of the code is not used for now. |
| 112 | */ |
| 113 | allSegmentIds.add(info.ipv4NodeSid); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 114 | }); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 115 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 116 | // Read gatewayIps and subnets from port subject. Ignore suppressed ports. |
| 117 | Set<ConnectPoint> portSubjects = srManager.cfgService |
| 118 | .getSubjects(ConnectPoint.class, InterfaceConfig.class); |
| 119 | portSubjects.stream().filter(subject -> !isSuppressedPort(subject)).forEach(subject -> { |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 120 | InterfaceConfig config = |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 121 | srManager.cfgService.getConfig(subject, InterfaceConfig.class); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 122 | Set<Interface> networkInterfaces; |
| 123 | try { |
| 124 | networkInterfaces = config.getInterfaces(); |
| 125 | } catch (ConfigException e) { |
| 126 | log.error("Error loading port configuration"); |
| 127 | return; |
| 128 | } |
| 129 | networkInterfaces.forEach(networkInterface -> { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 130 | VlanId vlanId = networkInterface.vlan(); |
| 131 | ConnectPoint connectPoint = networkInterface.connectPoint(); |
| 132 | DeviceId dpid = connectPoint.deviceId(); |
| 133 | PortNumber port = connectPoint.port(); |
| 134 | SegmentRouterInfo info = deviceConfigMap.get(dpid); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 135 | |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 136 | // skip if there is no corresponding device for this ConenctPoint |
| 137 | if (info != null) { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 138 | // Extract subnet information |
Jonathan Hart | 8fa9ec5 | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 139 | List<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddressesList(); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 140 | interfaceAddresses.forEach(interfaceAddress -> { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 141 | // Do not add /0 and /32 to gateway IP list |
| 142 | int prefixLength = interfaceAddress.subnetAddress().prefixLength(); |
| 143 | if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET_MASK_LENGTH) { |
| 144 | info.gatewayIps.put(port, interfaceAddress.ipAddress().getIp4Address()); |
| 145 | } |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 146 | info.subnets.put(port, interfaceAddress.subnetAddress().getIp4Prefix()); |
| 147 | }); |
| 148 | } |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 149 | }); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 150 | }); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 151 | } |
| 152 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 153 | @Override |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 154 | public boolean isConfigured(DeviceId deviceId) { |
| 155 | return deviceConfigMap.get(deviceId) != null; |
| 156 | } |
| 157 | |
| 158 | @Override |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 159 | public int getIPv4SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 160 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 161 | if (srinfo != null) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 162 | log.trace("getIPv4SegmentId for device{} is {}", deviceId, srinfo.ipv4NodeSid); |
| 163 | return srinfo.ipv4NodeSid; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 164 | } else { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 165 | String message = "getIPv4SegmentId fails for device: " + deviceId + "."; |
| 166 | throw new DeviceConfigNotFoundException(message); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public int getIPv6SegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException { |
| 172 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 173 | if (srinfo != null) { |
| 174 | log.trace("getIPv6SegmentId for device{} is {}", deviceId, srinfo.ipv6NodeSid); |
| 175 | return srinfo.ipv6NodeSid; |
| 176 | } else { |
| 177 | String message = "getIPv6SegmentId fails for device: " + deviceId + "."; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 178 | throw new DeviceConfigNotFoundException(message); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 182 | /** |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 183 | * Returns the IPv4 Node segment id of a segment router given its Router mac address. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 184 | * |
| 185 | * @param routerMac router mac address |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 186 | * @return node segment id, or -1 if not found in config |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 187 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 188 | public int getIPv4SegmentId(MacAddress routerMac) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 189 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 190 | deviceConfigMap.entrySet()) { |
| 191 | if (entry.getValue().mac.equals(routerMac)) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 192 | return entry.getValue().ipv4NodeSid; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 195 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | /** |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 200 | * Returns the IPv6 Node segment id of a segment router given its Router mac address. |
| 201 | * |
| 202 | * @param routerMac router mac address |
| 203 | * @return node segment id, or -1 if not found in config |
| 204 | */ |
| 205 | public int getIPv6SegmentId(MacAddress routerMac) { |
| 206 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 207 | deviceConfigMap.entrySet()) { |
| 208 | if (entry.getValue().mac.equals(routerMac)) { |
| 209 | return entry.getValue().ipv6NodeSid; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Returns the IPv4 Node segment id of a segment router given its Router ip address. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 218 | * |
| 219 | * @param routerAddress router ip address |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 220 | * @return node segment id, or -1 if not found in config |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 221 | */ |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 222 | public int getIPv4SegmentId(Ip4Address routerAddress) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 223 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 224 | deviceConfigMap.entrySet()) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 225 | if (entry.getValue().ipv4Loopback.equals(routerAddress)) { |
| 226 | return entry.getValue().ipv4NodeSid; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Returns the IPv6 Node segment id of a segment router given its Router ip address. |
| 235 | * |
| 236 | * @param routerAddress router ip address |
| 237 | * @return node segment id, or -1 if not found in config |
| 238 | */ |
| 239 | public int getIPv6SegmentId(Ip6Address routerAddress) { |
| 240 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 241 | deviceConfigMap.entrySet()) { |
| 242 | if (entry.getValue().ipv6Loopback.equals(routerAddress)) { |
| 243 | return entry.getValue().ipv6NodeSid; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | return -1; |
| 248 | } |
| 249 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 250 | @Override |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 251 | public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 252 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 253 | if (srinfo != null) { |
| 254 | log.trace("getDeviceMac for device{} is {}", deviceId, srinfo.mac); |
| 255 | return srinfo.mac; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 256 | } else { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 257 | String message = "getDeviceMac fails for device: " + deviceId + "."; |
| 258 | throw new DeviceConfigNotFoundException(message); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 262 | @Override |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 263 | public Ip4Address getRouterIpv4(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 264 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 265 | if (srinfo != null) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 266 | log.trace("getRouterIpv4 for device{} is {}", deviceId, srinfo.ipv4Loopback); |
| 267 | return srinfo.ipv4Loopback; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 268 | } else { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 269 | String message = "getRouterIpv4 fails for device: " + deviceId + "."; |
| 270 | throw new DeviceConfigNotFoundException(message); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public Ip6Address getRouterIpv6(DeviceId deviceId) throws DeviceConfigNotFoundException { |
| 276 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 277 | if (srinfo != null) { |
| 278 | log.trace("getRouterIpv6 for device{} is {}", deviceId, srinfo.ipv6Loopback); |
| 279 | return srinfo.ipv6Loopback; |
| 280 | } else { |
| 281 | String message = "getRouterIpv6 fails for device: " + deviceId + "."; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 282 | throw new DeviceConfigNotFoundException(message); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 283 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 286 | @Override |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 287 | public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 288 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 289 | if (srinfo != null) { |
| 290 | log.trace("isEdgeDevice for device{} is {}", deviceId, srinfo.isEdge); |
| 291 | return srinfo.isEdge; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 292 | } else { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 293 | String message = "isEdgeDevice fails for device: " + deviceId + "."; |
| 294 | throw new DeviceConfigNotFoundException(message); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 298 | @Override |
| 299 | public List<Integer> getAllDeviceSegmentIds() { |
| 300 | return allSegmentIds; |
| 301 | } |
| 302 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 303 | @Override |
Saurav Das | 52d4ed7 | 2016-03-28 19:00:18 -0700 | [diff] [blame] | 304 | public Map<Ip4Prefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId) |
| 305 | throws DeviceConfigNotFoundException { |
| 306 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 307 | if (srinfo == null) { |
| 308 | String message = "getSubnetPortsMap fails for device: " + deviceId + "."; |
| 309 | throw new DeviceConfigNotFoundException(message); |
| 310 | } |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 311 | // Construct subnet-port mapping from port-subnet mapping |
Saurav Das | 52d4ed7 | 2016-03-28 19:00:18 -0700 | [diff] [blame] | 312 | SetMultimap<PortNumber, Ip4Prefix> portSubnetMap = srinfo.subnets; |
| 313 | Map<Ip4Prefix, List<PortNumber>> subnetPortMap = new HashMap<>(); |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 314 | |
| 315 | portSubnetMap.entries().forEach(entry -> { |
| 316 | PortNumber port = entry.getKey(); |
| 317 | Ip4Prefix subnet = entry.getValue(); |
| 318 | |
Charles Chan | bbd004c | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 319 | if (subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) { |
| 320 | return; |
| 321 | } |
| 322 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 323 | if (subnetPortMap.containsKey(subnet)) { |
| 324 | subnetPortMap.get(subnet).add(port); |
| 325 | } else { |
| 326 | ArrayList<PortNumber> ports = new ArrayList<>(); |
| 327 | ports.add(port); |
| 328 | subnetPortMap.put(subnet, ports); |
| 329 | } |
| 330 | }); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 331 | return subnetPortMap; |
| 332 | } |
| 333 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 334 | /** |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 335 | * Returns the device identifier or data plane identifier (dpid) |
| 336 | * of a segment router given its segment id. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 337 | * |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 338 | * @param sid segment id |
| 339 | * @return deviceId device identifier |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 340 | */ |
| 341 | public DeviceId getDeviceId(int sid) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 342 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 343 | deviceConfigMap.entrySet()) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 344 | if (entry.getValue().ipv4NodeSid == sid || |
| 345 | entry.getValue().ipv6NodeSid == sid) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 346 | return entry.getValue().deviceId; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | return null; |
| 351 | } |
| 352 | |
| 353 | /** |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 354 | * Returns the device identifier or data plane identifier (dpid) |
| 355 | * of a segment router given its router ip address. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 356 | * |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 357 | * @param ipAddress router ip address |
| 358 | * @return deviceId device identifier |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 359 | */ |
| 360 | public DeviceId getDeviceId(Ip4Address ipAddress) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 361 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 362 | deviceConfigMap.entrySet()) { |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 363 | if (entry.getValue().ipv4Loopback.equals(ipAddress)) { |
| 364 | return entry.getValue().deviceId; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return null; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Returns the device identifier or data plane identifier (dpid) |
| 373 | * of a segment router given its router ipv6 address. |
| 374 | * |
| 375 | * @param ipAddress router ipv6 address |
| 376 | * @return deviceId device identifier |
| 377 | */ |
| 378 | public DeviceId getDeviceId(Ip6Address ipAddress) { |
| 379 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 380 | deviceConfigMap.entrySet()) { |
| 381 | if (entry.getValue().ipv6Loopback.equals(ipAddress)) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 382 | return entry.getValue().deviceId; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | return null; |
| 387 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 388 | |
| 389 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 390 | * Returns the configured port ip addresses for a segment router. |
| 391 | * These addresses serve as gateway IP addresses for the subnets configured |
| 392 | * on those ports. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 393 | * |
| 394 | * @param deviceId device identifier |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 395 | * @return immutable set of ip addresses configured on the ports or null if not found |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 396 | */ |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 397 | public Set<Ip4Address> getPortIPs(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 398 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 399 | if (srinfo != null) { |
| 400 | log.trace("getSubnetGatewayIps for device{} is {}", deviceId, |
| 401 | srinfo.gatewayIps.values()); |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 402 | return ImmutableSet.copyOf(srinfo.gatewayIps.values()); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 403 | } |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 404 | return null; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Returns the configured subnet prefixes for a segment router. |
| 409 | * |
| 410 | * @param deviceId device identifier |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 411 | * @return list of ip prefixes or null if not found |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 412 | */ |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 413 | public Set<Ip4Prefix> getSubnets(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 414 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 415 | if (srinfo != null) { |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 416 | ImmutableSet.Builder<Ip4Prefix> builder = ImmutableSet.builder(); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 417 | return builder.addAll(srinfo.subnets.values()).build(); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 418 | } |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 419 | return null; |
| 420 | } |
| 421 | |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 422 | |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 423 | /** |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 424 | * Returns the subnet configuration of given device and port. |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 425 | * |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 426 | * @param deviceId Device ID |
| 427 | * @param port Port number |
| 428 | * @return The subnet configured on given port or null if |
| 429 | * the port is unconfigured, misconfigured or suppressed. |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 430 | */ |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 431 | public Ip4Prefix getPortSubnet(DeviceId deviceId, PortNumber port) { |
| 432 | ConnectPoint connectPoint = new ConnectPoint(deviceId, port); |
| 433 | |
| 434 | if (isSuppressedPort(connectPoint)) { |
| 435 | return null; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 436 | } |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 437 | |
| 438 | Set<Ip4Prefix> subnets = |
| 439 | srManager.interfaceService.getInterfacesByPort(connectPoint).stream() |
| 440 | .flatMap(intf -> intf.ipAddressesList().stream()) |
| 441 | .map(InterfaceIpAddress::subnetAddress) |
| 442 | .map(IpPrefix::getIp4Prefix) |
| 443 | .collect(Collectors.toSet()); |
| 444 | |
Jon Hall | 31d8478 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 445 | if (subnets.isEmpty()) { |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 446 | log.info(NO_SUBNET, connectPoint); |
| 447 | return null; |
| 448 | } else if (subnets.size() > 1) { |
| 449 | log.warn(TOO_MANY_SUBNET, connectPoint); |
| 450 | return null; |
| 451 | } else { |
| 452 | return subnets.stream().findFirst().orElse(null); |
| 453 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Returns the router ip address of segment router that has the |
| 458 | * specified ip address in its subnets. |
| 459 | * |
| 460 | * @param destIpAddress target ip address |
| 461 | * @return router ip address |
| 462 | */ |
| 463 | public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) { |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 464 | Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress); |
| 465 | |
| 466 | if (matchIntf == null) { |
| 467 | log.debug("No router was found for {}", destIpAddress); |
| 468 | return null; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Charles Chan | 7066136 | 2016-12-09 12:54:49 -0800 | [diff] [blame] | 471 | DeviceId routerDeviceId = matchIntf.connectPoint().deviceId(); |
| 472 | SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId); |
| 473 | if (srInfo == null) { |
| 474 | log.debug("No device config was found for {}", routerDeviceId); |
| 475 | return null; |
| 476 | } |
| 477 | |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame^] | 478 | return srInfo.ipv4Loopback; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Returns the router ipv6 address of segment router that has the |
| 483 | * specified ip address in its subnets. |
| 484 | * |
| 485 | * @param destIpAddress target ip address |
| 486 | * @return router ip address |
| 487 | */ |
| 488 | public Ip6Address getRouterIpAddressForASubnetHost(Ip6Address destIpAddress) { |
| 489 | Interface matchIntf = srManager.interfaceService.getMatchingInterface(destIpAddress); |
| 490 | |
| 491 | if (matchIntf == null) { |
| 492 | log.debug("No router was found for {}", destIpAddress); |
| 493 | return null; |
| 494 | } |
| 495 | |
| 496 | DeviceId routerDeviceId = matchIntf.connectPoint().deviceId(); |
| 497 | SegmentRouterInfo srInfo = deviceConfigMap.get(routerDeviceId); |
| 498 | if (srInfo == null) { |
| 499 | log.debug("No device config was found for {}", routerDeviceId); |
| 500 | return null; |
| 501 | } |
| 502 | |
| 503 | return srInfo.ipv6Loopback; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Returns the router mac address of segment router that has the |
| 508 | * specified ip address as one of its subnet gateway ip address. |
| 509 | * |
| 510 | * @param gatewayIpAddress router gateway ip address |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 511 | * @return router mac address or null if not found |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 512 | */ |
| 513 | public MacAddress getRouterMacForAGatewayIp(Ip4Address gatewayIpAddress) { |
| 514 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 515 | deviceConfigMap.entrySet()) { |
| 516 | if (entry.getValue().gatewayIps. |
| 517 | values().contains(gatewayIpAddress)) { |
| 518 | return entry.getValue().mac; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | log.debug("Cannot find a router for {}", gatewayIpAddress); |
| 523 | return null; |
| 524 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 525 | |
| 526 | |
| 527 | /** |
| 528 | * Checks if the host is in the subnet defined in the router with the |
| 529 | * device ID given. |
| 530 | * |
| 531 | * @param deviceId device identification of the router |
| 532 | * @param hostIp host IP address to check |
| 533 | * @return true if the host is within the subnet of the router, |
| 534 | * false if no subnet is defined under the router or if the host is not |
| 535 | * within the subnet defined in the router |
| 536 | */ |
| 537 | public boolean inSameSubnet(DeviceId deviceId, Ip4Address hostIp) { |
| 538 | |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 539 | Set<Ip4Prefix> subnets = getSubnets(deviceId); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 540 | if (subnets == null) { |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | for (Ip4Prefix subnet: subnets) { |
Charles Chan | 82ab193 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 545 | // Exclude /0 since it is a special case used for default route |
| 546 | if (subnet.prefixLength() != 0 && subnet.contains(hostIp)) { |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 547 | return true; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | return false; |
| 552 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 553 | |
| 554 | /** |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 555 | * Checks if the IP is in the subnet defined on given connect point. |
| 556 | * |
| 557 | * @param connectPoint Connect point |
| 558 | * @param ip The IP address to check |
| 559 | * @return True if the IP belongs to the subnet. |
| 560 | * False if the IP does not belong to the subnet, or |
| 561 | * there is no subnet configuration on given connect point. |
| 562 | */ |
| 563 | public boolean inSameSubnet(ConnectPoint connectPoint, IpAddress ip) { |
| 564 | Ip4Prefix portSubnet = getPortSubnet(connectPoint.deviceId(), connectPoint.port()); |
| 565 | return portSubnet != null && portSubnet.contains(ip); |
| 566 | } |
| 567 | |
| 568 | /** |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 569 | * Returns the ports corresponding to the adjacency Sid given. |
| 570 | * |
| 571 | * @param deviceId device identification of the router |
| 572 | * @param sid adjacency Sid |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 573 | * @return set of port numbers |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 574 | */ |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 575 | public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 576 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 577 | return srinfo != null ? |
| 578 | ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) : |
| 579 | ImmutableSet.copyOf(new HashSet<>()); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /** |
| 583 | * Check if the Sid given is whether adjacency Sid of the router device or not. |
| 584 | * |
| 585 | * @param deviceId device identification of the router |
| 586 | * @param sid Sid to check |
| 587 | * @return true if the Sid given is the adjacency Sid of the device, |
| 588 | * otherwise false |
| 589 | */ |
| 590 | public boolean isAdjacencySid(DeviceId deviceId, int sid) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 591 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 592 | return srinfo != null && srinfo.adjacencySids.containsKey(sid); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 593 | } |
Charles Chan | 43547ca | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 594 | |
Charles Chan | c91c878 | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 595 | /** |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 596 | * Add subnet to specific connect point. |
| 597 | * |
| 598 | * @param cp connect point |
| 599 | * @param ip4Prefix subnet being added to the device |
| 600 | */ |
| 601 | public void addSubnet(ConnectPoint cp, Ip4Prefix ip4Prefix) { |
| 602 | checkNotNull(cp); |
| 603 | checkNotNull(ip4Prefix); |
| 604 | SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId()); |
| 605 | if (srinfo == null) { |
| 606 | log.warn("Device {} is not configured. Abort.", cp.deviceId()); |
| 607 | return; |
| 608 | } |
| 609 | srinfo.subnets.put(cp.port(), ip4Prefix); |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Remove subnet from specific connect point. |
| 614 | * |
| 615 | * @param cp connect point |
| 616 | * @param ip4Prefix subnet being removed to the device |
| 617 | */ |
| 618 | public void removeSubnet(ConnectPoint cp, Ip4Prefix ip4Prefix) { |
| 619 | checkNotNull(cp); |
| 620 | checkNotNull(ip4Prefix); |
| 621 | SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId()); |
| 622 | if (srinfo == null) { |
| 623 | log.warn("Device {} is not configured. Abort.", cp.deviceId()); |
| 624 | return; |
| 625 | } |
| 626 | srinfo.subnets.remove(cp.port(), ip4Prefix); |
| 627 | } |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 628 | |
| 629 | private boolean isSuppressedPort(ConnectPoint connectPoint) { |
| 630 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
| 631 | .getConfig(srManager.appId, SegmentRoutingAppConfig.class); |
| 632 | if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) { |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 633 | log.info("Interface configuration on port {} is ignored", connectPoint); |
Charles Chan | 46fdfaf | 2016-11-09 20:51:44 -0800 | [diff] [blame] | 634 | return true; |
| 635 | } |
| 636 | return false; |
| 637 | } |
Jonathan Hart | 8fa9ec5 | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 638 | } |