blob: e1a06c0580e86c88690a598afb60a5f93792cec4 [file] [log] [blame]
Jonathan Hart8f6dc092014-04-18 15:56:43 -07001package net.onrc.onos.apps.sdnip;
Jonathan Hart832a7cb2013-06-24 11:25:35 +12002
3import java.net.InetAddress;
4
Jonathan Hart23701d12014-04-03 10:45:48 -07005import net.onrc.onos.core.util.SwitchPort;
Jonathan Hart832a7cb2013-06-24 11:25:35 +12006
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +12007import org.codehaus.jackson.annotate.JsonCreator;
Jonathan Hart832a7cb2013-06-24 11:25:35 +12008import org.codehaus.jackson.annotate.JsonProperty;
Jonathan Hartc78b8f62014-08-07 22:31:09 -07009import org.projectfloodlight.openflow.util.HexString;
Jonathan Hart832a7cb2013-06-24 11:25:35 +120010
11import com.google.common.net.InetAddresses;
12
Jonathan Hart31e15f12014-04-10 10:33:00 -070013/**
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 Hart99ff20a2014-06-15 16:53:00 -070026 * {@link BgpPeer}s to {@code Interfaces}.
Jonathan Hart31e15f12014-04-10 10:33:00 -070027 */
Jonathan Hart832a7cb2013-06-24 11:25:35 +120028public class Interface {
Ray Milkey269ffb92014-04-03 14:43:30 -070029 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 Hart8dc7d482014-08-25 23:15:54 -070034 private final SwitchPort switchPort;
Jonathan Hart9575cb62013-07-05 13:43:49 +120035
Jonathan Hart31e15f12014-04-10 10:33:00 -070036 /**
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 Milkey269ffb92014-04-03 14:43:30 -070045 @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 Hart8dc7d482014-08-25 23:15:54 -070056 switchPort = new SwitchPort(this.dpid, this.port);
Ray Milkey269ffb92014-04-03 14:43:30 -070057 }
Jonathan Hart832a7cb2013-06-24 11:25:35 +120058
Jonathan Hart31e15f12014-04-10 10:33:00 -070059 /**
60 * Gets the name of the interface.
61 *
62 * @return the name of the interface
63 */
Ray Milkey269ffb92014-04-03 14:43:30 -070064 public String getName() {
65 return name;
66 }
Jonathan Hart832a7cb2013-06-24 11:25:35 +120067
Jonathan Hart31e15f12014-04-10 10:33:00 -070068 /**
69 * Gets the {@link SwitchPort} that this interface maps to.
70 *
71 * @return the switch port
72 */
Ray Milkey269ffb92014-04-03 14:43:30 -070073 public SwitchPort getSwitchPort() {
Jonathan Hart8dc7d482014-08-25 23:15:54 -070074 return switchPort;
Ray Milkey269ffb92014-04-03 14:43:30 -070075 }
Jonathan Hart832a7cb2013-06-24 11:25:35 +120076
Jonathan Hart31e15f12014-04-10 10:33:00 -070077 /**
78 * Gets the DPID of the switch.
79 *
80 * @return the DPID of the switch
81 */
Ray Milkey269ffb92014-04-03 14:43:30 -070082 public long getDpid() {
83 return dpid;
84 }
85
Jonathan Hart31e15f12014-04-10 10:33:00 -070086 /**
87 * Gets the port number this interface maps to.
88 *
89 * @return the port number
90 */
Ray Milkey269ffb92014-04-03 14:43:30 -070091 public short getPort() {
92 return port;
93 }
94
Jonathan Hart31e15f12014-04-10 10:33:00 -070095 /**
96 * Gets the IP address which is logically assigned to the switch port.
97 *
98 * @return the IP address
99 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 public InetAddress getIpAddress() {
101 return ipAddress;
102 }
103
Jonathan Hart31e15f12014-04-10 10:33:00 -0700104 /**
105 * Gets the prefix length of the interface's IP address.
106 *
107 * @return the prefix length
108 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 public int getPrefixLength() {
110 return prefixLength;
111 }
112
113 @Override
114 public boolean equals(Object other) {
Jonathan Hart738980f2014-04-04 10:11:15 -0700115 if (!(other instanceof Interface)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700116 return false;
117 }
118
119 Interface otherInterface = (Interface) other;
120
Jonathan Hart8dc7d482014-08-25 23:15:54 -0700121 // Don't check switchPort as it is comprised of dpid and port
Ray Milkey269ffb92014-04-03 14:43:30 -0700122 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 Hart832a7cb2013-06-24 11:25:35 +1200139}