blob: 829cffaf7f54dacffd7288b2ff068dae67bc09fc [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Jonathan Hartbac07a02014-10-13 21:29:54 -070019package org.onlab.onos.sdnip.config;
20
21import java.util.Objects;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070022import java.util.Set;
Jonathan Hartbac07a02014-10-13 21:29:54 -070023
Jonathan Hartbac07a02014-10-13 21:29:54 -070024import org.onlab.onos.net.ConnectPoint;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070025import org.onlab.onos.net.host.InterfaceIpAddress;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070026import org.onlab.onos.net.host.PortAddresses;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070027import org.onlab.packet.MacAddress;
28
29import com.google.common.base.MoreObjects;
30import com.google.common.collect.Sets;
Jonathan Hartbac07a02014-10-13 21:29:54 -070031
32/**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070033 * An Interface is a set of addresses that are logically mapped to a switch
34 * port in the network.
Jonathan Hartbac07a02014-10-13 21:29:54 -070035 */
36public class Interface {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070037 private final ConnectPoint connectPoint;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070038 private final Set<InterfaceIpAddress> ipAddresses;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070039 private final MacAddress macAddress;
Jonathan Hartbac07a02014-10-13 21:29:54 -070040
41 /**
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070042 * Creates an Interface based on a connection point, a set of interface
43 * IP addresses, and a MAC address.
Jonathan Hartbac07a02014-10-13 21:29:54 -070044 *
Jonathan Hartdc711bd2014-10-15 11:24:23 -070045 * @param connectPoint the connect point this interface is mapped to
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070046 * @param ipAddresses the IP addresses for the interface
Jonathan Hartdc711bd2014-10-15 11:24:23 -070047 * @param macAddress the MAC address of the interface
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 Hartdc711bd2014-10-15 11:24:23 -070051 MacAddress macAddress) {
52 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 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 Hartbac07a02014-10-13 21:29:54 -070066 }
67
68 /**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070069 * Retrieves the connection point that this interface maps to.
Jonathan Hartbac07a02014-10-13 21:29:54 -070070 *
Jonathan Hartdc711bd2014-10-15 11:24:23 -070071 * @return the connection point
Jonathan Hartbac07a02014-10-13 21:29:54 -070072 */
Jonathan Hartdc711bd2014-10-15 11:24:23 -070073 public ConnectPoint connectPoint() {
74 return connectPoint;
Jonathan Hartbac07a02014-10-13 21:29:54 -070075 }
76
77 /**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070078 * Retrieves the set of IP addresses that are assigned to the interface.
Jonathan Hartbac07a02014-10-13 21:29:54 -070079 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070080 * @return the set of interface IP addresses
Jonathan Hartbac07a02014-10-13 21:29:54 -070081 */
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070082 public Set<InterfaceIpAddress> ipAddresses() {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070083 return ipAddresses;
Jonathan Hartbac07a02014-10-13 21:29:54 -070084 }
85
Jonathan Hartdc711bd2014-10-15 11:24:23 -070086 /**
87 * Retrieves the MAC address that is assigned to the interface.
88 *
89 * @return the MAC address
90 */
91 public MacAddress mac() {
92 return macAddress;
93 }
94
Jonathan Hartbac07a02014-10-13 21:29:54 -070095 @Override
96 public boolean equals(Object other) {
97 if (!(other instanceof Interface)) {
98 return false;
99 }
100
101 Interface otherInterface = (Interface) other;
102
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700103 return connectPoint.equals(otherInterface.connectPoint) &&
104 ipAddresses.equals(otherInterface.ipAddresses) &&
105 macAddress.equals(otherInterface.macAddress);
Jonathan Hartbac07a02014-10-13 21:29:54 -0700106 }
107
108 @Override
109 public int hashCode() {
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700110 return Objects.hash(connectPoint, ipAddresses, macAddress);
111 }
112
113 @Override
114 public String toString() {
115 return MoreObjects.toStringHelper(getClass())
116 .add("connectPoint", connectPoint)
117 .add("ipAddresses", ipAddresses)
118 .add("macAddress", macAddress)
119 .toString();
Jonathan Hartbac07a02014-10-13 21:29:54 -0700120 }
121}