Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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'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 | |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
| 19 | |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 22 | import static com.google.common.base.Preconditions.checkArgument; |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 24 | |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Abstraction of a network connection point expressed as a pair of the |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 27 | * network element identifier and port number. |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 28 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 29 | public class ConnectPoint { |
| 30 | |
| 31 | private final ElementId elementId; |
| 32 | private final PortNumber portNumber; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 35 | * Creates a new connection point. |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 36 | * |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 37 | * @param elementId network element identifier |
| 38 | * @param portNumber port number |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 39 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 40 | 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 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 63 | public DeviceId deviceId() { |
| 64 | if (elementId instanceof DeviceId) { |
| 65 | return (DeviceId) elementId; |
| 66 | } |
| 67 | throw new IllegalStateException("Connection point not associated " + |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 68 | "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"); |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 85 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Returns the connection port number. |
| 89 | * |
| 90 | * @return port number |
| 91 | */ |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 92 | public PortNumber port() { |
| 93 | return portNumber; |
| 94 | } |
| 95 | |
Jonathan Hart | c3af35a | 2015-04-30 22:20:29 -0700 | [diff] [blame] | 96 | /** |
| 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 | |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 132 | @Override |
| 133 | public int hashCode() { |
| 134 | return Objects.hash(elementId, portNumber); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 139 | if (this == obj) { |
| 140 | return true; |
| 141 | } |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 142 | 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() { |
alshabib | df652ad | 2014-09-09 11:53:19 -0700 | [diff] [blame] | 152 | return MoreObjects.toStringHelper(this) |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 153 | .add("elementId", elementId) |
| 154 | .add("portNumber", portNumber) |
| 155 | .toString(); |
| 156 | } |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 157 | |
| 158 | } |