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 | |
| 5 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 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 | */ |
| 45 | @SuppressWarnings("unchecked") |
| 46 | public DeviceId deviceId() { |
| 47 | if (elementId instanceof DeviceId) { |
| 48 | return (DeviceId) elementId; |
| 49 | } |
| 50 | throw new IllegalStateException("Connection point not associated " + |
| 51 | "with an infrastructure device"); |
| 52 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Returns the connection port number. |
| 56 | * |
| 57 | * @return port number |
| 58 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 59 | public PortNumber port() { |
| 60 | return portNumber; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int hashCode() { |
| 65 | return Objects.hash(elementId, portNumber); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean equals(Object obj) { |
| 70 | if (obj instanceof ConnectPoint) { |
| 71 | final ConnectPoint other = (ConnectPoint) obj; |
| 72 | return Objects.equals(this.elementId, other.elementId) && |
| 73 | Objects.equals(this.portNumber, other.portNumber); |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public String toString() { |
| 80 | return toStringHelper(this) |
| 81 | .add("elementId", elementId) |
| 82 | .add("portNumber", portNumber) |
| 83 | .toString(); |
| 84 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 85 | |
| 86 | } |