2 * Copyright 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;
20 import org.onlab.packet.Ip4Address;
21 import org.onlab.packet.Ip4Prefix;
22 import org.onlab.packet.IpPrefix;
23 import org.onlab.packet.MacAddress;
24 import org.onosproject.net.DeviceId;
25 import org.onosproject.net.Link;
26 import org.onosproject.net.PortNumber;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import java.util.List;
34 * This class is temporary class and used only for test.
35 * It will be replaced with "real" Network Config Manager.
37 * TODO: Knock off this wrapper and directly use DeviceConfiguration class
40 public class NetworkConfigHandler {
42 private static Logger log = LoggerFactory.getLogger(NetworkConfigHandler.class);
43 private SegmentRoutingManager srManager;
44 private DeviceConfiguration deviceConfig;
46 public NetworkConfigHandler(SegmentRoutingManager srManager,
47 DeviceConfiguration deviceConfig) {
48 this.srManager = srManager;
49 this.deviceConfig = deviceConfig;
52 public List<Ip4Address> getGatewayIpAddress(DeviceId deviceId) {
53 return this.deviceConfig.getSubnetGatewayIps(deviceId);
56 public IpPrefix getRouterIpAddress(DeviceId deviceId) {
57 return IpPrefix.valueOf(deviceConfig.getRouterIp(deviceId), 32);
60 public MacAddress getRouterMacAddress(DeviceId deviceId) {
61 return deviceConfig.getDeviceMac(deviceId);
64 public boolean inSameSubnet(DeviceId deviceId, Ip4Address destIp) {
66 List<Ip4Prefix> subnets = getSubnetInfo(deviceId);
67 if (subnets == null) {
71 return subnets.stream()
72 .anyMatch((subnet) -> subnet.contains(destIp));
75 public boolean inSameSubnet(Ip4Address address, int sid) {
76 DeviceId deviceId = deviceConfig.getDeviceId(sid);
77 if (deviceId == null) {
78 log.warn("Cannot find a device for SID {}", sid);
82 return inSameSubnet(deviceId, address);
85 public List<Ip4Prefix> getSubnetInfo(DeviceId deviceId) {
86 return deviceConfig.getSubnets(deviceId);
89 public int getMplsId(DeviceId deviceId) {
90 return deviceConfig.getSegmentId(deviceId);
93 public int getMplsId(MacAddress routerMac) {
94 return deviceConfig.getSegmentId(routerMac);
97 public int getMplsId(Ip4Address routerIpAddress) {
98 return deviceConfig.getSegmentId(routerIpAddress);
101 public boolean isEcmpNotSupportedInTransit(DeviceId deviceId) {
102 //TODO: temporarily changing to true to test with Dell
106 public boolean isTransitRouter(DeviceId deviceId) {
107 return !(deviceConfig.isEdgeDevice(deviceId));
111 public boolean isEdgeRouter(DeviceId deviceId) {
112 return deviceConfig.isEdgeDevice(deviceId);
115 private List<PortNumber> getPortsToNeighbors(DeviceId deviceId, List<DeviceId> fwdSws) {
117 List<PortNumber> portNumbers = Lists.newArrayList();
119 Set<Link> links = srManager.linkService.getDeviceEgressLinks(deviceId);
120 for (Link link: links) {
121 for (DeviceId swId: fwdSws) {
122 if (link.dst().deviceId().equals(swId)) {
123 portNumbers.add(link.src().port());
132 public List<PortNumber> getPortsToDevice(DeviceId deviceId) {
133 List<PortNumber> portNumbers = Lists.newArrayList();
135 Set<Link> links = srManager.linkService.getDeviceEgressLinks(deviceId);
136 for (Link link: links) {
137 if (link.dst().deviceId().equals(deviceId)) {
138 portNumbers.add(link.src().port());
146 public Ip4Address getDestinationRouterAddress(Ip4Address destIpAddress) {
147 return deviceConfig.getRouterIpAddressForASubnetHost(destIpAddress);
150 public DeviceId getDeviceId(Ip4Address ip4Address) {
151 return deviceConfig.getDeviceId(ip4Address);
154 public MacAddress getRouterMac(Ip4Address targetAddress) {
155 return deviceConfig.getRouterMacForAGatewayIp(targetAddress);