Use exception chaining where possible
[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 org.opendaylight.protocol.framework.DocumentedException;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import com.google.common.base.Preconditions;
15 import com.google.common.primitives.UnsignedBytes;
16
17 /**
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.
20  */
21 public final class BGPDocumentedException extends DocumentedException {
22
23         private static final long serialVersionUID = -6212702584439430736L;
24
25         private static final Logger logger = LoggerFactory.getLogger(BGPDocumentedException.class);
26
27         private final BGPError error;
28
29         private final byte[] data;
30
31         /**
32          * Used when an error occurred that is described in an RFC or a draft.
33          * 
34          * @param message message bound with this exception
35          * @param error specific documented error
36          */
37         public BGPDocumentedException(final String message, final BGPError error) {
38                 this(message, error, null, null);
39         }
40
41         /**
42          * Used when an error occurred that is described in an RFC or a draft.
43          * 
44          * @param message message bound with this exception
45          * @param error specific documented error
46          * @param cause cause for the error
47          */
48         public BGPDocumentedException(final String message, final BGPError error, final Throwable cause) {
49                 this(message, error, null, cause);
50         }
51
52         /**
53          * Used when an error occurred that is described in an RFC or a draft.
54          * 
55          * @param message message bound with this exception
56          * @param error specific documented error
57          * @param data data associated with the error
58          */
59         public BGPDocumentedException(final String message, final BGPError error, final byte[] data) {
60                 this(message, error, data, null);
61         }
62
63         /**
64          * Used when an error occurred that is described in an RFC or a draft.
65          * 
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
70          */
71         public BGPDocumentedException(final String message, final BGPError error, final byte[] data, final Throwable cause) {
72                 super(message);
73                 this.error = error;
74                 this.data = data;
75                 logger.error("Error = " + error, this);
76         }
77
78         /**
79          * Returns specific documented error.
80          * 
81          * @return documented error
82          */
83         public BGPError getError() {
84                 return this.error;
85         }
86
87         /**
88          * Returns data associated with this error.
89          * 
90          * @return byte array data
91          */
92         public byte[] getData() {
93                 return this.data;
94         }
95
96         public static BGPDocumentedException badMessageLength(final String message, final int length) {
97                 Preconditions.checkArgument(length >= 0 && length <= 65535);
98
99                 return new BGPDocumentedException(message, BGPError.BAD_MSG_LENGTH, new byte[] {
100                                 UnsignedBytes.checkedCast(length / 256), UnsignedBytes.checkedCast(length % 256) });
101
102         }
103 }