Bug-731 Fixed few minor Sonar warnings
[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 com.google.common.collect.Maps;
11 import java.util.Map;
12
13
14 /**
15  * Possible errors from implemented RFCs and drafts. Each error consists of error code and error subcode (code/subcode
16  * in comments).
17  *
18  * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.5">BGP Notification Message</a>
19  */
20 public enum BGPError {
21     /**
22      * Connection Not Synchronized. 1/1
23      */
24     CONNECTION_NOT_SYNC((short) 1, (short) 1),
25     /**
26      * Bad Message Length. 1/2
27      */
28     BAD_MSG_LENGTH((short) 1, (short) 2),
29     /**
30      * Bad Message Type. 1/3
31      */
32     BAD_MSG_TYPE((short) 1, (short) 3),
33     /**
34      * Unspecific Open Message error.
35      */
36     UNSPECIFIC_OPEN_ERROR((short) 2, (short) 0),
37     /**
38      * Unsupported Version Number. 2/1
39      */
40     VERSION_NOT_SUPPORTED((short) 2, (short) 1),
41     /**
42      * Bad Peer AS. 2/2
43      */
44     BAD_PEER_AS((short) 2, (short) 2),
45     /**
46      * Bad BGP Identifier. 2/3
47      */
48     BAD_BGP_ID((short) 2, (short) 3),
49     /**
50      * Unsupported Optional Parameter. 2/4
51      */
52     OPT_PARAM_NOT_SUPPORTED((short) 2, (short) 4),
53     /**
54      * Unacceptable Hold Time. 2/6
55      */
56     HOLD_TIME_NOT_ACC((short) 2, (short) 6),
57     /**
58      * Malformed Attribute List. 3/1
59      */
60     MALFORMED_ATTR_LIST((short) 3, (short) 1),
61     /**
62      * Unrecognized Well-known Attribute. 3/2
63      */
64     WELL_KNOWN_ATTR_NOT_RECOGNIZED((short) 3, (short) 2),
65     /**
66      * Missing Well-known Attribute. 3/3
67      */
68     WELL_KNOWN_ATTR_MISSING((short) 3, (short) 3),
69     /**
70      * Attribute Flags Error. 3/4
71      */
72     ATTR_FLAGS_MISSING((short) 3, (short) 4),
73     /**
74      * Attribute Length Error. 3/5
75      */
76     ATTR_LENGTH_ERROR((short) 3, (short) 5),
77     /**
78      * Invalid ORIGIN Attribute. 3/6
79      */
80     ORIGIN_ATTR_NOT_VALID((short) 3, (short) 6),
81     /**
82      * Invalid NEXT_HOP Attribute. 3/8
83      */
84     NEXT_HOP_NOT_VALID((short) 3, (short) 8),
85     /**
86      * Optional Attribute Error. 3/9
87      */
88     OPT_ATTR_ERROR((short) 3, (short) 9),
89     /**
90      * Invalid Network Field. 3/10
91      */
92     NETWORK_NOT_VALID((short) 3, (short) 10),
93     /**
94      * Malformed AS_PATH. 3/11
95      */
96     AS_PATH_MALFORMED((short) 3, (short) 11),
97     /**
98      * Hold Timer Expired. 4/0
99      */
100     HOLD_TIMER_EXPIRED((short) 4, (short) 0),
101     /**
102      * Finite State Machine Error. 5/0
103      */
104     FSM_ERROR((short) 5, (short) 0),
105     /**
106      * Cease. 6/0
107      */
108     CEASE((short) 6, (short) 0);
109
110     private static final Map<BGPErrorIdentifier, BGPError> VALUE_MAP;
111
112     static {
113         VALUE_MAP = Maps.newHashMap();
114         for (final BGPError enumItem : BGPError.values()) {
115             VALUE_MAP.put(enumItem.getErrorIdentifier(), enumItem);
116         }
117     }
118
119     private final BGPErrorIdentifier errorId;
120
121     BGPError(final short code, final short subcode) {
122         this.errorId = new BGPErrorIdentifier(code, subcode);
123     }
124
125     public short getCode() {
126         return this.errorId.getCode();
127     }
128
129     public short getSubcode() {
130         return this.errorId.getSubCode();
131     }
132
133     private BGPErrorIdentifier getErrorIdentifier() {
134         return this.errorId;
135     }
136
137     /**
138      * Caret for combination of Error-type and Error-value
139      */
140     private static final class BGPErrorIdentifier {
141         private final short code;
142         private final short subcode;
143
144         BGPErrorIdentifier(final short code, final short subcode) {
145             this.code = code;
146             this.subcode = subcode;
147         }
148
149         public short getCode() {
150             return this.code;
151         }
152
153         public short getSubCode() {
154             return this.subcode;
155         }
156
157         @Override
158         public int hashCode() {
159             final int prime = 31;
160             int result = 1;
161             result = prime * result + this.code;
162             result = prime * result + this.subcode;
163             return result;
164         }
165
166         @Override
167         public boolean equals(final java.lang.Object obj) {
168             if (this == obj) {
169                 return true;
170             }
171             if (obj == null) {
172                 return false;
173             }
174             if (this.getClass() != obj.getClass()) {
175                 return false;
176             }
177             final BGPErrorIdentifier other = (BGPErrorIdentifier) obj;
178             if (this.code != other.code) {
179                 return false;
180             }
181             if (this.subcode != other.subcode) {
182                 return false;
183             }
184             return true;
185         }
186
187         @Override
188         public String toString() {
189             return "type " + this.code + " value " + this.subcode;
190         }
191     }
192
193     public static BGPError forValue(final int code, final int subcode) {
194         final BGPError e = VALUE_MAP.get(new BGPErrorIdentifier((short) code, (short) subcode));
195         if (e != null) {
196             return e;
197         }
198         throw new IllegalArgumentException("BGP Error code " + code + " and subcode " + subcode + " not recognized.");
199     }
200 }