blob: 33287addaf5c1f3bff456b73b915891483116866 [file] [log] [blame]
Jonathan Hartbac07a02014-10-13 21:29:54 -07001package org.onlab.onos.sdnip.config;
2
3import java.util.Objects;
4
5import org.codehaus.jackson.annotate.JsonCreator;
6import org.codehaus.jackson.annotate.JsonProperty;
7import org.onlab.onos.net.ConnectPoint;
8import org.onlab.onos.net.DeviceId;
9import org.onlab.onos.net.PortNumber;
10import org.onlab.packet.IpPrefix;
11
12/**
13 * Represents an interface, which is an external-facing switch port that
14 * connects to another network.
15 * <p/>
16 * SDN-IP treats external-facing ports similarly to router ports. Logically, it
17 * assigns an IP subnetwork prefix and several IP addresses to each port which
18 * are used for communication with the BGP peers located in other networks, for
19 * example, the BGP peering sessions. The peers in other networks will be
20 * configured to peer with the IP addresses (logically) assigned to the
21 * interface. The logical {@code Interface} construct maps on to a physical
22 * port in the data plane, which of course has no notion of IP addresses.
23 * <p/>
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
26 * {@link BgpPeer}s to {@code Interfaces}.
27 */
28public class Interface {
29 private final String name;
30 private final ConnectPoint switchPort;
31 private final IpPrefix ip4Prefix;
32
33 /**
34 * Class constructor used by the JSON library to create an object.
35 *
36 * @param name the name of the interface
37 * @param dpid the dpid of the switch
38 * @param port the port on the switch
39 * @param prefixAddress the network prefix address logically assigned to the
40 * interface
41 * @param prefixLength the length of the network prefix of the IP address
42 */
43 @JsonCreator
44 public Interface(@JsonProperty("name") String name,
45 @JsonProperty("dpid") String dpid,
46 @JsonProperty("port") int port,
47 @JsonProperty("ipAddress") String prefixAddress,
48 @JsonProperty("prefixLength") short prefixLength) {
49 this.name = name;
50 this.switchPort = new ConnectPoint(
51 DeviceId.deviceId(SdnIpConfigReader.dpidToUri(dpid)),
52 PortNumber.portNumber(port));
53 this.ip4Prefix = IpPrefix.valueOf(prefixAddress + "/" + prefixLength);
54 }
55
56 /**
57 * Gets the name of the interface.
58 *
59 * @return the name of the interface
60 */
61 public String getName() {
62 return name;
63 }
64
65 /**
66 * Gets the {@link SwitchPort} that this interface maps to.
67 *
68 * @return the switch port
69 */
70 public ConnectPoint getSwitchPort() {
71 return switchPort;
72 }
73
74 /**
75 * Gets the IP prefix of the subnetwork which is logically assigned
76 * to the switch port.
77 *
78 * @return the IP prefix
79 */
80 public IpPrefix getIp4Prefix() {
81 return ip4Prefix;
82 }
83
84 @Override
85 public boolean equals(Object other) {
86 if (!(other instanceof Interface)) {
87 return false;
88 }
89
90 Interface otherInterface = (Interface) other;
91
92 return name.equals(otherInterface.name) &&
93 switchPort.equals(otherInterface.switchPort) &&
94 ip4Prefix.equals(otherInterface.ip4Prefix);
95 }
96
97 @Override
98 public int hashCode() {
99 return Objects.hash(name, switchPort, ip4Prefix);
100 }
101}