Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 17 | |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 18 | import java.util.Objects; |
| 19 | |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 20 | import com.google.common.base.MoreObjects; |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 21 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 22 | /** |
| 23 | * Abstraction of a network connection point expressed as a pair of the |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 24 | * network element identifier and port number. |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 25 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 26 | public class ConnectPoint { |
| 27 | |
| 28 | private final ElementId elementId; |
| 29 | private final PortNumber portNumber; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 32 | * Creates a new connection point. |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 33 | * |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 34 | * @param elementId network element identifier |
| 35 | * @param portNumber port number |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 36 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 37 | public ConnectPoint(ElementId elementId, PortNumber portNumber) { |
| 38 | this.elementId = elementId; |
| 39 | this.portNumber = portNumber; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns the network element identifier. |
| 44 | * |
| 45 | * @return element identifier |
| 46 | */ |
| 47 | public ElementId elementId() { |
| 48 | return elementId; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns the identifier of the infrastructure device if the connection |
| 53 | * point belongs to a network element which is indeed an infrastructure |
| 54 | * device. |
| 55 | * |
| 56 | * @return network element identifier as a device identifier |
| 57 | * @throws java.lang.IllegalStateException if connection point is not |
| 58 | * associated with a device |
| 59 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 60 | public DeviceId deviceId() { |
| 61 | if (elementId instanceof DeviceId) { |
| 62 | return (DeviceId) elementId; |
| 63 | } |
| 64 | throw new IllegalStateException("Connection point not associated " + |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 65 | "with an infrastructure device"); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the identifier of the infrastructure device if the connection |
| 70 | * point belongs to a network element which is indeed an end-station host. |
| 71 | * |
| 72 | * @return network element identifier as a host identifier |
| 73 | * @throws java.lang.IllegalStateException if connection point is not |
| 74 | * associated with a host |
| 75 | */ |
| 76 | public HostId hostId() { |
| 77 | if (elementId instanceof HostId) { |
| 78 | return (HostId) elementId; |
| 79 | } |
| 80 | throw new IllegalStateException("Connection point not associated " + |
| 81 | "with an end-station host"); |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 82 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Returns the connection port number. |
| 86 | * |
| 87 | * @return port number |
| 88 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 89 | public PortNumber port() { |
| 90 | return portNumber; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public int hashCode() { |
| 95 | return Objects.hash(elementId, portNumber); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 100 | if (this == obj) { |
| 101 | return true; |
| 102 | } |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 103 | if (obj instanceof ConnectPoint) { |
| 104 | final ConnectPoint other = (ConnectPoint) obj; |
| 105 | return Objects.equals(this.elementId, other.elementId) && |
| 106 | Objects.equals(this.portNumber, other.portNumber); |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public String toString() { |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 113 | return MoreObjects.toStringHelper(this) |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 114 | .add("elementId", elementId) |
| 115 | .add("portNumber", portNumber) |
| 116 | .toString(); |
| 117 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 118 | |
| 119 | } |