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;
10 import java.util.Arrays;
12 import org.opendaylight.protocol.util.Values;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import com.google.common.base.Preconditions;
17 import com.google.common.primitives.UnsignedBytes;
20 * There are several errors documented in RFC4271 or in draft, that have specific meaning for the BGP. This exception is
21 * used, when any of those errors occurs.
23 public final class BGPDocumentedException extends Exception {
25 private static final long serialVersionUID = -6212702584439430736L;
27 private static final Logger LOG = LoggerFactory.getLogger(BGPDocumentedException.class);
29 private final BGPError error;
31 private final byte[] data;
34 * Used when an error occurred that is described in an RFC or a draft.
36 * @param message message bound with this exception
37 * @param error specific documented error
39 public BGPDocumentedException(final String message, final BGPError error) {
40 this(message, error, null, null);
44 * Used when an error occurred that is described in an RFC or a draft.
46 * @param message message bound with this exception
47 * @param error specific documented error
48 * @param cause cause for the error
50 public BGPDocumentedException(final String message, final BGPError error, final Exception cause) {
51 this(message, error, null, cause);
55 * Used when an error occurred that is described in an RFC or a draft.
57 * @param message message bound with this exception
58 * @param error specific documented error
59 * @param data data associated with the error
61 public BGPDocumentedException(final String message, final BGPError error, final byte[] data) {
62 this(message, error, data, null);
66 * Used when an error occurred that is described in an RFC or a draft.
68 * @param message message bound with this exception
69 * @param error specific documented error
70 * @param data data associated with the error
71 * @param cause cause for the error
73 public BGPDocumentedException(final String message, final BGPError error, final byte[] data, final Exception cause) {
74 super(message, cause);
76 this.data = data == null ? null : Arrays.copyOf(data, data.length);
77 LOG.error("Error = {}", error, this);
81 * Returns specific documented error.
83 * @return documented error
85 public BGPError getError() {
90 * Returns data associated with this error.
92 * @return byte array data
94 public byte[] getData() {
98 public static BGPDocumentedException badMessageLength(final String message, final int length) {
99 Preconditions.checkArgument(length >= 0 && length <= Values.UNSIGNED_SHORT_MAX_VALUE);
101 return new BGPDocumentedException(message, BGPError.BAD_MSG_LENGTH, new byte[] {
102 UnsignedBytes.checkedCast(length / (Values.UNSIGNED_BYTE_MAX_VALUE + 1)),
103 UnsignedBytes.checkedCast(length % (Values.UNSIGNED_BYTE_MAX_VALUE + 1)) });