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