Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.onlab.packet.dhcp; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
Yi Tseng | 440e2b7 | 2017-08-24 14:47:34 -0700 | [diff] [blame] | 20 | import com.google.common.primitives.UnsignedLongs; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 21 | import org.onlab.packet.VlanId; |
| 22 | |
| 23 | import java.nio.charset.StandardCharsets; |
| 24 | import java.util.List; |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | |
| 29 | /** |
| 30 | * Representation of DHCP option 82 Circuit id. |
| 31 | */ |
| 32 | public class CircuitId { |
| 33 | private static final String SEPARATOR = ":"; |
| 34 | private static final String CIRCUIT_ID_FORMAT = "%s" + SEPARATOR + "%s"; |
Yi Tseng | 440e2b7 | 2017-08-24 14:47:34 -0700 | [diff] [blame] | 35 | private static final String DEVICE_PORT_SEPARATOR = "/"; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 36 | private String connectPoint; |
| 37 | private VlanId vlanId; |
| 38 | |
| 39 | /** |
| 40 | * Creates a circuit id by given information. |
| 41 | * |
| 42 | * @param connectPoint the connect point of circuit id |
| 43 | * @param vlanId the vlan id of circuit id |
| 44 | */ |
| 45 | public CircuitId(String connectPoint, VlanId vlanId) { |
| 46 | this.connectPoint = connectPoint; |
| 47 | this.vlanId = vlanId; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Combines connect point with vlan id with separator ':' as circuit id. |
| 52 | * e.g. of:0000000000000204/1:100 |
| 53 | * |
| 54 | * @return serialized circuit id for connect point and vlan ID |
| 55 | */ |
| 56 | public byte[] serialize() { |
| 57 | return String |
| 58 | .format(CIRCUIT_ID_FORMAT, connectPoint, vlanId.toString()) |
| 59 | .getBytes(StandardCharsets.US_ASCII); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Deserialize circuit id from byte string. |
| 64 | * |
| 65 | * @param circuitId the circuit id byte string |
| 66 | * @return a Circuit Id |
| 67 | */ |
| 68 | public static CircuitId deserialize(byte[] circuitId) { |
| 69 | String cIdString = new String(circuitId, StandardCharsets.US_ASCII); |
Yi Tseng | 440e2b7 | 2017-08-24 14:47:34 -0700 | [diff] [blame] | 70 | List<String> splittedCircuitId = Lists.newArrayList(cIdString.split(SEPARATOR)); |
| 71 | checkArgument(splittedCircuitId.size() > 1, "Illegal circuit id."); |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 72 | // remove last element (vlan id) |
Yi Tseng | 440e2b7 | 2017-08-24 14:47:34 -0700 | [diff] [blame] | 73 | String vlanId = splittedCircuitId.remove(splittedCircuitId.size() - 1); |
| 74 | |
| 75 | // Reconstruct device Id |
| 76 | String connectPoint = String.join(SEPARATOR, splittedCircuitId); |
| 77 | |
| 78 | String[] splittedConnectPoint = connectPoint.split(DEVICE_PORT_SEPARATOR); |
| 79 | // Check connect point is valid or not |
| 80 | checkArgument(splittedConnectPoint.length == 2, |
| 81 | "Connect point must be in \"deviceUri/portNumber\" format"); |
| 82 | |
| 83 | // Check the port number is a number or not |
| 84 | UnsignedLongs.decode(splittedConnectPoint[1]); |
| 85 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 86 | return new CircuitId(connectPoint, VlanId.vlanId(vlanId)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Gets the connect point of circuit id. |
| 91 | * |
| 92 | * @return the connect point |
| 93 | */ |
| 94 | public String connectPoint() { |
| 95 | return connectPoint; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Gets the vlan id of circuit id. |
| 100 | * |
| 101 | * @return the vlan id |
| 102 | */ |
| 103 | public VlanId vlanId() { |
| 104 | return vlanId; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean equals(Object obj) { |
| 109 | if (obj == this) { |
| 110 | return true; |
| 111 | } |
| 112 | if (!(obj instanceof CircuitId)) { |
| 113 | return false; |
| 114 | } |
| 115 | CircuitId that = (CircuitId) obj; |
| 116 | return Objects.equals(this.connectPoint, that.connectPoint) && |
| 117 | Objects.equals(this.vlanId, that.vlanId); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public int hashCode() { |
| 122 | return Objects.hash(connectPoint, vlanId); |
| 123 | } |
| 124 | } |