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