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 com.google.common.base.Preconditions;
11 import com.google.common.collect.Maps;
12 import java.io.Serializable;
17 * Possible errors from implemented RFCs and drafts. Each error consists of error code and error subcode
18 * (code/subcode in comments).
20 * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.5">BGP Notification Message</a>
22 public enum BGPError {
24 * Connection Not Synchronized. 1/1
26 CONNECTION_NOT_SYNC((short) 1, (short) 1),
28 * Bad Message Length. 1/2
30 BAD_MSG_LENGTH((short) 1, (short) 2),
32 * Bad Message Type. 1/3
34 BAD_MSG_TYPE((short) 1, (short) 3),
36 * Unspecific Open Message error.
38 UNSPECIFIC_OPEN_ERROR((short) 2, (short) 0),
40 * Unsupported Version Number. 2/1
42 VERSION_NOT_SUPPORTED((short) 2, (short) 1),
46 BAD_PEER_AS((short) 2, (short) 2),
48 * Bad BGP Identifier. 2/3
50 BAD_BGP_ID((short) 2, (short) 3),
52 * Unsupported Optional Parameter. 2/4
54 OPT_PARAM_NOT_SUPPORTED((short) 2, (short) 4),
56 * Unacceptable Hold Time. 2/6
58 HOLD_TIME_NOT_ACC((short) 2, (short) 6),
60 * Malformed Attribute List. 3/1
62 MALFORMED_ATTR_LIST((short) 3, (short) 1),
64 * Unrecognized Well-known Attribute. 3/2
66 WELL_KNOWN_ATTR_NOT_RECOGNIZED((short) 3, (short) 2),
68 * Missing Well-known Attribute. 3/3
70 WELL_KNOWN_ATTR_MISSING((short) 3, (short) 3),
72 * Attribute Flags Error. 3/4
74 ATTR_FLAGS_MISSING((short) 3, (short) 4),
76 * Attribute Length Error. 3/5
78 ATTR_LENGTH_ERROR((short) 3, (short) 5),
80 * Invalid ORIGIN Attribute. 3/6
82 ORIGIN_ATTR_NOT_VALID((short) 3, (short) 6),
84 * Invalid NEXT_HOP Attribute. 3/8
86 NEXT_HOP_NOT_VALID((short) 3, (short) 8),
88 * Optional Attribute Error. 3/9
90 OPT_ATTR_ERROR((short) 3, (short) 9),
92 * Invalid Network Field. 3/10
94 NETWORK_NOT_VALID((short) 3, (short) 10),
96 * Malformed AS_PATH. 3/11
98 AS_PATH_MALFORMED((short) 3, (short) 11),
100 * Hold Timer Expired. 4/0
102 HOLD_TIMER_EXPIRED((short) 4, (short) 0),
104 * Finite State Machine Error. 5/0
106 FSM_ERROR((short) 5, (short) 0),
110 CEASE((short) 6, (short) 0),
112 * Maximum Number of Prefixes Reached. 6/1
114 MAX_NUMBER_OF_PREFIXES_REACHED((short) 6, (short) 1),
116 * Administrative Shutdown. 6/2
118 ADMINISTRATIVE_SHUTDOWN((short) 6, (short) 2),
120 * Peer De-configured. 6/3
122 PEER_DECONFIGURED((short) 6, (short) 3),
124 * Administrative Reset. 6/4
126 ADMINISTRATIVE_RESTART((short) 6, (short) 4),
128 * Connection Rejected. 6/5
130 CONNECTION_REJECTED((short) 6, (short) 5),
132 * Other Configuration Change. 6/6
134 OTHER_CONFIGURATION_CHANGE((short) 6, (short) 6),
136 * Connection Collision Resolution. 6/7
138 CONNECTION_COLLISION_RESOLUTION((short) 6, (short) 7),
140 * Out of Resources. 6/8
142 OUT_OF_RESOURCES((short) 6, (short) 8),
144 * Unsupported Capability. 2/7
146 UNSUPPORTED_CAPABILITY((short) 2, (short) 7);
148 public static final String MANDATORY_ATTR_MISSING_MSG = "Well known mandatory attribute missing: ";
150 private static final Map<BGPErrorIdentifier, BGPError> VALUE_MAP;
153 VALUE_MAP = Maps.newHashMap();
154 for (final BGPError enumItem : BGPError.values()) {
155 VALUE_MAP.put(enumItem.getErrorIdentifier(), enumItem);
159 private final BGPErrorIdentifier errorId;
161 BGPError(final short code, final short subcode) {
162 this.errorId = new BGPErrorIdentifier(code, subcode);
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.",
172 public short getCode() {
173 return this.errorId.code;
176 public short getSubcode() {
177 return this.errorId.subcode;
180 private BGPErrorIdentifier getErrorIdentifier() {
185 * Caret for combination of Error-type and Error-value.
187 private static class BGPErrorIdentifier implements Serializable {
188 private static final long serialVersionUID = 5722575354944165734L;
192 BGPErrorIdentifier(final short code, final short subcode) {
194 this.subcode = subcode;
198 public int hashCode() {
199 final int prime = 31;
201 result = prime * result + this.code;
202 result = prime * result + this.subcode;
207 public boolean equals(final java.lang.Object obj) {
211 if (obj == null || this.getClass() != obj.getClass()) {
214 final BGPErrorIdentifier other = (BGPErrorIdentifier) obj;
215 return this.code == other.code && this.subcode == other.subcode;
219 public String toString() {
220 return "type " + this.code + " value " + this.subcode;