blob: 1c05dae472f03775650e0e01d8d7ba1a9805d159 [file] [log] [blame]
Shashikanth VH95bd85e2015-10-27 14:56:33 +05301/*
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
17package org.onosproject.bgpio.protocol.ver4;
18
19import org.jboss.netty.buffer.ChannelBuffer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053020import org.onosproject.bgpio.exceptions.BgpParseException;
21import org.onosproject.bgpio.protocol.BgpFactories;
22import org.onosproject.bgpio.protocol.BgpMessage;
23import org.onosproject.bgpio.protocol.BgpMessageReader;
24import org.onosproject.bgpio.types.BgpErrorType;
25import org.onosproject.bgpio.types.BgpHeader;
Shashikanth VH95bd85e2015-10-27 14:56:33 +053026import org.onosproject.bgpio.util.Validation;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30/**
31 * Provides BGP messages.
32 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053033public abstract class BgpMessageVer4 {
Shashikanth VH95bd85e2015-10-27 14:56:33 +053034
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053035 protected static final Logger log = LoggerFactory.getLogger(BgpFactories.class);
Shashikanth VH95bd85e2015-10-27 14:56:33 +053036
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
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053045 public static final BgpMessageVer4.Reader READER = new Reader();
Shashikanth VH95bd85e2015-10-27 14:56:33 +053046
47 /**
48 * Reader class for reading BGP messages from channel buffer.
49 *
50 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053051 static class Reader implements BgpMessageReader<BgpMessage> {
Shashikanth VH95bd85e2015-10-27 14:56:33 +053052 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053053 public BgpMessage readFrom(ChannelBuffer cb, BgpHeader bgpHeader)
54 throws BgpParseException {
Shashikanth VH95bd85e2015-10-27 14:56:33 +053055
56 if (cb.readableBytes() < MINIMUM_COMMON_HEADER_LENGTH) {
57 log.error("Packet should have minimum length.");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053058 Validation.validateLen(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH,
Shashikanth VH95bd85e2015-10-27 14:56:33 +053059 cb.readableBytes());
60 }
61 if (cb.readableBytes() > MAXIMUM_PACKET_LENGTH) {
62 log.error("Packet length should not exceed {}.", MAXIMUM_PACKET_LENGTH);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053063 Validation.validateLen(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH,
Shashikanth VH95bd85e2015-10-27 14:56:33 +053064 cb.readableBytes());
65 }
66 try {
67 // fixed value property version == 4
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053068 byte[] marker = new byte[BgpHeader.MARKER_LENGTH];
69 cb.readBytes(marker, 0, BgpHeader.MARKER_LENGTH);
Shashikanth VH95bd85e2015-10-27 14:56:33 +053070 bgpHeader.setMarker(marker);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053071 for (int i = 0; i < BgpHeader.MARKER_LENGTH; i++) {
Shashikanth VH95bd85e2015-10-27 14:56:33 +053072 if (marker[i] != (byte) 0xff) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053073 throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR,
74 BgpErrorType.CONNECTION_NOT_SYNCHRONIZED, null);
Shashikanth VH95bd85e2015-10-27 14:56:33 +053075 }
76 }
77 short length = cb.readShort();
Shashikanth VHe24dcd12015-11-23 22:11:36 +053078 if (length > cb.readableBytes() + HEADER_AND_MSG_LEN) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053079 Validation.validateLen(BgpErrorType.MESSAGE_HEADER_ERROR,
80 BgpErrorType.BAD_MESSAGE_LENGTH, length);
Shashikanth VH95bd85e2015-10-27 14:56:33 +053081 }
82 bgpHeader.setLength(length);
83 byte type = cb.readByte();
84 bgpHeader.setType(type);
85 log.debug("Reading update message of type " + type);
86
87 int len = length - MINIMUM_COMMON_HEADER_LENGTH;
88 switch (type) {
89 case OPEN_MSG_TYPE:
90 log.debug("OPEN MESSAGE is received");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053091 return BgpOpenMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader);
Shashikanth VH95bd85e2015-10-27 14:56:33 +053092 case KEEPALIVE_MSG_TYPE:
93 log.debug("KEEPALIVE MESSAGE is received");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053094 return BgpKeepaliveMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader);
Shashikanth VH95bd85e2015-10-27 14:56:33 +053095 case UPDATE_MSG_TYPE:
96 log.debug("UPDATE MESSAGE is received");
Shashikanth VHe24dcd12015-11-23 22:11:36 +053097 return BgpUpdateMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader);
Shashikanth VH95bd85e2015-10-27 14:56:33 +053098 case NOTIFICATION_MSG_TYPE:
99 log.debug("NOTIFICATION MESSAGE is received");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530100 return BgpNotificationMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader);
Shashikanth VH95bd85e2015-10-27 14:56:33 +0530101 default:
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530102 Validation.validateType(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_TYPE, type);
Shashikanth VH95bd85e2015-10-27 14:56:33 +0530103 return null;
104 }
105 } catch (IndexOutOfBoundsException e) {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530106 throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR,
107 BgpErrorType.BAD_MESSAGE_LENGTH, null);
Shashikanth VH95bd85e2015-10-27 14:56:33 +0530108 }
109 }
110 }
111}