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