blob: ac9e34bd8b01b73b1c7bb530120d5ec4ad4500b4 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hart2da1e602015-02-18 19:09:24 -080016package org.onosproject.routing.config;
Jonathan Hartbac07a02014-10-13 21:29:54 -070017
Jonathan Hart90a02c22015-02-13 11:52:07 -080018import com.google.common.base.MoreObjects;
19import com.google.common.collect.Sets;
Pavlin Radoslavov2020eac2015-01-06 17:26:10 -080020import org.onlab.packet.MacAddress;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080021import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.host.InterfaceIpAddress;
24import org.onosproject.net.host.PortAddresses;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070025
Jonathan Hart90a02c22015-02-13 11:52:07 -080026import java.util.Objects;
27import java.util.Set;
Jonathan Hartbac07a02014-10-13 21:29:54 -070028
29/**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070030 * An Interface is a set of addresses that are logically mapped to a switch
31 * port in the network.
Jonathan Hartbac07a02014-10-13 21:29:54 -070032 */
33public class Interface {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070034 private final ConnectPoint connectPoint;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070035 private final Set<InterfaceIpAddress> ipAddresses;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070036 private final MacAddress macAddress;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080037 private final VlanId vlan;
Jonathan Hartbac07a02014-10-13 21:29:54 -070038
39 /**
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070040 * Creates an Interface based on a connection point, a set of interface
41 * IP addresses, and a MAC address.
Jonathan Hartbac07a02014-10-13 21:29:54 -070042 *
Jonathan Hartdc711bd2014-10-15 11:24:23 -070043 * @param connectPoint the connect point this interface is mapped to
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070044 * @param ipAddresses the IP addresses for the interface
Jonathan Hartdc711bd2014-10-15 11:24:23 -070045 * @param macAddress the MAC address of the interface
Ray Milkeyf25d1352015-01-23 15:14:08 -080046 * @param vlan VLAN identifier
Jonathan Hartbac07a02014-10-13 21:29:54 -070047 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070048 public Interface(ConnectPoint connectPoint,
49 Set<InterfaceIpAddress> ipAddresses,
Jonathan Hart6cd2f352015-01-13 17:44:45 -080050 MacAddress macAddress, VlanId vlan) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070051 this.connectPoint = connectPoint;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070052 this.ipAddresses = Sets.newHashSet(ipAddresses);
Jonathan Hartdc711bd2014-10-15 11:24:23 -070053 this.macAddress = macAddress;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080054 this.vlan = vlan;
Jonathan Hartbac07a02014-10-13 21:29:54 -070055 }
56
57 /**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070058 * Creates an Interface based on a PortAddresses object.
Jonathan Hartbac07a02014-10-13 21:29:54 -070059 *
Jonathan Hartdc711bd2014-10-15 11:24:23 -070060 * @param portAddresses the PortAddresses object to turn into an Interface
Jonathan Hartbac07a02014-10-13 21:29:54 -070061 */
Jonathan Hartdc711bd2014-10-15 11:24:23 -070062 public Interface(PortAddresses portAddresses) {
63 connectPoint = portAddresses.connectPoint();
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070064 ipAddresses = Sets.newHashSet(portAddresses.ipAddresses());
Jonathan Hartdc711bd2014-10-15 11:24:23 -070065 macAddress = portAddresses.mac();
Jonathan Hart6cd2f352015-01-13 17:44:45 -080066 vlan = portAddresses.vlan();
Jonathan Hartbac07a02014-10-13 21:29:54 -070067 }
68
69 /**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070070 * Retrieves the connection point that this interface maps to.
Jonathan Hartbac07a02014-10-13 21:29:54 -070071 *
Jonathan Hartdc711bd2014-10-15 11:24:23 -070072 * @return the connection point
Jonathan Hartbac07a02014-10-13 21:29:54 -070073 */
Jonathan Hartdc711bd2014-10-15 11:24:23 -070074 public ConnectPoint connectPoint() {
75 return connectPoint;
Jonathan Hartbac07a02014-10-13 21:29:54 -070076 }
77
78 /**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070079 * Retrieves the set of IP addresses that are assigned to the interface.
Jonathan Hartbac07a02014-10-13 21:29:54 -070080 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070081 * @return the set of interface IP addresses
Jonathan Hartbac07a02014-10-13 21:29:54 -070082 */
Jonathan Hart6cd2f352015-01-13 17:44:45 -080083 public Set<InterfaceIpAddress> ipAddresses() {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070084 return ipAddresses;
Jonathan Hartbac07a02014-10-13 21:29:54 -070085 }
86
Jonathan Hart6cd2f352015-01-13 17:44:45 -080087 /**
88 * Retrieves the MAC address that is assigned to the interface.
89 *
90 * @return the MAC address
91 */
92 public MacAddress mac() {
93 return macAddress;
94 }
95
96 /**
97 * Retrieves the VLAN ID that is assigned to the interface.
98 *
99 * @return the VLAN ID
100 */
101 public VlanId vlan() {
102 return vlan;
103 }
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700104
Jonathan Hartbac07a02014-10-13 21:29:54 -0700105 @Override
106 public boolean equals(Object other) {
107 if (!(other instanceof Interface)) {
108 return false;
109 }
110
111 Interface otherInterface = (Interface) other;
112
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700113 return connectPoint.equals(otherInterface.connectPoint) &&
114 ipAddresses.equals(otherInterface.ipAddresses) &&
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800115 macAddress.equals(otherInterface.macAddress) &&
116 vlan.equals(otherInterface.vlan);
Jonathan Hartbac07a02014-10-13 21:29:54 -0700117 }
118
119 @Override
120 public int hashCode() {
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800121 return Objects.hash(connectPoint, ipAddresses, macAddress, vlan);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700122 }
123
124 @Override
125 public String toString() {
126 return MoreObjects.toStringHelper(getClass())
127 .add("connectPoint", connectPoint)
128 .add("ipAddresses", ipAddresses)
129 .add("macAddress", macAddress)
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800130 .add("vlan", vlan)
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700131 .toString();
Jonathan Hartbac07a02014-10-13 21:29:54 -0700132 }
133}