blob: e1d86957ac6a0afd854ae3d727aa5e56fb353644 [file] [log] [blame]
Daniele Moro94660a02019-12-02 12:02:07 -08001/*
Joey Armstronga68e7852024-01-28 13:27:02 -05002 * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
Daniele Moro94660a02019-12-02 12:02:07 -08003 *
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.opencord.bng.packets;
18
19import com.google.common.base.Objects;
20import org.onlab.packet.DeserializationException;
21
22import java.nio.ByteBuffer;
23import java.util.Arrays;
24
25/**
26 * Implements IPCP Type-Length-Value options.
27 */
28public class PppTlv {
29 public static final byte IPCPTLV_IP_ADDRESS = 0x03;
30 private byte type;
31 private byte length; // Including the 2 byte of Minimum header
32 private byte[] value;
33
34 /**
35 * Get the TLV type.
36 *
37 * @return the TLV type
38 */
39 public byte getType() {
40 return type;
41 }
42
43 /**
44 * Set the TLV type.
45 *
46 * @param type the TLV type to set
47 * @return this
48 */
49 public PppTlv setType(byte type) {
50 this.type = type;
51 return this;
52 }
53
54 /**
55 * Get the TLV length. The length include the 2 bytes of minimum header
56 * length.
57 *
58 * @return the TLV length
59 */
60 public byte getLength() {
61 return length;
62 }
63
64 /**
65 * Set the TLV length. Length must include the 2 bytes of minimum header
66 * length.
67 *
68 * @param length the TLV length to set.
69 * @return this
70 */
71 public PppTlv setLength(byte length) {
72 this.length = length;
73 return this;
74 }
75
76 /**
77 * Get the TLV value field as byte array.
78 *
79 * @return the TLV value field
80 */
81 public byte[] getValue() {
82 return value;
83 }
84
85 /**
86 * Set the TLV valued field.
87 *
88 * @param value the TLV value to set
89 * @return this
90 */
91 public PppTlv setValue(byte[] value) {
92 this.value = value;
93 return this;
94 }
95
96 public byte[] serialize() {
97 final byte[] data = new byte[this.length];
98 final ByteBuffer bb = ByteBuffer.wrap(data);
99 bb.put(type);
100 bb.put(length);
101 if (this.value != null) {
102 bb.put(this.value);
103 }
104 return data;
105 }
106
107 public PppTlv deserialize(final ByteBuffer bb) throws DeserializationException {
108 if (bb.remaining() < 2) {
109 throw new DeserializationException(
110 "Not enough bytes to deserialize PPP TLV options");
111 }
112 this.type = bb.get();
113 this.length = bb.get();
114 if (this.length > 2) {
115 this.value = new byte[this.length - 2];
116
117 // if there is an underrun just toss the TLV
118 // Length include the length of the TLV header itself
119 if (bb.remaining() < this.length - 2) {
120 throw new DeserializationException(
121 "Remaining bytes are less then the length of the PPP TLV tag");
122 }
123 bb.get(this.value);
124 return this;
125 } else {
126 return null;
127 }
128 }
129
130 @Override
131 public String toString() {
132 StringBuilder sb = new StringBuilder();
133 sb.append("[");
134 sb.append("type= ");
135 sb.append(this.type);
136 sb.append("length= ");
137 sb.append(this.length);
138 sb.append("value= ");
139 sb.append(Arrays.toString(this.value));
140 sb.append("]");
141 return sb.toString();
142 }
143
144 /*
145 * (non-Javadoc)
146 *
147 * @see java.lang.Object#equals(java.lang.Object)
148 */
149 @Override
150 public boolean equals(final Object obj) {
151 if (this == obj) {
152 return true;
153 }
154 if (obj == null) {
155 return false;
156 }
157 if (!(obj instanceof PppTlv)) {
158 return false;
159 }
160 final PppTlv other = (PppTlv) obj;
161 if (this.length != other.length) {
162 return false;
163 }
164 if (this.type != other.type) {
165 return false;
166 }
167 if (!Arrays.equals(this.value, other.value)) {
168 return false;
169 }
170 return true;
171 }
172
173 @Override
174 public int hashCode() {
175 return Objects.hashCode(type, length, Arrays.hashCode(value));
176 }
177}