blob: 52c4cdeec288a8384fe1a85403999bbbdafcf7c3 [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;
Jonathan Hartdc711bd2014-10-15 11:24:23 -070025import org.onlab.onos.net.host.PortAddresses;
Jonathan Hartbac07a02014-10-13 21:29:54 -070026import org.onlab.packet.IpPrefix;
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;
38 private final Set<IpPrefix> ipAddresses;
39 private final MacAddress macAddress;
Jonathan Hartbac07a02014-10-13 21:29:54 -070040
41 /**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070042 * Creates an Interface based on a connection point, a set of IP addresses
43 * 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
46 * @param prefixAddress the IP addresses for the interface
47 * @param macAddress the MAC address of the interface
Jonathan Hartbac07a02014-10-13 21:29:54 -070048 */
Jonathan Hartdc711bd2014-10-15 11:24:23 -070049 public Interface(ConnectPoint connectPoint, Set<IpPrefix> prefixAddress,
50 MacAddress macAddress) {
51 this.connectPoint = connectPoint;
52 this.ipAddresses = Sets.newHashSet(prefixAddress);
53 this.macAddress = macAddress;
Jonathan Hartbac07a02014-10-13 21:29:54 -070054 }
55
56 /**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070057 * Creates an Interface based on a PortAddresses object.
Jonathan Hartbac07a02014-10-13 21:29:54 -070058 *
Jonathan Hartdc711bd2014-10-15 11:24:23 -070059 * @param portAddresses the PortAddresses object to turn into an Interface
Jonathan Hartbac07a02014-10-13 21:29:54 -070060 */
Jonathan Hartdc711bd2014-10-15 11:24:23 -070061 public Interface(PortAddresses portAddresses) {
62 connectPoint = portAddresses.connectPoint();
63 ipAddresses = Sets.newHashSet(portAddresses.ips());
64 macAddress = portAddresses.mac();
Jonathan Hartbac07a02014-10-13 21:29:54 -070065 }
66
67 /**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070068 * Retrieves the connection point that this interface maps to.
Jonathan Hartbac07a02014-10-13 21:29:54 -070069 *
Jonathan Hartdc711bd2014-10-15 11:24:23 -070070 * @return the connection point
Jonathan Hartbac07a02014-10-13 21:29:54 -070071 */
Jonathan Hartdc711bd2014-10-15 11:24:23 -070072 public ConnectPoint connectPoint() {
73 return connectPoint;
Jonathan Hartbac07a02014-10-13 21:29:54 -070074 }
75
76 /**
Jonathan Hartdc711bd2014-10-15 11:24:23 -070077 * Retrieves the set of IP addresses that are assigned to the interface.
Jonathan Hartbac07a02014-10-13 21:29:54 -070078 *
Jonathan Hartdc711bd2014-10-15 11:24:23 -070079 * @return the set of IP addresses
Jonathan Hartbac07a02014-10-13 21:29:54 -070080 */
Jonathan Hartdc711bd2014-10-15 11:24:23 -070081 public Set<IpPrefix> ips() {
82 return ipAddresses;
Jonathan Hartbac07a02014-10-13 21:29:54 -070083 }
84
Jonathan Hartdc711bd2014-10-15 11:24:23 -070085 /**
86 * Retrieves the MAC address that is assigned to the interface.
87 *
88 * @return the MAC address
89 */
90 public MacAddress mac() {
91 return macAddress;
92 }
93
Jonathan Hartbac07a02014-10-13 21:29:54 -070094 @Override
95 public boolean equals(Object other) {
96 if (!(other instanceof Interface)) {
97 return false;
98 }
99
100 Interface otherInterface = (Interface) other;
101
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700102 return connectPoint.equals(otherInterface.connectPoint) &&
103 ipAddresses.equals(otherInterface.ipAddresses) &&
104 macAddress.equals(otherInterface.macAddress);
Jonathan Hartbac07a02014-10-13 21:29:54 -0700105 }
106
107 @Override
108 public int hashCode() {
Jonathan Hartdc711bd2014-10-15 11:24:23 -0700109 return Objects.hash(connectPoint, ipAddresses, macAddress);
110 }
111
112 @Override
113 public String toString() {
114 return MoreObjects.toStringHelper(getClass())
115 .add("connectPoint", connectPoint)
116 .add("ipAddresses", ipAddresses)
117 .add("macAddress", macAddress)
118 .toString();
Jonathan Hartbac07a02014-10-13 21:29:54 -0700119 }
120}