blob: 9c059e49a48b9f1f0285a5a597dffc3c2a9ebc3a [file] [log] [blame]
Jonathan Harteb8c9472015-08-05 07:43:13 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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 */
Ray Milkeyfacf2862017-08-03 11:58:29 -070016package org.onosproject.net.intf;
Jonathan Harteb8c9472015-08-05 07:43:13 -070017
Ray Milkeyfacf2862017-08-03 11:58:29 -070018import java.util.List;
19import java.util.Objects;
20import java.util.Set;
21
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
Ray Milkeyfacf2862017-08-03 11:58:29 -070027import com.google.common.base.MoreObjects;
28import com.google.common.collect.ImmutableSet;
29import com.google.common.collect.Lists;
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 */
Ray Milkeyfacf2862017-08-03 11:58:29 -070037
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;
Charles Chanbae2cb22017-01-18 19:35:20 -080045 // TODO: Deprecate this due to ambiguity
Jonathan Harteb8c9472015-08-05 07:43:13 -070046 private final VlanId vlan;
Charles Chanbae2cb22017-01-18 19:35:20 -080047 private final VlanId vlanUntagged;
48 private final Set<VlanId> vlanTagged;
49 private final VlanId vlanNative;
Jonathan Harteb8c9472015-08-05 07:43:13 -070050
51 /**
52 * Creates new Interface with the provided configuration.
53 *
Jonathan Hart7dbe6292015-11-10 08:33:17 -080054 * @param name name of the interface
55 * @param connectPoint the connect point this interface maps to
Jonathan Hart00cddda2016-02-16 10:30:37 -080056 * @param ipAddresses list of IP addresses
Jonathan Hart7dbe6292015-11-10 08:33:17 -080057 * @param macAddress MAC address
58 * @param vlan VLAN ID
59 */
60 public Interface(String name, ConnectPoint connectPoint,
Jonathan Hart00cddda2016-02-16 10:30:37 -080061 List<InterfaceIpAddress> ipAddresses,
62 MacAddress macAddress, VlanId vlan) {
Charles Chanbae2cb22017-01-18 19:35:20 -080063 this(name, connectPoint, ipAddresses, macAddress, vlan, null, null, null);
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 list of IP addresses
72 * @param macAddress MAC address
73 * @param vlan VLAN ID
74 * @param vlanUntagged untagged VLAN.
75 * Cannot be used with vlanTagged or vlanNative.
76 * @param vlanTagged tagged VLANs.
77 * Cannot be used with vlanUntagged.
78 * @param vlanNative native vLAN. Optional.
79 * Can only be used when vlanTagged is specified. Cannot be used with vlanUntagged.
80 */
81 public Interface(String name, ConnectPoint connectPoint,
82 List<InterfaceIpAddress> ipAddresses,
83 MacAddress macAddress, VlanId vlan,
84 VlanId vlanUntagged, Set<VlanId> vlanTagged, VlanId vlanNative) {
Jonathan Hart00cddda2016-02-16 10:30:37 -080085 this.name = name == null ? NO_INTERFACE_NAME : name;
86 this.connectPoint = checkNotNull(connectPoint);
87 this.ipAddresses = ipAddresses == null ? Lists.newArrayList() : ipAddresses;
88 this.macAddress = macAddress == null ? MacAddress.NONE : macAddress;
89 this.vlan = vlan == null ? VlanId.NONE : vlan;
Charles Chanbae2cb22017-01-18 19:35:20 -080090 this.vlanUntagged = vlanUntagged == null ? VlanId.NONE : vlanUntagged;
91 this.vlanTagged = vlanTagged == null ? ImmutableSet.of() : ImmutableSet.copyOf(vlanTagged);
92 this.vlanNative = vlanNative == null ? VlanId.NONE : vlanNative;
Jonathan Hart00cddda2016-02-16 10:30:37 -080093 }
94
95 /**
Jonathan Hart7dbe6292015-11-10 08:33:17 -080096 * Retrieves the name of the interface.
97 *
98 * @return name
99 */
100 public String name() {
101 return name;
Jonathan Harteb8c9472015-08-05 07:43:13 -0700102 }
103
104 /**
105 * Retrieves the connection point that this interface maps to.
106 *
107 * @return the connection point
108 */
109 public ConnectPoint connectPoint() {
110 return connectPoint;
111 }
112
113 /**
Jonathan Hart00cddda2016-02-16 10:30:37 -0800114 * Retrieves a list of IP addresses that are assigned to the interface in
115 * the order that they were configured.
116 *
117 * @return list of IP addresses
118 */
119 public List<InterfaceIpAddress> ipAddressesList() {
Jonathan Harteb8c9472015-08-05 07:43:13 -0700120 return ipAddresses;
121 }
122
123 /**
124 * Retrieves the MAC address that is assigned to the interface.
125 *
126 * @return the MAC address
127 */
128 public MacAddress mac() {
129 return macAddress;
130 }
131
132 /**
133 * Retrieves the VLAN ID that is assigned to the interface.
134 *
135 * @return the VLAN ID
136 */
137 public VlanId vlan() {
138 return vlan;
139 }
140
Charles Chanbae2cb22017-01-18 19:35:20 -0800141 /**
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800142 * Retrieves the VLAN ID that is assigned to untagged packets.
Charles Chanbae2cb22017-01-18 19:35:20 -0800143 *
144 * @return the VLAN ID
145 */
146 public VlanId vlanUntagged() {
147 return vlanUntagged;
148 }
149
150 /**
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800151 * Retrieves the set of VLAN IDs that are allowed on this interface.
Charles Chanbae2cb22017-01-18 19:35:20 -0800152 *
153 * @return the VLAN ID
154 */
155 public Set<VlanId> vlanTagged() {
156 return vlanTagged;
157 }
158
159 /**
Saurav Dasb0ae6ee2017-03-04 16:08:47 -0800160 * Retrieves the VLAN ID that is assigned to untagged packets on this
161 * tagged interface.
Charles Chanbae2cb22017-01-18 19:35:20 -0800162 *
163 * @return the VLAN ID
164 */
165 public VlanId vlanNative() {
166 return vlanNative;
167 }
168
Jonathan Harteb8c9472015-08-05 07:43:13 -0700169 @Override
170 public boolean equals(Object other) {
171 if (!(other instanceof Interface)) {
172 return false;
173 }
174
175 Interface otherInterface = (Interface) other;
176
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800177 return Objects.equals(name, otherInterface.name) &&
178 Objects.equals(connectPoint, otherInterface.connectPoint) &&
Jonathan Harteb8c9472015-08-05 07:43:13 -0700179 Objects.equals(ipAddresses, otherInterface.ipAddresses) &&
180 Objects.equals(macAddress, otherInterface.macAddress) &&
Charles Chanbae2cb22017-01-18 19:35:20 -0800181 Objects.equals(vlan, otherInterface.vlan) &&
182 Objects.equals(vlanUntagged, otherInterface.vlanUntagged) &&
183 Objects.equals(vlanTagged, otherInterface.vlanTagged) &&
184 Objects.equals(vlanNative, otherInterface.vlanNative);
Jonathan Harteb8c9472015-08-05 07:43:13 -0700185 }
186
187 @Override
188 public int hashCode() {
Charles Chanbae2cb22017-01-18 19:35:20 -0800189 return Objects.hash(connectPoint, name, ipAddresses, macAddress, vlan,
190 vlanUntagged, vlanTagged, vlanNative);
Jonathan Harteb8c9472015-08-05 07:43:13 -0700191 }
192
193 @Override
194 public String toString() {
195 return MoreObjects.toStringHelper(getClass())
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800196 .add("name", name)
Jonathan Harteb8c9472015-08-05 07:43:13 -0700197 .add("connectPoint", connectPoint)
198 .add("ipAddresses", ipAddresses)
199 .add("macAddress", macAddress)
200 .add("vlan", vlan)
Charles Chanbae2cb22017-01-18 19:35:20 -0800201 .add("vlanUntagged", vlanUntagged)
202 .add("vlanTagged", vlanTagged)
203 .add("vlanNative", vlanNative)
Jonathan Harteb8c9472015-08-05 07:43:13 -0700204 .toString();
205 }
206}