Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 16 | package org.onosproject.segmentrouting.config; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 17 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 20 | import com.google.common.collect.SetMultimap; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 21 | import org.onlab.packet.Ip4Address; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 22 | import org.onlab.packet.Ip4Prefix; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 23 | import org.onlab.packet.IpPrefix; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 24 | import org.onlab.packet.MacAddress; |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 25 | import org.onlab.packet.VlanId; |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 27 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 28 | import org.onosproject.incubator.net.config.basics.InterfaceConfig; |
| 29 | import org.onosproject.incubator.net.intf.Interface; |
| 30 | import org.onosproject.net.ConnectPoint; |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 31 | import org.onosproject.net.config.NetworkConfigRegistry; |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 32 | import org.onosproject.net.config.NetworkConfigService; |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 33 | import org.onosproject.net.host.InterfaceIpAddress; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 34 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 35 | import org.onosproject.net.PortNumber; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 39 | import java.util.ArrayList; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 40 | import java.util.HashMap; |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 41 | import java.util.HashSet; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | import java.util.List; |
| 43 | import java.util.Map; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 44 | import java.util.Optional; |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 45 | import java.util.Set; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 46 | import java.util.concurrent.ConcurrentHashMap; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 47 | |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 48 | import static com.google.common.base.Preconditions.checkNotNull; |
| 49 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Segment Routing configuration component that reads the |
| 52 | * segment routing related configuration from Network Configuration Manager |
| 53 | * component and organizes in more accessible formats. |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 54 | */ |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 55 | public class DeviceConfiguration implements DeviceProperties { |
| 56 | |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 57 | private static final Logger log = LoggerFactory.getLogger(DeviceConfiguration.class); |
Sho SHIMIZU | 6cfc02d | 2015-09-11 11:19:11 -0700 | [diff] [blame] | 58 | private final List<Integer> allSegmentIds = new ArrayList<>(); |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 59 | private final Map<DeviceId, SegmentRouterInfo> deviceConfigMap = new ConcurrentHashMap<>(); |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 60 | private ApplicationId appId; |
| 61 | private NetworkConfigService cfgService; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 62 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 63 | private class SegmentRouterInfo { |
| 64 | int nodeSid; |
| 65 | DeviceId deviceId; |
| 66 | Ip4Address ip; |
| 67 | MacAddress mac; |
| 68 | boolean isEdge; |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 69 | Map<PortNumber, Ip4Address> gatewayIps; |
| 70 | SetMultimap<PortNumber, Ip4Prefix> subnets; |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 71 | Map<Integer, Set<Integer>> adjacencySids; |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 72 | |
| 73 | public SegmentRouterInfo() { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 74 | gatewayIps = new HashMap<>(); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 75 | subnets = HashMultimap.create(); |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 76 | } |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 77 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 78 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 79 | /** |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 80 | * Constructs device configuration for all Segment Router devices, |
| 81 | * organizing the data into various maps for easier access. |
Brian O'Connor | 5251562 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 82 | * |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 83 | * @param appId application id |
Brian O'Connor | 5251562 | 2015-10-09 17:03:44 -0700 | [diff] [blame] | 84 | * @param cfgService config service |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 85 | */ |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 86 | public DeviceConfiguration(ApplicationId appId, |
| 87 | NetworkConfigRegistry cfgService) { |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 88 | this.appId = appId; |
| 89 | this.cfgService = cfgService; |
| 90 | |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 91 | // Read config from device subject, excluding gatewayIps and subnets. |
| 92 | Set<DeviceId> deviceSubjects = |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 93 | cfgService.getSubjects(DeviceId.class, SegmentRoutingDeviceConfig.class); |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 94 | deviceSubjects.forEach(subject -> { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 95 | SegmentRoutingDeviceConfig config = |
| 96 | cfgService.getConfig(subject, SegmentRoutingDeviceConfig.class); |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 97 | SegmentRouterInfo info = new SegmentRouterInfo(); |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 98 | info.deviceId = subject; |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 99 | info.nodeSid = config.nodeSid(); |
| 100 | info.ip = config.routerIp(); |
| 101 | info.mac = config.routerMac(); |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 102 | info.isEdge = config.isEdgeRouter(); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 103 | info.adjacencySids = config.adjacencySids(); |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 104 | deviceConfigMap.put(info.deviceId, info); |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 105 | log.info("Read device config for device: {}", info.deviceId); |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 106 | allSegmentIds.add(info.nodeSid); |
Charles Chan | d683288 | 2015-10-05 17:50:33 -0700 | [diff] [blame] | 107 | }); |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 108 | |
| 109 | // Read gatewayIps and subnets from port subject. |
| 110 | Set<ConnectPoint> portSubjects = |
| 111 | cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class); |
| 112 | portSubjects.forEach(subject -> { |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 113 | // Do not process excluded ports |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 114 | SegmentRoutingAppConfig appConfig = |
| 115 | cfgService.getConfig(appId, SegmentRoutingAppConfig.class); |
| 116 | if (appConfig != null && appConfig.suppressSubnet().contains(subject)) { |
| 117 | log.info("Ignore suppressed port {}", subject); |
Charles Chan | d9681e7 | 2016-02-22 19:27:29 -0800 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 121 | InterfaceConfig config = |
| 122 | cfgService.getConfig(subject, InterfaceConfig.class); |
| 123 | Set<Interface> networkInterfaces; |
| 124 | try { |
| 125 | networkInterfaces = config.getInterfaces(); |
| 126 | } catch (ConfigException e) { |
| 127 | log.error("Error loading port configuration"); |
| 128 | return; |
| 129 | } |
| 130 | networkInterfaces.forEach(networkInterface -> { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 131 | VlanId vlanId = networkInterface.vlan(); |
| 132 | ConnectPoint connectPoint = networkInterface.connectPoint(); |
| 133 | DeviceId dpid = connectPoint.deviceId(); |
| 134 | PortNumber port = connectPoint.port(); |
| 135 | SegmentRouterInfo info = deviceConfigMap.get(dpid); |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 136 | |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 137 | // skip if there is no corresponding device for this ConenctPoint |
| 138 | if (info != null) { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 139 | // Extract subnet information |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 140 | List<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddressesList(); |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 141 | interfaceAddresses.forEach(interfaceAddress -> { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 142 | // Do not add /0 and /32 to gateway IP list |
| 143 | int prefixLength = interfaceAddress.subnetAddress().prefixLength(); |
| 144 | if (prefixLength != 0 && prefixLength != IpPrefix.MAX_INET_MASK_LENGTH) { |
| 145 | info.gatewayIps.put(port, interfaceAddress.ipAddress().getIp4Address()); |
| 146 | } |
Charles Chan | b8e10c8 | 2015-10-14 11:24:40 -0700 | [diff] [blame] | 147 | info.subnets.put(port, interfaceAddress.subnetAddress().getIp4Prefix()); |
| 148 | }); |
| 149 | } |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 150 | }); |
Charles Chan | 4636be0 | 2015-10-07 14:21:45 -0700 | [diff] [blame] | 151 | }); |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 152 | } |
| 153 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 154 | @Override |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 155 | public boolean isConfigured(DeviceId deviceId) { |
| 156 | return deviceConfigMap.get(deviceId) != null; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public int getSegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 161 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 162 | if (srinfo != null) { |
| 163 | log.trace("getSegmentId for device{} is {}", deviceId, srinfo.nodeSid); |
| 164 | return srinfo.nodeSid; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 165 | } else { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 166 | String message = "getSegmentId fails for device: " + deviceId + "."; |
| 167 | throw new DeviceConfigNotFoundException(message); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 171 | /** |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 172 | * 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] | 173 | * |
| 174 | * @param routerMac router mac address |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 175 | * @return node segment id, or -1 if not found in config |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 176 | */ |
| 177 | public int getSegmentId(MacAddress routerMac) { |
| 178 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 179 | deviceConfigMap.entrySet()) { |
| 180 | if (entry.getValue().mac.equals(routerMac)) { |
| 181 | return entry.getValue().nodeSid; |
| 182 | } |
| 183 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 184 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | /** |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 189 | * 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] | 190 | * |
| 191 | * @param routerAddress router ip address |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 192 | * @return node segment id, or -1 if not found in config |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 193 | */ |
| 194 | public int getSegmentId(Ip4Address routerAddress) { |
| 195 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 196 | deviceConfigMap.entrySet()) { |
| 197 | if (entry.getValue().ip.equals(routerAddress)) { |
| 198 | return entry.getValue().nodeSid; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return -1; |
| 203 | } |
| 204 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 205 | @Override |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 206 | public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 207 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 208 | if (srinfo != null) { |
| 209 | log.trace("getDeviceMac for device{} is {}", deviceId, srinfo.mac); |
| 210 | return srinfo.mac; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 211 | } else { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 212 | String message = "getDeviceMac fails for device: " + deviceId + "."; |
| 213 | throw new DeviceConfigNotFoundException(message); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 217 | @Override |
| 218 | public Ip4Address getRouterIp(DeviceId deviceId) throws DeviceConfigNotFoundException { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 219 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 220 | if (srinfo != null) { |
| 221 | log.trace("getDeviceIp for device{} is {}", deviceId, srinfo.ip); |
| 222 | return srinfo.ip; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 223 | } else { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 224 | String message = "getRouterIp fails for device: " + deviceId + "."; |
| 225 | throw new DeviceConfigNotFoundException(message); |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 226 | } |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 229 | @Override |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 230 | public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException { |
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 { |
Charles Chan | 0b4e618 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 236 | String message = "isEdgeDevice fails for device: " + deviceId + "."; |
| 237 | throw new DeviceConfigNotFoundException(message); |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 241 | @Override |
| 242 | public List<Integer> getAllDeviceSegmentIds() { |
| 243 | return allSegmentIds; |
| 244 | } |
| 245 | |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 246 | @Override |
Saurav Das | 7a1ffca | 2016-03-28 19:00:18 -0700 | [diff] [blame] | 247 | public Map<Ip4Prefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId) |
| 248 | throws DeviceConfigNotFoundException { |
| 249 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 250 | if (srinfo == null) { |
| 251 | String message = "getSubnetPortsMap fails for device: " + deviceId + "."; |
| 252 | throw new DeviceConfigNotFoundException(message); |
| 253 | } |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 254 | // Construct subnet-port mapping from port-subnet mapping |
Saurav Das | 7a1ffca | 2016-03-28 19:00:18 -0700 | [diff] [blame] | 255 | SetMultimap<PortNumber, Ip4Prefix> portSubnetMap = srinfo.subnets; |
| 256 | Map<Ip4Prefix, List<PortNumber>> subnetPortMap = new HashMap<>(); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 257 | |
| 258 | portSubnetMap.entries().forEach(entry -> { |
| 259 | PortNumber port = entry.getKey(); |
| 260 | Ip4Prefix subnet = entry.getValue(); |
| 261 | |
Charles Chan | d0fd5dc | 2016-02-16 23:14:49 -0800 | [diff] [blame] | 262 | if (subnet.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH) { |
| 263 | return; |
| 264 | } |
| 265 | |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 266 | if (subnetPortMap.containsKey(subnet)) { |
| 267 | subnetPortMap.get(subnet).add(port); |
| 268 | } else { |
| 269 | ArrayList<PortNumber> ports = new ArrayList<>(); |
| 270 | ports.add(port); |
| 271 | subnetPortMap.put(subnet, ports); |
| 272 | } |
| 273 | }); |
Charles Chan | c42e84e | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 274 | return subnetPortMap; |
| 275 | } |
| 276 | |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 277 | /** |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 278 | * Returns the device identifier or data plane identifier (dpid) |
| 279 | * of a segment router given its segment id. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 280 | * |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 281 | * @param sid segment id |
| 282 | * @return deviceId device identifier |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 283 | */ |
| 284 | public DeviceId getDeviceId(int sid) { |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 285 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 286 | deviceConfigMap.entrySet()) { |
| 287 | if (entry.getValue().nodeSid == sid) { |
| 288 | return entry.getValue().deviceId; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | return null; |
| 293 | } |
| 294 | |
| 295 | /** |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 296 | * Returns the device identifier or data plane identifier (dpid) |
| 297 | * of a segment router given its router ip address. |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 298 | * |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 299 | * @param ipAddress router ip address |
| 300 | * @return deviceId device identifier |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 301 | */ |
| 302 | public DeviceId getDeviceId(Ip4Address ipAddress) { |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 303 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 304 | deviceConfigMap.entrySet()) { |
| 305 | if (entry.getValue().ip.equals(ipAddress)) { |
| 306 | return entry.getValue().deviceId; |
sangho | b35a619 | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | return null; |
| 311 | } |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 312 | |
| 313 | /** |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 314 | * Returns the configured port ip addresses for a segment router. |
| 315 | * These addresses serve as gateway IP addresses for the subnets configured |
| 316 | * on those ports. |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 317 | * |
| 318 | * @param deviceId device identifier |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 319 | * @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] | 320 | */ |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 321 | public Set<Ip4Address> getPortIPs(DeviceId deviceId) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 322 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 323 | if (srinfo != null) { |
| 324 | log.trace("getSubnetGatewayIps for device{} is {}", deviceId, |
| 325 | srinfo.gatewayIps.values()); |
Saurav Das | 837e0bb | 2015-10-30 17:45:38 -0700 | [diff] [blame] | 326 | return ImmutableSet.copyOf(srinfo.gatewayIps.values()); |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 327 | } |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 328 | return null; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Returns the configured subnet prefixes for a segment router. |
| 333 | * |
| 334 | * @param deviceId device identifier |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 335 | * @return list of ip prefixes or null if not found |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 336 | */ |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 337 | public Set<Ip4Prefix> getSubnets(DeviceId deviceId) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 338 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
| 339 | if (srinfo != null) { |
| 340 | log.trace("getSubnets for device{} is {}", deviceId, |
| 341 | srinfo.subnets.values()); |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 342 | |
| 343 | ImmutableSet.Builder<Ip4Prefix> builder = ImmutableSet.builder(); |
| 344 | builder.addAll(srinfo.subnets.values()); |
Charles Chan | 2196a92 | 2016-03-07 00:49:33 -0800 | [diff] [blame] | 345 | SegmentRoutingAppConfig appConfig = |
| 346 | cfgService.getConfig(appId, SegmentRoutingAppConfig.class); |
| 347 | if (appConfig != null) { |
| 348 | if (deviceId.equals(appConfig.vRouterId().orElse(null))) { |
| 349 | builder.add(Ip4Prefix.valueOf("0.0.0.0/0")); |
| 350 | } |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 351 | } |
| 352 | return builder.build(); |
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 | /** |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 358 | * Returns the configured non-/32 and non-/0 subnet on the given port, |
| 359 | * or null if no subnet has been configured on the port. |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 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) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 368 | Optional<Ip4Prefix> result = srinfo.subnets.get(pnum).stream() |
| 369 | .filter(subnet -> |
| 370 | subnet.getIp4Prefix().prefixLength() != IpPrefix.MAX_INET_MASK_LENGTH && |
| 371 | subnet.getIp4Prefix().prefixLength() != 0) |
| 372 | .findFirst(); |
| 373 | return (result.isPresent()) ? result.get() : null; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 374 | } |
| 375 | return null; |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Returns the router ip address of segment router that has the |
| 380 | * specified ip address in its subnets. |
| 381 | * |
| 382 | * @param destIpAddress target ip address |
| 383 | * @return router ip address |
| 384 | */ |
| 385 | public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) { |
| 386 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 387 | deviceConfigMap.entrySet()) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 388 | for (Ip4Prefix prefix : entry.getValue().subnets.values()) { |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 389 | if (prefix.contains(destIpAddress)) { |
| 390 | return entry.getValue().ip; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | log.debug("No router was found for {}", destIpAddress); |
| 396 | return null; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Returns the router mac address of segment router that has the |
| 401 | * specified ip address as one of its subnet gateway ip address. |
| 402 | * |
| 403 | * @param gatewayIpAddress router gateway ip address |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 404 | * @return router mac address or null if not found |
Srikanth Vavilapalli | 4db76e3 | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 405 | */ |
| 406 | public MacAddress getRouterMacForAGatewayIp(Ip4Address gatewayIpAddress) { |
| 407 | for (Map.Entry<DeviceId, SegmentRouterInfo> entry: |
| 408 | deviceConfigMap.entrySet()) { |
| 409 | if (entry.getValue().gatewayIps. |
| 410 | values().contains(gatewayIpAddress)) { |
| 411 | return entry.getValue().mac; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | log.debug("Cannot find a router for {}", gatewayIpAddress); |
| 416 | return null; |
| 417 | } |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 418 | |
| 419 | |
| 420 | /** |
| 421 | * Checks if the host is in the subnet defined in the router with the |
| 422 | * device ID given. |
| 423 | * |
| 424 | * @param deviceId device identification of the router |
| 425 | * @param hostIp host IP address to check |
| 426 | * @return true if the host is within the subnet of the router, |
| 427 | * false if no subnet is defined under the router or if the host is not |
| 428 | * within the subnet defined in the router |
| 429 | */ |
| 430 | public boolean inSameSubnet(DeviceId deviceId, Ip4Address hostIp) { |
| 431 | |
Charles Chan | 9f676b6 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 432 | Set<Ip4Prefix> subnets = getSubnets(deviceId); |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 433 | if (subnets == null) { |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | for (Ip4Prefix subnet: subnets) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 438 | // Exclude /0 since it is a special case used for default route |
| 439 | if (subnet.prefixLength() != 0 && subnet.contains(hostIp)) { |
sangho | 666cd6d | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 440 | return true; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | return false; |
| 445 | } |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 446 | |
| 447 | /** |
| 448 | * Returns the ports corresponding to the adjacency Sid given. |
| 449 | * |
| 450 | * @param deviceId device identification of the router |
| 451 | * @param sid adjacency Sid |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 452 | * @return set of port numbers |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 453 | */ |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 454 | public Set<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 455 | SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 456 | return srinfo != null ? |
| 457 | ImmutableSet.copyOf(srinfo.adjacencySids.get(sid)) : |
| 458 | ImmutableSet.copyOf(new HashSet<>()); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 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); |
Charles Chan | 531a78b | 2015-12-01 10:00:51 -0800 | [diff] [blame] | 471 | return srinfo != null && srinfo.adjacencySids.containsKey(sid); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 472 | } |
Charles Chan | f2565a9 | 2016-02-10 20:46:58 -0800 | [diff] [blame] | 473 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 474 | /** |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 475 | * Add subnet to specific connect point. |
| 476 | * |
| 477 | * @param cp connect point |
| 478 | * @param ip4Prefix subnet being added to the device |
| 479 | */ |
| 480 | public void addSubnet(ConnectPoint cp, Ip4Prefix ip4Prefix) { |
| 481 | checkNotNull(cp); |
| 482 | checkNotNull(ip4Prefix); |
| 483 | SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId()); |
| 484 | if (srinfo == null) { |
| 485 | log.warn("Device {} is not configured. Abort.", cp.deviceId()); |
| 486 | return; |
| 487 | } |
| 488 | srinfo.subnets.put(cp.port(), ip4Prefix); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Remove subnet from specific connect point. |
| 493 | * |
| 494 | * @param cp connect point |
| 495 | * @param ip4Prefix subnet being removed to the device |
| 496 | */ |
| 497 | public void removeSubnet(ConnectPoint cp, Ip4Prefix ip4Prefix) { |
| 498 | checkNotNull(cp); |
| 499 | checkNotNull(ip4Prefix); |
| 500 | SegmentRouterInfo srinfo = deviceConfigMap.get(cp.deviceId()); |
| 501 | if (srinfo == null) { |
| 502 | log.warn("Device {} is not configured. Abort.", cp.deviceId()); |
| 503 | return; |
| 504 | } |
| 505 | srinfo.subnets.remove(cp.port(), ip4Prefix); |
| 506 | } |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 507 | } |