blob: 64726801de9908be5a2214380a428cb90daffc62 [file] [log] [blame]
Jonathan Harteb8c9472015-08-05 07:43:13 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Harteb8c9472015-08-05 07:43:13 -07003 *
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.incubator.net.intf;
17
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070018import com.google.common.annotations.Beta;
Jonathan Harteb8c9472015-08-05 07:43:13 -070019import com.google.common.base.MoreObjects;
Jonathan Hart00cddda2016-02-16 10:30:37 -080020import com.google.common.collect.Lists;
Jonathan Harteb8c9472015-08-05 07:43:13 -070021import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.host.InterfaceIpAddress;
25
Jonathan Hart00cddda2016-02-16 10:30:37 -080026import java.util.List;
Jonathan Harteb8c9472015-08-05 07:43:13 -070027import java.util.Objects;
28import java.util.Set;
Jonathan Hart00cddda2016-02-16 10:30:37 -080029import java.util.stream.Collectors;
Jonathan Harteb8c9472015-08-05 07:43:13 -070030
Jonathan Hart4cb39882015-08-12 23:50:55 -040031import static com.google.common.base.Preconditions.checkNotNull;
32
Jonathan Harteb8c9472015-08-05 07:43:13 -070033/**
34 * An Interface maps network configuration information (such as addresses and
Jonathan Hart7dbe6292015-11-10 08:33:17 -080035 * vlans) to a port in the network.
Jonathan Harteb8c9472015-08-05 07:43:13 -070036 */
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070037@Beta
Jonathan Harteb8c9472015-08-05 07:43:13 -070038public class Interface {
Jonathan Hart7dbe6292015-11-10 08:33:17 -080039 public static final String NO_INTERFACE_NAME = "";
40
41 private final String name;
Jonathan Harteb8c9472015-08-05 07:43:13 -070042 private final ConnectPoint connectPoint;
Jonathan Hart00cddda2016-02-16 10:30:37 -080043 private final List<InterfaceIpAddress> ipAddresses;
Jonathan Harteb8c9472015-08-05 07:43:13 -070044 private final MacAddress macAddress;
45 private final VlanId vlan;
46
47 /**
48 * Creates new Interface with the provided configuration.
49 *
Jonathan Hart7dbe6292015-11-10 08:33:17 -080050 * @param name name of the interface
51 * @param connectPoint the connect point this interface maps to
Jonathan Hart00cddda2016-02-16 10:30:37 -080052 * @param ipAddresses list of IP addresses
Jonathan Hart7dbe6292015-11-10 08:33:17 -080053 * @param macAddress MAC address
54 * @param vlan VLAN ID
55 */
56 public Interface(String name, ConnectPoint connectPoint,
Jonathan Hart00cddda2016-02-16 10:30:37 -080057 List<InterfaceIpAddress> ipAddresses,
58 MacAddress macAddress, VlanId vlan) {
59 this.name = name == null ? NO_INTERFACE_NAME : name;
60 this.connectPoint = checkNotNull(connectPoint);
61 this.ipAddresses = ipAddresses == null ? Lists.newArrayList() : ipAddresses;
62 this.macAddress = macAddress == null ? MacAddress.NONE : macAddress;
63 this.vlan = vlan == null ? VlanId.NONE : vlan;
64 }
65
66 /**
Jonathan Hart7dbe6292015-11-10 08:33:17 -080067 * Retrieves the name of the interface.
68 *
69 * @return name
70 */
71 public String name() {
72 return name;
Jonathan Harteb8c9472015-08-05 07:43:13 -070073 }
74
75 /**
76 * Retrieves the connection point that this interface maps to.
77 *
78 * @return the connection point
79 */
80 public ConnectPoint connectPoint() {
81 return connectPoint;
82 }
83
84 /**
85 * Retrieves the set of IP addresses that are assigned to the interface.
86 *
87 * @return the set of interface IP addresses
Jonathan Hart00cddda2016-02-16 10:30:37 -080088 * @deprecated in Falcon release in favour of an ordered list
Jonathan Harteb8c9472015-08-05 07:43:13 -070089 */
Jonathan Hart00cddda2016-02-16 10:30:37 -080090 @Deprecated
Jonathan Harteb8c9472015-08-05 07:43:13 -070091 public Set<InterfaceIpAddress> ipAddresses() {
Jonathan Hart00cddda2016-02-16 10:30:37 -080092 return ipAddresses.stream().collect(Collectors.toSet());
93 }
94
95 /**
96 * Retrieves a list of IP addresses that are assigned to the interface in
97 * the order that they were configured.
98 *
99 * @return list of IP addresses
100 */
101 public List<InterfaceIpAddress> ipAddressesList() {
Jonathan Harteb8c9472015-08-05 07:43:13 -0700102 return ipAddresses;
103 }
104
105 /**
106 * Retrieves the MAC address that is assigned to the interface.
107 *
108 * @return the MAC address
109 */
110 public MacAddress mac() {
111 return macAddress;
112 }
113
114 /**
115 * Retrieves the VLAN ID that is assigned to the interface.
116 *
117 * @return the VLAN ID
118 */
119 public VlanId vlan() {
120 return vlan;
121 }
122
123 @Override
124 public boolean equals(Object other) {
125 if (!(other instanceof Interface)) {
126 return false;
127 }
128
129 Interface otherInterface = (Interface) other;
130
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800131 return Objects.equals(name, otherInterface.name) &&
132 Objects.equals(connectPoint, otherInterface.connectPoint) &&
Jonathan Harteb8c9472015-08-05 07:43:13 -0700133 Objects.equals(ipAddresses, otherInterface.ipAddresses) &&
134 Objects.equals(macAddress, otherInterface.macAddress) &&
135 Objects.equals(vlan, otherInterface.vlan);
136 }
137
138 @Override
139 public int hashCode() {
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800140 return Objects.hash(connectPoint, name, ipAddresses, macAddress, vlan);
Jonathan Harteb8c9472015-08-05 07:43:13 -0700141 }
142
143 @Override
144 public String toString() {
145 return MoreObjects.toStringHelper(getClass())
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800146 .add("name", name)
Jonathan Harteb8c9472015-08-05 07:43:13 -0700147 .add("connectPoint", connectPoint)
148 .add("ipAddresses", ipAddresses)
149 .add("macAddress", macAddress)
150 .add("vlan", vlan)
151 .toString();
152 }
153}