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