Added unit tests for bgp-parser-api.
[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 java.util.Arrays;
11
12 import org.opendaylight.protocol.util.Values;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import com.google.common.base.Preconditions;
17 import com.google.common.primitives.UnsignedBytes;
18
19 /**
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.
22  */
23 public final class BGPDocumentedException extends Exception {
24
25         private static final long serialVersionUID = -6212702584439430736L;
26
27         private static final Logger LOG = LoggerFactory.getLogger(BGPDocumentedException.class);
28
29         private final BGPError error;
30
31         private final byte[] data;
32
33         /**
34          * Used when an error occurred that is described in an RFC or a draft.
35          * 
36          * @param message message bound with this exception
37          * @param error specific documented error
38          */
39         public BGPDocumentedException(final String message, final BGPError error) {
40                 this(message, error, null, null);
41         }
42
43         /**
44          * Used when an error occurred that is described in an RFC or a draft.
45          * 
46          * @param message message bound with this exception
47          * @param error specific documented error
48          * @param cause cause for the error
49          */
50         public BGPDocumentedException(final String message, final BGPError error, final Exception cause) {
51                 this(message, error, null, cause);
52         }
53
54         /**
55          * Used when an error occurred that is described in an RFC or a draft.
56          * 
57          * @param message message bound with this exception
58          * @param error specific documented error
59          * @param data data associated with the error
60          */
61         public BGPDocumentedException(final String message, final BGPError error, final byte[] data) {
62                 this(message, error, data, null);
63         }
64
65         /**
66          * Used when an error occurred that is described in an RFC or a draft.
67          * 
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
72          */
73         public BGPDocumentedException(final String message, final BGPError error, final byte[] data, final Exception cause) {
74                 super(message, cause);
75                 this.error = error;
76                 this.data = data == null ? null : Arrays.copyOf(data, data.length);
77                 LOG.error("Error = {}", error, this);
78         }
79
80         /**
81          * Returns specific documented error.
82          * 
83          * @return documented error
84          */
85         public BGPError getError() {
86                 return this.error;
87         }
88
89         /**
90          * Returns data associated with this error.
91          * 
92          * @return byte array data
93          */
94         public byte[] getData() {
95                 return this.data;
96         }
97
98         public static BGPDocumentedException badMessageLength(final String message, final int length) {
99                 Preconditions.checkArgument(length >= 0 && length <= Values.UNSIGNED_SHORT_MAX_VALUE);
100
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)) });
104
105         }
106 }