d34681781988b32c121096ff7a5733ba6c8934c6
[onosfw.git] /
1 /*
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  */
16 package org.onosproject.segmentrouting;
17
18 import com.google.common.collect.Lists;
19
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;
29
30 import java.util.List;
31 import 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.
36  *
37  * TODO: Knock off this wrapper and directly use DeviceConfiguration class
38  */
39
40 public class NetworkConfigHandler {
41
42     private static Logger log = LoggerFactory.getLogger(NetworkConfigHandler.class);
43     private SegmentRoutingManager srManager;
44     private DeviceConfiguration deviceConfig;
45
46     public NetworkConfigHandler(SegmentRoutingManager srManager,
47                                 DeviceConfiguration deviceConfig) {
48         this.srManager = srManager;
49         this.deviceConfig = deviceConfig;
50     }
51
52     public List<Ip4Address> getGatewayIpAddress(DeviceId deviceId) {
53         return this.deviceConfig.getSubnetGatewayIps(deviceId);
54     }
55
56     public IpPrefix getRouterIpAddress(DeviceId deviceId) {
57         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
66         List<Ip4Prefix> subnets = getSubnetInfo(deviceId);
67         if (subnets == null) {
68             return false;
69         }
70
71         return subnets.stream()
72                .anyMatch((subnet) -> subnet.contains(destIp));
73     }
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
82         return inSameSubnet(deviceId, address);
83     }
84
85     public List<Ip4Prefix> getSubnetInfo(DeviceId deviceId) {
86         return deviceConfig.getSubnets(deviceId);
87     }
88
89     public int getMplsId(DeviceId deviceId) {
90         return deviceConfig.getSegmentId(deviceId);
91     }
92
93     public int getMplsId(MacAddress routerMac) {
94         return deviceConfig.getSegmentId(routerMac);
95     }
96
97     public int getMplsId(Ip4Address routerIpAddress) {
98         return deviceConfig.getSegmentId(routerIpAddress);
99     }
100
101     public boolean isEcmpNotSupportedInTransit(DeviceId deviceId) {
102         //TODO: temporarily changing to true to test with Dell
103         return true;
104     }
105
106     public boolean isTransitRouter(DeviceId deviceId) {
107         return !(deviceConfig.isEdgeDevice(deviceId));
108     }
109
110
111     public boolean isEdgeRouter(DeviceId deviceId) {
112         return deviceConfig.isEdgeDevice(deviceId);
113     }
114
115     private List<PortNumber> getPortsToNeighbors(DeviceId deviceId, List<DeviceId> fwdSws) {
116
117         List<PortNumber> portNumbers = Lists.newArrayList();
118
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());
124                     break;
125                 }
126             }
127         }
128
129         return portNumbers;
130     }
131
132     public List<PortNumber> getPortsToDevice(DeviceId deviceId) {
133         List<PortNumber> portNumbers = Lists.newArrayList();
134
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());
139             }
140         }
141
142         return portNumbers;
143     }
144
145
146     public Ip4Address getDestinationRouterAddress(Ip4Address destIpAddress) {
147         return deviceConfig.getRouterIpAddressForASubnetHost(destIpAddress);
148     }
149
150     public DeviceId getDeviceId(Ip4Address ip4Address) {
151         return deviceConfig.getDeviceId(ip4Address);
152     }
153
154     public MacAddress getRouterMac(Ip4Address targetAddress) {
155         return deviceConfig.getRouterMacForAGatewayIp(targetAddress);
156     }
157 }