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