aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/NetworkConfigHandler.java
blob: d34681781988b32c121096ff7a5733ba6c8934c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.segmentrouting;

import com.google.common.collect.Lists;

import org.onlab.packet.Ip4Address;
import org.onlab.packet.Ip4Prefix;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
import org.onosproject.net.PortNumber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Set;

/**
 * This class is temporary class and used only for test.
 * It will be replaced with "real" Network Config Manager.
 *
 * TODO: Knock off this wrapper and directly use DeviceConfiguration class
 */

public class NetworkConfigHandler {

    private static Logger log = LoggerFactory.getLogger(NetworkConfigHandler.class);
    private SegmentRoutingManager srManager;
    private DeviceConfiguration deviceConfig;

    public NetworkConfigHandler(SegmentRoutingManager srManager,
                                DeviceConfiguration deviceConfig) {
        this.srManager = srManager;
        this.deviceConfig = deviceConfig;
    }

    public List<Ip4Address> getGatewayIpAddress(DeviceId deviceId) {
        return this.deviceConfig.getSubnetGatewayIps(deviceId);
    }

    public IpPrefix getRouterIpAddress(DeviceId deviceId) {
        return IpPrefix.valueOf(deviceConfig.getRouterIp(deviceId), 32);
    }

    public MacAddress getRouterMacAddress(DeviceId deviceId) {
        return deviceConfig.getDeviceMac(deviceId);
    }

    public boolean inSameSubnet(DeviceId deviceId, Ip4Address destIp) {

        List<Ip4Prefix> subnets = getSubnetInfo(deviceId);
        if (subnets == null) {
            return false;
        }

        return subnets.stream()
               .anyMatch((subnet) -> subnet.contains(destIp));
    }

    public boolean inSameSubnet(Ip4Address address, int sid) {
        DeviceId deviceId = deviceConfig.getDeviceId(sid);
        if (deviceId == null) {
            log.warn("Cannot find a device for SID {}", sid);
            return false;
        }

        return inSameSubnet(deviceId, address);
    }

    public List<Ip4Prefix> getSubnetInfo(DeviceId deviceId) {
        return deviceConfig.getSubnets(deviceId);
    }

    public int getMplsId(DeviceId deviceId) {
        return deviceConfig.getSegmentId(deviceId);
    }

    public int getMplsId(MacAddress routerMac) {
        return deviceConfig.getSegmentId(routerMac);
    }

    public int getMplsId(Ip4Address routerIpAddress) {
        return deviceConfig.getSegmentId(routerIpAddress);
    }

    public boolean isEcmpNotSupportedInTransit(DeviceId deviceId) {
        //TODO: temporarily changing to true to test with Dell
        return true;
    }

    public boolean isTransitRouter(DeviceId deviceId) {
        return !(deviceConfig.isEdgeDevice(deviceId));
    }


    public boolean isEdgeRouter(DeviceId deviceId) {
        return deviceConfig.isEdgeDevice(deviceId);
    }

    private List<PortNumber> getPortsToNeighbors(DeviceId deviceId, List<DeviceId> fwdSws) {

        List<PortNumber> portNumbers = Lists.newArrayList();

        Set<Link> links = srManager.linkService.getDeviceEgressLinks(deviceId);
        for (Link link: links) {
            for (DeviceId swId: fwdSws) {
                if (link.dst().deviceId().equals(swId)) {
                    portNumbers.add(link.src().port());
                    break;
                }
            }
        }

        return portNumbers;
    }

    public List<PortNumber> getPortsToDevice(DeviceId deviceId) {
        List<PortNumber> portNumbers = Lists.newArrayList();

        Set<Link> links = srManager.linkService.getDeviceEgressLinks(deviceId);
        for (Link link: links) {
            if (link.dst().deviceId().equals(deviceId)) {
                portNumbers.add(link.src().port());
            }
        }

        return portNumbers;
    }


    public Ip4Address getDestinationRouterAddress(Ip4Address destIpAddress) {
        return deviceConfig.getRouterIpAddressForASubnetHost(destIpAddress);
    }

    public DeviceId getDeviceId(Ip4Address ip4Address) {
        return deviceConfig.getDeviceId(ip4Address);
    }

    public MacAddress getRouterMac(Ip4Address targetAddress) {
        return deviceConfig.getRouterMacForAGatewayIp(targetAddress);
    }
}