blob: e3d8ee0c17a95ffed263111f3092350a414ed9b8 [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;
Charles Chanbae2cb22017-01-18 19:35:20 -080020import com.google.common.collect.ImmutableSet;
Jonathan Hart00cddda2016-02-16 10:30:37 -080021import com.google.common.collect.Lists;
Jonathan Harteb8c9472015-08-05 07:43:13 -070022import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
24import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.host.InterfaceIpAddress;
26
Jonathan Hart00cddda2016-02-16 10:30:37 -080027import java.util.List;
Jonathan Harteb8c9472015-08-05 07:43:13 -070028import java.util.Objects;
29import java.util.Set;
Jonathan Hart00cddda2016-02-16 10:30:37 -080030import java.util.stream.Collectors;
Jonathan Harteb8c9472015-08-05 07:43:13 -070031
Jonathan Hart4cb39882015-08-12 23:50:55 -040032import static com.google.common.base.Preconditions.checkNotNull;
33
Jonathan Harteb8c9472015-08-05 07:43:13 -070034/**
35 * An Interface maps network configuration information (such as addresses and
Jonathan Hart7dbe6292015-11-10 08:33:17 -080036 * vlans) to a port in the network.
Jonathan Harteb8c9472015-08-05 07:43:13 -070037 */
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070038@Beta
Jonathan Harteb8c9472015-08-05 07:43:13 -070039public class Interface {
Jonathan Hart7dbe6292015-11-10 08:33:17 -080040 public static final String NO_INTERFACE_NAME = "";
41
42 private final String name;
Jonathan Harteb8c9472015-08-05 07:43:13 -070043 private final ConnectPoint connectPoint;
Jonathan Hart00cddda2016-02-16 10:30:37 -080044 private final List<InterfaceIpAddress> ipAddresses;
Jonathan Harteb8c9472015-08-05 07:43:13 -070045 private final MacAddress macAddress;
Charles Chanbae2cb22017-01-18 19:35:20 -080046 // TODO: Deprecate this due to ambiguity
Jonathan Harteb8c9472015-08-05 07:43:13 -070047 private final VlanId vlan;
Charles Chanbae2cb22017-01-18 19:35:20 -080048 private final VlanId vlanUntagged;
49 private final Set<VlanId> vlanTagged;
50 private final VlanId vlanNative;
Jonathan Harteb8c9472015-08-05 07:43:13 -070051
52 /**
53 * Creates new Interface with the provided configuration.
54 *
Jonathan Hart7dbe6292015-11-10 08:33:17 -080055 * @param name name of the interface
56 * @param connectPoint the connect point this interface maps to
Jonathan Hart00cddda2016-02-16 10:30:37 -080057 * @param ipAddresses list of IP addresses
Jonathan Hart7dbe6292015-11-10 08:33:17 -080058 * @param macAddress MAC address
59 * @param vlan VLAN ID
60 */
61 public Interface(String name, ConnectPoint connectPoint,
Jonathan Hart00cddda2016-02-16 10:30:37 -080062 List<InterfaceIpAddress> ipAddresses,
63 MacAddress macAddress, VlanId vlan) {
Charles Chanbae2cb22017-01-18 19:35:20 -080064 this(name, connectPoint, ipAddresses, macAddress, vlan, null, null, null);
65 }
66
67 /**
68 * Creates new Interface with the provided configuration.
69 *
70 * @param name name of the interface
71 * @param connectPoint the connect point this interface maps to
72 * @param ipAddresses list of IP addresses
73 * @param macAddress MAC address
74 * @param vlan VLAN ID
75 * @param vlanUntagged untagged VLAN.
76 * Cannot be used with vlanTagged or vlanNative.
77 * @param vlanTagged tagged VLANs.
78 * Cannot be used with vlanUntagged.
79 * @param vlanNative native vLAN. Optional.
80 * Can only be used when vlanTagged is specified. Cannot be used with vlanUntagged.
81 */
82 public Interface(String name, ConnectPoint connectPoint,
83 List<InterfaceIpAddress> ipAddresses,
84 MacAddress macAddress, VlanId vlan,
85 VlanId vlanUntagged, Set<VlanId> vlanTagged, VlanId vlanNative) {
Jonathan Hart00cddda2016-02-16 10:30:37 -080086 this.name = name == null ? NO_INTERFACE_NAME : name;
87 this.connectPoint = checkNotNull(connectPoint);
88 this.ipAddresses = ipAddresses == null ? Lists.newArrayList() : ipAddresses;
89 this.macAddress = macAddress == null ? MacAddress.NONE : macAddress;
90 this.vlan = vlan == null ? VlanId.NONE : vlan;
Charles Chanbae2cb22017-01-18 19:35:20 -080091 this.vlanUntagged = vlanUntagged == null ? VlanId.NONE : vlanUntagged;
92 this.vlanTagged = vlanTagged == null ? ImmutableSet.of() : ImmutableSet.copyOf(vlanTagged);
93 this.vlanNative = vlanNative == null ? VlanId.NONE : vlanNative;
Jonathan Hart00cddda2016-02-16 10:30:37 -080094 }
95
96 /**
Jonathan Hart7dbe6292015-11-10 08:33:17 -080097 * Retrieves the name of the interface.
98 *
99 * @return name
100 */
101 public String name() {
102 return name;
Jonathan Harteb8c9472015-08-05 07:43:13 -0700103 }
104
105 /**
106 * Retrieves the connection point that this interface maps to.
107 *
108 * @return the connection point
109 */
110 public ConnectPoint connectPoint() {
111 return connectPoint;
112 }
113
114 /**
115 * Retrieves the set of IP addresses that are assigned to the interface.
116 *
117 * @return the set of interface IP addresses
Jonathan Hart00cddda2016-02-16 10:30:37 -0800118 * @deprecated in Falcon release in favour of an ordered list
Jonathan Harteb8c9472015-08-05 07:43:13 -0700119 */
Jonathan Hart00cddda2016-02-16 10:30:37 -0800120 @Deprecated
Jonathan Harteb8c9472015-08-05 07:43:13 -0700121 public Set<InterfaceIpAddress> ipAddresses() {
Jonathan Hart00cddda2016-02-16 10:30:37 -0800122 return ipAddresses.stream().collect(Collectors.toSet());
123 }
124
125 /**
126 * Retrieves a list of IP addresses that are assigned to the interface in
127 * the order that they were configured.
128 *
129 * @return list of IP addresses
130 */
131 public List<InterfaceIpAddress> ipAddressesList() {
Jonathan Harteb8c9472015-08-05 07:43:13 -0700132 return ipAddresses;
133 }
134
135 /**
136 * Retrieves the MAC address that is assigned to the interface.
137 *
138 * @return the MAC address
139 */
140 public MacAddress mac() {
141 return macAddress;
142 }
143
144 /**
145 * Retrieves the VLAN ID that is assigned to the interface.
146 *
147 * @return the VLAN ID
148 */
149 public VlanId vlan() {
150 return vlan;
151 }
152
Charles Chanbae2cb22017-01-18 19:35:20 -0800153 /**
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800154 * Retrieves the VLAN ID that is assigned to untagged packets.
Charles Chanbae2cb22017-01-18 19:35:20 -0800155 *
156 * @return the VLAN ID
157 */
158 public VlanId vlanUntagged() {
159 return vlanUntagged;
160 }
161
162 /**
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800163 * Retrieves the set of VLAN IDs that are allowed on this interface.
Charles Chanbae2cb22017-01-18 19:35:20 -0800164 *
165 * @return the VLAN ID
166 */
167 public Set<VlanId> vlanTagged() {
168 return vlanTagged;
169 }
170
171 /**
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800172 * Retrieves the VLAN ID that is assigned to untagged packets on this
173 * tagged interface.
Charles Chanbae2cb22017-01-18 19:35:20 -0800174 *
175 * @return the VLAN ID
176 */
177 public VlanId vlanNative() {
178 return vlanNative;
179 }
180
Jonathan Harteb8c9472015-08-05 07:43:13 -0700181 @Override
182 public boolean equals(Object other) {
183 if (!(other instanceof Interface)) {
184 return false;
185 }
186
187 Interface otherInterface = (Interface) other;
188
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800189 return Objects.equals(name, otherInterface.name) &&
190 Objects.equals(connectPoint, otherInterface.connectPoint) &&
Jonathan Harteb8c9472015-08-05 07:43:13 -0700191 Objects.equals(ipAddresses, otherInterface.ipAddresses) &&
192 Objects.equals(macAddress, otherInterface.macAddress) &&
Charles Chanbae2cb22017-01-18 19:35:20 -0800193 Objects.equals(vlan, otherInterface.vlan) &&
194 Objects.equals(vlanUntagged, otherInterface.vlanUntagged) &&
195 Objects.equals(vlanTagged, otherInterface.vlanTagged) &&
196 Objects.equals(vlanNative, otherInterface.vlanNative);
Jonathan Harteb8c9472015-08-05 07:43:13 -0700197 }
198
199 @Override
200 public int hashCode() {
Charles Chanbae2cb22017-01-18 19:35:20 -0800201 return Objects.hash(connectPoint, name, ipAddresses, macAddress, vlan,
202 vlanUntagged, vlanTagged, vlanNative);
Jonathan Harteb8c9472015-08-05 07:43:13 -0700203 }
204
205 @Override
206 public String toString() {
207 return MoreObjects.toStringHelper(getClass())
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800208 .add("name", name)
Jonathan Harteb8c9472015-08-05 07:43:13 -0700209 .add("connectPoint", connectPoint)
210 .add("ipAddresses", ipAddresses)
211 .add("macAddress", macAddress)
212 .add("vlan", vlan)
Charles Chanbae2cb22017-01-18 19:35:20 -0800213 .add("vlanUntagged", vlanUntagged)
214 .add("vlanTagged", vlanTagged)
215 .add("vlanNative", vlanNative)
Jonathan Harteb8c9472015-08-05 07:43:13 -0700216 .toString();
217 }
218}