2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.parser.impl.message;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPError;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
22 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
23 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
24 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
25 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
26 import org.opendaylight.protocol.util.Ipv4Util;
27 import org.opendaylight.protocol.util.Values;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Open;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OpenBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
33 import org.opendaylight.yangtools.yang.binding.Notification;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Parser for BGP Open message.
40 public final class BGPOpenMessageParser implements MessageParser, MessageSerializer {
42 private static final Logger LOG = LoggerFactory.getLogger(BGPOpenMessageParser.class);
44 public static final int TYPE = 1;
46 private static final int VERSION_SIZE = 1;
47 private static final int AS_SIZE = 2;
48 private static final int HOLD_TIME_SIZE = 2;
49 private static final int BGP_ID_SIZE = 4;
50 private static final int OPT_PARAM_LENGTH_SIZE = 1;
52 private static final int MIN_MSG_LENGTH = VERSION_SIZE + AS_SIZE
53 + HOLD_TIME_SIZE + BGP_ID_SIZE + OPT_PARAM_LENGTH_SIZE;
55 private static final int BGP_VERSION = 4;
57 public static final int AS_TRANS = 23456;
59 private final ParameterRegistry reg;
61 public BGPOpenMessageParser(final ParameterRegistry reg) {
62 this.reg = requireNonNull(reg);
66 * Serializes given BGP Open message to byte array, without the header.
68 * @param msg BGP Open message to be serialized.
69 * @param bytes ByteBuf where the message will be serialized
72 public void serializeMessage(final Notification msg, final ByteBuf bytes) {
73 checkArgument(msg instanceof Open, "Message needs to be of type Open, not %s", msg);
74 final Open open = (Open) msg;
75 final ByteBuf msgBody = Unpooled.buffer();
77 msgBody.writeByte(BGP_VERSION);
79 // When our AS number does not fit into two bytes, we report it as AS_TRANS
80 int openAS = open.getMyAsNumber();
81 if (openAS > Values.UNSIGNED_SHORT_MAX_VALUE) {
84 msgBody.writeShort(openAS);
85 msgBody.writeShort(open.getHoldTimer());
86 msgBody.writeBytes(Ipv4Util.bytesForAddress(open.getBgpIdentifier()));
88 final ByteBuf paramsBuffer = Unpooled.buffer();
89 final List<BgpParameters> params = open.getBgpParameters();
91 for (final BgpParameters param : params) {
92 this.reg.serializeParameter(param, paramsBuffer);
95 final int paramsLength = paramsBuffer.writerIndex();
96 if (paramsLength > 255) {
97 LOG.error("OPEN message message optional parameter length {} exceeds maximum length supported by BGP. "
98 + "Adjust advertized capabilities toward the peer to fit into limit of 255 bytes by trimming {}",
99 paramsLength, params);
100 throw new IllegalArgumentException(String.format(
101 "Cannot encode OPEN message because optional parameter length %s exceeds length field size.",
104 msgBody.writeByte(paramsLength);
105 msgBody.writeBytes(paramsBuffer);
107 MessageUtil.formatMessage(TYPE, msgBody, bytes);
111 * Parses given byte array to BGP Open message.
113 * @param body byte array representing BGP Open message, without header
114 * @param messageLength the length of the message
115 * @return {@link Open} BGP Open Message
116 * @throws BGPDocumentedException if the parsing was unsuccessful
119 public Open parseMessageBody(final ByteBuf body, final int messageLength,
120 final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
121 checkArgument(body != null, "Buffer cannot be null.");
123 if (body.readableBytes() < MIN_MSG_LENGTH) {
124 throw BGPDocumentedException.badMessageLength("Open message too small.", messageLength);
126 final int version = body.readUnsignedByte();
127 if (version != BGP_VERSION) {
128 throw new BGPDocumentedException("BGP Protocol version " + version + " not supported.",
129 BGPError.VERSION_NOT_SUPPORTED);
131 final AsNumber as = new AsNumber((long) body.readUnsignedShort());
132 final int holdTime = body.readUnsignedShort();
133 if (holdTime == 1 || holdTime == 2) {
134 throw new BGPDocumentedException("Hold time value not acceptable.", BGPError.HOLD_TIME_NOT_ACC);
138 bgpId = Ipv4Util.addressForByteBuf(body);
139 } catch (final IllegalArgumentException e) {
140 throw new BGPDocumentedException("BGP Identifier is not a valid IPv4 Address", BGPError.BAD_BGP_ID, e);
142 final int optLength = body.readUnsignedByte();
144 final List<BgpParameters> optParams = new ArrayList<>();
146 fillParams(body.slice(), optParams);
148 LOG.debug("BGP Open message was parsed: AS = {}, holdTimer = {}, bgpId = {}, optParams = {}", as,
149 holdTime, bgpId, optParams);
150 return new OpenBuilder().setMyAsNumber(as.getValue().intValue()).setHoldTimer(holdTime)
151 .setBgpIdentifier(bgpId).setBgpParameters(optParams).build();
154 private void fillParams(final ByteBuf buffer, final List<BgpParameters> params) throws BGPDocumentedException {
155 checkArgument(buffer != null && buffer.isReadable(), "Buffer cannot be null or empty.");
156 if (LOG.isTraceEnabled()) {
157 LOG.trace("Started parsing of BGP parameter: {}", ByteBufUtil.hexDump(buffer));
159 while (buffer.isReadable()) {
160 if (buffer.readableBytes() <= 2) {
161 throw new BGPDocumentedException("Malformed parameter encountered (" + buffer.readableBytes()
162 + " bytes left)", BGPError.OPT_PARAM_NOT_SUPPORTED);
164 final int paramType = buffer.readUnsignedByte();
165 final int paramLength = buffer.readUnsignedByte();
166 final ByteBuf paramBody = buffer.readSlice(paramLength);
168 final BgpParameters param;
170 param = this.reg.parseParameter(paramType, paramBody);
171 } catch (final BGPParsingException e) {
172 throw new BGPDocumentedException("Optional parameter not parsed", BGPError.UNSPECIFIC_OPEN_ERROR, e);
177 LOG.debug("Ignoring BGP Parameter type: {}", paramType);
180 LOG.trace("Parsed BGP parameters: {}", params);