Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame^] | 20 | import com.google.common.collect.Lists; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.host.InterfaceIpAddress; |
| 25 | |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame^] | 26 | import java.util.List; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 27 | import java.util.Objects; |
| 28 | import java.util.Set; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame^] | 29 | import java.util.stream.Collectors; |
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 | */ |
Thomas Vachuska | 4c571ae | 2015-09-10 16:31:59 -0700 | [diff] [blame] | 37 | @Beta |
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; |
| 45 | private final VlanId vlan; |
| 46 | |
| 47 | /** |
| 48 | * Creates new Interface with the provided configuration. |
| 49 | * |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 50 | * @param name name of the interface |
| 51 | * @param connectPoint the connect point this interface maps to |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame^] | 52 | * @param ipAddresses list of IP addresses |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 53 | * @param macAddress MAC address |
| 54 | * @param vlan VLAN ID |
| 55 | */ |
| 56 | public Interface(String name, ConnectPoint connectPoint, |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame^] | 57 | 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 Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 78 | Set<InterfaceIpAddress> ipAddresses, |
| 79 | MacAddress macAddress, VlanId vlan) { |
| 80 | this.name = name == null ? NO_INTERFACE_NAME : name; |
| 81 | this.connectPoint = checkNotNull(connectPoint); |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame^] | 82 | this.ipAddresses = ipAddresses == null ? Lists.newArrayList() : |
| 83 | ipAddresses.stream().collect(Collectors.toList()); |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 84 | 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 Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 91 | * @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 Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame^] | 95 | * @deprecated in Falcon release - use constructors with names instead |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 96 | */ |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame^] | 97 | @Deprecated |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 98 | public Interface(ConnectPoint connectPoint, |
| 99 | Set<InterfaceIpAddress> ipAddresses, |
| 100 | MacAddress macAddress, VlanId vlan) { |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 101 | this(NO_INTERFACE_NAME, connectPoint, ipAddresses, macAddress, vlan); |
| 102 | } |
| 103 | |
| 104 | /** |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame^] | 105 | * 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 Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 121 | * Retrieves the name of the interface. |
| 122 | * |
| 123 | * @return name |
| 124 | */ |
| 125 | public String name() { |
| 126 | return name; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 127 | } |
| 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 Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame^] | 142 | * @deprecated in Falcon release in favour of an ordered list |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 143 | */ |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame^] | 144 | @Deprecated |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 145 | public Set<InterfaceIpAddress> ipAddresses() { |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame^] | 146 | 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 Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 156 | 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 Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 185 | return Objects.equals(name, otherInterface.name) && |
| 186 | Objects.equals(connectPoint, otherInterface.connectPoint) && |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 187 | 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 Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 194 | return Objects.hash(connectPoint, name, ipAddresses, macAddress, vlan); |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public String toString() { |
| 199 | return MoreObjects.toStringHelper(getClass()) |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 200 | .add("name", name) |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 201 | .add("connectPoint", connectPoint) |
| 202 | .add("ipAddresses", ipAddresses) |
| 203 | .add("macAddress", macAddress) |
| 204 | .add("vlan", vlan) |
| 205 | .toString(); |
| 206 | } |
| 207 | } |