Fix junit dependencies in poms. Reuse existing from parent,
[bgpcep.git] / bgp / parser-api / src / main / java / org / opendaylight / protocol / bgp / parser / BGPError.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 /**
11  * Possible errors from implemented RFCs and drafts. Each error consists of error code and error subcode (code/subcode
12  * in comments).
13  * 
14  * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.5">BGP Notification Message</a>
15  */
16 public enum BGPError {
17         /**
18          * Connection Not Synchronized. 1/1
19          */
20         CONNECTION_NOT_SYNC((short) 1, (short) 1),
21         /**
22          * Bad Message Length. 1/2
23          */
24         BAD_MSG_LENGTH((short) 1, (short) 2),
25         /**
26          * Bad Message Type. 1/3
27          */
28         BAD_MSG_TYPE((short) 1, (short) 3),
29         /**
30          * Unspecific Open Message error.
31          */
32         UNSPECIFIC_OPEN_ERROR((short) 2, (short) 0),
33         /**
34          * Unsupported Version Number. 2/1
35          */
36         VERSION_NOT_SUPPORTED((short) 2, (short) 1),
37         /**
38          * Bad Peer AS. 2/2
39          */
40         BAD_PEER_AS((short) 2, (short) 2),
41         /**
42          * Bad BGP Identifier. 2/3
43          */
44         BAD_BGP_ID((short) 2, (short) 3),
45         /**
46          * Unsupported Optional Parameter. 2/4
47          */
48         OPT_PARAM_NOT_SUPPORTED((short) 2, (short) 4),
49         /**
50          * Unacceptable Hold Time. 2/6
51          */
52         HOLD_TIME_NOT_ACC((short) 2, (short) 6),
53         /**
54          * Malformed Attribute List. 3/1
55          */
56         MALFORMED_ATTR_LIST((short) 3, (short) 1),
57         /**
58          * Unrecognized Well-known Attribute. 3/2
59          */
60         WELL_KNOWN_ATTR_NOT_RECOGNIZED((short) 3, (short) 2),
61         /**
62          * Missing Well-known Attribute. 3/3
63          */
64         WELL_KNOWN_ATTR_MISSING((short) 3, (short) 3),
65         /**
66          * Attribute Flags Error. 3/4
67          */
68         ATTR_FLAGS_MISSING((short) 3, (short) 4),
69         /**
70          * Attribute Length Error. 3/5
71          */
72         ATTR_LENGTH_ERROR((short) 3, (short) 5),
73         /**
74          * Invalid ORIGIN Attribute. 3/6
75          */
76         ORIGIN_ATTR_NOT_VALID((short) 3, (short) 6),
77         /**
78          * Invalid NEXT_HOP Attribute. 3/8
79          */
80         NEXT_HOP_NOT_VALID((short) 3, (short) 8),
81         /**
82          * Optional Attribute Error. 3/9
83          */
84         OPT_ATTR_ERROR((short) 3, (short) 9),
85         /**
86          * Invalid Network Field. 3/10
87          */
88         NETWORK_NOT_VALID((short) 3, (short) 10),
89         /**
90          * Malformed AS_PATH. 3/11
91          */
92         AS_PATH_MALFORMED((short) 3, (short) 11),
93         /**
94          * Hold Timer Expired. 4/0
95          */
96         HOLD_TIMER_EXPIRED((short) 4, (short) 0),
97         /**
98          * Finite State Machine Error. 5/0
99          */
100         FSM_ERROR((short) 5, (short) 0),
101         /**
102          * Cease. 6/0
103          */
104         CEASE((short) 6, (short) 0);
105
106         private final short code;
107
108         private final short subcode;
109
110         BGPError(final short code, final short subcode) {
111                 this.code = code;
112                 this.subcode = subcode;
113         }
114
115         public short getCode() {
116                 return this.code;
117         }
118
119         public short getSubcode() {
120                 return this.subcode;
121         }
122
123         public static BGPError forValue(final int e, final int s) {
124                 if (e == 1) {
125                         if (s == 1)
126                                 return BGPError.CONNECTION_NOT_SYNC;
127                         if (s == 2)
128                                 return BGPError.BAD_MSG_LENGTH;
129                         if (s == 3)
130                                 return BGPError.BAD_MSG_TYPE;
131                 } else if (e == 2) {
132                         if (s == 0)
133                                 return BGPError.UNSPECIFIC_OPEN_ERROR;
134                         if (s == 1)
135                                 return BGPError.VERSION_NOT_SUPPORTED;
136                         if (s == 2)
137                                 return BGPError.BAD_PEER_AS;
138                         if (s == 3)
139                                 return BGPError.BAD_BGP_ID;
140                         if (s == 4)
141                                 return BGPError.OPT_PARAM_NOT_SUPPORTED;
142                         if (s == 6)
143                                 return BGPError.HOLD_TIME_NOT_ACC;
144                 } else if (e == 3) {
145                         if (s == 1)
146                                 return BGPError.MALFORMED_ATTR_LIST;
147                         if (s == 2)
148                                 return BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED;
149                         if (s == 3)
150                                 return BGPError.WELL_KNOWN_ATTR_MISSING;
151                         if (s == 4)
152                                 return BGPError.ATTR_FLAGS_MISSING;
153                         if (s == 5)
154                                 return BGPError.ATTR_LENGTH_ERROR;
155                         if (s == 6)
156                                 return BGPError.ORIGIN_ATTR_NOT_VALID;
157                         if (s == 8)
158                                 return BGPError.NEXT_HOP_NOT_VALID;
159                         if (s == 9)
160                                 return BGPError.OPT_ATTR_ERROR;
161                         if (s == 10)
162                                 return BGPError.NETWORK_NOT_VALID;
163                         if (s == 11)
164                                 return BGPError.AS_PATH_MALFORMED;
165                 } else if (e == 4)
166                         return BGPError.HOLD_TIMER_EXPIRED;
167                 else if (e == 5)
168                         return BGPError.FSM_ERROR;
169                 else if (e == 6)
170                         return BGPError.CEASE;
171                 throw new IllegalArgumentException("BGP Error code " + e + " and subcode " + s + " not recognized.");
172         }
173 }