Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 58de416 | 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 | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 16 | package org.onosproject.segmentrouting.config; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 17 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 18 | import com.google.common.collect.HashMultimap; |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 20 | import com.google.common.collect.SetMultimap; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 21 | import org.onlab.packet.Ip4Address; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 22 | import org.onlab.packet.Ip4Prefix; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 23 | import org.onlab.packet.IpPrefix; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 24 | import org.onlab.packet.MacAddress; |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 25 | import org.onlab.packet.VlanId; |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 26 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 27 | import org.onosproject.incubator.net.config.basics.InterfaceConfig; |
| 28 | import org.onosproject.incubator.net.intf.Interface; |
| 29 | import org.onosproject.net.ConnectPoint; |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 30 | import org.onosproject.net.config.NetworkConfigRegistry; |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 31 | import org.onosproject.net.host.InterfaceIpAddress; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 33 | import org.onosproject.net.PortNumber; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 38 | import java.util.HashMap; |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 39 | import java.util.HashSet; |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 40 | import java.util.LinkedList; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 41 | import java.util.List; |
| 42 | import java.util.Map; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 43 | import java.util.Optional; |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 44 | import java.util.Set; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 45 | import java.util.concurrent.ConcurrentHashMap; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 46 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Segment Routing configuration component that reads the |
| 49 | * segment routing related configuration from Network Configuration Manager |
| 50 | * component and organizes in more accessible formats. |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 51 | */ |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 52 | public class DeviceConfiguration implements DeviceProperties { |
| 53 | |
| 54 | private static final Logger log = LoggerFactory |
| 55 | .getLogger(DeviceConfiguration.class); |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 56 | private final List<Integer> allSegmentIds = new ArrayList<>(); |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 57 | private final Map<DeviceId, SegmentRouterInfo> deviceConfigMap = new ConcurrentHashMap<>(); |
| 58 | private final Map<VlanId, List<ConnectPoint>> xConnects = new ConcurrentHashMap<>(); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 59 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 60 | private class SegmentRouterInfo { |
| 61 | int nodeSid; |
| 62 | DeviceId deviceId; |
| 63 | Ip4Address ip; |
| 64 | MacAddress mac; |
| 65 | boolean isEdge; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 66 | Map<PortNumber, Ip4Address> gatewayIps; |
| 67 | SetMultimap<PortNumber, Ip4Prefix> subnets; |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 68 | Map<Integer, Set<Integer>> adjacencySids; |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 69 | |
| 70 | public SegmentRouterInfo() { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 71 | gatewayIps = new HashMap<>(); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 72 | subnets = HashMultimap.create(); |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 73 | } |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 74 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 75 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 76 | /** |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 77 | * Constructs device configuration for all Segment Router devices, |
| 78 | * organizing the data into various maps for easier access. |
Brian O'Connor | 5251562 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 79 | * |
| 80 | * @param cfgService config service |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 81 | */ |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 82 | public DeviceConfiguration(NetworkConfigRegistry cfgService) { |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 83 | // Read config from device subject, excluding gatewayIps and subnets. |
| 84 | Set<DeviceId> deviceSubjects = |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 85 | cfgService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class); |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 86 | deviceSubjects.forEach(subject -> { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 87 | SegmentRoutingDeviceConfig config = |
| 88 | cfgService.getConfig(subject, SegmentRoutingDeviceConfig.class); |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 89 | SegmentRouterInfo info = new SegmentRouterInfo(); |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 90 | info.deviceId = subject; |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 91 | info.nodeSid = config.nodeSid(); |
| 92 | info.ip = config.routerIp(); |
| 93 | info.mac = config.routerMac(); |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 94 | info.isEdge = config.isEdgeRouter(); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 95 | info.adjacencySids = config.adjacencySids(); |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 96 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 97 | deviceConfigMap.put(info.deviceId, info); |
| 98 | allSegmentIds.add(info.nodeSid); |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 99 | }); |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 100 | |
| 101 | // Read gatewayIps and subnets from port subject. |
| 102 | Set<ConnectPoint> portSubjects = |
| 103 | cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class); |
| 104 | portSubjects.forEach(subject -> { |
| 105 | InterfaceConfig config = |
| 106 | cfgService.getConfig(subject, InterfaceConfig.class); |
| 107 | Set<Interface> networkInterfaces; |
| 108 | try { |
| 109 | networkInterfaces = config.getInterfaces(); |
| 110 | } catch (ConfigException e) { |
| 111 | log.error("Error loading port configuration"); |
| 112 | return; |
| 113 | } |
| 114 | networkInterfaces.forEach(networkInterface -> { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 115 | VlanId vlanId = networkInterface.vlan(); |
| 116 | ConnectPoint connectPoint = networkInterface.connectPoint(); |
| 117 | DeviceId dpid = connectPoint.deviceId(); |
| 118 | PortNumber port = connectPoint.port(); |
| 119 | SegmentRouterInfo info = deviceConfigMap.get(dpid); |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 120 | |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 121 | // skip if there is no corresponding device for this ConenctPoint |
| 122 | if (info != null) { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 123 | // Extract subnet information |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 124 | Set<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddresses(); |
| 125 | interfaceAddresses.forEach(interfaceAddress -> { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 126 | // Do not add /0 and /32 to gateway IP list |
| 127 | int prefixLength = interfaceAddress.subnetAddress().prefixLength(); |
| 128 | if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET_MASK_LENGTH) { |
| 129 | info.gatewayIps.put(port, interfaceAddress.ipAddress().getIp4Address()); |
| 130 | } |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 131 | info.subnets.put(port, interfaceAddress.subnetAddress().getIp4Prefix()); |
| 132 | }); |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 133 | |
| 134 | // Extract VLAN cross-connect information |
| 135 | // Do not setup cross-connect if VLAN is NONE |
| 136 | if (vlanId.equals(VlanId.NONE)) { |
| 137 | return; |
| 138 | } |
| 139 | List<ConnectPoint> connectPoints = xConnects.get(vlanId); |
| 140 | if (connectPoints != null) { |
| 141 | if (connectPoints.size() != 1) { |
| 142 | log.warn("Cross-connect should only have two endpoints. Aborting."); |
| 143 | return; |
| 144 | } |
| 145 | if (!connectPoints.get(0).deviceId().equals(connectPoint.deviceId())) { |
| 146 | log.warn("Cross-connect endpoints must be on the same switch. Aborting."); |
| 147 | return; |
| 148 | } |
| 149 | connectPoints.add(connectPoint); |
| 150 | } else { |
| 151 | connectPoints = new LinkedList<>(); |
| 152 | connectPoints.add(connectPoint); |
| 153 | xConnects.put(vlanId, connectPoints); |
| 154 | } |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 155 | } |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 156 | }); |
| 157 | |
| 158 | }); |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 159 | } |
| 160 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 161 | @Override |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 162 | public boolean isConfigured(DeviceId deviceId) { |
| 163 | return deviceConfigMap.get(deviceId) != null; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public int getSegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 168 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 169 | if (srinfo != null) { |
| 170 | log.trace("getSegmentId for device{} is {}", deviceId, srinfo.nodeSid); |
| 171 | return srinfo.nodeSid; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 172 | } else { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 173 | String message = "getSegmentId fails for device: " + deviceId + "."; |
| 174 | throw new DeviceConfigNotFoundException(message); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 178 | /** |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 179 | * Returns the Node segment id of a segment router given its Router mac address. |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 180 | * |
| 181 | * @param routerMac router mac address |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 182 | * @return node segment id, or -1 if not found in config |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 183 | */ |
| 184 | public int getSegmentId(MacAddress routerMac) { |
| 185 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 186 | deviceConfigMap.entrySet()) { |
| 187 | if (entry.getValue().mac.equals(routerMac)) { |
| 188 | return entry.getValue().nodeSid; |
| 189 | } |
| 190 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 191 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 192 | return -1; |
| 193 | } |
| 194 | |
| 195 | /** |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 196 | * Returns the Node segment id of a segment router given its Router ip address. |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 197 | * |
| 198 | * @param routerAddress router ip address |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 199 | * @return node segment id, or -1 if not found in config |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 200 | */ |
| 201 | public int getSegmentId(Ip4Address routerAddress) { |
| 202 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 203 | deviceConfigMap.entrySet()) { |
| 204 | if (entry.getValue().ip.equals(routerAddress)) { |
| 205 | return entry.getValue().nodeSid; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return -1; |
| 210 | } |
| 211 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 212 | @Override |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 213 | public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 214 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 215 | if (srinfo != null) { |
| 216 | log.trace("getDeviceMac for device{} is {}", deviceId, srinfo.mac); |
| 217 | return srinfo.mac; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 218 | } else { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 219 | String message = "getDeviceMac fails for device: " + deviceId + "."; |
| 220 | throw new DeviceConfigNotFoundException(message); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 224 | @Override |
| 225 | public Ip4Address getRouterIp(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 226 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 227 | if (srinfo != null) { |
| 228 | log.trace("getDeviceIp for device{} is {}", deviceId, srinfo.ip); |
| 229 | return srinfo.ip; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 230 | } else { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 231 | String message = "getRouterIp fails for device: " + deviceId + "."; |
| 232 | throw new DeviceConfigNotFoundException(message); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 233 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 236 | @Override |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 237 | public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 238 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 239 | if (srinfo != null) { |
| 240 | log.trace("isEdgeDevice for device{} is {}", deviceId, srinfo.isEdge); |
| 241 | return srinfo.isEdge; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 242 | } else { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 243 | String message = "isEdgeDevice fails for device: " + deviceId + "."; |
| 244 | throw new DeviceConfigNotFoundException(message); |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 248 | @Override |
| 249 | public List<Integer> getAllDeviceSegmentIds() { |
| 250 | return allSegmentIds; |
| 251 | } |
| 252 | |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 253 | @Override |
| 254 | public Map<Ip4Prefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId) { |
| 255 | Map<Ip4Prefix, List<PortNumber>> subnetPortMap = new HashMap<>(); |
| 256 | |
| 257 | // Construct subnet-port mapping from port-subnet mapping |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 258 | SetMultimap<PortNumber, Ip4Prefix> portSubnetMap = |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 259 | this.deviceConfigMap.get(deviceId).subnets; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 260 | |
| 261 | portSubnetMap.entries().forEach(entry -> { |
| 262 | PortNumber port = entry.getKey(); |
| 263 | Ip4Prefix subnet = entry.getValue(); |
| 264 | |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 265 | if (subnetPortMap.containsKey(subnet)) { |
| 266 | subnetPortMap.get(subnet).add(port); |
| 267 | } else { |
| 268 | ArrayList<PortNumber> ports = new ArrayList<>(); |
| 269 | ports.add(port); |
| 270 | subnetPortMap.put(subnet, ports); |
| 271 | } |
| 272 | }); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 273 | return subnetPortMap; |
| 274 | } |
| 275 | |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 276 | @Override |
| 277 | public Map<VlanId, List<ConnectPoint>> getXConnects() { |
| 278 | return xConnects; |
| 279 | } |
| 280 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 281 | /** |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 282 | * Returns the device identifier or data plane identifier (dpid) |
| 283 | * of a segment router given its segment id. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 284 | * |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 285 | * @param sid segment id |
| 286 | * @return deviceId device identifier |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 287 | */ |
| 288 | public DeviceId getDeviceId(int sid) { |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 289 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 290 | deviceConfigMap.entrySet()) { |
| 291 | if (entry.getValue().nodeSid == sid) { |
| 292 | return entry.getValue().deviceId; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | return null; |
| 297 | } |
| 298 | |
| 299 | /** |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 300 | * Returns the device identifier or data plane identifier (dpid) |
| 301 | * of a segment router given its router ip address. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 302 | * |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 303 | * @param ipAddress router ip address |
| 304 | * @return deviceId device identifier |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 305 | */ |
| 306 | public DeviceId getDeviceId(Ip4Address ipAddress) { |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 307 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 308 | deviceConfigMap.entrySet()) { |
| 309 | if (entry.getValue().ip.equals(ipAddress)) { |
| 310 | return entry.getValue().deviceId; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | return null; |
| 315 | } |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 316 | |
| 317 | /** |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 318 | * Returns the configured port ip addresses for a segment router. |
| 319 | * These addresses serve as gateway IP addresses for the subnets configured |
| 320 | * on those ports. |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 321 | * |
| 322 | * @param deviceId device identifier |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 323 | * @return immutable set of ip addresses configured on the ports or null if not found |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 324 | */ |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 325 | public Set<Ip4Address> getPortIPs(DeviceId deviceId) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 326 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 327 | if (srinfo != null) { |
| 328 | log.trace("getSubnetGatewayIps for device{} is {}", deviceId, |
| 329 | srinfo.gatewayIps.values()); |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 330 | return ImmutableSet.copyOf(srinfo.gatewayIps.values()); |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 331 | } |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 332 | return null; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Returns the configured subnet prefixes for a segment router. |
| 337 | * |
| 338 | * @param deviceId device identifier |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 339 | * @return list of ip prefixes or null if not found |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 340 | */ |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 341 | public Set<Ip4Prefix> getSubnets(DeviceId deviceId) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 342 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 343 | if (srinfo != null) { |
| 344 | log.trace("getSubnets for device{} is {}", deviceId, |
| 345 | srinfo.subnets.values()); |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 346 | return ImmutableSet.copyOf(srinfo.subnets.values()); |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 347 | } |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 348 | return null; |
| 349 | } |
| 350 | |
| 351 | /** |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 352 | * Returns the configured non-/32 and non-/0 subnet on the given port, |
| 353 | * or null if no subnet has been configured on the port. |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 354 | * |
| 355 | * @param deviceId device identifier |
| 356 | * @param pnum port identifier |
| 357 | * @return configured subnet on port, or null |
| 358 | */ |
| 359 | public Ip4Prefix getPortSubnet(DeviceId deviceId, PortNumber pnum) { |
| 360 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 361 | if (srinfo != null) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 362 | Optional<Ip4Prefix> result = srinfo.subnets.get(pnum).stream() |
| 363 | .filter(subnet -> |
| 364 | subnet.getIp4Prefix().prefixLength() != IpPrefix.MAX_INET_MASK_LENGTH && |
| 365 | subnet.getIp4Prefix().prefixLength() != 0) |
| 366 | .findFirst(); |
| 367 | return (result.isPresent()) ? result.get() : null; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 368 | } |
| 369 | return null; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Returns the router ip address of segment router that has the |
| 374 | * specified ip address in its subnets. |
| 375 | * |
| 376 | * @param destIpAddress target ip address |
| 377 | * @return router ip address |
| 378 | */ |
| 379 | public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) { |
| 380 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 381 | deviceConfigMap.entrySet()) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 382 | for (Ip4Prefix prefix : entry.getValue().subnets.values()) { |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 383 | if (prefix.contains(destIpAddress)) { |
| 384 | return entry.getValue().ip; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | log.debug("No router was found for {}", destIpAddress); |
| 390 | return null; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Returns the router mac address of segment router that has the |
| 395 | * specified ip address as one of its subnet gateway ip address. |
| 396 | * |
| 397 | * @param gatewayIpAddress router gateway ip address |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 398 | * @return router mac address or null if not found |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 399 | */ |
| 400 | public MacAddress getRouterMacForAGatewayIp(Ip4Address gatewayIpAddress) { |
| 401 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 402 | deviceConfigMap.entrySet()) { |
| 403 | if (entry.getValue().gatewayIps. |
| 404 | values().contains(gatewayIpAddress)) { |
| 405 | return entry.getValue().mac; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | log.debug("Cannot find a router for {}", gatewayIpAddress); |
| 410 | return null; |
| 411 | } |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 412 | |
| 413 | |
| 414 | /** |
| 415 | * Checks if the host is in the subnet defined in the router with the |
| 416 | * device ID given. |
| 417 | * |
| 418 | * @param deviceId device identification of the router |
| 419 | * @param hostIp host IP address to check |
| 420 | * @return true if the host is within the subnet of the router, |
| 421 | * false if no subnet is defined under the router or if the host is not |
| 422 | * within the subnet defined in the router |
| 423 | */ |
| 424 | public boolean inSameSubnet(DeviceId deviceId, Ip4Address hostIp) { |
| 425 | |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 426 | Set<Ip4Prefix> subnets = getSubnets(deviceId); |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 427 | if (subnets == null) { |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | for (Ip4Prefix subnet: subnets) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame^] | 432 | // Exclude /0 since it is a special case used for default route |
| 433 | if (subnet.prefixLength() != 0 && subnet.contains(hostIp)) { |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 434 | return true; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return false; |
| 439 | } |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 440 | |
| 441 | /** |
| 442 | * Returns the ports corresponding to the adjacency Sid given. |
| 443 | * |
| 444 | * @param deviceId device identification of the router |
| 445 | * @param sid adjacency Sid |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 446 | * @return set of port numbers |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 447 | */ |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 448 | public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 449 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 450 | return srinfo != null ? |
| 451 | ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) : |
| 452 | ImmutableSet.copyOf(new HashSet<>()); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Check if the Sid given is whether adjacency Sid of the router device or not. |
| 457 | * |
| 458 | * @param deviceId device identification of the router |
| 459 | * @param sid Sid to check |
| 460 | * @return true if the Sid given is the adjacency Sid of the device, |
| 461 | * otherwise false |
| 462 | */ |
| 463 | public boolean isAdjacencySid(DeviceId deviceId, int sid) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 464 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 465 | return srinfo != null && srinfo.adjacencySids.containsKey(sid); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 466 | } |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 467 | } |