blob: e2109689b24f67d0d043ea3d4c4c49b0ba79f84e [file] [log] [blame]
Jonathan Harteb8c9472015-08-05 07:43:13 -07001/*
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 */
16package org.onosproject.incubator.net.intf;
17
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070018import com.google.common.annotations.Beta;
Jonathan Harteb8c9472015-08-05 07:43:13 -070019import com.google.common.base.MoreObjects;
20import com.google.common.collect.Sets;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.host.InterfaceIpAddress;
25
26import java.util.Objects;
27import java.util.Set;
28
Jonathan Hart4cb39882015-08-12 23:50:55 -040029import static com.google.common.base.Preconditions.checkNotNull;
30
Jonathan Harteb8c9472015-08-05 07:43:13 -070031/**
32 * An Interface maps network configuration information (such as addresses and
Jonathan Hart7dbe6292015-11-10 08:33:17 -080033 * vlans) to a port in the network.
Jonathan Harteb8c9472015-08-05 07:43:13 -070034 */
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070035@Beta
Jonathan Harteb8c9472015-08-05 07:43:13 -070036public class Interface {
Jonathan Hart7dbe6292015-11-10 08:33:17 -080037 public static final String NO_INTERFACE_NAME = "";
38
39 private final String name;
Jonathan Harteb8c9472015-08-05 07:43:13 -070040 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 Hart7dbe6292015-11-10 08:33:17 -080048 * @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 Harteb8c9472015-08-05 07:43:13 -070067 * @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 Hart7dbe6292015-11-10 08:33:17 -080075 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 Harteb8c9472015-08-05 07:43:13 -070085 }
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 Hart7dbe6292015-11-10 08:33:17 -0800131 return Objects.equals(name, otherInterface.name) &&
132 Objects.equals(connectPoint, otherInterface.connectPoint) &&
Jonathan Harteb8c9472015-08-05 07:43:13 -0700133 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 Hart7dbe6292015-11-10 08:33:17 -0800140 return Objects.hash(connectPoint, name, ipAddresses, macAddress, vlan);
Jonathan Harteb8c9472015-08-05 07:43:13 -0700141 }
142
143 @Override
144 public String toString() {
145 return MoreObjects.toStringHelper(getClass())
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800146 .add("name", name)
Jonathan Harteb8c9472015-08-05 07:43:13 -0700147 .add("connectPoint", connectPoint)
148 .add("ipAddresses", ipAddresses)
149 .add("macAddress", macAddress)
150 .add("vlan", vlan)
151 .toString();
152 }
153}