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; |
| 20 | import com.google.common.collect.Sets; |
| 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 | |
| 26 | import java.util.Objects; |
| 27 | import java.util.Set; |
| 28 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 31 | /** |
| 32 | * An Interface maps network configuration information (such as addresses and |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 33 | * vlans) to a port in the network. |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 34 | */ |
Thomas Vachuska | 4c571ae | 2015-09-10 16:31:59 -0700 | [diff] [blame] | 35 | @Beta |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 36 | public class Interface { |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 37 | public static final String NO_INTERFACE_NAME = ""; |
| 38 | |
| 39 | private final String name; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 40 | private final ConnectPoint connectPoint; |
| 41 | private final Set<InterfaceIpAddress> ipAddresses; |
| 42 | private final MacAddress macAddress; |
| 43 | private final VlanId vlan; |
| 44 | |
| 45 | /** |
| 46 | * Creates new Interface with the provided configuration. |
| 47 | * |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 48 | * @param name name of the interface |
| 49 | * @param connectPoint the connect point this interface maps to |
| 50 | * @param ipAddresses Set of IP addresses |
| 51 | * @param macAddress MAC address |
| 52 | * @param vlan VLAN ID |
| 53 | */ |
| 54 | public Interface(String name, ConnectPoint connectPoint, |
| 55 | Set<InterfaceIpAddress> ipAddresses, |
| 56 | MacAddress macAddress, VlanId vlan) { |
| 57 | this.name = name == null ? NO_INTERFACE_NAME : name; |
| 58 | this.connectPoint = checkNotNull(connectPoint); |
| 59 | this.ipAddresses = ipAddresses == null ? Sets.newHashSet() : ipAddresses; |
| 60 | this.macAddress = macAddress == null ? MacAddress.NONE : macAddress; |
| 61 | this.vlan = vlan == null ? VlanId.NONE : vlan; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Creates new Interface with the provided configuration. |
| 66 | * |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 67 | * @param connectPoint the connect point this interface maps to |
| 68 | * @param ipAddresses Set of IP addresses |
| 69 | * @param macAddress MAC address |
| 70 | * @param vlan VLAN ID |
| 71 | */ |
| 72 | public Interface(ConnectPoint connectPoint, |
| 73 | Set<InterfaceIpAddress> ipAddresses, |
| 74 | MacAddress macAddress, VlanId vlan) { |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 75 | this(NO_INTERFACE_NAME, connectPoint, ipAddresses, macAddress, vlan); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Retrieves the name of the interface. |
| 80 | * |
| 81 | * @return name |
| 82 | */ |
| 83 | public String name() { |
| 84 | return name; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Retrieves the connection point that this interface maps to. |
| 89 | * |
| 90 | * @return the connection point |
| 91 | */ |
| 92 | public ConnectPoint connectPoint() { |
| 93 | return connectPoint; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Retrieves the set of IP addresses that are assigned to the interface. |
| 98 | * |
| 99 | * @return the set of interface IP addresses |
| 100 | */ |
| 101 | public Set<InterfaceIpAddress> ipAddresses() { |
| 102 | return ipAddresses; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Retrieves the MAC address that is assigned to the interface. |
| 107 | * |
| 108 | * @return the MAC address |
| 109 | */ |
| 110 | public MacAddress mac() { |
| 111 | return macAddress; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Retrieves the VLAN ID that is assigned to the interface. |
| 116 | * |
| 117 | * @return the VLAN ID |
| 118 | */ |
| 119 | public VlanId vlan() { |
| 120 | return vlan; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public boolean equals(Object other) { |
| 125 | if (!(other instanceof Interface)) { |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | Interface otherInterface = (Interface) other; |
| 130 | |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 131 | return Objects.equals(name, otherInterface.name) && |
| 132 | Objects.equals(connectPoint, otherInterface.connectPoint) && |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 133 | Objects.equals(ipAddresses, otherInterface.ipAddresses) && |
| 134 | Objects.equals(macAddress, otherInterface.macAddress) && |
| 135 | Objects.equals(vlan, otherInterface.vlan); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public int hashCode() { |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 140 | return Objects.hash(connectPoint, name, ipAddresses, macAddress, vlan); |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public String toString() { |
| 145 | return MoreObjects.toStringHelper(getClass()) |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 146 | .add("name", name) |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 147 | .add("connectPoint", connectPoint) |
| 148 | .add("ipAddresses", ipAddresses) |
| 149 | .add("macAddress", macAddress) |
| 150 | .add("vlan", vlan) |
| 151 | .toString(); |
| 152 | } |
| 153 | } |