Fix most bgp-parser-spi checkstyle violations
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / AbstractMessageRegistry.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.parser.spi;
9
10 import static java.util.Objects.requireNonNull;
11
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;
21
22 public abstract class AbstractMessageRegistry implements MessageRegistry {
23
24     private static final byte[] MARKER;
25
26     protected abstract Notification parseBody(int type, ByteBuf body, int messageLength,
27             PeerSpecificParserConstraint constraint) throws BGPDocumentedException;
28
29     protected abstract void serializeMessageImpl(Notification message, ByteBuf buffer);
30
31     static {
32         MARKER = new byte[MessageUtil.MARKER_LENGTH];
33         Arrays.fill(MARKER, UnsignedBytes.MAX_VALUE);
34     }
35
36     @Override
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);
44
45         if (!Arrays.equals(marker, MARKER)) {
46             throw new BGPDocumentedException("Marker not set to ones.", BGPError.CONNECTION_NOT_SYNC);
47         }
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);
52
53         if (messageLength < MessageUtil.COMMON_HEADER_LENGTH) {
54             throw BGPDocumentedException.badMessageLength("Message length field not within valid range.",
55                 messageLength);
56         }
57
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)
61                     + ". ");
62         }
63
64         final ByteBuf msgBody = buffer.readSlice(messageLength - MessageUtil.COMMON_HEADER_LENGTH);
65
66         final Notification msg = parseBody(messageType, msgBody, messageLength, constraint);
67         if (msg == null) {
68             throw new BGPDocumentedException("Unhandled message type " + messageType, BGPError.BAD_MSG_TYPE,
69                 new byte[] { typeBytes });
70         }
71         return msg;
72     }
73
74     @Override
75     public final void serializeMessage(final Notification message, final ByteBuf buffer) {
76         requireNonNull(message, "BGPMessage is mandatory.");
77         serializeMessageImpl(message, buffer);
78     }
79 }