blob: 7a311aa80db2cb4917b24595c2b4012539f1c2ac [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
tom64b7aac2014-08-26 00:18:21 -070017
tombb58c202014-09-07 22:51:50 -070018import java.util.Objects;
19
Jonathan Hartc3af35a2015-04-30 22:20:29 -070020import static com.google.common.base.Preconditions.checkArgument;
21import static com.google.common.base.Preconditions.checkNotNull;
tombb58c202014-09-07 22:51:50 -070022
tom64b7aac2014-08-26 00:18:21 -070023/**
24 * Abstraction of a network connection point expressed as a pair of the
tombb58c202014-09-07 22:51:50 -070025 * network element identifier and port number.
tom64b7aac2014-08-26 00:18:21 -070026 */
Simon Hunt86943082017-06-15 13:18:42 -070027public class ConnectPoint implements Comparable<ConnectPoint> {
tombb58c202014-09-07 22:51:50 -070028
David K. Bainbridge56e90232018-12-18 23:25:08 -080029 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
tombb58c202014-09-07 22:51:50 -070037 private final ElementId elementId;
38 private final PortNumber portNumber;
tom64b7aac2014-08-26 00:18:21 -070039
40 /**
tombb58c202014-09-07 22:51:50 -070041 * Creates a new connection point.
tom64b7aac2014-08-26 00:18:21 -070042 *
tombb58c202014-09-07 22:51:50 -070043 * @param elementId network element identifier
44 * @param portNumber port number
tom64b7aac2014-08-26 00:18:21 -070045 */
tombb58c202014-09-07 22:51:50 -070046 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 */
tombb58c202014-09-07 22:51:50 -070069 public DeviceId deviceId() {
70 if (elementId instanceof DeviceId) {
71 return (DeviceId) elementId;
72 }
73 throw new IllegalStateException("Connection point not associated " +
tomf5c9d922014-10-03 15:22:03 -070074 "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");
tombb58c202014-09-07 22:51:50 -070091 }
tom64b7aac2014-08-26 00:18:21 -070092
93 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +053094 * 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 /**
tom64b7aac2014-08-26 00:18:21 -0700111 * Returns the connection port number.
112 *
113 * @return port number
114 */
tombb58c202014-09-07 22:51:50 -0700115 public PortNumber port() {
116 return portNumber;
117 }
118
Jonathan Hartc3af35a2015-04-30 22:20:29 -0700119 /**
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. Bainbridge56e90232018-12-18 23:25:08 -0800127 /*
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 Hartc3af35a2015-04-30 22:20:29 -0700136 checkNotNull(string);
David K. Bainbridge56e90232018-12-18 23:25:08 -0800137 int idx = string.lastIndexOf("/");
138 checkArgument(idx != -1, NO_SEP_SPECIFIED);
Jonathan Hartc3af35a2015-04-30 22:20:29 -0700139
David K. Bainbridge56e90232018-12-18 23:25:08 -0800140 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 Hartc3af35a2015-04-30 22:20:29 -0700145 }
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
hirokib0bc9da2018-05-19 13:01:21 -0700166 /**
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. Bainbridge56e90232018-12-18 23:25:08 -0800174 /*
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
181 * from the connection point number.
182 */
hirokib0bc9da2018-05-19 13:01:21 -0700183 checkNotNull(string);
David K. Bainbridge56e90232018-12-18 23:25:08 -0800184 int idx = string.lastIndexOf("/");
185 checkArgument(idx != -1, NO_SEP_SPECIFIED);
hirokib0bc9da2018-05-19 13:01:21 -0700186
David K. Bainbridge56e90232018-12-18 23:25:08 -0800187 String id = string.substring(0, idx);
188 String cp = string.substring(idx + 1);
189 checkArgument(!cp.isEmpty(), SEP_NO_VALUE);
190
191 return new ConnectPoint(DeviceId.deviceId(id), PortNumber.fromString(cp));
hirokib0bc9da2018-05-19 13:01:21 -0700192 }
193
tombb58c202014-09-07 22:51:50 -0700194 @Override
195 public int hashCode() {
196 return Objects.hash(elementId, portNumber);
197 }
198
199 @Override
200 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700201 if (this == obj) {
202 return true;
203 }
tombb58c202014-09-07 22:51:50 -0700204 if (obj instanceof ConnectPoint) {
205 final ConnectPoint other = (ConnectPoint) obj;
206 return Objects.equals(this.elementId, other.elementId) &&
207 Objects.equals(this.portNumber, other.portNumber);
208 }
209 return false;
210 }
211
212 @Override
213 public String toString() {
Yuta HIGUCHIbef16cd2016-08-10 14:55:48 -0700214 return elementId + "/" + portNumber;
tombb58c202014-09-07 22:51:50 -0700215 }
tom64b7aac2014-08-26 00:18:21 -0700216
Simon Hunt86943082017-06-15 13:18:42 -0700217 @Override
218 public int compareTo(ConnectPoint o) {
219 int result = deviceId().toString().compareTo(o.deviceId().toString());
220 if (result == 0) {
221 long delta = port().toLong() - o.port().toLong();
222 result = delta == 0 ? 0 : (delta < 0 ? -1 : +1);
223 }
224 return result;
225 }
tom64b7aac2014-08-26 00:18:21 -0700226}