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 private static final Map<BGPErrorIdentifier, BGPError> VALUE_MAP;
113 VALUE_MAP = Maps.newHashMap();
114 for (final BGPError enumItem : BGPError.values()) {
115 VALUE_MAP.put(enumItem.getErrorIdentifier(), enumItem);
119 private final BGPErrorIdentifier errorId;
121 BGPError(final short code, final short subcode) {
122 this.errorId = new BGPErrorIdentifier(code, subcode);
125 public short getCode() {
126 return this.errorId.getCode();
129 public short getSubcode() {
130 return this.errorId.getSubCode();
133 private BGPErrorIdentifier getErrorIdentifier() {
138 * Caret for combination of Error-type and Error-value
140 private static class BGPErrorIdentifier {
141 private final short code;
142 private final short subcode;
144 BGPErrorIdentifier(final short code, final short subcode) {
146 this.subcode = subcode;
149 public short getCode() {
153 public short getSubCode() {
158 public int hashCode() {
159 final int prime = 31;
161 result = prime * result + this.code;
162 result = prime * result + this.subcode;
167 public boolean equals(final java.lang.Object obj) {
174 if (this.getClass() != obj.getClass()) {
177 final BGPErrorIdentifier other = (BGPErrorIdentifier) obj;
178 if (this.code != other.code) {
181 if (this.subcode != other.subcode) {
188 public String toString() {
189 return "type " + this.code + " value " + this.subcode;
193 public static BGPError forValue(final int code, final int subcode) {
194 final BGPError e = VALUE_MAP.get(new BGPErrorIdentifier((short) code, (short) subcode));
198 throw new IllegalArgumentException("BGP Error code " + code + " and subcode " + subcode + " not recognized.");