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