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; |
| 20 | import org.onlab.packet.VlanId; |
| 21 | |
| 22 | import java.nio.charset.StandardCharsets; |
| 23 | import java.util.List; |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | |
| 28 | /** |
| 29 | * Representation of DHCP option 82 Circuit id. |
| 30 | */ |
| 31 | public class CircuitId { |
| 32 | private static final String SEPARATOR = ":"; |
| 33 | private static final String CIRCUIT_ID_FORMAT = "%s" + SEPARATOR + "%s"; |
| 34 | private String connectPoint; |
| 35 | private VlanId vlanId; |
| 36 | |
| 37 | /** |
| 38 | * Creates a circuit id by given information. |
| 39 | * |
| 40 | * @param connectPoint the connect point of circuit id |
| 41 | * @param vlanId the vlan id of circuit id |
| 42 | */ |
| 43 | public CircuitId(String connectPoint, VlanId vlanId) { |
| 44 | this.connectPoint = connectPoint; |
| 45 | this.vlanId = vlanId; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Combines connect point with vlan id with separator ':' as circuit id. |
| 50 | * e.g. of:0000000000000204/1:100 |
| 51 | * |
| 52 | * @return serialized circuit id for connect point and vlan ID |
| 53 | */ |
| 54 | public byte[] serialize() { |
| 55 | return String |
| 56 | .format(CIRCUIT_ID_FORMAT, connectPoint, vlanId.toString()) |
| 57 | .getBytes(StandardCharsets.US_ASCII); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Deserialize circuit id from byte string. |
| 62 | * |
| 63 | * @param circuitId the circuit id byte string |
| 64 | * @return a Circuit Id |
| 65 | */ |
| 66 | public static CircuitId deserialize(byte[] circuitId) { |
| 67 | String cIdString = new String(circuitId, StandardCharsets.US_ASCII); |
| 68 | List<String> split = Lists.newArrayList(cIdString.split(SEPARATOR)); |
| 69 | checkArgument(split.size() > 1, "Illegal circuit id."); |
| 70 | // remove last element (vlan id) |
| 71 | String vlanId = split.remove(split.size() - 1); |
| 72 | String connectPoint = String.join(SEPARATOR, split); |
| 73 | return new CircuitId(connectPoint, VlanId.vlanId(vlanId)); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Gets the connect point of circuit id. |
| 78 | * |
| 79 | * @return the connect point |
| 80 | */ |
| 81 | public String connectPoint() { |
| 82 | return connectPoint; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Gets the vlan id of circuit id. |
| 87 | * |
| 88 | * @return the vlan id |
| 89 | */ |
| 90 | public VlanId vlanId() { |
| 91 | return vlanId; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public boolean equals(Object obj) { |
| 96 | if (obj == this) { |
| 97 | return true; |
| 98 | } |
| 99 | if (!(obj instanceof CircuitId)) { |
| 100 | return false; |
| 101 | } |
| 102 | CircuitId that = (CircuitId) obj; |
| 103 | return Objects.equals(this.connectPoint, that.connectPoint) && |
| 104 | Objects.equals(this.vlanId, that.vlanId); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public int hashCode() { |
| 109 | return Objects.hash(connectPoint, vlanId); |
| 110 | } |
| 111 | } |