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