Jonathan Hart | 8f6dc09 | 2014-04-18 15:56:43 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.sdnip; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 2 | |
| 3 | import java.net.InetAddress; |
| 4 | |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.util.SwitchPort; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 6 | |
Jonathan Hart | 4aa2b4e | 2013-09-24 14:50:23 +1200 | [diff] [blame] | 7 | import org.codehaus.jackson.annotate.JsonCreator; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 8 | import org.codehaus.jackson.annotate.JsonProperty; |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 9 | import org.projectfloodlight.openflow.util.HexString; |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 10 | |
| 11 | import com.google.common.net.InetAddresses; |
| 12 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 13 | /** |
| 14 | * Represents an interface, which is an external-facing switch port that |
| 15 | * connects to another network. |
| 16 | * |
| 17 | * SDN-IP treats external-facing ports similarly to router ports. Logically, it |
| 18 | * assigns an IP address to these ports which is used for communication with |
| 19 | * the BGP peers, for example, the BGP peering session. The other peer will be |
| 20 | * configured to peer with the IP address (logically) assigned to the |
| 21 | * interface. The logical {@code Interface} construct maps on to a physical port in the |
| 22 | * data plane, which of course has no notion of IP addresses. |
| 23 | * |
| 24 | * Each interface has a name, which is a unique identifying String that is used |
| 25 | * to reference this interface in the configuration (for example, to map |
Jonathan Hart | 99ff20a | 2014-06-15 16:53:00 -0700 | [diff] [blame] | 26 | * {@link BgpPeer}s to {@code Interfaces}. |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 27 | */ |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 28 | public class Interface { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 29 | private final String name; |
| 30 | private final long dpid; |
| 31 | private final short port; |
| 32 | private final InetAddress ipAddress; |
| 33 | private final int prefixLength; |
Jonathan Hart | 8dc7d48 | 2014-08-25 23:15:54 -0700 | [diff] [blame] | 34 | private final SwitchPort switchPort; |
Jonathan Hart | 9575cb6 | 2013-07-05 13:43:49 +1200 | [diff] [blame] | 35 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Class constructor used by the JSON library to create an object. |
| 38 | * |
| 39 | * @param name the name of the interface |
| 40 | * @param dpid the dpid of the switch |
| 41 | * @param port the port on the switch |
| 42 | * @param ipAddress the IP address logically assigned to the interface |
| 43 | * @param prefixLength the length of the network prefix of the IP address |
| 44 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 45 | @JsonCreator |
| 46 | public Interface(@JsonProperty("name") String name, |
| 47 | @JsonProperty("dpid") String dpid, |
| 48 | @JsonProperty("port") short port, |
| 49 | @JsonProperty("ipAddress") String ipAddress, |
| 50 | @JsonProperty("prefixLength") int prefixLength) { |
| 51 | this.name = name; |
| 52 | this.dpid = HexString.toLong(dpid); |
| 53 | this.port = port; |
| 54 | this.ipAddress = InetAddresses.forString(ipAddress); |
| 55 | this.prefixLength = prefixLength; |
Jonathan Hart | 8dc7d48 | 2014-08-25 23:15:54 -0700 | [diff] [blame] | 56 | switchPort = new SwitchPort(this.dpid, this.port); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | } |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 58 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Gets the name of the interface. |
| 61 | * |
| 62 | * @return the name of the interface |
| 63 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | public String getName() { |
| 65 | return name; |
| 66 | } |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 67 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Gets the {@link SwitchPort} that this interface maps to. |
| 70 | * |
| 71 | * @return the switch port |
| 72 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | public SwitchPort getSwitchPort() { |
Jonathan Hart | 8dc7d48 | 2014-08-25 23:15:54 -0700 | [diff] [blame] | 74 | return switchPort; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | } |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 76 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Gets the DPID of the switch. |
| 79 | * |
| 80 | * @return the DPID of the switch |
| 81 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | public long getDpid() { |
| 83 | return dpid; |
| 84 | } |
| 85 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Gets the port number this interface maps to. |
| 88 | * |
| 89 | * @return the port number |
| 90 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | public short getPort() { |
| 92 | return port; |
| 93 | } |
| 94 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Gets the IP address which is logically assigned to the switch port. |
| 97 | * |
| 98 | * @return the IP address |
| 99 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | public InetAddress getIpAddress() { |
| 101 | return ipAddress; |
| 102 | } |
| 103 | |
Jonathan Hart | 31e15f1 | 2014-04-10 10:33:00 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Gets the prefix length of the interface's IP address. |
| 106 | * |
| 107 | * @return the prefix length |
| 108 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | public int getPrefixLength() { |
| 110 | return prefixLength; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean equals(Object other) { |
Jonathan Hart | 738980f | 2014-04-04 10:11:15 -0700 | [diff] [blame] | 115 | if (!(other instanceof Interface)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | |
| 119 | Interface otherInterface = (Interface) other; |
| 120 | |
Jonathan Hart | 8dc7d48 | 2014-08-25 23:15:54 -0700 | [diff] [blame] | 121 | // Don't check switchPort as it is comprised of dpid and port |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 122 | return (name.equals(otherInterface.name)) && |
| 123 | (dpid == otherInterface.dpid) && |
| 124 | (port == otherInterface.port) && |
| 125 | (ipAddress.equals(otherInterface.ipAddress)) && |
| 126 | (prefixLength == otherInterface.prefixLength); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public int hashCode() { |
| 131 | int hash = 17; |
| 132 | hash = 31 * hash + name.hashCode(); |
| 133 | hash = 31 * hash + (int) (dpid ^ dpid >>> 32); |
| 134 | hash = 31 * hash + (int) port; |
| 135 | hash = 31 * hash + ipAddress.hashCode(); |
| 136 | hash = 31 * hash + prefixLength; |
| 137 | return hash; |
| 138 | } |
Jonathan Hart | 832a7cb | 2013-06-24 11:25:35 +1200 | [diff] [blame] | 139 | } |