blob: 13471ec6b3dc575c7950a5a151dbfdea80f10568 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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 */
tombb58c202014-09-07 22:51:50 -070027public class ConnectPoint {
28
29 private final ElementId elementId;
30 private final PortNumber portNumber;
tom64b7aac2014-08-26 00:18:21 -070031
32 /**
tombb58c202014-09-07 22:51:50 -070033 * Creates a new connection point.
tom64b7aac2014-08-26 00:18:21 -070034 *
tombb58c202014-09-07 22:51:50 -070035 * @param elementId network element identifier
36 * @param portNumber port number
tom64b7aac2014-08-26 00:18:21 -070037 */
tombb58c202014-09-07 22:51:50 -070038 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 */
tombb58c202014-09-07 22:51:50 -070061 public DeviceId deviceId() {
62 if (elementId instanceof DeviceId) {
63 return (DeviceId) elementId;
64 }
65 throw new IllegalStateException("Connection point not associated " +
tomf5c9d922014-10-03 15:22:03 -070066 "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");
tombb58c202014-09-07 22:51:50 -070083 }
tom64b7aac2014-08-26 00:18:21 -070084
85 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +053086 * 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 /**
tom64b7aac2014-08-26 00:18:21 -0700103 * Returns the connection port number.
104 *
105 * @return port number
106 */
tombb58c202014-09-07 22:51:50 -0700107 public PortNumber port() {
108 return portNumber;
109 }
110
Jonathan Hartc3af35a2015-04-30 22:20:29 -0700111 /**
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
tombb58c202014-09-07 22:51:50 -0700147 @Override
148 public int hashCode() {
149 return Objects.hash(elementId, portNumber);
150 }
151
152 @Override
153 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700154 if (this == obj) {
155 return true;
156 }
tombb58c202014-09-07 22:51:50 -0700157 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 HIGUCHIbef16cd2016-08-10 14:55:48 -0700167 return elementId + "/" + portNumber;
tombb58c202014-09-07 22:51:50 -0700168 }
tom64b7aac2014-08-26 00:18:21 -0700169
170}