Shashikanth VH | 95bd85e | 2015-10-27 14:56:33 +0530 | [diff] [blame] | 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 | } |