8e2ed85b70521891a3989e14ce06239e73545755
[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             }
128             if (s == 2) {
129                 return BGPError.BAD_MSG_LENGTH;
130             }
131             if (s == 3) {
132                 return BGPError.BAD_MSG_TYPE;
133             }
134         } else if (e == 2) {
135             if (s == 0) {
136                 return BGPError.UNSPECIFIC_OPEN_ERROR;
137             }
138             if (s == 1) {
139                 return BGPError.VERSION_NOT_SUPPORTED;
140             }
141             if (s == 2) {
142                 return BGPError.BAD_PEER_AS;
143             }
144             if (s == 3) {
145                 return BGPError.BAD_BGP_ID;
146             }
147             if (s == 4) {
148                 return BGPError.OPT_PARAM_NOT_SUPPORTED;
149             }
150             if (s == 6) {
151                 return BGPError.HOLD_TIME_NOT_ACC;
152             }
153         } else if (e == 3) {
154             if (s == 1) {
155                 return BGPError.MALFORMED_ATTR_LIST;
156             }
157             if (s == 2) {
158                 return BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED;
159             }
160             if (s == 3) {
161                 return BGPError.WELL_KNOWN_ATTR_MISSING;
162             }
163             if (s == 4) {
164                 return BGPError.ATTR_FLAGS_MISSING;
165             }
166             if (s == 5) {
167                 return BGPError.ATTR_LENGTH_ERROR;
168             }
169             if (s == 6) {
170                 return BGPError.ORIGIN_ATTR_NOT_VALID;
171             }
172             if (s == 8) {
173                 return BGPError.NEXT_HOP_NOT_VALID;
174             }
175             if (s == 9) {
176                 return BGPError.OPT_ATTR_ERROR;
177             }
178             if (s == 10) {
179                 return BGPError.NETWORK_NOT_VALID;
180             }
181             if (s == 11) {
182                 return BGPError.AS_PATH_MALFORMED;
183             }
184         } else if (e == 4) {
185             return BGPError.HOLD_TIMER_EXPIRED;
186         } else if (e == 5) {
187             return BGPError.FSM_ERROR;
188         } else if (e == 6) {
189             return BGPError.CEASE;
190         }
191         throw new IllegalArgumentException("BGP Error code " + e + " and subcode " + s + " not recognized.");
192     }
193 }