blob: ad210bb2c792cd9c4a6bae26b703705ec9632078 [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.segmentrouting;
17
18import com.google.common.collect.Lists;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070019
sanghob35a6192015-04-01 13:05:26 -070020import org.onlab.packet.Ip4Address;
21import org.onlab.packet.Ip4Prefix;
22import org.onlab.packet.IpPrefix;
23import org.onlab.packet.MacAddress;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Link;
26import org.onosproject.net.PortNumber;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
sanghob35a6192015-04-01 13:05:26 -070030import java.util.List;
31import java.util.Set;
32
33/**
34 * This class is temporary class and used only for test.
35 * It will be replaced with "real" Network Config Manager.
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070036 *
37 * TODO: Knock off this wrapper and directly use DeviceConfiguration class
sanghob35a6192015-04-01 13:05:26 -070038 */
39
40public class NetworkConfigHandler {
41
42 private static Logger log = LoggerFactory.getLogger(NetworkConfigHandler.class);
43 private SegmentRoutingManager srManager;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070044 private DeviceConfiguration deviceConfig;
sanghob35a6192015-04-01 13:05:26 -070045
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070046 public NetworkConfigHandler(SegmentRoutingManager srManager,
47 DeviceConfiguration deviceConfig) {
sanghob35a6192015-04-01 13:05:26 -070048 this.srManager = srManager;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070049 this.deviceConfig = deviceConfig;
sanghob35a6192015-04-01 13:05:26 -070050 }
51
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070052 public List<Ip4Address> getGatewayIpAddress(DeviceId deviceId) {
53 return this.deviceConfig.getSubnetGatewayIps(deviceId);
sanghob35a6192015-04-01 13:05:26 -070054 }
55
56 public IpPrefix getRouterIpAddress(DeviceId deviceId) {
sanghob35a6192015-04-01 13:05:26 -070057 return IpPrefix.valueOf(deviceConfig.getRouterIp(deviceId), 32);
58 }
59
60 public MacAddress getRouterMacAddress(DeviceId deviceId) {
61 return deviceConfig.getDeviceMac(deviceId);
62 }
63
64 public boolean inSameSubnet(DeviceId deviceId, Ip4Address destIp) {
65
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070066 List<Ip4Prefix> subnets = getSubnetInfo(deviceId);
67 if (subnets == null) {
sanghob35a6192015-04-01 13:05:26 -070068 return false;
69 }
70
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070071 return subnets.stream()
72 .anyMatch((subnet) -> subnet.contains(destIp));
sanghob35a6192015-04-01 13:05:26 -070073 }
74
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);
79 return false;
80 }
81
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070082 return inSameSubnet(deviceId, address);
sanghob35a6192015-04-01 13:05:26 -070083 }
84
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070085 public List<Ip4Prefix> getSubnetInfo(DeviceId deviceId) {
86 return deviceConfig.getSubnets(deviceId);
sanghob35a6192015-04-01 13:05:26 -070087 }
88
89 public int getMplsId(DeviceId deviceId) {
90 return deviceConfig.getSegmentId(deviceId);
91 }
92
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070093 public int getMplsId(MacAddress routerMac) {
94 return deviceConfig.getSegmentId(routerMac);
sanghob35a6192015-04-01 13:05:26 -070095 }
96
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070097 public int getMplsId(Ip4Address routerIpAddress) {
98 return deviceConfig.getSegmentId(routerIpAddress);
sanghob35a6192015-04-01 13:05:26 -070099 }
100
101 public boolean isEcmpNotSupportedInTransit(DeviceId deviceId) {
102 return false;
103 }
104
105 public boolean isTransitRouter(DeviceId deviceId) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700106 return !(deviceConfig.isEdgeDevice(deviceId));
sanghob35a6192015-04-01 13:05:26 -0700107 }
108
109
110 public boolean isEdgeRouter(DeviceId deviceId) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700111 return deviceConfig.isEdgeDevice(deviceId);
sanghob35a6192015-04-01 13:05:26 -0700112 }
113
114 private List<PortNumber> getPortsToNeighbors(DeviceId deviceId, List<DeviceId> fwdSws) {
115
116 List<PortNumber> portNumbers = Lists.newArrayList();
117
118 Set<Link> links = srManager.linkService.getDeviceEgressLinks(deviceId);
119 for (Link link: links) {
120 for (DeviceId swId: fwdSws) {
121 if (link.dst().deviceId().equals(swId)) {
122 portNumbers.add(link.src().port());
123 break;
124 }
125 }
126 }
127
128 return portNumbers;
129 }
130
131 public List<PortNumber> getPortsToDevice(DeviceId deviceId) {
132 List<PortNumber> portNumbers = Lists.newArrayList();
133
134 Set<Link> links = srManager.linkService.getDeviceEgressLinks(deviceId);
135 for (Link link: links) {
136 if (link.dst().deviceId().equals(deviceId)) {
137 portNumbers.add(link.src().port());
138 }
139 }
140
141 return portNumbers;
142 }
143
144
145 public Ip4Address getDestinationRouterAddress(Ip4Address destIpAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700146 return deviceConfig.getRouterIpAddressForASubnetHost(destIpAddress);
sanghob35a6192015-04-01 13:05:26 -0700147 }
148
149 public DeviceId getDeviceId(Ip4Address ip4Address) {
150 return deviceConfig.getDeviceId(ip4Address);
151 }
152
153 public MacAddress getRouterMac(Ip4Address targetAddress) {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700154 return deviceConfig.getRouterMacForAGatewayIp(targetAddress);
sanghob35a6192015-04-01 13:05:26 -0700155 }
156}