blob: f8d47b5a3ed22c3bc50164bbbfbd0f1d1dc726b2 [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 the PPPoE Type-Length-Value TAGs.
27 */
28public class PppoeTlvTag {
29
30 public static final short PPPOED_TAGTYPE_EOL = 0;
31 public static final short PPPOED_TAGTYPE_SERVICENAME = 0x0101;
32 public static final short PPPOED_TAGTYPE_ACNAME = 0x0102;
33
34 private short tagType;
35 private short length; // Length excluded the header (4 bytes minimum header)
36 private byte[] value;
37
38 /**
39 * Gets the TLV TAG type.
40 *
41 * @return the TLV TAG type
42 */
43 public short getTagType() {
44 return tagType;
45 }
46
47 /**
48 * Sets the TLV TAG type.
49 *
50 * @param tagType the type to set
51 * @return this
52 */
53 public PppoeTlvTag setTagType(short tagType) {
54 this.tagType = tagType;
55 return this;
56 }
57
58 /**
59 * Gets the length, number of bytes of value field.
60 *
61 * @return the length
62 */
63 public short getLength() {
64 return length;
65 }
66
67 /**
68 * Sets the length.
69 *
70 * @param length the length to set excluded the header length
71 * @return this
72 */
73 public PppoeTlvTag setLength(final short length) {
74 this.length = length;
75 return this;
76 }
77
78 /**
79 * The TLV value.
80 *
81 * @return the value
82 */
83 public byte[] getValue() {
84 return this.value;
85 }
86
87 /**
88 * Set the TLV value.
89 *
90 * @param value the value to set
91 * @return this
92 */
93 public PppoeTlvTag setValue(final byte[] value) {
94 this.value = value;
95 return this;
96 }
97
98 public byte[] serialize() {
99 final byte[] data = new byte[4 + this.length];
100 final ByteBuffer bb = ByteBuffer.wrap(data);
101 bb.putShort(tagType);
102 bb.putShort(length);
103 if (this.value != null) {
104 bb.put(this.value);
105 }
106 return data;
107 }
108
109 public PppoeTlvTag deserialize(final ByteBuffer bb) throws DeserializationException {
110 if (bb.remaining() < 4) {
111 throw new DeserializationException(
112 "Not enough bytes to deserialize PPPoE TLV tag type and length");
113 }
114 this.tagType = bb.getShort();
115 this.length = bb.getShort();
116
117 if (this.length > 0) {
118 this.value = new byte[this.length];
119
120 // if there is an underrun just toss the TLV
121 if (bb.remaining() < this.length) {
122 throw new DeserializationException(
123 "Remaining bytes are less then the length of the PPPoE TLV tag");
124 }
125 bb.get(this.value);
126 }
127 return this;
128 }
129
130 @Override
131 public String toString() {
132 StringBuilder sb = new StringBuilder();
133 sb.append("[");
134 sb.append("type= ");
135 sb.append(this.tagType);
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 PppoeTlvTag)) {
158 return false;
159 }
160 final PppoeTlvTag other = (PppoeTlvTag) obj;
161 if (this.length != other.length) {
162 return false;
163 }
164 if (this.tagType != other.tagType) {
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(tagType, length, Arrays.hashCode(value));
176 }
177}