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.spi;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import com.google.common.primitives.UnsignedBytes;
14 import io.netty.buffer.ByteBuf;
15 import java.util.Arrays;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPError;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yangtools.yang.binding.Notification;
22 public abstract class AbstractMessageRegistry implements MessageRegistry {
24 private static final byte[] MARKER;
26 protected abstract Notification parseBody(int type, ByteBuf body, int messageLength,
27 PeerSpecificParserConstraint constraint) throws BGPDocumentedException;
29 protected abstract void serializeMessageImpl(Notification message, ByteBuf buffer);
32 MARKER = new byte[MessageUtil.MARKER_LENGTH];
33 Arrays.fill(MARKER, UnsignedBytes.MAX_VALUE);
37 public Notification parseMessage(final ByteBuf buffer, final PeerSpecificParserConstraint constraint)
38 throws BGPDocumentedException, BGPParsingException {
39 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes cannot be null or empty.");
40 Preconditions.checkArgument(buffer.readableBytes() >= MessageUtil.COMMON_HEADER_LENGTH,
41 "Too few bytes in passed array. Passed: %s. Expected: >= %s.", buffer.readableBytes(),
42 MessageUtil.COMMON_HEADER_LENGTH);
43 final byte[] marker = ByteArray.readBytes(buffer, MessageUtil.MARKER_LENGTH);
45 if (!Arrays.equals(marker, MARKER)) {
46 throw new BGPDocumentedException("Marker not set to ones.", BGPError.CONNECTION_NOT_SYNC);
48 final int messageLength = buffer.readUnsignedShort();
49 // to be sent with Error message
50 final byte typeBytes = buffer.readByte();
51 final int messageType = UnsignedBytes.toInt(typeBytes);
53 if (messageLength < MessageUtil.COMMON_HEADER_LENGTH) {
54 throw BGPDocumentedException.badMessageLength("Message length field not within valid range.",
58 if (messageLength - MessageUtil.COMMON_HEADER_LENGTH != buffer.readableBytes()) {
59 throw new BGPParsingException("Size doesn't match size specified in header. Passed: "
60 + buffer.readableBytes() + "; Expected: " + (messageLength - MessageUtil.COMMON_HEADER_LENGTH)
64 final ByteBuf msgBody = buffer.readSlice(messageLength - MessageUtil.COMMON_HEADER_LENGTH);
66 final Notification msg = parseBody(messageType, msgBody, messageLength, constraint);
68 throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE,
69 new byte[] { typeBytes });
75 public final void serializeMessage(final Notification message, final ByteBuf buffer) {
76 requireNonNull(message, "BGPMessage is mandatory.");
77 serializeMessageImpl(message, buffer);