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