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