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