blob: 08a4d1ee42172ecfe9b60cbdf159814957a38951 [file] [log] [blame]
Jonathan Harteb8c9472015-08-05 07:43:13 -07001/*
2 * Copyright 2014-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.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 /**
67 * Creates new Interface with the provided configuration.
68 *
69 * @param name name of the interface
70 * @param connectPoint the connect point this interface maps to
71 * @param ipAddresses set of IP addresses
72 * @param macAddress MAC address
73 * @param vlan VLAN ID
74 * @deprecated in Falcon release, in favour of ordered list
75 */
76 @Deprecated
77 public Interface(String name, ConnectPoint connectPoint,
Jonathan Hart7dbe6292015-11-10 08:33:17 -080078 Set<InterfaceIpAddress> ipAddresses,
79 MacAddress macAddress, VlanId vlan) {
80 this.name = name == null ? NO_INTERFACE_NAME : name;
81 this.connectPoint = checkNotNull(connectPoint);
Jonathan Hart00cddda2016-02-16 10:30:37 -080082 this.ipAddresses = ipAddresses == null ? Lists.newArrayList() :
83 ipAddresses.stream().collect(Collectors.toList());
Jonathan Hart7dbe6292015-11-10 08:33:17 -080084 this.macAddress = macAddress == null ? MacAddress.NONE : macAddress;
85 this.vlan = vlan == null ? VlanId.NONE : vlan;
86 }
87
88 /**
89 * Creates new Interface with the provided configuration.
90 *
Jonathan Harteb8c9472015-08-05 07:43:13 -070091 * @param connectPoint the connect point this interface maps to
92 * @param ipAddresses Set of IP addresses
93 * @param macAddress MAC address
94 * @param vlan VLAN ID
Jonathan Hart00cddda2016-02-16 10:30:37 -080095 * @deprecated in Falcon release - use constructors with names instead
Jonathan Harteb8c9472015-08-05 07:43:13 -070096 */
Jonathan Hart00cddda2016-02-16 10:30:37 -080097 @Deprecated
Jonathan Harteb8c9472015-08-05 07:43:13 -070098 public Interface(ConnectPoint connectPoint,
99 Set<InterfaceIpAddress> ipAddresses,
100 MacAddress macAddress, VlanId vlan) {
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800101 this(NO_INTERFACE_NAME, connectPoint, ipAddresses, macAddress, vlan);
102 }
103
104 /**
Jonathan Hart00cddda2016-02-16 10:30:37 -0800105 * Creates new Interface with the provided configuration.
106 *
107 * @param connectPoint the connect point this interface maps to
108 * @param ipAddresses Set of IP addresses
109 * @param macAddress MAC address
110 * @param vlan VLAN ID
111 * @deprecated in Falcon release - use constructors with names instead
112 */
113 @Deprecated
114 public Interface(ConnectPoint connectPoint,
115 List<InterfaceIpAddress> ipAddresses,
116 MacAddress macAddress, VlanId vlan) {
117 this(NO_INTERFACE_NAME, connectPoint, ipAddresses, macAddress, vlan);
118 }
119
120 /**
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800121 * Retrieves the name of the interface.
122 *
123 * @return name
124 */
125 public String name() {
126 return name;
Jonathan Harteb8c9472015-08-05 07:43:13 -0700127 }
128
129 /**
130 * Retrieves the connection point that this interface maps to.
131 *
132 * @return the connection point
133 */
134 public ConnectPoint connectPoint() {
135 return connectPoint;
136 }
137
138 /**
139 * Retrieves the set of IP addresses that are assigned to the interface.
140 *
141 * @return the set of interface IP addresses
Jonathan Hart00cddda2016-02-16 10:30:37 -0800142 * @deprecated in Falcon release in favour of an ordered list
Jonathan Harteb8c9472015-08-05 07:43:13 -0700143 */
Jonathan Hart00cddda2016-02-16 10:30:37 -0800144 @Deprecated
Jonathan Harteb8c9472015-08-05 07:43:13 -0700145 public Set<InterfaceIpAddress> ipAddresses() {
Jonathan Hart00cddda2016-02-16 10:30:37 -0800146 return ipAddresses.stream().collect(Collectors.toSet());
147 }
148
149 /**
150 * Retrieves a list of IP addresses that are assigned to the interface in
151 * the order that they were configured.
152 *
153 * @return list of IP addresses
154 */
155 public List<InterfaceIpAddress> ipAddressesList() {
Jonathan Harteb8c9472015-08-05 07:43:13 -0700156 return ipAddresses;
157 }
158
159 /**
160 * Retrieves the MAC address that is assigned to the interface.
161 *
162 * @return the MAC address
163 */
164 public MacAddress mac() {
165 return macAddress;
166 }
167
168 /**
169 * Retrieves the VLAN ID that is assigned to the interface.
170 *
171 * @return the VLAN ID
172 */
173 public VlanId vlan() {
174 return vlan;
175 }
176
177 @Override
178 public boolean equals(Object other) {
179 if (!(other instanceof Interface)) {
180 return false;
181 }
182
183 Interface otherInterface = (Interface) other;
184
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800185 return Objects.equals(name, otherInterface.name) &&
186 Objects.equals(connectPoint, otherInterface.connectPoint) &&
Jonathan Harteb8c9472015-08-05 07:43:13 -0700187 Objects.equals(ipAddresses, otherInterface.ipAddresses) &&
188 Objects.equals(macAddress, otherInterface.macAddress) &&
189 Objects.equals(vlan, otherInterface.vlan);
190 }
191
192 @Override
193 public int hashCode() {
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800194 return Objects.hash(connectPoint, name, ipAddresses, macAddress, vlan);
Jonathan Harteb8c9472015-08-05 07:43:13 -0700195 }
196
197 @Override
198 public String toString() {
199 return MoreObjects.toStringHelper(getClass())
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800200 .add("name", name)
Jonathan Harteb8c9472015-08-05 07:43:13 -0700201 .add("connectPoint", connectPoint)
202 .add("ipAddresses", ipAddresses)
203 .add("macAddress", macAddress)
204 .add("vlan", vlan)
205 .toString();
206 }
207}