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