Thomas Vachuska | 8fd2505 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 19 | import org.onlab.packet.Ip4Address; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 20 | import org.onlab.packet.Ip4Prefix; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 21 | import org.onlab.packet.MacAddress; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 22 | import org.onlab.packet.VlanId; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 23 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 24 | import org.onosproject.incubator.net.config.basics.InterfaceConfig; |
| 25 | import org.onosproject.incubator.net.intf.Interface; |
| 26 | import org.onosproject.net.ConnectPoint; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 27 | import org.onosproject.net.config.NetworkConfigRegistry; |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 28 | import org.onosproject.net.host.InterfaceIpAddress; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 30 | import org.onosproject.net.PortNumber; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 34 | import java.util.ArrayList; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 35 | import java.util.HashMap; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 36 | import java.util.HashSet; |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 37 | import java.util.LinkedList; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 38 | import java.util.List; |
| 39 | import java.util.Map; |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 40 | import java.util.Set; |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 41 | import java.util.concurrent.ConcurrentHashMap; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 43 | /** |
| 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 Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 47 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 48 | public class DeviceConfiguration implements DeviceProperties { |
| 49 | |
| 50 | private static final Logger log = LoggerFactory |
| 51 | .getLogger(DeviceConfiguration.class); |
Sho SHIMIZU | 47b8aa2 | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 52 | private final List<Integer> allSegmentIds = new ArrayList<>(); |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 53 | private final Map<DeviceId, SegmentRouterInfo> deviceConfigMap = new ConcurrentHashMap<>(); |
| 54 | private final Map<VlanId, List<ConnectPoint>> xConnects = new ConcurrentHashMap<>(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 55 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 56 | 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 Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 64 | Map<Integer, Set<Integer>> adjacencySids; |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 65 | |
| 66 | public SegmentRouterInfo() { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 67 | gatewayIps = new HashMap<>(); |
| 68 | subnets = new HashMap<>(); |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 69 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 70 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 71 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 72 | /** |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 73 | * Constructs device configuration for all Segment Router devices, |
| 74 | * organizing the data into various maps for easier access. |
Brian O'Connor | 65eeb57 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 75 | * |
| 76 | * @param cfgService config service |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 77 | */ |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 78 | public DeviceConfiguration(NetworkConfigRegistry cfgService) { |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 79 | // Read config from device subject, excluding gatewayIps and subnets. |
| 80 | Set<DeviceId> deviceSubjects = |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 81 | cfgService.getSubjects(DeviceId.class, SegmentRoutingConfig.class); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 82 | deviceSubjects.forEach(subject -> { |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 83 | SegmentRoutingConfig config = |
| 84 | cfgService.getConfig(subject, SegmentRoutingConfig.class); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 85 | SegmentRouterInfo info = new SegmentRouterInfo(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 86 | info.deviceId = subject; |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 87 | info.nodeSid = config.nodeSid(); |
| 88 | info.ip = config.routerIp(); |
| 89 | info.mac = config.routerMac(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 90 | info.isEdge = config.isEdgeRouter(); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 91 | info.adjacencySids = config.adjacencySids(); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 92 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 93 | deviceConfigMap.put(info.deviceId, info); |
| 94 | allSegmentIds.add(info.nodeSid); |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 95 | }); |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 96 | |
| 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 Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 111 | 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 Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 116 | |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 117 | // skip if there is no corresponding device for this ConenctPoint |
| 118 | if (info != null) { |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 119 | // Extract subnet information |
Charles Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 120 | 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 Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 125 | |
| 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 Chan | 2b078ae | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 147 | } |
Charles Chan | e7c6102 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 148 | }); |
| 149 | |
| 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 |
| 159 | public int getSegmentId(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) { |
| 162 | log.trace("getSegmentId for device{} is {}", deviceId, srinfo.nodeSid); |
| 163 | return srinfo.nodeSid; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 164 | } else { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 165 | String message = "getSegmentId fails for device: " + deviceId + "."; |
| 166 | throw new DeviceConfigNotFoundException(message); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 170 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 171 | * Returns the Node segment id of a segment router given its Router mac address. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 172 | * |
| 173 | * @param routerMac router mac address |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 174 | * @return node segment id, or -1 if not found in config |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 175 | */ |
| 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 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 183 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 188 | * Returns the Node segment id of a segment router given its Router ip address. |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 189 | * |
| 190 | * @param routerAddress router ip address |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 191 | * @return node segment id, or -1 if not found in config |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 192 | */ |
| 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 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 204 | @Override |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 205 | public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 206 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 207 | if (srinfo != null) { |
| 208 | log.trace("getDeviceMac for device{} is {}", deviceId, srinfo.mac); |
| 209 | return srinfo.mac; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 210 | } else { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 211 | String message = "getDeviceMac fails for device: " + deviceId + "."; |
| 212 | throw new DeviceConfigNotFoundException(message); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 216 | @Override |
| 217 | public Ip4Address getRouterIp(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 218 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 219 | if (srinfo != null) { |
| 220 | log.trace("getDeviceIp for device{} is {}", deviceId, srinfo.ip); |
| 221 | return srinfo.ip; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 222 | } else { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 223 | String message = "getRouterIp fails for device: " + deviceId + "."; |
| 224 | throw new DeviceConfigNotFoundException(message); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 225 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 228 | @Override |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 229 | public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 230 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 231 | if (srinfo != null) { |
| 232 | log.trace("isEdgeDevice for device{} is {}", deviceId, srinfo.isEdge); |
| 233 | return srinfo.isEdge; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 234 | } else { |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 235 | String message = "isEdgeDevice fails for device: " + deviceId + "."; |
| 236 | throw new DeviceConfigNotFoundException(message); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 240 | @Override |
| 241 | public List<Integer> getAllDeviceSegmentIds() { |
| 242 | return allSegmentIds; |
| 243 | } |
| 244 | |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 245 | @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 Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 265 | @Override |
| 266 | public Map<VlanId, List<ConnectPoint>> getXConnects() { |
| 267 | return xConnects; |
| 268 | } |
| 269 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 270 | /** |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 271 | * Returns the device identifier or data plane identifier (dpid) |
| 272 | * of a segment router given its segment id. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 273 | * |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 274 | * @param sid segment id |
| 275 | * @return deviceId device identifier |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 276 | */ |
| 277 | public DeviceId getDeviceId(int sid) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 278 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 279 | deviceConfigMap.entrySet()) { |
| 280 | if (entry.getValue().nodeSid == sid) { |
| 281 | return entry.getValue().deviceId; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | return null; |
| 286 | } |
| 287 | |
| 288 | /** |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 289 | * Returns the device identifier or data plane identifier (dpid) |
| 290 | * of a segment router given its router ip address. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 291 | * |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 292 | * @param ipAddress router ip address |
| 293 | * @return deviceId device identifier |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 294 | */ |
| 295 | public DeviceId getDeviceId(Ip4Address ipAddress) { |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 296 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 297 | deviceConfigMap.entrySet()) { |
| 298 | if (entry.getValue().ip.equals(ipAddress)) { |
| 299 | return entry.getValue().deviceId; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | return null; |
| 304 | } |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 305 | |
| 306 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 307 | * 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 Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 310 | * |
| 311 | * @param deviceId device identifier |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 312 | * @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] | 313 | */ |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 314 | public Set<Ip4Address> getPortIPs(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 315 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 316 | if (srinfo != null) { |
| 317 | log.trace("getSubnetGatewayIps for device{} is {}", deviceId, |
| 318 | srinfo.gatewayIps.values()); |
Saurav Das | c28b343 | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 319 | return ImmutableSet.copyOf(srinfo.gatewayIps.values()); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 320 | } |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 321 | return null; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 325 | * Returns the configured IP addresses per port |
| 326 | * for a segment router. |
| 327 | * |
| 328 | * @param deviceId device identifier |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 329 | * @return map of port to gateway IP addresses or null if not found |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 330 | */ |
| 331 | public Map<PortNumber, Ip4Address> getPortIPMap(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 332 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 333 | if (srinfo != null) { |
| 334 | return srinfo.gatewayIps; |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 335 | } |
| 336 | return null; |
| 337 | } |
| 338 | |
| 339 | /** |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 340 | * Returns the configured subnet prefixes for a segment router. |
| 341 | * |
| 342 | * @param deviceId device identifier |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 343 | * @return list of ip prefixes or null if not found |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 344 | */ |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 345 | public Set<Ip4Prefix> getSubnets(DeviceId deviceId) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 346 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 347 | if (srinfo != null) { |
| 348 | log.trace("getSubnets for device{} is {}", deviceId, |
| 349 | srinfo.subnets.values()); |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 350 | return ImmutableSet.copyOf(srinfo.subnets.values()); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 351 | } |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 352 | 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 Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 369 | } |
| 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 Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 397 | * @return router mac address or null if not found |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 398 | */ |
| 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 | } |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 411 | |
| 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 Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 425 | Set<Ip4Prefix> subnets = getSubnets(deviceId); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 426 | 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 | } |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 438 | |
| 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 Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 444 | * @return set of port numbers |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 445 | */ |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 446 | public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) { |
Saurav Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 447 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 448 | return srinfo != null ? |
| 449 | ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) : |
| 450 | ImmutableSet.copyOf(new HashSet<>()); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 451 | } |
| 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 Das | 7c30537 | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 462 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Charles Chan | 9bec8a3 | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 463 | return srinfo != null && srinfo.adjacencySids.containsKey(sid); |
sangho | 27462c6 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 464 | } |
Charles Chan | 72f556a | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 465 | } |