Committed by
Thomas Vachuska
[Emu][ONOS-2597] BGP Keepalive message encoding and decoding
Change-Id: I4dd39f1725aa426827d5c4f011f5a0e02557a5c3
Showing
2 changed files
with
230 additions
and
0 deletions
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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 | +package org.onosproject.bgpio.protocol; | ||
17 | + | ||
18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
19 | +import org.onosproject.bgpio.types.BGPHeader; | ||
20 | + | ||
21 | +/** | ||
22 | + * Abstraction of an entity providing BGP Keepalive Message. | ||
23 | + */ | ||
24 | +public interface BGPKeepaliveMsg extends BGPMessage { | ||
25 | + | ||
26 | + @Override | ||
27 | + BGPVersion getVersion(); | ||
28 | + | ||
29 | + @Override | ||
30 | + BGPType getType(); | ||
31 | + | ||
32 | + @Override | ||
33 | + void writeTo(ChannelBuffer channelBuffer); | ||
34 | + | ||
35 | + @Override | ||
36 | + BGPHeader getHeader(); | ||
37 | + | ||
38 | + /** | ||
39 | + * Builder interface with get and set functions to build Keepalive message. | ||
40 | + */ | ||
41 | + interface Builder extends BGPMessage.Builder { | ||
42 | + | ||
43 | + @Override | ||
44 | + BGPKeepaliveMsg build(); | ||
45 | + | ||
46 | + @Override | ||
47 | + BGPVersion getVersion(); | ||
48 | + | ||
49 | + @Override | ||
50 | + BGPType getType(); | ||
51 | + | ||
52 | + @Override | ||
53 | + Builder setHeader(BGPHeader bgpMsgHeader); | ||
54 | + | ||
55 | + @Override | ||
56 | + BGPHeader getHeader(); | ||
57 | + } | ||
58 | +} |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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 | +package org.onosproject.bgpio.protocol.ver4; | ||
17 | + | ||
18 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
19 | +import org.onosproject.bgpio.exceptions.BGPParseException; | ||
20 | +import org.onosproject.bgpio.protocol.BGPKeepaliveMsg; | ||
21 | +import org.onosproject.bgpio.protocol.BGPMessageReader; | ||
22 | +import org.onosproject.bgpio.protocol.BGPMessageWriter; | ||
23 | +import org.onosproject.bgpio.types.BGPHeader; | ||
24 | +import org.onosproject.bgpio.protocol.BGPType; | ||
25 | +import org.onosproject.bgpio.protocol.BGPVersion; | ||
26 | +import org.slf4j.Logger; | ||
27 | +import org.slf4j.LoggerFactory; | ||
28 | + | ||
29 | +import com.google.common.base.MoreObjects; | ||
30 | + | ||
31 | +/** | ||
32 | + * Provides BGP keep alive message. | ||
33 | + */ | ||
34 | +class BGPKeepaliveMsgVer4 implements BGPKeepaliveMsg { | ||
35 | + | ||
36 | + /* | ||
37 | + <Keepalive Message>::= <Common Header> | ||
38 | + A KEEPALIVE message consists of only the message header and has a | ||
39 | + length of 19 octets. | ||
40 | + | ||
41 | + 0 1 2 3 | ||
42 | + 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 | ||
43 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
44 | + | | | ||
45 | + + + | ||
46 | + | | | ||
47 | + + + | ||
48 | + | Marker | | ||
49 | + + + | ||
50 | + | | | ||
51 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
52 | + | Length | Type | | ||
53 | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
54 | + | ||
55 | + REFERENCE : RFC 4271 | ||
56 | + */ | ||
57 | + | ||
58 | + protected static final Logger log = LoggerFactory | ||
59 | + .getLogger(BGPKeepaliveMsgVer4.class); | ||
60 | + | ||
61 | + private BGPHeader bgpMsgHeader; | ||
62 | + public static final byte PACKET_VERSION = 4; | ||
63 | + public static final int PACKET_MINIMUM_LENGTH = 19; | ||
64 | + public static final int MARKER_LENGTH = 16; | ||
65 | + public static final BGPType MSG_TYPE = BGPType.KEEP_ALIVE; | ||
66 | + public static byte[] marker = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, | ||
67 | + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, | ||
68 | + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, | ||
69 | + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; | ||
70 | + | ||
71 | + public static final BGPKeepaliveMsgVer4.Reader READER = new Reader(); | ||
72 | + | ||
73 | + /** | ||
74 | + * Reader class for reading BGP keepalive message from channel buffer. | ||
75 | + */ | ||
76 | + static class Reader implements BGPMessageReader<BGPKeepaliveMsg> { | ||
77 | + | ||
78 | + @Override | ||
79 | + public BGPKeepaliveMsg readFrom(ChannelBuffer cb, BGPHeader bgpHeader) | ||
80 | + throws BGPParseException { | ||
81 | + | ||
82 | + /* bgpHeader is not required in case of keepalive message and | ||
83 | + Header is already read and no other fields except header in keepalive message.*/ | ||
84 | + return new BGPKeepaliveMsgVer4(); | ||
85 | + } | ||
86 | + } | ||
87 | + | ||
88 | + /** | ||
89 | + * Default constructor. | ||
90 | + */ | ||
91 | + BGPKeepaliveMsgVer4() { | ||
92 | + } | ||
93 | + | ||
94 | + /** | ||
95 | + * Builder class for BGP keepalive message. | ||
96 | + */ | ||
97 | + static class Builder implements BGPKeepaliveMsg.Builder { | ||
98 | + BGPHeader bgpMsgHeader; | ||
99 | + | ||
100 | + @Override | ||
101 | + public BGPVersion getVersion() { | ||
102 | + return BGPVersion.BGP_4; | ||
103 | + } | ||
104 | + | ||
105 | + @Override | ||
106 | + public BGPType getType() { | ||
107 | + return BGPType.KEEP_ALIVE; | ||
108 | + } | ||
109 | + | ||
110 | + @Override | ||
111 | + public BGPHeader getHeader() { | ||
112 | + return this.bgpMsgHeader; | ||
113 | + } | ||
114 | + | ||
115 | + @Override | ||
116 | + public Builder setHeader(BGPHeader bgpMsgHeader) { | ||
117 | + this.bgpMsgHeader = bgpMsgHeader; | ||
118 | + return this; | ||
119 | + } | ||
120 | + | ||
121 | + @Override | ||
122 | + public BGPKeepaliveMsg build() { | ||
123 | + return new BGPKeepaliveMsgVer4(); | ||
124 | + } | ||
125 | + } | ||
126 | + | ||
127 | + @Override | ||
128 | + public void writeTo(ChannelBuffer cb) { | ||
129 | + WRITER.write(cb, this); | ||
130 | + } | ||
131 | + | ||
132 | + static final Writer WRITER = new Writer(); | ||
133 | + | ||
134 | + /** | ||
135 | + * Writer class for writing the BGP keepalive message to channel buffer. | ||
136 | + */ | ||
137 | + static class Writer implements BGPMessageWriter<BGPKeepaliveMsgVer4> { | ||
138 | + | ||
139 | + @Override | ||
140 | + public void write(ChannelBuffer cb, BGPKeepaliveMsgVer4 message) { | ||
141 | + | ||
142 | + // write marker | ||
143 | + cb.writeBytes(marker, 0, MARKER_LENGTH); | ||
144 | + | ||
145 | + // write length of header | ||
146 | + cb.writeShort(PACKET_MINIMUM_LENGTH); | ||
147 | + | ||
148 | + // write the type of message | ||
149 | + cb.writeByte(MSG_TYPE.getType()); | ||
150 | + } | ||
151 | + } | ||
152 | + | ||
153 | + @Override | ||
154 | + public BGPVersion getVersion() { | ||
155 | + return BGPVersion.BGP_4; | ||
156 | + } | ||
157 | + | ||
158 | + @Override | ||
159 | + public BGPType getType() { | ||
160 | + return MSG_TYPE; | ||
161 | + } | ||
162 | + | ||
163 | + @Override | ||
164 | + public BGPHeader getHeader() { | ||
165 | + return this.bgpMsgHeader; | ||
166 | + } | ||
167 | + | ||
168 | + @Override | ||
169 | + public String toString() { | ||
170 | + return MoreObjects.toStringHelper(getClass()).toString(); | ||
171 | + } | ||
172 | +} |
-
Please register or login to post a comment