blob: b64e34b543b2d1d4b2ac170f9968d2d04c4ba888 [file] [log] [blame]
Niraj Dubeya8287192021-03-23 19:54:17 +05301/*
2 * Copyright 2021-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.onlab.packet.bmp;
18
19import com.google.common.base.MoreObjects;
20import org.onlab.packet.BasePacket;
21import org.onlab.packet.Deserializer;
22
23import java.nio.ByteBuffer;
24
25import static org.onlab.packet.PacketUtils.checkInput;
26
27/**
28 * The following common header appears in all BMP messages. The rest of
29 * the data in a BMP message is dependent on the Message Type field in
30 * the common header.
31 * <p>
32 * Version (1 byte): Indicates the BMP version. This is set to '3'
33 * for all messages defined in this specification. ('1' and '2' were
34 * used by draft versions of this document.) Version 0 is reserved
35 * and MUST NOT be sent.
36 * <p>
37 * Message Length (4 bytes): Length of the message in bytes
38 * (including headers, data, and encapsulated messages, if any).
39 * <p>
40 * Message Type (1 byte): This identifies the type of the BMP
41 * message. A BMP implementation MUST ignore unrecognized message
42 * types upon receipt.
43 * <p>
44 * Type = 0: Route Monitoring
45 * Type = 1: Statistics Report
46 * Type = 2: Peer Down Notification
47 * Type = 3: Peer Up Notification
48 * Type = 4: Initiation Message
49 * Type = 5: Termination Message
50 * Type = 6: Route Mirroring Message
51 */
52public class Bmp extends BasePacket {
53
54 /*
55
56 0 1 2 3
57 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
58 +-+-+-+-+-+-+-+-+
59 | Version |
60 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61 | Message Length |
62 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 | Msg. Type |
64 +---------------+
65
66 */
67
68
69 public static final short DEFAULT_HEADER_LENGTH = 6;
70 public static final int DEFAULT_PACKET_MINIMUM_LENGTH = 4;
71
72 protected byte version;
73 protected byte type;
74 protected int length;
75
76
77 /**
78 * Sets version field.
79 *
80 * @param version message version field
81 */
82 public void setVersion(byte version) {
83 this.version = version;
84 }
85
86 /**
87 * Sets message type.
88 *
89 * @param type message type
90 */
91 public void setType(byte type) {
92 this.type = type;
93 }
94
95 /**
96 * Sets message length.
97 *
98 * @param length message length
99 */
100 public void setLength(int length) {
101 this.length = length;
102 }
103
104 /**
105 * Returns message length.
106 *
107 * @return message length
108 */
109 public int getLength() {
110 return this.length;
111 }
112
113 /**
114 * Returns message version.
115 *
116 * @return message version
117 */
118 public byte getVersion() {
119 return this.version;
120 }
121
122 /**
123 * Returns message type.
124 *
125 * @return message type
126 */
127 public byte getType() {
128 return this.type;
129 }
130
131
132 @Override
133 public byte[] serialize() {
134 final byte[] data = new byte[DEFAULT_HEADER_LENGTH];
135 final ByteBuffer bb = ByteBuffer.wrap(data);
136
137 bb.put(this.version);
138 bb.put(this.type);
139 bb.putInt(this.length);
140
141 return data;
142 }
143
144
145 /**
146 * Deserializer function for Bmp Packets.
147 *
148 * @return deserializer function
149 */
150 public static Deserializer<Bmp> deserializer() {
151 return (data, offset, length) -> {
152 checkInput(data, offset, length, DEFAULT_HEADER_LENGTH);
153
154 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
155 Bmp bmp = new Bmp();
156
157 bmp.version = bb.get();
158 bmp.length = bb.getInt();
159 bmp.type = bb.get();
160
161 return bmp;
162 };
163 }
164
165 @Override
166 public String toString() {
167 return MoreObjects.toStringHelper(getClass())
168 .add("version", version)
169 .add("type", type)
170 .add("length", length)
171 .toString();
172 }
173}
174