blob: 21796534e5498a2f12df9b3f2496bb4a72362df7 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip.config;
Jonathan Hartbac07a02014-10-13 21:29:54 -070017
18import java.util.Objects;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070019import java.util.Set;
Jonathan Hartbac07a02014-10-13 21:29:54 -070020
Pavlin Radoslavov2020eac2015-01-06 17:26:10 -080021import org.onlab.packet.MacAddress;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080022import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.host.InterfaceIpAddress;
25import org.onosproject.net.host.PortAddresses;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070026
27import com.google.common.base.MoreObjects;
28import com.google.common.collect.Sets;
Jonathan Hartbac07a02014-10-13 21:29:54 -070029
30/**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070031 * An Interface is a set of addresses that are logically mapped to a switch
32 * port in the network.
Jonathan Hartbac07a02014-10-13 21:29:54 -070033 */
34public class Interface {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070035 private final ConnectPoint connectPoint;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070036 private final Set<InterfaceIpAddress> ipAddresses;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070037 private final MacAddress macAddress;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080038 private final VlanId vlan;
Jonathan Hartbac07a02014-10-13 21:29:54 -070039
40 /**
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070041 * Creates an Interface based on a connection point, a set of interface
42 * IP addresses, and a MAC address.
Jonathan Hartbac07a02014-10-13 21:29:54 -070043 *
Jonathan Hartdc711bd2014-10-15 11:24:23 -070044 * @param connectPoint the connect point this interface is mapped to
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070045 * @param ipAddresses the IP addresses for the interface
Jonathan Hartdc711bd2014-10-15 11:24:23 -070046 * @param macAddress the MAC address of the interface
Ray Milkeyf25d1352015-01-23 15:14:08 -080047 * @param vlan VLAN identifier
Jonathan Hartbac07a02014-10-13 21:29:54 -070048 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070049 public Interface(ConnectPoint connectPoint,
50 Set<InterfaceIpAddress> ipAddresses,
Jonathan Hart6cd2f352015-01-13 17:44:45 -080051 MacAddress macAddress, VlanId vlan) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070052 this.connectPoint = connectPoint;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070053 this.ipAddresses = Sets.newHashSet(ipAddresses);
Jonathan Hartdc711bd2014-10-15 11:24:23 -070054 this.macAddress = macAddress;
Jonathan Hart6cd2f352015-01-13 17:44:45 -080055 this.vlan = vlan;
Jonathan Hartbac07a02014-10-13 21:29:54 -070056 }
57
58 /**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070059 * Creates an Interface based on a PortAddresses object.
Jonathan Hartbac07a02014-10-13 21:29:54 -070060 *
Jonathan Hartdc711bd2014-10-15 11:24:23 -070061 * @param portAddresses the PortAddresses object to turn into an Interface
Jonathan Hartbac07a02014-10-13 21:29:54 -070062 */
Jonathan Hartdc711bd2014-10-15 11:24:23 -070063 public Interface(PortAddresses portAddresses) {
64 connectPoint = portAddresses.connectPoint();
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070065 ipAddresses = Sets.newHashSet(portAddresses.ipAddresses());
Jonathan Hartdc711bd2014-10-15 11:24:23 -070066 macAddress = portAddresses.mac();
Jonathan Hart6cd2f352015-01-13 17:44:45 -080067 vlan = portAddresses.vlan();
Jonathan Hartbac07a02014-10-13 21:29:54 -070068 }
69
70 /**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070071 * Retrieves the connection point that this interface maps to.
Jonathan Hartbac07a02014-10-13 21:29:54 -070072 *
Jonathan Hartdc711bd2014-10-15 11:24:23 -070073 * @return the connection point
Jonathan Hartbac07a02014-10-13 21:29:54 -070074 */
Jonathan Hartdc711bd2014-10-15 11:24:23 -070075 public ConnectPoint connectPoint() {
76 return connectPoint;
Jonathan Hartbac07a02014-10-13 21:29:54 -070077 }
78
79 /**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070080 * Retrieves the set of IP addresses that are assigned to the interface.
Jonathan Hartbac07a02014-10-13 21:29:54 -070081 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070082 * @return the set of interface IP addresses
Jonathan Hartbac07a02014-10-13 21:29:54 -070083 */
Jonathan Hart6cd2f352015-01-13 17:44:45 -080084 public Set<InterfaceIpAddress> ipAddresses() {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070085 return ipAddresses;
Jonathan Hartbac07a02014-10-13 21:29:54 -070086 }
87
Jonathan Hart6cd2f352015-01-13 17:44:45 -080088 /**
89 * Retrieves the MAC address that is assigned to the interface.
90 *
91 * @return the MAC address
92 */
93 public MacAddress mac() {
94 return macAddress;
95 }
96
97 /**
98 * Retrieves the VLAN ID that is assigned to the interface.
99 *
100 * @return the VLAN ID
101 */
102 public VlanId vlan() {
103 return vlan;
104 }
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700105
Jonathan Hartbac07a02014-10-13 21:29:54 -0700106 @Override
107 public boolean equals(Object other) {
108 if (!(other instanceof Interface)) {
109 return false;
110 }
111
112 Interface otherInterface = (Interface) other;
113
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700114 return connectPoint.equals(otherInterface.connectPoint) &&
115 ipAddresses.equals(otherInterface.ipAddresses) &&
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800116 macAddress.equals(otherInterface.macAddress) &&
117 vlan.equals(otherInterface.vlan);
Jonathan Hartbac07a02014-10-13 21:29:54 -0700118 }
119
120 @Override
121 public int hashCode() {
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800122 return Objects.hash(connectPoint, ipAddresses, macAddress, vlan);
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700123 }
124
125 @Override
126 public String toString() {
127 return MoreObjects.toStringHelper(getClass())
128 .add("connectPoint", connectPoint)
129 .add("ipAddresses", ipAddresses)
130 .add("macAddress", macAddress)
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800131 .add("vlan", vlan)
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700132 .toString();
Jonathan Hartbac07a02014-10-13 21:29:54 -0700133 }
134}