Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.incubator.net.intf; |
| 17 | |
Thomas Vachuska | 4c571ae | 2015-09-10 16:31:59 -0700 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 19 | import com.google.common.base.MoreObjects; |
Charles Chan | bae2cb2 | 2017-01-18 19:35:20 -0800 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.host.InterfaceIpAddress; |
| 26 | |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 27 | import java.util.List; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 28 | import java.util.Objects; |
| 29 | import java.util.Set; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 30 | import java.util.stream.Collectors; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 31 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 34 | /** |
| 35 | * An Interface maps network configuration information (such as addresses and |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 36 | * vlans) to a port in the network. |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 37 | */ |
Thomas Vachuska | 4c571ae | 2015-09-10 16:31:59 -0700 | [diff] [blame] | 38 | @Beta |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 39 | public class Interface { |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 40 | public static final String NO_INTERFACE_NAME = ""; |
| 41 | |
| 42 | private final String name; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 43 | private final ConnectPoint connectPoint; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 44 | private final List<InterfaceIpAddress> ipAddresses; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 45 | private final MacAddress macAddress; |
Charles Chan | bae2cb2 | 2017-01-18 19:35:20 -0800 | [diff] [blame] | 46 | // TODO: Deprecate this due to ambiguity |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 47 | private final VlanId vlan; |
Charles Chan | bae2cb2 | 2017-01-18 19:35:20 -0800 | [diff] [blame] | 48 | private final VlanId vlanUntagged; |
| 49 | private final Set<VlanId> vlanTagged; |
| 50 | private final VlanId vlanNative; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Creates new Interface with the provided configuration. |
| 54 | * |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 55 | * @param name name of the interface |
| 56 | * @param connectPoint the connect point this interface maps to |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 57 | * @param ipAddresses list of IP addresses |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 58 | * @param macAddress MAC address |
| 59 | * @param vlan VLAN ID |
| 60 | */ |
| 61 | public Interface(String name, ConnectPoint connectPoint, |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 62 | List<InterfaceIpAddress> ipAddresses, |
| 63 | MacAddress macAddress, VlanId vlan) { |
Charles Chan | bae2cb2 | 2017-01-18 19:35:20 -0800 | [diff] [blame] | 64 | 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 Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 86 | 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 Chan | bae2cb2 | 2017-01-18 19:35:20 -0800 | [diff] [blame] | 91 | 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 Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 97 | * Retrieves the name of the interface. |
| 98 | * |
| 99 | * @return name |
| 100 | */ |
| 101 | public String name() { |
| 102 | return name; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 103 | } |
| 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 Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 118 | * @deprecated in Falcon release in favour of an ordered list |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 119 | */ |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 120 | @Deprecated |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 121 | public Set<InterfaceIpAddress> ipAddresses() { |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 122 | 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 Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 132 | 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 Chan | bae2cb2 | 2017-01-18 19:35:20 -0800 | [diff] [blame] | 153 | /** |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 154 | * Retrieves the VLAN ID that is assigned to untagged packets. |
Charles Chan | bae2cb2 | 2017-01-18 19:35:20 -0800 | [diff] [blame] | 155 | * |
| 156 | * @return the VLAN ID |
| 157 | */ |
| 158 | public VlanId vlanUntagged() { |
| 159 | return vlanUntagged; |
| 160 | } |
| 161 | |
| 162 | /** |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 163 | * Retrieves the set of VLAN IDs that are allowed on this interface. |
Charles Chan | bae2cb2 | 2017-01-18 19:35:20 -0800 | [diff] [blame] | 164 | * |
| 165 | * @return the VLAN ID |
| 166 | */ |
| 167 | public Set<VlanId> vlanTagged() { |
| 168 | return vlanTagged; |
| 169 | } |
| 170 | |
| 171 | /** |
Saurav Das | b0ae6ee | 2017-03-04 16:08:47 -0800 | [diff] [blame] | 172 | * Retrieves the VLAN ID that is assigned to untagged packets on this |
| 173 | * tagged interface. |
Charles Chan | bae2cb2 | 2017-01-18 19:35:20 -0800 | [diff] [blame] | 174 | * |
| 175 | * @return the VLAN ID |
| 176 | */ |
| 177 | public VlanId vlanNative() { |
| 178 | return vlanNative; |
| 179 | } |
| 180 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 181 | @Override |
| 182 | public boolean equals(Object other) { |
| 183 | if (!(other instanceof Interface)) { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | Interface otherInterface = (Interface) other; |
| 188 | |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 189 | return Objects.equals(name, otherInterface.name) && |
| 190 | Objects.equals(connectPoint, otherInterface.connectPoint) && |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 191 | Objects.equals(ipAddresses, otherInterface.ipAddresses) && |
| 192 | Objects.equals(macAddress, otherInterface.macAddress) && |
Charles Chan | bae2cb2 | 2017-01-18 19:35:20 -0800 | [diff] [blame] | 193 | Objects.equals(vlan, otherInterface.vlan) && |
| 194 | Objects.equals(vlanUntagged, otherInterface.vlanUntagged) && |
| 195 | Objects.equals(vlanTagged, otherInterface.vlanTagged) && |
| 196 | Objects.equals(vlanNative, otherInterface.vlanNative); |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public int hashCode() { |
Charles Chan | bae2cb2 | 2017-01-18 19:35:20 -0800 | [diff] [blame] | 201 | return Objects.hash(connectPoint, name, ipAddresses, macAddress, vlan, |
| 202 | vlanUntagged, vlanTagged, vlanNative); |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public String toString() { |
| 207 | return MoreObjects.toStringHelper(getClass()) |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 208 | .add("name", name) |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 209 | .add("connectPoint", connectPoint) |
| 210 | .add("ipAddresses", ipAddresses) |
| 211 | .add("macAddress", macAddress) |
| 212 | .add("vlan", vlan) |
Charles Chan | bae2cb2 | 2017-01-18 19:35:20 -0800 | [diff] [blame] | 213 | .add("vlanUntagged", vlanUntagged) |
| 214 | .add("vlanTagged", vlanTagged) |
| 215 | .add("vlanNative", vlanNative) |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 216 | .toString(); |
| 217 | } |
| 218 | } |