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