Committed by
Gerrit Code Review
[ONOS-2600] Implementation of basic BGP protocol message parsing factories
Change-Id: Ie8ff454a36c9bff2a785ea198ed47ce5f60666cd
Showing
4 changed files
with
309 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 | + | ||
17 | +package org.onosproject.bgpio.protocol; | ||
18 | + | ||
19 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
20 | +import org.onosproject.bgpio.exceptions.BGPParseException; | ||
21 | +import org.onosproject.bgpio.protocol.ver4.BGPFactoryVer4; | ||
22 | +import org.onosproject.bgpio.types.BGPHeader; | ||
23 | +import org.slf4j.Logger; | ||
24 | +import org.slf4j.LoggerFactory; | ||
25 | + | ||
26 | +/** | ||
27 | + * Abstraction to provide the version for BGP. | ||
28 | + */ | ||
29 | +public final class BGPFactories { | ||
30 | + | ||
31 | + protected static final Logger log = LoggerFactory.getLogger(BGPFactories.class); | ||
32 | + | ||
33 | + private static final GenericReader GENERIC_READER = new GenericReader(); | ||
34 | + | ||
35 | + private BGPFactories() { | ||
36 | + } | ||
37 | + | ||
38 | + /** | ||
39 | + * Returns the instance of BGP Version. | ||
40 | + * | ||
41 | + * @param version BGP version | ||
42 | + * @return BGP version | ||
43 | + */ | ||
44 | + public static BGPFactory getFactory(BGPVersion version) { | ||
45 | + switch (version) { | ||
46 | + case BGP_4: | ||
47 | + return BGPFactoryVer4.INSTANCE; | ||
48 | + default: | ||
49 | + throw new IllegalArgumentException("[BGPFactory:]Unknown version: " + version); | ||
50 | + } | ||
51 | + } | ||
52 | + | ||
53 | + /** | ||
54 | + * Reader class for reading BGP messages from channel buffer. | ||
55 | + * | ||
56 | + */ | ||
57 | + private static class GenericReader implements BGPMessageReader<BGPMessage> { | ||
58 | + | ||
59 | + @Override | ||
60 | + public BGPMessage readFrom(ChannelBuffer bb, BGPHeader bgpHeader) | ||
61 | + throws BGPParseException { | ||
62 | + BGPFactory factory; | ||
63 | + | ||
64 | + if (!bb.readable()) { | ||
65 | + log.error("Empty message received"); | ||
66 | + throw new BGPParseException("Empty message received"); | ||
67 | + } | ||
68 | + // TODO: Currently only BGP version 4 is supported | ||
69 | + factory = org.onosproject.bgpio.protocol.ver4.BGPFactoryVer4.INSTANCE; | ||
70 | + return factory.getReader().readFrom(bb, bgpHeader); | ||
71 | + } | ||
72 | + } | ||
73 | + | ||
74 | + /** | ||
75 | + * Returns BGP messsage generic reader. | ||
76 | + * | ||
77 | + * @return bgp message generic reader | ||
78 | + */ | ||
79 | + public static BGPMessageReader<BGPMessage> getGenericReader() { | ||
80 | + return GENERIC_READER; | ||
81 | + } | ||
82 | +} |
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 | + | ||
17 | +package org.onosproject.bgpio.protocol; | ||
18 | + | ||
19 | +/** | ||
20 | + * Abstraction of an message factory providing builder functions to BGP messages | ||
21 | + * and objects. | ||
22 | + * | ||
23 | + */ | ||
24 | +public interface BGPFactory { | ||
25 | + | ||
26 | + /** | ||
27 | + * Gets the builder object for a open message. | ||
28 | + * | ||
29 | + * @return builder object for open message | ||
30 | + */ | ||
31 | + BGPOpenMsg.Builder openMessageBuilder(); | ||
32 | + | ||
33 | + /** | ||
34 | + * Gets the builder object for a keepalive message. | ||
35 | + * | ||
36 | + * @return builder object for keepalive message | ||
37 | + */ | ||
38 | + BGPKeepaliveMsg.Builder keepaliveMessageBuilder(); | ||
39 | + | ||
40 | + /** | ||
41 | + * Gets the builder object for a notification message. | ||
42 | + * | ||
43 | + * @return builder object for notification message. | ||
44 | + */ | ||
45 | + BGPNotificationMsg.Builder notificationMessageBuilder(); | ||
46 | + | ||
47 | + /** | ||
48 | + * Gets the BGP message reader. | ||
49 | + * | ||
50 | + * @return BGP message reader | ||
51 | + */ | ||
52 | + BGPMessageReader<BGPMessage> getReader(); | ||
53 | + | ||
54 | + /** | ||
55 | + * Returns BGP version. | ||
56 | + * | ||
57 | + * @return BGP version | ||
58 | + */ | ||
59 | + BGPVersion getVersion(); | ||
60 | +} |
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 | + | ||
17 | +package org.onosproject.bgpio.protocol.ver4; | ||
18 | + | ||
19 | +import org.onosproject.bgpio.protocol.BGPFactory; | ||
20 | +import org.onosproject.bgpio.protocol.BGPKeepaliveMsg; | ||
21 | +import org.onosproject.bgpio.protocol.BGPMessage; | ||
22 | +import org.onosproject.bgpio.protocol.BGPMessageReader; | ||
23 | +import org.onosproject.bgpio.protocol.BGPNotificationMsg; | ||
24 | +import org.onosproject.bgpio.protocol.BGPOpenMsg; | ||
25 | +import org.onosproject.bgpio.protocol.BGPVersion; | ||
26 | + | ||
27 | +/** | ||
28 | + * Provides BGP Factory and returns builder classes for all objects and messages. | ||
29 | + */ | ||
30 | +public class BGPFactoryVer4 implements BGPFactory { | ||
31 | + | ||
32 | + public static final BGPFactoryVer4 INSTANCE = new BGPFactoryVer4(); | ||
33 | + | ||
34 | + @Override | ||
35 | + public BGPOpenMsg.Builder openMessageBuilder() { | ||
36 | + return new BGPOpenMsgVer4.Builder(); | ||
37 | + } | ||
38 | + | ||
39 | + @Override | ||
40 | + public BGPKeepaliveMsg.Builder keepaliveMessageBuilder() { | ||
41 | + return new BGPKeepaliveMsgVer4.Builder(); | ||
42 | + } | ||
43 | + | ||
44 | + @Override | ||
45 | + public BGPNotificationMsg.Builder notificationMessageBuilder() { | ||
46 | + return new BGPNotificationMsgVer4.Builder(); | ||
47 | + } | ||
48 | + | ||
49 | + @Override | ||
50 | + public BGPMessageReader<BGPMessage> getReader() { | ||
51 | + return BGPMessageVer4.READER; | ||
52 | + } | ||
53 | + | ||
54 | + @Override | ||
55 | + public BGPVersion getVersion() { | ||
56 | + return BGPVersion.BGP_4; | ||
57 | + } | ||
58 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
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 | + | ||
17 | +package org.onosproject.bgpio.protocol.ver4; | ||
18 | + | ||
19 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
20 | +import org.onosproject.bgpio.exceptions.BGPParseException; | ||
21 | +import org.onosproject.bgpio.protocol.BGPFactories; | ||
22 | +import org.onosproject.bgpio.protocol.BGPMessage; | ||
23 | +import org.onosproject.bgpio.protocol.BGPMessageReader; | ||
24 | +import org.onosproject.bgpio.types.BGPErrorType; | ||
25 | +import org.onosproject.bgpio.types.BGPHeader; | ||
26 | +import org.onosproject.bgpio.util.Validation; | ||
27 | +import org.slf4j.Logger; | ||
28 | +import org.slf4j.LoggerFactory; | ||
29 | + | ||
30 | +/** | ||
31 | + * Provides BGP messages. | ||
32 | + */ | ||
33 | +public abstract class BGPMessageVer4 { | ||
34 | + | ||
35 | + protected static final Logger log = LoggerFactory.getLogger(BGPFactories.class); | ||
36 | + | ||
37 | + static final byte OPEN_MSG_TYPE = 0x1; | ||
38 | + static final byte KEEPALIVE_MSG_TYPE = 0x4; | ||
39 | + static final byte UPDATE_MSG_TYPE = 0x2; | ||
40 | + static final byte NOTIFICATION_MSG_TYPE = 0x3; | ||
41 | + static final int MINIMUM_COMMON_HEADER_LENGTH = 19; | ||
42 | + static final int HEADER_AND_MSG_LEN = 18; | ||
43 | + static final int MAXIMUM_PACKET_LENGTH = 4096; | ||
44 | + | ||
45 | + public static final BGPMessageVer4.Reader READER = new Reader(); | ||
46 | + | ||
47 | + /** | ||
48 | + * Reader class for reading BGP messages from channel buffer. | ||
49 | + * | ||
50 | + */ | ||
51 | + static class Reader implements BGPMessageReader<BGPMessage> { | ||
52 | + @Override | ||
53 | + public BGPMessage readFrom(ChannelBuffer cb, BGPHeader bgpHeader) | ||
54 | + throws BGPParseException { | ||
55 | + | ||
56 | + if (cb.readableBytes() < MINIMUM_COMMON_HEADER_LENGTH) { | ||
57 | + log.error("Packet should have minimum length."); | ||
58 | + Validation.validateLen(BGPErrorType.MESSAGE_HEADER_ERROR, BGPErrorType.BAD_MESSAGE_LENGTH, | ||
59 | + cb.readableBytes()); | ||
60 | + } | ||
61 | + if (cb.readableBytes() > MAXIMUM_PACKET_LENGTH) { | ||
62 | + log.error("Packet length should not exceed {}.", MAXIMUM_PACKET_LENGTH); | ||
63 | + Validation.validateLen(BGPErrorType.MESSAGE_HEADER_ERROR, BGPErrorType.BAD_MESSAGE_LENGTH, | ||
64 | + cb.readableBytes()); | ||
65 | + } | ||
66 | + try { | ||
67 | + // fixed value property version == 4 | ||
68 | + byte[] marker = new byte[BGPHeader.MARKER_LENGTH]; | ||
69 | + cb.readBytes(marker, 0, BGPHeader.MARKER_LENGTH); | ||
70 | + bgpHeader.setMarker(marker); | ||
71 | + for (int i = 0; i < BGPHeader.MARKER_LENGTH; i++) { | ||
72 | + if (marker[i] != (byte) 0xff) { | ||
73 | + throw new BGPParseException(BGPErrorType.MESSAGE_HEADER_ERROR, | ||
74 | + BGPErrorType.CONNECTION_NOT_SYNCHRONIZED, null); | ||
75 | + } | ||
76 | + } | ||
77 | + short length = cb.readShort(); | ||
78 | + if (length != (cb.readableBytes() + HEADER_AND_MSG_LEN)) { | ||
79 | + Validation.validateLen(BGPErrorType.MESSAGE_HEADER_ERROR, BGPErrorType.BAD_MESSAGE_LENGTH, length); | ||
80 | + } | ||
81 | + bgpHeader.setLength(length); | ||
82 | + byte type = cb.readByte(); | ||
83 | + bgpHeader.setType(type); | ||
84 | + log.debug("Reading update message of type " + type); | ||
85 | + | ||
86 | + int len = length - MINIMUM_COMMON_HEADER_LENGTH; | ||
87 | + switch (type) { | ||
88 | + case OPEN_MSG_TYPE: | ||
89 | + log.debug("OPEN MESSAGE is received"); | ||
90 | + return BGPOpenMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader); | ||
91 | + case KEEPALIVE_MSG_TYPE: | ||
92 | + log.debug("KEEPALIVE MESSAGE is received"); | ||
93 | + return BGPKeepaliveMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader); | ||
94 | + case UPDATE_MSG_TYPE: | ||
95 | + log.debug("UPDATE MESSAGE is received"); | ||
96 | + // TODO: Update message version 4 | ||
97 | + case NOTIFICATION_MSG_TYPE: | ||
98 | + log.debug("NOTIFICATION MESSAGE is received"); | ||
99 | + return BGPNotificationMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader); | ||
100 | + default: | ||
101 | + Validation.validateType(BGPErrorType.MESSAGE_HEADER_ERROR, BGPErrorType.BAD_MESSAGE_TYPE, type); | ||
102 | + return null; | ||
103 | + } | ||
104 | + } catch (IndexOutOfBoundsException e) { | ||
105 | + throw new BGPParseException(BGPErrorType.MESSAGE_HEADER_ERROR, BGPErrorType.BAD_MESSAGE_LENGTH, null); | ||
106 | + } | ||
107 | + } | ||
108 | + } | ||
109 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment