blob: b5ff42cab3f41d87651f11072f776f0c257b51c8 [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 /**
88 * Returns the connection port number.
89 *
90 * @return port number
91 */
tombb58c202014-09-07 22:51:50 -070092 public PortNumber port() {
93 return portNumber;
94 }
95
Jonathan Hartc3af35a2015-04-30 22:20:29 -070096 /**
97 * Parse a device connect point from a string.
98 * The connect point should be in the format "deviceUri/portNumber".
99 *
100 * @param string string to parse
101 * @return a ConnectPoint based on the information in the string.
102 */
103 public static ConnectPoint deviceConnectPoint(String string) {
104 checkNotNull(string);
105 String[] splitted = string.split("/");
106 checkArgument(splitted.length == 2,
107 "Connect point must be in \"deviceUri/portNumber\" format");
108
109 return new ConnectPoint(DeviceId.deviceId(splitted[0]),
110 PortNumber.portNumber(splitted[1]));
111 }
112
113 /**
114 * Parse a host connect point from a string.
115 * The connect point should be in the format "hostId/vlanId/portNumber".
116 *
117 * @param string string to parse
118 * @return a ConnectPoint based on the information in the string.
119 */
120 public static ConnectPoint hostConnectPoint(String string) {
121 checkNotNull(string);
122 String[] splitted = string.split("/");
123 checkArgument(splitted.length == 3,
124 "Connect point must be in \"hostId/vlanId/portNumber\" format");
125
126 int lastSlash = string.lastIndexOf("/");
127
128 return new ConnectPoint(HostId.hostId(string.substring(0, lastSlash)),
129 PortNumber.portNumber(string.substring(lastSlash + 1, string.length())));
130 }
131
tombb58c202014-09-07 22:51:50 -0700132 @Override
133 public int hashCode() {
134 return Objects.hash(elementId, portNumber);
135 }
136
137 @Override
138 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700139 if (this == obj) {
140 return true;
141 }
tombb58c202014-09-07 22:51:50 -0700142 if (obj instanceof ConnectPoint) {
143 final ConnectPoint other = (ConnectPoint) obj;
144 return Objects.equals(this.elementId, other.elementId) &&
145 Objects.equals(this.portNumber, other.portNumber);
146 }
147 return false;
148 }
149
150 @Override
151 public String toString() {
alshabibdf652ad2014-09-09 11:53:19 -0700152 return MoreObjects.toStringHelper(this)
tombb58c202014-09-07 22:51:50 -0700153 .add("elementId", elementId)
154 .add("portNumber", portNumber)
155 .toString();
156 }
tom64b7aac2014-08-26 00:18:21 -0700157
158}