Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | */ |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 27 | public class ConnectPoint implements Comparable<ConnectPoint> { |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 28 | |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 29 | private static final String NO_SEP_SPECIFIED = |
| 30 | "Connect point not specified, Connect point must be in \"deviceUri/portNumber\" format"; |
| 31 | |
| 32 | private static final String SEP_NO_VALUE = |
| 33 | "Connect point separator specified, but no port number included, connect point must " |
| 34 | + " be in \"deviceUri/portNumber\" format"; |
| 35 | |
| 36 | |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 37 | private final ElementId elementId; |
| 38 | private final PortNumber portNumber; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 41 | * Creates a new connection point. |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 42 | * |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 43 | * @param elementId network element identifier |
| 44 | * @param portNumber port number |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 45 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 46 | public ConnectPoint(ElementId elementId, PortNumber portNumber) { |
| 47 | this.elementId = elementId; |
| 48 | this.portNumber = portNumber; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns the network element identifier. |
| 53 | * |
| 54 | * @return element identifier |
| 55 | */ |
| 56 | public ElementId elementId() { |
| 57 | return elementId; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns the identifier of the infrastructure device if the connection |
| 62 | * point belongs to a network element which is indeed an infrastructure |
| 63 | * device. |
| 64 | * |
| 65 | * @return network element identifier as a device identifier |
| 66 | * @throws java.lang.IllegalStateException if connection point is not |
| 67 | * associated with a device |
| 68 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 69 | public DeviceId deviceId() { |
| 70 | if (elementId instanceof DeviceId) { |
| 71 | return (DeviceId) elementId; |
| 72 | } |
| 73 | throw new IllegalStateException("Connection point not associated " + |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 74 | "with an infrastructure device"); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the identifier of the infrastructure device if the connection |
| 79 | * point belongs to a network element which is indeed an end-station host. |
| 80 | * |
| 81 | * @return network element identifier as a host identifier |
| 82 | * @throws java.lang.IllegalStateException if connection point is not |
| 83 | * associated with a host |
| 84 | */ |
| 85 | public HostId hostId() { |
| 86 | if (elementId instanceof HostId) { |
| 87 | return (HostId) elementId; |
| 88 | } |
| 89 | throw new IllegalStateException("Connection point not associated " + |
| 90 | "with an end-station host"); |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 91 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
Phanendra Manda | 37b97fb | 2015-08-15 02:04:24 +0530 | [diff] [blame] | 94 | * Returns the identifier of the infrastructure device if the connection |
| 95 | * point belongs to a network element which is indeed an ip of pcc |
| 96 | * client identifier. |
| 97 | * |
| 98 | * @return network element identifier as a pcc client identifier |
| 99 | * @throws java.lang.IllegalStateException if connection point is not |
| 100 | * associated with a pcc client |
| 101 | */ |
| 102 | public IpElementId ipElementId() { |
| 103 | if (elementId instanceof IpElementId) { |
| 104 | return (IpElementId) elementId; |
| 105 | } |
| 106 | throw new IllegalStateException("Connection point not associated " + |
| 107 | "with an pcc client"); |
| 108 | } |
| 109 | |
| 110 | /** |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 111 | * Returns the connection port number. |
| 112 | * |
| 113 | * @return port number |
| 114 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 115 | public PortNumber port() { |
| 116 | return portNumber; |
| 117 | } |
| 118 | |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 119 | /** |
| 120 | * Parse a device connect point from a string. |
| 121 | * The connect point should be in the format "deviceUri/portNumber". |
| 122 | * |
| 123 | * @param string string to parse |
| 124 | * @return a ConnectPoint based on the information in the string. |
| 125 | */ |
| 126 | public static ConnectPoint deviceConnectPoint(String string) { |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 127 | /* |
| 128 | * As device IDs may have a path component, we are expecting one |
| 129 | * of: |
| 130 | * - scheme:ip:port/cp |
| 131 | * - scheme:ip:port/path/cp |
| 132 | * |
| 133 | * The assumption is the last `/` will separate the device ID |
| 134 | * from the connection point number. |
| 135 | */ |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 136 | checkNotNull(string); |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 137 | int idx = string.lastIndexOf("/"); |
| 138 | checkArgument(idx != -1, NO_SEP_SPECIFIED); |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 139 | |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 140 | String id = string.substring(0, idx); |
| 141 | String cp = string.substring(idx + 1); |
| 142 | checkArgument(!cp.isEmpty(), SEP_NO_VALUE); |
| 143 | |
| 144 | return new ConnectPoint(DeviceId.deviceId(id), PortNumber.portNumber(cp)); |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Parse a host connect point from a string. |
| 149 | * The connect point should be in the format "hostId/vlanId/portNumber". |
| 150 | * |
| 151 | * @param string string to parse |
| 152 | * @return a ConnectPoint based on the information in the string. |
| 153 | */ |
| 154 | public static ConnectPoint hostConnectPoint(String string) { |
| 155 | checkNotNull(string); |
| 156 | String[] splitted = string.split("/"); |
| 157 | checkArgument(splitted.length == 3, |
| 158 | "Connect point must be in \"hostId/vlanId/portNumber\" format"); |
| 159 | |
| 160 | int lastSlash = string.lastIndexOf("/"); |
| 161 | |
| 162 | return new ConnectPoint(HostId.hostId(string.substring(0, lastSlash)), |
| 163 | PortNumber.portNumber(string.substring(lastSlash + 1, string.length()))); |
| 164 | } |
| 165 | |
hiroki | b0bc9da | 2018-05-19 13:01:21 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Parse a device connect point from a string. |
| 168 | * The connect point should be in the same format as toString output. |
| 169 | * |
| 170 | * @param string string to parse |
| 171 | * @return a ConnectPoint based on the information in the string. |
| 172 | */ |
| 173 | public static ConnectPoint fromString(String string) { |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 174 | /* |
| 175 | * As device IDs may have a path component, we are expecting one |
| 176 | * of: |
| 177 | * - scheme:ip:port/cp |
| 178 | * - scheme:ip:port/path/cp |
| 179 | * |
| 180 | * The assumption is the last `/` will separate the device ID |
pierventre | 178046b | 2021-10-20 17:37:52 +0200 | [diff] [blame] | 181 | * from the connection point number. If the cp is a named port |
| 182 | * `/` can be included in the port name. We use `[` as heuristic |
| 183 | * to separate the device ID from the port number. |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 184 | */ |
hiroki | b0bc9da | 2018-05-19 13:01:21 -0700 | [diff] [blame] | 185 | checkNotNull(string); |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 186 | int idx = string.lastIndexOf("/"); |
| 187 | checkArgument(idx != -1, NO_SEP_SPECIFIED); |
hiroki | b0bc9da | 2018-05-19 13:01:21 -0700 | [diff] [blame] | 188 | |
pierventre | 178046b | 2021-10-20 17:37:52 +0200 | [diff] [blame] | 189 | String id = ""; |
| 190 | String cp = ""; |
| 191 | // deviceId/[ which means the id is 2 chars behind |
| 192 | int nameIdx = string.lastIndexOf("["); |
| 193 | if (nameIdx > 0) { |
| 194 | id = string.substring(0, nameIdx - 1); |
| 195 | cp = string.substring(nameIdx); |
| 196 | } else if (nameIdx < 0) { |
| 197 | id = string.substring(0, idx); |
| 198 | cp = string.substring(idx + 1); |
| 199 | } |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 200 | checkArgument(!cp.isEmpty(), SEP_NO_VALUE); |
| 201 | |
| 202 | return new ConnectPoint(DeviceId.deviceId(id), PortNumber.fromString(cp)); |
hiroki | b0bc9da | 2018-05-19 13:01:21 -0700 | [diff] [blame] | 203 | } |
| 204 | |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 205 | @Override |
| 206 | public int hashCode() { |
| 207 | return Objects.hash(elementId, portNumber); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 212 | if (this == obj) { |
| 213 | return true; |
| 214 | } |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 215 | if (obj instanceof ConnectPoint) { |
| 216 | final ConnectPoint other = (ConnectPoint) obj; |
| 217 | return Objects.equals(this.elementId, other.elementId) && |
| 218 | Objects.equals(this.portNumber, other.portNumber); |
| 219 | } |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public String toString() { |
Yuta HIGUCHI | bef16cd | 2016-08-10 14:55:48 -0700 | [diff] [blame] | 225 | return elementId + "/" + portNumber; |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 226 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 227 | |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 228 | @Override |
| 229 | public int compareTo(ConnectPoint o) { |
| 230 | int result = deviceId().toString().compareTo(o.deviceId().toString()); |
| 231 | if (result == 0) { |
| 232 | long delta = port().toLong() - o.port().toLong(); |
| 233 | result = delta == 0 ? 0 : (delta < 0 ? -1 : +1); |
| 234 | } |
| 235 | return result; |
| 236 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 237 | } |