Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 20 | import static com.google.common.base.Preconditions.checkArgument; |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 22 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 23 | /** |
| 24 | * Abstraction of a network connection point expressed as a pair of the |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 25 | * network element identifier and port number. |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 26 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 27 | public class ConnectPoint { |
| 28 | |
| 29 | private final ElementId elementId; |
| 30 | private final PortNumber portNumber; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 33 | * Creates a new connection point. |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 34 | * |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 35 | * @param elementId network element identifier |
| 36 | * @param portNumber port number |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 37 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 38 | public ConnectPoint(ElementId elementId, PortNumber portNumber) { |
| 39 | this.elementId = elementId; |
| 40 | this.portNumber = portNumber; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns the network element identifier. |
| 45 | * |
| 46 | * @return element identifier |
| 47 | */ |
| 48 | public ElementId elementId() { |
| 49 | return elementId; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns the identifier of the infrastructure device if the connection |
| 54 | * point belongs to a network element which is indeed an infrastructure |
| 55 | * device. |
| 56 | * |
| 57 | * @return network element identifier as a device identifier |
| 58 | * @throws java.lang.IllegalStateException if connection point is not |
| 59 | * associated with a device |
| 60 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 61 | public DeviceId deviceId() { |
| 62 | if (elementId instanceof DeviceId) { |
| 63 | return (DeviceId) elementId; |
| 64 | } |
| 65 | throw new IllegalStateException("Connection point not associated " + |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 66 | "with an infrastructure device"); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the identifier of the infrastructure device if the connection |
| 71 | * point belongs to a network element which is indeed an end-station host. |
| 72 | * |
| 73 | * @return network element identifier as a host identifier |
| 74 | * @throws java.lang.IllegalStateException if connection point is not |
| 75 | * associated with a host |
| 76 | */ |
| 77 | public HostId hostId() { |
| 78 | if (elementId instanceof HostId) { |
| 79 | return (HostId) elementId; |
| 80 | } |
| 81 | throw new IllegalStateException("Connection point not associated " + |
| 82 | "with an end-station host"); |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 83 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
Phanendra Manda | 37b97fb | 2015-08-15 02:04:24 +0530 | [diff] [blame] | 86 | * Returns the identifier of the infrastructure device if the connection |
| 87 | * point belongs to a network element which is indeed an ip of pcc |
| 88 | * client identifier. |
| 89 | * |
| 90 | * @return network element identifier as a pcc client identifier |
| 91 | * @throws java.lang.IllegalStateException if connection point is not |
| 92 | * associated with a pcc client |
| 93 | */ |
| 94 | public IpElementId ipElementId() { |
| 95 | if (elementId instanceof IpElementId) { |
| 96 | return (IpElementId) elementId; |
| 97 | } |
| 98 | throw new IllegalStateException("Connection point not associated " + |
| 99 | "with an pcc client"); |
| 100 | } |
| 101 | |
| 102 | /** |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 103 | * Returns the connection port number. |
| 104 | * |
| 105 | * @return port number |
| 106 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 107 | public PortNumber port() { |
| 108 | return portNumber; |
| 109 | } |
| 110 | |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Parse a device connect point from a string. |
| 113 | * The connect point should be in the format "deviceUri/portNumber". |
| 114 | * |
| 115 | * @param string string to parse |
| 116 | * @return a ConnectPoint based on the information in the string. |
| 117 | */ |
| 118 | public static ConnectPoint deviceConnectPoint(String string) { |
| 119 | checkNotNull(string); |
| 120 | String[] splitted = string.split("/"); |
| 121 | checkArgument(splitted.length == 2, |
| 122 | "Connect point must be in \"deviceUri/portNumber\" format"); |
| 123 | |
| 124 | return new ConnectPoint(DeviceId.deviceId(splitted[0]), |
| 125 | PortNumber.portNumber(splitted[1])); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Parse a host connect point from a string. |
| 130 | * The connect point should be in the format "hostId/vlanId/portNumber". |
| 131 | * |
| 132 | * @param string string to parse |
| 133 | * @return a ConnectPoint based on the information in the string. |
| 134 | */ |
| 135 | public static ConnectPoint hostConnectPoint(String string) { |
| 136 | checkNotNull(string); |
| 137 | String[] splitted = string.split("/"); |
| 138 | checkArgument(splitted.length == 3, |
| 139 | "Connect point must be in \"hostId/vlanId/portNumber\" format"); |
| 140 | |
| 141 | int lastSlash = string.lastIndexOf("/"); |
| 142 | |
| 143 | return new ConnectPoint(HostId.hostId(string.substring(0, lastSlash)), |
| 144 | PortNumber.portNumber(string.substring(lastSlash + 1, string.length()))); |
| 145 | } |
| 146 | |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 147 | @Override |
| 148 | public int hashCode() { |
| 149 | return Objects.hash(elementId, portNumber); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 154 | if (this == obj) { |
| 155 | return true; |
| 156 | } |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 157 | if (obj instanceof ConnectPoint) { |
| 158 | final ConnectPoint other = (ConnectPoint) obj; |
| 159 | return Objects.equals(this.elementId, other.elementId) && |
| 160 | Objects.equals(this.portNumber, other.portNumber); |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public String toString() { |
Yuta HIGUCHI | bef16cd | 2016-08-10 14:55:48 -0700 | [diff] [blame] | 167 | return elementId + "/" + portNumber; |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 168 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 169 | |
| 170 | } |