blob: 63919a9bce4f58dd34e1012fa293564f520b6778 [file] [log] [blame]
karthik19773c2d04e2024-03-22 16:55:55 +05301/*
2 * Copyright 2024-present Open Networking Foundation
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
17package org.onosproject.bgpmonitoring.type;
18
19
20import com.google.common.base.MoreObjects;
21
22import java.nio.charset.StandardCharsets;
23import java.nio.ByteBuffer;
24
25import org.onlab.packet.BasePacket;
26import org.onlab.packet.Deserializer;
27
28import java.util.function.BiPredicate;
29
30import org.onosproject.bgpmonitoring.BmpMsg;
31import org.onosproject.bgpmonitoring.BmpParseException;
32
33
34public final class BmpInitMsg extends BasePacket implements BmpMsg {
35
36 public static final int MESSAGE_HEADER_MIN_LENGTH = 4;
37
38 private short type;
39
40 private short length;
41
42 private String data;
43
44 private BmpInitMsg(Builder builder) {
45 this.type = builder.type;
46 this.length = builder.length;
47 this.data = builder.data;
48 }
49
50 /**
51 * Returns initiation message type.
52 *
53 * @return initiation message type
54 */
55 @Override
56 public short getType() {
57 return type;
58 }
59
60 /**
61 * Returns initiation message length.
62 *
63 * @return initiation message length
64 */
65 @Override
66 public short getLength() {
67 return length;
68 }
69
70 /**
71 * Returns initiation message data.
72 *
73 * @return initiation message data
74 */
75 @Override
76 public String getData() {
77 return data;
78 }
79
80 /**
81 * Data deserializer function for BMP initiation message.
82 *
83 * @return data deserializer function
84 */
85 public static Deserializer<BmpInitMsg> deserializer() {
86 return (data, offset, length) -> {
87 BiPredicate<ByteBuffer, Integer> isValidBuffer = (b, l)
88 -> b.hasRemaining() && b.remaining() >= l;
89
90 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
91 if (!isValidBuffer.test(bb, MESSAGE_HEADER_MIN_LENGTH)) {
92 throw new BmpParseException("Invalid bmp initiation message buffer size.");
93 }
94 return new Builder().type(bb.getShort())
95 .length(bb.getShort())
96 .data(bb)
97 .build();
98 };
99 }
100
101 @Override
102 public byte[] serialize() {
103 throw new UnsupportedOperationException("Not supported yet.");
104 }
105
106 @Override
107 public String toString() {
108
109 return MoreObjects.toStringHelper(getClass())
110 .add("type", type)
111 .add("length", length)
112 .add("data", data)
113 .toString();
114 }
115
116 /**
117 * Builder for BMP initiation message.
118 */
119 private static class Builder {
120
121 private short type;
122
123 private short length;
124
125 private String data;
126
127 /**
128 * Setter bmp initiation message type.
129 *
130 * @param type bmp initiation message type.
131 * @return this class builder.
132 */
133 public Builder type(short type) {
134 this.type = type;
135 return this;
136 }
137
138 /**
139 * Setter bmp initiation message length.
140 *
141 * @param length bmp initiation message length.
142 * @return this class builder.
143 */
144 public Builder length(short length) {
145 this.length = length;
146 return this;
147 }
148
149 /**
150 * Setter bmp initiation message.
151 *
152 * @param bb byte buffer.
153 * @return this class builder.
154 */
155 public Builder data(ByteBuffer bb) {
156 if (length != 0 && bb.remaining() > length) {
157 throw new BmpParseException("Not enough readable bytes");
158 }
159 byte[] dataBytes = new byte[length];
160 bb.get(dataBytes);
161 switch (type) {
162 case 0:
163 this.data = new String(dataBytes, StandardCharsets.UTF_8);
164 break;
165 case 1:
166 case 2:
167 this.data = new String(dataBytes, StandardCharsets.US_ASCII);
168 break;
169 default:
170 this.data = new String(dataBytes, StandardCharsets.UTF_16);
171 }
172 return this;
173 }
174
175
176 /**
177 * Builds bmp initiation message.
178 *
179 * @return bmp initiation message.
180 */
181 public BmpInitMsg build() {
182 return new BmpInitMsg(this);
183 }
184 }
185}