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 org.opendaylight.protocol.framework.DocumentedException;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
14 import com.google.common.base.Preconditions;
15 import com.google.common.primitives.UnsignedBytes;
18 * There are several errors documented in RFC4271 or in draft, that have specific meaning for the BGP. This exception is
19 * used, when any of those errors occurs.
21 public final class BGPDocumentedException extends DocumentedException {
23 private static final long serialVersionUID = -6212702584439430736L;
25 private static final Logger logger = LoggerFactory.getLogger(BGPDocumentedException.class);
27 private final BGPError error;
29 private final byte[] data;
32 * Used when an error occurred that is described in an RFC or a draft.
34 * @param message message bound with this exception
35 * @param error specific documented error
37 public BGPDocumentedException(final String message, final BGPError error) {
38 this(message, error, null, null);
42 * Used when an error occurred that is described in an RFC or a draft.
44 * @param message message bound with this exception
45 * @param error specific documented error
46 * @param cause cause for the error
48 public BGPDocumentedException(final String message, final BGPError error, final Throwable cause) {
49 this(message, error, null, cause);
53 * Used when an error occurred that is described in an RFC or a draft.
55 * @param message message bound with this exception
56 * @param error specific documented error
57 * @param data data associated with the error
59 public BGPDocumentedException(final String message, final BGPError error, final byte[] data) {
60 this(message, error, data, null);
64 * Used when an error occurred that is described in an RFC or a draft.
66 * @param message message bound with this exception
67 * @param error specific documented error
68 * @param data data associated with the error
69 * @param cause cause for the error
71 public BGPDocumentedException(final String message, final BGPError error, final byte[] data, final Throwable cause) {
75 logger.error("Error = " + error, this);
79 * Returns specific documented error.
81 * @return documented error
83 public BGPError getError() {
88 * Returns data associated with this error.
90 * @return byte array data
92 public byte[] getData() {
96 public static BGPDocumentedException badMessageLength(final String message, final int length) {
97 Preconditions.checkArgument(length >= 0 && length <= 65535);
99 return new BGPDocumentedException(message, BGPError.BAD_MSG_LENGTH, new byte[] {
100 UnsignedBytes.checkedCast(length / 256), UnsignedBytes.checkedCast(length % 256) });