blob: 22342465ab78b2de27a40260b53e0b213dd5027f [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;
20import org.onlab.packet.VlanId;
21
22import java.nio.charset.StandardCharsets;
23import java.util.List;
24import java.util.Objects;
25
26import static com.google.common.base.Preconditions.checkArgument;
27
28/**
29 * Representation of DHCP option 82 Circuit id.
30 */
31public 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);
Yi Tseng440e2b72017-08-24 14:47:34 -070068 List<String> splittedCircuitId = Lists.newArrayList(cIdString.split(SEPARATOR));
69 checkArgument(splittedCircuitId.size() > 1, "Illegal circuit id.");
Yi Tsengc7403c22017-06-19 16:23:22 -070070 // remove last element (vlan id)
Yi Tseng440e2b72017-08-24 14:47:34 -070071 String vlanId = splittedCircuitId.remove(splittedCircuitId.size() - 1);
72
pierventre581c8402021-09-23 19:03:14 +020073 // Reconstruct the connect point string
Yi Tseng440e2b72017-08-24 14:47:34 -070074 String connectPoint = String.join(SEPARATOR, splittedCircuitId);
75
pierventre581c8402021-09-23 19:03:14 +020076 /*
77 * As device IDs may have a path component, we are expecting one
78 * of:
79 * - scheme:ip:port/cp
80 * - scheme:ip:port/path/cp
81 *
82 * The assumption is the last `/` will separate the device ID
83 * from the connection point number. Please note that we are
84 * not actually checking here the content of the strings
85 */
86 int idx = connectPoint.lastIndexOf("/");
87 checkArgument(idx != -1, "Connect point not specified, " +
88 "Connect point must be in \"deviceUri/portNumber\" format");
Yi Tseng440e2b72017-08-24 14:47:34 -070089
pierventre581c8402021-09-23 19:03:14 +020090 String cp = connectPoint.substring(idx + 1);
91 checkArgument(!cp.isEmpty(), "Connect point separator specified, " +
92 "but no port number included, connect point must be in \"deviceUri/portNumber\" format");
Yi Tseng440e2b72017-08-24 14:47:34 -070093
Yi Tsengc7403c22017-06-19 16:23:22 -070094 return new CircuitId(connectPoint, VlanId.vlanId(vlanId));
95 }
96
97 /**
98 * Gets the connect point of circuit id.
99 *
100 * @return the connect point
101 */
102 public String connectPoint() {
103 return connectPoint;
104 }
105
106 /**
107 * Gets the vlan id of circuit id.
108 *
109 * @return the vlan id
110 */
111 public VlanId vlanId() {
112 return vlanId;
113 }
114
115 @Override
116 public boolean equals(Object obj) {
117 if (obj == this) {
118 return true;
119 }
120 if (!(obj instanceof CircuitId)) {
121 return false;
122 }
123 CircuitId that = (CircuitId) obj;
124 return Objects.equals(this.connectPoint, that.connectPoint) &&
125 Objects.equals(this.vlanId, that.vlanId);
126 }
127
128 @Override
129 public int hashCode() {
130 return Objects.hash(connectPoint, vlanId);
131 }
132}