tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net; |
| 2 | |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 3 | import java.util.Objects; |
| 4 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 5 | import com.google.common.base.MoreObjects; |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 6 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 7 | /** |
| 8 | * Abstraction of a network connection point expressed as a pair of the |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 9 | * network element identifier and port number. |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 10 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 11 | public class ConnectPoint { |
| 12 | |
| 13 | private final ElementId elementId; |
| 14 | private final PortNumber portNumber; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 15 | |
| 16 | /** |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 17 | * Creates a new connection point. |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 18 | * |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 19 | * @param elementId network element identifier |
| 20 | * @param portNumber port number |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 21 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 22 | public ConnectPoint(ElementId elementId, PortNumber portNumber) { |
| 23 | this.elementId = elementId; |
| 24 | this.portNumber = portNumber; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Returns the network element identifier. |
| 29 | * |
| 30 | * @return element identifier |
| 31 | */ |
| 32 | public ElementId elementId() { |
| 33 | return elementId; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Returns the identifier of the infrastructure device if the connection |
| 38 | * point belongs to a network element which is indeed an infrastructure |
| 39 | * device. |
| 40 | * |
| 41 | * @return network element identifier as a device identifier |
| 42 | * @throws java.lang.IllegalStateException if connection point is not |
| 43 | * associated with a device |
| 44 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 45 | public DeviceId deviceId() { |
| 46 | if (elementId instanceof DeviceId) { |
| 47 | return (DeviceId) elementId; |
| 48 | } |
| 49 | throw new IllegalStateException("Connection point not associated " + |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame^] | 50 | "with an infrastructure device"); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the identifier of the infrastructure device if the connection |
| 55 | * point belongs to a network element which is indeed an end-station host. |
| 56 | * |
| 57 | * @return network element identifier as a host identifier |
| 58 | * @throws java.lang.IllegalStateException if connection point is not |
| 59 | * associated with a host |
| 60 | */ |
| 61 | public HostId hostId() { |
| 62 | if (elementId instanceof HostId) { |
| 63 | return (HostId) elementId; |
| 64 | } |
| 65 | throw new IllegalStateException("Connection point not associated " + |
| 66 | "with an end-station host"); |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 67 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Returns the connection port number. |
| 71 | * |
| 72 | * @return port number |
| 73 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 74 | public PortNumber port() { |
| 75 | return portNumber; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public int hashCode() { |
| 80 | return Objects.hash(elementId, portNumber); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 85 | if (this == obj) { |
| 86 | return true; |
| 87 | } |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 88 | if (obj instanceof ConnectPoint) { |
| 89 | final ConnectPoint other = (ConnectPoint) obj; |
| 90 | return Objects.equals(this.elementId, other.elementId) && |
| 91 | Objects.equals(this.portNumber, other.portNumber); |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public String toString() { |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 98 | return MoreObjects.toStringHelper(this) |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 99 | .add("elementId", elementId) |
| 100 | .add("portNumber", portNumber) |
| 101 | .toString(); |
| 102 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 103 | |
| 104 | } |