blob: 4052e814da965018b6a1ff70c747dace8d231264 [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.Dpid;
Yuta HIGUCHIfb564502014-06-16 21:29:00 -07006import net.onrc.onos.core.util.PortNumber;
Jonathan Hart23701d12014-04-03 10:45:48 -07007import net.onrc.onos.core.util.SwitchPort;
Jonathan Hart832a7cb2013-06-24 11:25:35 +12008
Jonathan Hart4aa2b4e2013-09-24 14:50:23 +12009import org.codehaus.jackson.annotate.JsonCreator;
Jonathan Hart832a7cb2013-06-24 11:25:35 +120010import org.codehaus.jackson.annotate.JsonProperty;
Jonathan Hartc78b8f62014-08-07 22:31:09 -070011import org.projectfloodlight.openflow.util.HexString;
Jonathan Hart832a7cb2013-06-24 11:25:35 +120012
13import com.google.common.net.InetAddresses;
14
Jonathan Hart31e15f12014-04-10 10:33:00 -070015/**
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 Hart99ff20a2014-06-15 16:53:00 -070028 * {@link BgpPeer}s to {@code Interfaces}.
Jonathan Hart31e15f12014-04-10 10:33:00 -070029 */
Jonathan Hart832a7cb2013-06-24 11:25:35 +120030public class Interface {
Ray Milkey269ffb92014-04-03 14:43:30 -070031 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 Hart9575cb62013-07-05 13:43:49 +120036
Jonathan Hart31e15f12014-04-10 10:33:00 -070037 /**
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 Milkey269ffb92014-04-03 14:43:30 -070046 @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 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() {
74 //TODO SwitchPort, Dpid and Port are mutable, but they could probably
75 //be made immutable which would prevent the need to copy
Yuta HIGUCHIa507baf2014-08-22 13:42:40 -070076 return new SwitchPort(new Dpid(dpid), PortNumber.uint16(port));
Ray Milkey269ffb92014-04-03 14:43:30 -070077 }
Jonathan Hart832a7cb2013-06-24 11:25:35 +120078
Jonathan Hart31e15f12014-04-10 10:33:00 -070079 /**
80 * Gets the DPID of the switch.
81 *
82 * @return the DPID of the switch
83 */
Ray Milkey269ffb92014-04-03 14:43:30 -070084 public long getDpid() {
85 return dpid;
86 }
87
Jonathan Hart31e15f12014-04-10 10:33:00 -070088 /**
89 * Gets the port number this interface maps to.
90 *
91 * @return the port number
92 */
Ray Milkey269ffb92014-04-03 14:43:30 -070093 public short getPort() {
94 return port;
95 }
96
Jonathan Hart31e15f12014-04-10 10:33:00 -070097 /**
98 * Gets the IP address which is logically assigned to the switch port.
99 *
100 * @return the IP address
101 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 public InetAddress getIpAddress() {
103 return ipAddress;
104 }
105
Jonathan Hart31e15f12014-04-10 10:33:00 -0700106 /**
107 * Gets the prefix length of the interface's IP address.
108 *
109 * @return the prefix length
110 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 public int getPrefixLength() {
112 return prefixLength;
113 }
114
115 @Override
116 public boolean equals(Object other) {
Jonathan Hart738980f2014-04-04 10:11:15 -0700117 if (!(other instanceof Interface)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 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 Hart832a7cb2013-06-24 11:25:35 +1200141}