Bug-2116: Added CEASE error code subcodes as defined in RFC4486.
[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 import com.google.common.collect.Maps;
11 import java.util.Map;
12
13
14 /**
15  * Possible errors from implemented RFCs and drafts. Each error consists of error code and error subcode (code/subcode
16  * in comments).
17  *
18  * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.5">BGP Notification Message</a>
19  */
20 public enum BGPError {
21     /**
22      * Connection Not Synchronized. 1/1
23      */
24     CONNECTION_NOT_SYNC((short) 1, (short) 1),
25     /**
26      * Bad Message Length. 1/2
27      */
28     BAD_MSG_LENGTH((short) 1, (short) 2),
29     /**
30      * Bad Message Type. 1/3
31      */
32     BAD_MSG_TYPE((short) 1, (short) 3),
33     /**
34      * Unspecific Open Message error.
35      */
36     UNSPECIFIC_OPEN_ERROR((short) 2, (short) 0),
37     /**
38      * Unsupported Version Number. 2/1
39      */
40     VERSION_NOT_SUPPORTED((short) 2, (short) 1),
41     /**
42      * Bad Peer AS. 2/2
43      */
44     BAD_PEER_AS((short) 2, (short) 2),
45     /**
46      * Bad BGP Identifier. 2/3
47      */
48     BAD_BGP_ID((short) 2, (short) 3),
49     /**
50      * Unsupported Optional Parameter. 2/4
51      */
52     OPT_PARAM_NOT_SUPPORTED((short) 2, (short) 4),
53     /**
54      * Unacceptable Hold Time. 2/6
55      */
56     HOLD_TIME_NOT_ACC((short) 2, (short) 6),
57     /**
58      * Malformed Attribute List. 3/1
59      */
60     MALFORMED_ATTR_LIST((short) 3, (short) 1),
61     /**
62      * Unrecognized Well-known Attribute. 3/2
63      */
64     WELL_KNOWN_ATTR_NOT_RECOGNIZED((short) 3, (short) 2),
65     /**
66      * Missing Well-known Attribute. 3/3
67      */
68     WELL_KNOWN_ATTR_MISSING((short) 3, (short) 3),
69     /**
70      * Attribute Flags Error. 3/4
71      */
72     ATTR_FLAGS_MISSING((short) 3, (short) 4),
73     /**
74      * Attribute Length Error. 3/5
75      */
76     ATTR_LENGTH_ERROR((short) 3, (short) 5),
77     /**
78      * Invalid ORIGIN Attribute. 3/6
79      */
80     ORIGIN_ATTR_NOT_VALID((short) 3, (short) 6),
81     /**
82      * Invalid NEXT_HOP Attribute. 3/8
83      */
84     NEXT_HOP_NOT_VALID((short) 3, (short) 8),
85     /**
86      * Optional Attribute Error. 3/9
87      */
88     OPT_ATTR_ERROR((short) 3, (short) 9),
89     /**
90      * Invalid Network Field. 3/10
91      */
92     NETWORK_NOT_VALID((short) 3, (short) 10),
93     /**
94      * Malformed AS_PATH. 3/11
95      */
96     AS_PATH_MALFORMED((short) 3, (short) 11),
97     /**
98      * Hold Timer Expired. 4/0
99      */
100     HOLD_TIMER_EXPIRED((short) 4, (short) 0),
101     /**
102      * Finite State Machine Error. 5/0
103      */
104     FSM_ERROR((short) 5, (short) 0),
105     /**
106      * Cease. 6/0
107      */
108     CEASE((short) 6, (short) 0),
109     /**
110      * Maximum Number of Prefixes Reached. 6/1
111      */
112     MAX_NUMBER_OF_PREFIXES_REACHED((short) 6, (short) 1),
113     /**
114      * Administrative Shutdown. 6/2
115      */
116     ADMINISTRATIVE_SHUTDOWN((short) 6, (short) 2),
117     /**
118      * Peer De-configured. 6/3
119      */
120     PEER_DECONFIGURED((short) 6, (short) 3),
121     /**
122      * Administrative Reset. 6/4
123      */
124     ADMINISTRATIVE_RESTART((short) 6, (short) 4),
125     /**
126      * Connection Rejected. 6/5
127      */
128     CONNECTION_REJECTED((short) 6, (short) 5),
129     /**
130      * Other Configuration Change. 6/6
131      */
132     OTHER_CONFIGURATION_CHANGE((short) 6, (short) 6),
133     /**
134      * Connection Collision Resolution. 6/7
135      */
136     CONNECTION_COLLISION_RESOLUTION((short) 6, (short) 7),
137     /**
138      * Out of Resources. 6/8
139      */
140     OUT_OF_RESOURCES((short) 6, (short) 8);
141
142     private static final Map<BGPErrorIdentifier, BGPError> VALUE_MAP;
143
144     static {
145         VALUE_MAP = Maps.newHashMap();
146         for (final BGPError enumItem : BGPError.values()) {
147             VALUE_MAP.put(enumItem.getErrorIdentifier(), enumItem);
148         }
149     }
150
151     private final BGPErrorIdentifier errorId;
152
153     BGPError(final short code, final short subcode) {
154         this.errorId = new BGPErrorIdentifier(code, subcode);
155     }
156
157     public short getCode() {
158         return this.errorId.getCode();
159     }
160
161     public short getSubcode() {
162         return this.errorId.getSubCode();
163     }
164
165     private BGPErrorIdentifier getErrorIdentifier() {
166         return this.errorId;
167     }
168
169     /**
170      * Caret for combination of Error-type and Error-value
171      */
172     private static class BGPErrorIdentifier {
173         private final short code;
174         private final short subcode;
175
176         BGPErrorIdentifier(final short code, final short subcode) {
177             this.code = code;
178             this.subcode = subcode;
179         }
180
181         public short getCode() {
182             return this.code;
183         }
184
185         public short getSubCode() {
186             return this.subcode;
187         }
188
189         @Override
190         public int hashCode() {
191             final int prime = 31;
192             int result = 1;
193             result = prime * result + this.code;
194             result = prime * result + this.subcode;
195             return result;
196         }
197
198         @Override
199         public boolean equals(final java.lang.Object obj) {
200             if (this == obj) {
201                 return true;
202             }
203             if (obj == null) {
204                 return false;
205             }
206             if (this.getClass() != obj.getClass()) {
207                 return false;
208             }
209             final BGPErrorIdentifier other = (BGPErrorIdentifier) obj;
210             if (this.code != other.code) {
211                 return false;
212             }
213             if (this.subcode != other.subcode) {
214                 return false;
215             }
216             return true;
217         }
218
219         @Override
220         public String toString() {
221             return "type " + this.code + " value " + this.subcode;
222         }
223     }
224
225     public static BGPError forValue(final int code, final int subcode) {
226         final BGPError e = VALUE_MAP.get(new BGPErrorIdentifier((short) code, (short) subcode));
227         if (e != null) {
228             return e;
229         }
230         throw new IllegalArgumentException("BGP Error code " + code + " and subcode " + subcode + " not recognized.");
231     }
232 }