blob: 1b53a5b9dcc93e76d7961f21c4118ed786de0681 [file] [log] [blame]
Yi Tsengc7403c22017-06-19 16:23:22 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengc7403c22017-06-19 16:23:22 -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 */
16
17package org.onlab.packet.dhcp;
18
19import com.google.common.collect.Lists;
Yi Tseng440e2b72017-08-24 14:47:34 -070020import com.google.common.primitives.UnsignedLongs;
Yi Tsengc7403c22017-06-19 16:23:22 -070021import org.onlab.packet.VlanId;
22
23import java.nio.charset.StandardCharsets;
24import java.util.List;
25import java.util.Objects;
26
27import static com.google.common.base.Preconditions.checkArgument;
28
29/**
30 * Representation of DHCP option 82 Circuit id.
31 */
32public class CircuitId {
33 private static final String SEPARATOR = ":";
34 private static final String CIRCUIT_ID_FORMAT = "%s" + SEPARATOR + "%s";
Yi Tseng440e2b72017-08-24 14:47:34 -070035 private static final String DEVICE_PORT_SEPARATOR = "/";
Yi Tsengc7403c22017-06-19 16:23:22 -070036 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 Tseng440e2b72017-08-24 14:47:34 -070070 List<String> splittedCircuitId = Lists.newArrayList(cIdString.split(SEPARATOR));
71 checkArgument(splittedCircuitId.size() > 1, "Illegal circuit id.");
Yi Tsengc7403c22017-06-19 16:23:22 -070072 // remove last element (vlan id)
Yi Tseng440e2b72017-08-24 14:47:34 -070073 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 Tsengc7403c22017-06-19 16:23:22 -070086 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}