blob: 0d13f4aa602b7cc25fb8e06e6e57f51e65885c65 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
tom64b7aac2014-08-26 00:18:21 -070017
Jonathan Hartc3af35a2015-04-30 22:20:29 -070018import com.google.common.base.MoreObjects;
19
tombb58c202014-09-07 22:51:50 -070020import java.util.Objects;
21
Jonathan Hartc3af35a2015-04-30 22:20:29 -070022import static com.google.common.base.Preconditions.checkArgument;
23import static com.google.common.base.Preconditions.checkNotNull;
tombb58c202014-09-07 22:51:50 -070024
tom64b7aac2014-08-26 00:18:21 -070025/**
26 * Abstraction of a network connection point expressed as a pair of the
tombb58c202014-09-07 22:51:50 -070027 * network element identifier and port number.
tom64b7aac2014-08-26 00:18:21 -070028 */
tombb58c202014-09-07 22:51:50 -070029public class ConnectPoint {
30
31 private final ElementId elementId;
32 private final PortNumber portNumber;
tom64b7aac2014-08-26 00:18:21 -070033
34 /**
tombb58c202014-09-07 22:51:50 -070035 * Creates a new connection point.
tom64b7aac2014-08-26 00:18:21 -070036 *
tombb58c202014-09-07 22:51:50 -070037 * @param elementId network element identifier
38 * @param portNumber port number
tom64b7aac2014-08-26 00:18:21 -070039 */
tombb58c202014-09-07 22:51:50 -070040 public ConnectPoint(ElementId elementId, PortNumber portNumber) {
41 this.elementId = elementId;
42 this.portNumber = portNumber;
43 }
44
45 /**
46 * Returns the network element identifier.
47 *
48 * @return element identifier
49 */
50 public ElementId elementId() {
51 return elementId;
52 }
53
54 /**
55 * Returns the identifier of the infrastructure device if the connection
56 * point belongs to a network element which is indeed an infrastructure
57 * device.
58 *
59 * @return network element identifier as a device identifier
60 * @throws java.lang.IllegalStateException if connection point is not
61 * associated with a device
62 */
tombb58c202014-09-07 22:51:50 -070063 public DeviceId deviceId() {
64 if (elementId instanceof DeviceId) {
65 return (DeviceId) elementId;
66 }
67 throw new IllegalStateException("Connection point not associated " +
tomf5c9d922014-10-03 15:22:03 -070068 "with an infrastructure device");
69 }
70
71 /**
72 * Returns the identifier of the infrastructure device if the connection
73 * point belongs to a network element which is indeed an end-station host.
74 *
75 * @return network element identifier as a host identifier
76 * @throws java.lang.IllegalStateException if connection point is not
77 * associated with a host
78 */
79 public HostId hostId() {
80 if (elementId instanceof HostId) {
81 return (HostId) elementId;
82 }
83 throw new IllegalStateException("Connection point not associated " +
84 "with an end-station host");
tombb58c202014-09-07 22:51:50 -070085 }
tom64b7aac2014-08-26 00:18:21 -070086
87 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +053088 * Returns the identifier of the infrastructure device if the connection
89 * point belongs to a network element which is indeed an ip of pcc
90 * client identifier.
91 *
92 * @return network element identifier as a pcc client identifier
93 * @throws java.lang.IllegalStateException if connection point is not
94 * associated with a pcc client
95 */
96 public IpElementId ipElementId() {
97 if (elementId instanceof IpElementId) {
98 return (IpElementId) elementId;
99 }
100 throw new IllegalStateException("Connection point not associated " +
101 "with an pcc client");
102 }
103
104 /**
tom64b7aac2014-08-26 00:18:21 -0700105 * Returns the connection port number.
106 *
107 * @return port number
108 */
tombb58c202014-09-07 22:51:50 -0700109 public PortNumber port() {
110 return portNumber;
111 }
112
Jonathan Hartc3af35a2015-04-30 22:20:29 -0700113 /**
114 * Parse a device connect point from a string.
115 * The connect point should be in the format "deviceUri/portNumber".
116 *
117 * @param string string to parse
118 * @return a ConnectPoint based on the information in the string.
119 */
120 public static ConnectPoint deviceConnectPoint(String string) {
121 checkNotNull(string);
122 String[] splitted = string.split("/");
123 checkArgument(splitted.length == 2,
124 "Connect point must be in \"deviceUri/portNumber\" format");
125
126 return new ConnectPoint(DeviceId.deviceId(splitted[0]),
127 PortNumber.portNumber(splitted[1]));
128 }
129
130 /**
131 * Parse a host connect point from a string.
132 * The connect point should be in the format "hostId/vlanId/portNumber".
133 *
134 * @param string string to parse
135 * @return a ConnectPoint based on the information in the string.
136 */
137 public static ConnectPoint hostConnectPoint(String string) {
138 checkNotNull(string);
139 String[] splitted = string.split("/");
140 checkArgument(splitted.length == 3,
141 "Connect point must be in \"hostId/vlanId/portNumber\" format");
142
143 int lastSlash = string.lastIndexOf("/");
144
145 return new ConnectPoint(HostId.hostId(string.substring(0, lastSlash)),
146 PortNumber.portNumber(string.substring(lastSlash + 1, string.length())));
147 }
148
tombb58c202014-09-07 22:51:50 -0700149 @Override
150 public int hashCode() {
151 return Objects.hash(elementId, portNumber);
152 }
153
154 @Override
155 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700156 if (this == obj) {
157 return true;
158 }
tombb58c202014-09-07 22:51:50 -0700159 if (obj instanceof ConnectPoint) {
160 final ConnectPoint other = (ConnectPoint) obj;
161 return Objects.equals(this.elementId, other.elementId) &&
162 Objects.equals(this.portNumber, other.portNumber);
163 }
164 return false;
165 }
166
167 @Override
168 public String toString() {
alshabibdf652ad2014-09-09 11:53:19 -0700169 return MoreObjects.toStringHelper(this)
tombb58c202014-09-07 22:51:50 -0700170 .add("elementId", elementId)
171 .add("portNumber", portNumber)
172 .toString();
173 }
tom64b7aac2014-08-26 00:18:21 -0700174
175}