2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.parser;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
22 * Possible errors from implemented RFCs and drafts. Each error consists of error code and error subcode
23 * (code/subcode in comments).
25 * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.5">BGP Notification Message</a>
27 public enum BGPError {
29 * Unspecific header error. 1/0, <a href="https://www.rfc-editor.org/errata_search.php?eid=4493">Errata 4493</a>.
31 UNSPECIFIC_HEADER_ERROR(1, 0),
33 * Connection Not Synchronized. 1/1
35 CONNECTION_NOT_SYNC(1, 1),
37 * Bad Message Length. 1/2
41 * Bad Message Type. 1/3
45 * Unspecific Open Message error, <a href="https://www.rfc-editor.org/errata_search.php?eid=4493">Errata 4493</a>.
47 UNSPECIFIC_OPEN_ERROR(2, 0),
49 * Unsupported Version Number. 2/1
51 VERSION_NOT_SUPPORTED(2, 1),
57 * Bad BGP Identifier. 2/3
61 * Unsupported Optional Parameter. 2/4
63 OPT_PARAM_NOT_SUPPORTED(2, 4),
65 * Unacceptable Hold Time. 2/6
67 HOLD_TIME_NOT_ACC(2, 6),
69 * Unspecific UPDATE error. 3/0, <a href="https://www.rfc-editor.org/errata_search.php?eid=4493">Errata 4493</a>.
71 UNSPECIFIC_UPDATE_ERROR(3, 0),
73 * Malformed Attribute List. 3/1
75 MALFORMED_ATTR_LIST(3, 1),
77 * Unrecognized Well-known Attribute. 3/2
79 WELL_KNOWN_ATTR_NOT_RECOGNIZED(3, 2),
81 * Missing Well-known Attribute. 3/3
83 WELL_KNOWN_ATTR_MISSING(3, 3),
85 * Attribute Flags Error. 3/4
87 ATTR_FLAGS_MISSING(3, 4),
89 * Attribute Length Error. 3/5
91 ATTR_LENGTH_ERROR(3, 5),
93 * Invalid ORIGIN Attribute. 3/6
95 ORIGIN_ATTR_NOT_VALID(3, 6),
97 * Invalid NEXT_HOP Attribute. 3/8
99 NEXT_HOP_NOT_VALID(3, 8),
101 * Optional Attribute Error. 3/9
103 OPT_ATTR_ERROR(3, 9),
105 * Invalid Network Field. 3/10
107 NETWORK_NOT_VALID(3, 10),
109 * Malformed AS_PATH. 3/11
111 AS_PATH_MALFORMED(3, 11),
113 * Hold Timer Expired. 4/0
115 HOLD_TIMER_EXPIRED(4, 0),
117 * Finite State Machine Error. 5/0
125 * Maximum Number of Prefixes Reached. 6/1
127 MAX_NUMBER_OF_PREFIXES_REACHED(6, 1),
129 * Administrative Shutdown. 6/2
131 ADMINISTRATIVE_SHUTDOWN(6, 2),
133 * Peer De-configured. 6/3
135 PEER_DECONFIGURED(6, 3),
137 * Administrative Reset. 6/4
139 ADMINISTRATIVE_RESTART(6, 4),
141 * Connection Rejected. 6/5
143 CONNECTION_REJECTED(6, 5),
145 * Other Configuration Change. 6/6
147 OTHER_CONFIGURATION_CHANGE(6, 6),
149 * Connection Collision Resolution. 6/7
151 CONNECTION_COLLISION_RESOLUTION(6, 7),
153 * Out of Resources. 6/8
155 OUT_OF_RESOURCES(6, 8),
157 * Unsupported Capability. 2/7
159 UNSUPPORTED_CAPABILITY(2, 7);
161 public static final String MANDATORY_ATTR_MISSING_MSG = "Well known mandatory attribute missing: ";
163 private static final ImmutableMap<BGPErrorIdentifier, BGPError> VALUE_MAP = Maps.uniqueIndex(
164 Arrays.asList(values()), BGPError::getErrorIdentifier);
166 private final BGPErrorIdentifier errorId;
168 BGPError(final int code, final int subcode) {
169 this.errorId = new BGPErrorIdentifier(Uint8.valueOf(code), Uint8.valueOf(subcode));
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);
178 public @NonNull Uint8 getCode() {
179 return this.errorId.code;
182 public @NonNull Uint8 getSubcode() {
183 return this.errorId.subcode;
186 private BGPErrorIdentifier getErrorIdentifier() {
191 * Caret for combination of Error-type and Error-value.
193 private static final class BGPErrorIdentifier implements Serializable {
194 private static final long serialVersionUID = 5722575354944165734L;
196 final @NonNull Uint8 code;
197 final @NonNull Uint8 subcode;
199 BGPErrorIdentifier(final Uint8 code, final Uint8 subcode) {
200 this.code = requireNonNull(code);
201 this.subcode = requireNonNull(subcode);
205 public int hashCode() {
206 return Objects.hash(code, subcode);
210 public boolean equals(final java.lang.Object obj) {
214 if (!(obj instanceof BGPErrorIdentifier)) {
217 final BGPErrorIdentifier other = (BGPErrorIdentifier) obj;
218 return code.equals(other.code) && subcode.equals(other.subcode);
222 public String toString() {
223 return "type " + code + " value " + subcode;