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.collect.Maps;
15 * Possible errors from implemented RFCs and drafts. Each error consists of error code and error subcode (code/subcode
18 * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.5">BGP Notification Message</a>
20 public enum BGPError {
22 * Connection Not Synchronized. 1/1
24 CONNECTION_NOT_SYNC((short) 1, (short) 1),
26 * Bad Message Length. 1/2
28 BAD_MSG_LENGTH((short) 1, (short) 2),
30 * Bad Message Type. 1/3
32 BAD_MSG_TYPE((short) 1, (short) 3),
34 * Unspecific Open Message error.
36 UNSPECIFIC_OPEN_ERROR((short) 2, (short) 0),
38 * Unsupported Version Number. 2/1
40 VERSION_NOT_SUPPORTED((short) 2, (short) 1),
44 BAD_PEER_AS((short) 2, (short) 2),
46 * Bad BGP Identifier. 2/3
48 BAD_BGP_ID((short) 2, (short) 3),
50 * Unsupported Optional Parameter. 2/4
52 OPT_PARAM_NOT_SUPPORTED((short) 2, (short) 4),
54 * Unacceptable Hold Time. 2/6
56 HOLD_TIME_NOT_ACC((short) 2, (short) 6),
58 * Malformed Attribute List. 3/1
60 MALFORMED_ATTR_LIST((short) 3, (short) 1),
62 * Unrecognized Well-known Attribute. 3/2
64 WELL_KNOWN_ATTR_NOT_RECOGNIZED((short) 3, (short) 2),
66 * Missing Well-known Attribute. 3/3
68 WELL_KNOWN_ATTR_MISSING((short) 3, (short) 3),
70 * Attribute Flags Error. 3/4
72 ATTR_FLAGS_MISSING((short) 3, (short) 4),
74 * Attribute Length Error. 3/5
76 ATTR_LENGTH_ERROR((short) 3, (short) 5),
78 * Invalid ORIGIN Attribute. 3/6
80 ORIGIN_ATTR_NOT_VALID((short) 3, (short) 6),
82 * Invalid NEXT_HOP Attribute. 3/8
84 NEXT_HOP_NOT_VALID((short) 3, (short) 8),
86 * Optional Attribute Error. 3/9
88 OPT_ATTR_ERROR((short) 3, (short) 9),
90 * Invalid Network Field. 3/10
92 NETWORK_NOT_VALID((short) 3, (short) 10),
94 * Malformed AS_PATH. 3/11
96 AS_PATH_MALFORMED((short) 3, (short) 11),
98 * Hold Timer Expired. 4/0
100 HOLD_TIMER_EXPIRED((short) 4, (short) 0),
102 * Finite State Machine Error. 5/0
104 FSM_ERROR((short) 5, (short) 0),
108 CEASE((short) 6, (short) 0),
110 * Maximum Number of Prefixes Reached. 6/1
112 MAX_NUMBER_OF_PREFIXES_REACHED((short) 6, (short) 1),
114 * Administrative Shutdown. 6/2
116 ADMINISTRATIVE_SHUTDOWN((short) 6, (short) 2),
118 * Peer De-configured. 6/3
120 PEER_DECONFIGURED((short) 6, (short) 3),
122 * Administrative Reset. 6/4
124 ADMINISTRATIVE_RESTART((short) 6, (short) 4),
126 * Connection Rejected. 6/5
128 CONNECTION_REJECTED((short) 6, (short) 5),
130 * Other Configuration Change. 6/6
132 OTHER_CONFIGURATION_CHANGE((short) 6, (short) 6),
134 * Connection Collision Resolution. 6/7
136 CONNECTION_COLLISION_RESOLUTION((short) 6, (short) 7),
138 * Out of Resources. 6/8
140 OUT_OF_RESOURCES((short) 6, (short) 8);
142 private static final Map<BGPErrorIdentifier, BGPError> VALUE_MAP;
145 VALUE_MAP = Maps.newHashMap();
146 for (final BGPError enumItem : BGPError.values()) {
147 VALUE_MAP.put(enumItem.getErrorIdentifier(), enumItem);
151 private final BGPErrorIdentifier errorId;
153 BGPError(final short code, final short subcode) {
154 this.errorId = new BGPErrorIdentifier(code, subcode);
157 public short getCode() {
158 return this.errorId.getCode();
161 public short getSubcode() {
162 return this.errorId.getSubCode();
165 private BGPErrorIdentifier getErrorIdentifier() {
170 * Caret for combination of Error-type and Error-value
172 private static class BGPErrorIdentifier {
173 private final short code;
174 private final short subcode;
176 BGPErrorIdentifier(final short code, final short subcode) {
178 this.subcode = subcode;
181 public short getCode() {
185 public short getSubCode() {
190 public int hashCode() {
191 final int prime = 31;
193 result = prime * result + this.code;
194 result = prime * result + this.subcode;
199 public boolean equals(final java.lang.Object obj) {
206 if (this.getClass() != obj.getClass()) {
209 final BGPErrorIdentifier other = (BGPErrorIdentifier) obj;
210 if (this.code != other.code) {
213 if (this.subcode != other.subcode) {
220 public String toString() {
221 return "type " + this.code + " value " + this.subcode;
225 public static BGPError forValue(final int code, final int subcode) {
226 final BGPError e = VALUE_MAP.get(new BGPErrorIdentifier((short) code, (short) subcode));
230 throw new IllegalArgumentException("BGP Error code " + code + " and subcode " + subcode + " not recognized.");