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;
18 import com.google.common.collect.Lists;
19 import org.onlab.packet.Ip4Address;
20 import org.onlab.packet.Ip4Prefix;
21 import org.onlab.packet.MacAddress;
22 import org.onosproject.incubator.net.config.basics.ConfigException;
23 import org.onosproject.incubator.net.config.basics.InterfaceConfig;
24 import org.onosproject.incubator.net.intf.Interface;
25 import org.onosproject.net.ConnectPoint;
26 import org.onosproject.net.config.NetworkConfigRegistry;
27 import org.onosproject.net.host.InterfaceIpAddress;
28 import org.onosproject.segmentrouting.config.SegmentRoutingConfig;
29 import org.onosproject.segmentrouting.config.SegmentRoutingConfig.AdjacencySid;
30 import org.onosproject.segmentrouting.grouphandler.DeviceProperties;
31 import org.onosproject.net.DeviceId;
32 import org.onosproject.net.PortNumber;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
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 * TODO: Merge multiple Segment Routing configuration wrapper classes into one.
49 public class DeviceConfiguration implements DeviceProperties {
51 private static final Logger log = LoggerFactory
52 .getLogger(DeviceConfiguration.class);
53 private final List<Integer> allSegmentIds = new ArrayList<>();
54 private final HashMap<DeviceId, SegmentRouterInfo> deviceConfigMap = new HashMap<>();
56 private class SegmentRouterInfo {
62 HashMap<PortNumber, Ip4Address> gatewayIps;
63 HashMap<PortNumber, Ip4Prefix> subnets;
64 List<AdjacencySid> adjacencySids;
68 * Constructor. Reads all the configuration for all devices of type
69 * Segment Router and organizes into various maps for easier access.
71 public DeviceConfiguration(NetworkConfigRegistry cfgService) {
72 // Read config from device subject, excluding gatewayIps and subnets.
73 Set<DeviceId> deviceSubjects =
74 cfgService.getSubjects(DeviceId.class, SegmentRoutingConfig.class);
75 deviceSubjects.forEach(subject -> {
76 SegmentRoutingConfig config =
77 cfgService.getConfig(subject, SegmentRoutingConfig.class);
78 SegmentRouterInfo info = new SegmentRouterInfo();
79 info.deviceId = subject;
80 info.nodeSid = config.getSid();
81 info.ip = config.getIp();
82 info.mac = config.getMac();
83 info.isEdge = config.isEdgeRouter();
84 info.adjacencySids = config.getAdjacencySids();
85 info.gatewayIps = new HashMap<>();
86 info.subnets = new HashMap<>();
88 this.deviceConfigMap.put(info.deviceId, info);
89 this.allSegmentIds.add(info.nodeSid);
92 // Read gatewayIps and subnets from port subject.
93 Set<ConnectPoint> portSubjects =
94 cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class);
95 portSubjects.forEach(subject -> {
96 InterfaceConfig config =
97 cfgService.getConfig(subject, InterfaceConfig.class);
98 Set<Interface> networkInterfaces;
100 networkInterfaces = config.getInterfaces();
101 } catch (ConfigException e) {
102 log.error("Error loading port configuration");
105 networkInterfaces.forEach(networkInterface -> {
106 DeviceId dpid = networkInterface.connectPoint().deviceId();
107 PortNumber port = networkInterface.connectPoint().port();
108 SegmentRouterInfo info = this.deviceConfigMap.get(dpid);
110 Set<InterfaceIpAddress> interfaceAddresses = networkInterface.ipAddresses();
111 interfaceAddresses.forEach(interfaceAddress -> {
112 info.gatewayIps.put(port, interfaceAddress.ipAddress().getIp4Address());
113 info.subnets.put(port, interfaceAddress.subnetAddress().getIp4Prefix());
121 * Returns the segment id of a segment router.
123 * @param deviceId device identifier
127 public int getSegmentId(DeviceId deviceId) {
128 if (deviceConfigMap.get(deviceId) != null) {
129 log.debug("getSegmentId for device{} is {}",
131 deviceConfigMap.get(deviceId).nodeSid);
132 return deviceConfigMap.get(deviceId).nodeSid;
134 log.warn("getSegmentId for device {} "
135 + "throwing IllegalStateException "
136 + "because device does not exist in config", deviceId);
137 throw new IllegalStateException();
142 * Returns the segment id of a segment router given its mac address.
144 * @param routerMac router mac address
147 public int getSegmentId(MacAddress routerMac) {
148 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
149 deviceConfigMap.entrySet()) {
150 if (entry.getValue().mac.equals(routerMac)) {
151 return entry.getValue().nodeSid;
159 * Returns the segment id of a segment router given its router ip address.
161 * @param routerAddress router ip address
164 public int getSegmentId(Ip4Address routerAddress) {
165 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
166 deviceConfigMap.entrySet()) {
167 if (entry.getValue().ip.equals(routerAddress)) {
168 return entry.getValue().nodeSid;
176 * Returns the router mac of a segment router.
178 * @param deviceId device identifier
179 * @return router mac address
182 public MacAddress getDeviceMac(DeviceId deviceId) {
183 if (deviceConfigMap.get(deviceId) != null) {
184 log.debug("getDeviceMac for device{} is {}",
186 deviceConfigMap.get(deviceId).mac);
187 return deviceConfigMap.get(deviceId).mac;
189 log.warn("getDeviceMac for device {} "
190 + "throwing IllegalStateException "
191 + "because device does not exist in config", deviceId);
192 throw new IllegalStateException();
197 * Returns the router ip address of a segment router.
199 * @param deviceId device identifier
200 * @return router ip address
202 public Ip4Address getRouterIp(DeviceId deviceId) {
203 if (deviceConfigMap.get(deviceId) != null) {
204 log.debug("getDeviceIp for device{} is {}",
206 deviceConfigMap.get(deviceId).ip);
207 return deviceConfigMap.get(deviceId).ip;
209 log.warn("getRouterIp for device {} "
210 + "throwing IllegalStateException "
211 + "because device does not exist in config", deviceId);
212 throw new IllegalStateException();
217 * Indicates if the segment router is a edge router or
218 * a transit/back bone router.
220 * @param deviceId device identifier
224 public boolean isEdgeDevice(DeviceId deviceId) {
225 if (deviceConfigMap.get(deviceId) != null) {
226 log.debug("isEdgeDevice for device{} is {}",
228 deviceConfigMap.get(deviceId).isEdge);
229 return deviceConfigMap.get(deviceId).isEdge;
231 log.warn("isEdgeDevice for device {} "
232 + "throwing IllegalStateException "
233 + "because device does not exist in config", deviceId);
234 throw new IllegalStateException();
239 * Returns the segment ids of all configured segment routers.
241 * @return list of segment ids
244 public List<Integer> getAllDeviceSegmentIds() {
245 return allSegmentIds;
249 * Returns the device identifier or data plane identifier (dpid)
250 * of a segment router given its segment id.
252 * @param sid segment id
253 * @return deviceId device identifier
255 public DeviceId getDeviceId(int sid) {
256 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
257 deviceConfigMap.entrySet()) {
258 if (entry.getValue().nodeSid == sid) {
259 return entry.getValue().deviceId;
267 * Returns the device identifier or data plane identifier (dpid)
268 * of a segment router given its router ip address.
270 * @param ipAddress router ip address
271 * @return deviceId device identifier
273 public DeviceId getDeviceId(Ip4Address ipAddress) {
274 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
275 deviceConfigMap.entrySet()) {
276 if (entry.getValue().ip.equals(ipAddress)) {
277 return entry.getValue().deviceId;
285 * Returns the configured subnet gateway ip addresses for a segment router.
287 * @param deviceId device identifier
288 * @return list of ip addresses
290 public List<Ip4Address> getSubnetGatewayIps(DeviceId deviceId) {
291 if (deviceConfigMap.get(deviceId) != null) {
292 log.debug("getSubnetGatewayIps for device{} is {}",
294 deviceConfigMap.get(deviceId).gatewayIps.values());
295 return new ArrayList<>(deviceConfigMap.get(deviceId).gatewayIps.values());
302 * Returns the configured subnet prefixes for a segment router.
304 * @param deviceId device identifier
305 * @return list of ip prefixes
307 public List<Ip4Prefix> getSubnets(DeviceId deviceId) {
308 if (deviceConfigMap.get(deviceId) != null) {
309 log.debug("getSubnets for device{} is {}",
311 deviceConfigMap.get(deviceId).subnets.values());
312 return new ArrayList<>(deviceConfigMap.get(deviceId).subnets.values());
319 * Returns the router ip address of segment router that has the
320 * specified ip address in its subnets.
322 * @param destIpAddress target ip address
323 * @return router ip address
325 public Ip4Address getRouterIpAddressForASubnetHost(Ip4Address destIpAddress) {
326 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
327 deviceConfigMap.entrySet()) {
328 for (Ip4Prefix prefix:entry.getValue().subnets.values()) {
329 if (prefix.contains(destIpAddress)) {
330 return entry.getValue().ip;
335 log.debug("No router was found for {}", destIpAddress);
340 * Returns the router mac address of segment router that has the
341 * specified ip address as one of its subnet gateway ip address.
343 * @param gatewayIpAddress router gateway ip address
344 * @return router mac address
346 public MacAddress getRouterMacForAGatewayIp(Ip4Address gatewayIpAddress) {
347 for (Map.Entry<DeviceId, SegmentRouterInfo> entry:
348 deviceConfigMap.entrySet()) {
349 if (entry.getValue().gatewayIps.
350 values().contains(gatewayIpAddress)) {
351 return entry.getValue().mac;
355 log.debug("Cannot find a router for {}", gatewayIpAddress);
361 * Checks if the host is in the subnet defined in the router with the
364 * @param deviceId device identification of the router
365 * @param hostIp host IP address to check
366 * @return true if the host is within the subnet of the router,
367 * false if no subnet is defined under the router or if the host is not
368 * within the subnet defined in the router
370 public boolean inSameSubnet(DeviceId deviceId, Ip4Address hostIp) {
372 List<Ip4Prefix> subnets = getSubnets(deviceId);
373 if (subnets == null) {
377 for (Ip4Prefix subnet: subnets) {
378 if (subnet.contains(hostIp)) {
387 * Returns the ports corresponding to the adjacency Sid given.
389 * @param deviceId device identification of the router
390 * @param sid adjacency Sid
391 * @return list of port numbers
393 public List<Integer> getPortsForAdjacencySid(DeviceId deviceId, int sid) {
394 if (deviceConfigMap.get(deviceId) != null) {
395 for (AdjacencySid asid : deviceConfigMap.get(deviceId).adjacencySids) {
396 if (asid.getAsid() == sid) {
397 return asid.getPorts();
402 return Lists.newArrayList();
406 * Check if the Sid given is whether adjacency Sid of the router device or not.
408 * @param deviceId device identification of the router
409 * @param sid Sid to check
410 * @return true if the Sid given is the adjacency Sid of the device,
413 public boolean isAdjacencySid(DeviceId deviceId, int sid) {
414 if (deviceConfigMap.get(deviceId) != null) {
415 if (deviceConfigMap.get(deviceId).adjacencySids.isEmpty()) {
418 for (AdjacencySid asid:
419 deviceConfigMap.get(deviceId).adjacencySids) {
420 if (asid.getAsid() == sid) {