Reduce BGPDocumentedException noise
[bgpcep.git] / bgp / parser-api / src / main / java / org / opendaylight / protocol / bgp / parser / BGPDocumentedException.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;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12 import org.opendaylight.protocol.util.Values;
13
14 /**
15  * There are several errors documented in RFC4271 or in draft, that have specific meaning for the BGP.
16  * This exception is used, when any of those errors occurs.
17  */
18 public final class BGPDocumentedException extends AbstractBGPException {
19     private static final long serialVersionUID = -6212702584439430736L;
20
21     /**
22      * Used when an error occurred that is described in an RFC or a draft.
23      *
24      * @param error specific documented error
25      */
26     public BGPDocumentedException(final BGPError error) {
27         this(null, error, null, null);
28     }
29
30     /**
31      * Used when an error occurred that is described in an RFC or a draft.
32      *
33      * @param message message bound with this exception
34      * @param error specific documented error
35      */
36     public BGPDocumentedException(final String message, final BGPError error) {
37         this(message, error, null, null);
38     }
39
40     /**
41      * Used when an error occurred that is described in an RFC or a draft.
42      *
43      * @param message message bound with this exception
44      * @param error specific documented error
45      * @param cause cause for the error
46      */
47     public BGPDocumentedException(final String message, final BGPError error, final Exception cause) {
48         this(message, error, null, cause);
49     }
50
51     /**
52      * Used when an error occurred that is described in an RFC or a draft.
53      *
54      * @param message message bound with this exception
55      * @param error specific documented error
56      * @param data data associated with the error
57      */
58     public BGPDocumentedException(final String message, final BGPError error, final byte[] data) {
59         this(message, error, data, null);
60     }
61
62     /**
63      * Used when an error occurred that is described in an RFC or a draft.
64      *
65      * @param message message bound with this exception
66      * @param error specific documented error
67      * @param data data associated with the error
68      * @param cause cause for the error
69      */
70     public BGPDocumentedException(final String message, final BGPError error, final byte[] data,
71             final Exception cause) {
72         super(message, error, data, cause);
73     }
74
75     /**
76      * Used when an error occurred that is described in an RFC or a draft.
77      *
78      * @param cause cause for the error
79      */
80     public BGPDocumentedException(final BGPTreatAsWithdrawException cause) {
81         this(cause.getMessage(), cause.getError(), cause.getData(), cause);
82     }
83
84     public static BGPDocumentedException badMessageLength(final String message, final int length) {
85         Preconditions.checkArgument(length >= 0 && length <= Values.UNSIGNED_SHORT_MAX_VALUE);
86
87         return new BGPDocumentedException(message, BGPError.BAD_MSG_LENGTH, new byte[] {
88             UnsignedBytes.checkedCast(length / (Values.UNSIGNED_BYTE_MAX_VALUE + 1)),
89             UnsignedBytes.checkedCast(length % (Values.UNSIGNED_BYTE_MAX_VALUE + 1)) });
90     }
91 }