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