2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.segmentrouting.config;
18 import com.google.common.collect.ImmutableSet;
19 import com.google.common.collect.Lists;
20 import org.onlab.packet.Ip4Address;
21 import org.onlab.packet.Ip4Prefix;
22 import org.onlab.packet.MacAddress;
23 import org.onosproject.incubator.net.config.basics.ConfigException;
24 import org.onosproject.incubator.net.config.basics.InterfaceConfig;
25 import org.onosproject.incubator.net.intf.Interface;
26 import org.onosproject.net.ConnectPoint;
27 import org.onosproject.net.config.NetworkConfigRegistry;
28 import org.onosproject.net.host.InterfaceIpAddress;
29 import org.onosproject.segmentrouting.config.SegmentRoutingConfig.AdjacencySid;
30 import org.onosproject.net.DeviceId;
31 import org.onosproject.net.PortNumber;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
40 import java.util.concurrent.ConcurrentHashMap;
43 * Segment Routing configuration component that reads the
44 * segment routing related configuration from Network Configuration Manager
45 * component and organizes in more accessible formats.
47 public class DeviceConfiguration implements DeviceProperties {
49 private static final Logger log = LoggerFactory
50 .getLogger(DeviceConfiguration.class);
51 private final List<Integer> allSegmentIds = new ArrayList<>();
52 private final ConcurrentHashMap<DeviceId, SegmentRouterInfo> deviceConfigMap
53 = new ConcurrentHashMap<>();
55 private class SegmentRouterInfo {
61 HashMap<PortNumber, Ip4Address> gatewayIps;
62 HashMap<PortNumber, Ip4Prefix> subnets;
63 List<AdjacencySid> adjacencySids;
65 public SegmentRouterInfo() {
66 this.gatewayIps = new HashMap<>();
67 this.subnets = new HashMap<>();
72 * Constructor. Reads all the configuration for all devices of type
73 * Segment Router and organizes into various maps for easier access.
75 * @param cfgService config service
77 public DeviceConfiguration(NetworkConfigRegistry cfgService) {
78 // Read config from device subject, excluding gatewayIps and subnets.
79 Set<DeviceId> deviceSubjects =
80 cfgService.getSubjects(DeviceId.class, SegmentRoutingConfig.class);
81 deviceSubjects.forEach(subject -> {
82 SegmentRoutingConfig config =
83 cfgService.getConfig(subject, SegmentRoutingConfig.class);
84 SegmentRouterInfo info = new SegmentRouterInfo();
85 info.deviceId = subject;
86 info.nodeSid = config.getSid();
87 info.ip = config.getIp();
88 info.mac = config.getMac();
89 info.isEdge = config.isEdgeRouter();
90 info.adjacencySids = config.getAdjacencySids();
92 this.deviceConfigMap.put(info.deviceId, info);
93 this.allSegmentIds.add(info.nodeSid);
96 // Read gatewayIps and subnets from port subject.
97 Set<ConnectPoint> portSubjects =
98 cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class);
99 portSubjects.forEach(subject -> {
100 InterfaceConfig config =
101 cfgService.getConfig(subject, InterfaceConfig.class);
102 Set<Interface> networkInterfaces;
104 networkInterfaces = config.getInterfaces();
105 } catch (ConfigException e) {
106 log.error("Error loading port configuration");
109 networkInterfaces.forEach(networkInterface -> {
110 DeviceId dpid = networkInterface.connectPoint().deviceId();
111 PortNumber port = networkInterface.connectPoint().port();
112 SegmentRouterInfo info = this.deviceConfigMap.get(dpid);
114 // skip if there is no corresponding device for this ConenctPoint
116 Set<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddresses();
117 interfaceAddresses.forEach(interfaceAddress -> {
118 info.gatewayIps.put(port, interfaceAddress.ipAddress().getIp4Address());
119 info.subnets.put(port, interfaceAddress.subnetAddress().getIp4Prefix());
128 public boolean isConfigured(DeviceId deviceId) {
129 return deviceConfigMap.get(deviceId) != null;
133 public int getSegmentId(DeviceId deviceId) throws DeviceConfigNotFoundException {
134 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
135 if (srinfo != null) {
136 log.trace("getSegmentId for device{} is {}", deviceId, srinfo.nodeSid);
137 return srinfo.nodeSid;
139 String message = "getSegmentId fails for device: " + deviceId + ".";
140 throw new DeviceConfigNotFoundException(message);
145 * Returns the Node segment id of a segment router given its Router mac address.
147 * @param routerMac router mac address
148 * @return node segment id, or -1 if not found in config
150 public int getSegmentId(MacAddress routerMac) {
151 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
152 deviceConfigMap.entrySet()) {
153 if (entry.getValue().mac.equals(routerMac)) {
154 return entry.getValue().nodeSid;
162 * Returns the Node segment id of a segment router given its Router ip address.
164 * @param routerAddress router ip address
165 * @return node segment id, or -1 if not found in config
167 public int getSegmentId(Ip4Address routerAddress) {
168 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
169 deviceConfigMap.entrySet()) {
170 if (entry.getValue().ip.equals(routerAddress)) {
171 return entry.getValue().nodeSid;
179 public MacAddress getDeviceMac(DeviceId deviceId) throws DeviceConfigNotFoundException {
180 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
181 if (srinfo != null) {
182 log.trace("getDeviceMac for device{} is {}", deviceId, srinfo.mac);
185 String message = "getDeviceMac fails for device: " + deviceId + ".";
186 throw new DeviceConfigNotFoundException(message);
191 public Ip4Address getRouterIp(DeviceId deviceId) throws DeviceConfigNotFoundException {
192 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
193 if (srinfo != null) {
194 log.trace("getDeviceIp for device{} is {}", deviceId, srinfo.ip);
197 String message = "getRouterIp fails for device: " + deviceId + ".";
198 throw new DeviceConfigNotFoundException(message);
203 public boolean isEdgeDevice(DeviceId deviceId) throws DeviceConfigNotFoundException {
204 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
205 if (srinfo != null) {
206 log.trace("isEdgeDevice for device{} is {}", deviceId, srinfo.isEdge);
207 return srinfo.isEdge;
209 String message = "isEdgeDevice fails for device: " + deviceId + ".";
210 throw new DeviceConfigNotFoundException(message);
215 public List<Integer> getAllDeviceSegmentIds() {
216 return allSegmentIds;
220 public Map<Ip4Prefix, List<PortNumber>> getSubnetPortsMap(DeviceId deviceId) {
221 Map<Ip4Prefix, List<PortNumber>> subnetPortMap = new HashMap<>();
223 // Construct subnet-port mapping from port-subnet mapping
224 Map<PortNumber, Ip4Prefix> portSubnetMap =
225 this.deviceConfigMap.get(deviceId).subnets;
226 portSubnetMap.forEach((port, subnet) -> {
227 if (subnetPortMap.containsKey(subnet)) {
228 subnetPortMap.get(subnet).add(port);
230 ArrayList<PortNumber> ports = new ArrayList<>();
232 subnetPortMap.put(subnet, ports);
236 return subnetPortMap;
240 * Returns the device identifier or data plane identifier (dpid)
241 * of a segment router given its segment id.
243 * @param sid segment id
244 * @return deviceId device identifier
246 public DeviceId getDeviceId(int sid) {
247 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
248 deviceConfigMap.entrySet()) {
249 if (entry.getValue().nodeSid == sid) {
250 return entry.getValue().deviceId;
258 * Returns the device identifier or data plane identifier (dpid)
259 * of a segment router given its router ip address.
261 * @param ipAddress router ip address
262 * @return deviceId device identifier
264 public DeviceId getDeviceId(Ip4Address ipAddress) {
265 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
266 deviceConfigMap.entrySet()) {
267 if (entry.getValue().ip.equals(ipAddress)) {
268 return entry.getValue().deviceId;
276 * Returns the configured port ip addresses for a segment router.
277 * These addresses serve as gateway IP addresses for the subnets configured
280 * @param deviceId device identifier
281 * @return immutable set of ip addresses configured on the ports or null if not found
283 public Set<Ip4Address> getPortIPs(DeviceId deviceId) {
284 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
285 if (srinfo != null) {
286 log.trace("getSubnetGatewayIps for device{} is {}", deviceId,
287 srinfo.gatewayIps.values());
288 return ImmutableSet.copyOf(srinfo.gatewayIps.values());
294 * Returns the configured IP addresses per port
295 * for a segment router.
297 * @param deviceId device identifier
298 * @return map of port to gateway IP addresses or null if not found
300 public Map<PortNumber, Ip4Address> getPortIPMap(DeviceId deviceId) {
301 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
302 if (srinfo != null) {
303 return srinfo.gatewayIps;
309 * Returns the configured subnet prefixes for a segment router.
311 * @param deviceId device identifier
312 * @return list of ip prefixes or null if not found
314 public Set<Ip4Prefix> getSubnets(DeviceId deviceId) {
315 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
316 if (srinfo != null) {
317 log.trace("getSubnets for device{} is {}", deviceId,
318 srinfo.subnets.values());
319 return ImmutableSet.copyOf(srinfo.subnets.values());
325 * Returns the configured subnet on the given port, or null if no
326 * subnet has been configured on the port.
328 * @param deviceId device identifier
329 * @param pnum port identifier
330 * @return configured subnet on port, or null
332 public Ip4Prefix getPortSubnet(DeviceId deviceId, PortNumber pnum) {
333 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
334 if (srinfo != null) {
335 return srinfo.subnets.get(pnum);
341 * Returns the router ip address of segment router that has the
342 * specified ip address in its subnets.
344 * @param destIpAddress target ip address
345 * @return router ip address
347 public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) {
348 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
349 deviceConfigMap.entrySet()) {
350 for (Ip4Prefix prefix:entry.getValue().subnets.values()) {
351 if (prefix.contains(destIpAddress)) {
352 return entry.getValue().ip;
357 log.debug("No router was found for {}", destIpAddress);
362 * Returns the router mac address of segment router that has the
363 * specified ip address as one of its subnet gateway ip address.
365 * @param gatewayIpAddress router gateway ip address
366 * @return router mac address or null if not found
368 public MacAddress getRouterMacForAGatewayIp(Ip4Address gatewayIpAddress) {
369 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
370 deviceConfigMap.entrySet()) {
371 if (entry.getValue().gatewayIps.
372 values().contains(gatewayIpAddress)) {
373 return entry.getValue().mac;
377 log.debug("Cannot find a router for {}", gatewayIpAddress);
383 * Checks if the host is in the subnet defined in the router with the
386 * @param deviceId device identification of the router
387 * @param hostIp host IP address to check
388 * @return true if the host is within the subnet of the router,
389 * false if no subnet is defined under the router or if the host is not
390 * within the subnet defined in the router
392 public boolean inSameSubnet(DeviceId deviceId, Ip4Address hostIp) {
394 Set<Ip4Prefix> subnets = getSubnets(deviceId);
395 if (subnets == null) {
399 for (Ip4Prefix subnet: subnets) {
400 if (subnet.contains(hostIp)) {
409 * Returns the ports corresponding to the adjacency Sid given.
411 * @param deviceId device identification of the router
412 * @param sid adjacency Sid
413 * @return list of port numbers
415 public List<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) {
416 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
417 if (srinfo != null) {
418 for (AdjacencySid asid : srinfo.adjacencySids) {
419 if (asid.getAsid() == sid) {
420 return asid.getPorts();
425 return Lists.newArrayList();
429 * Check if the Sid given is whether adjacency Sid of the router device or not.
431 * @param deviceId device identification of the router
432 * @param sid Sid to check
433 * @return true if the Sid given is the adjacency Sid of the device,
436 public boolean isAdjacencySid(DeviceId deviceId, int sid) {
437 SegmentRouterInfo srinfo = deviceConfigMap.get(deviceId);
438 if (srinfo != null) {
439 if (srinfo.adjacencySids.isEmpty()) {
442 for (AdjacencySid asid:
443 srinfo.adjacencySids) {
444 if (asid.getAsid() == sid) {