Fix most bgp-parser-spi checkstyle violations
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / RevisedErrorHandling.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.spi;
9
10 import org.eclipse.jdt.annotation.NonNullByDefault;
11 import org.eclipse.jdt.annotation.Nullable;
12 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
13 import org.opendaylight.protocol.bgp.parser.BGPError;
14 import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
15
16 /**
17  * Enumeration of possible treatments an UPDATE message and attributes can get based on the configuration of a peer.
18  *
19  * @author Robert Varga
20  */
21 @NonNullByDefault
22 public enum RevisedErrorHandling {
23     /**
24      * Do not use RFC7606 Revised Error Handling.
25      */
26     NONE {
27         @Override
28         public BGPDocumentedException reportError(final BGPError error, final @Nullable Exception cause,
29                 final String format, final Object... args) throws BGPDocumentedException {
30             throw new BGPDocumentedException(String.format(format, args), error, cause);
31         }
32     },
33     /**
34      * Use RFC7606 Revised Error Handling, the peer is an internal neighbor.
35      */
36     INTERNAL,
37     /**
38      * Use RFC7606 Revised Error Handling, the peer is an external neighbor.
39      */
40     EXTERNAL;
41
42     /**
43      * Determine Revised Error Handling from the contents of a {@link PeerSpecificParserConstraint}.
44      *
45      * @param constraint Peer-specific constraint
46      * @return Revised Error Handling treatment message/attributes should receive.
47      */
48     public static RevisedErrorHandling from(final @Nullable PeerSpecificParserConstraint constraint) {
49         return constraint == null ? NONE : constraint.getPeerConstraint(RevisedErrorHandlingSupport.class)
50                 .map(support -> support.isExternalPeer() ? EXTERNAL : INTERNAL).orElse(NONE);
51     }
52
53     /**
54      * Report a failure to parse an attribute resulting either in treat-as-withdraw if RFC7606 is in effect, or
55      * connection teardown if it is not.
56      *
57      * @param error {@link BGPError} to report in case of a session teardown
58      * @param cause Parsing failure cause
59      * @param format Message format string
60      * @param args Message format arguments
61      * @return This method does not return
62      * @throws BGPTreatAsWithdrawException if Revised Error Handling is in effect
63      * @throws BGPDocumentedException if Revised Error Handling is in not effect
64      */
65     public BGPDocumentedException reportError(final BGPError error, final @Nullable Exception cause,
66             final String format, final Object... args) throws BGPDocumentedException, BGPTreatAsWithdrawException {
67         throw new BGPTreatAsWithdrawException(error, cause, format, args);
68     }
69
70     /**
71      * Report a failure to parse an attribute resulting either in treat-as-withdraw if RFC7606 is in effect, or
72      * connection teardown if it is not.
73      *
74      * @param error {@link BGPError} to report in case of a session teardown
75      * @param format Message format string
76      * @param args Message format arguments
77      * @return This method does not return
78      * @throws BGPTreatAsWithdrawException if Revised Error Handling is in effect
79      * @throws BGPDocumentedException if Revised Error Handling is in not effect
80      */
81     public BGPDocumentedException reportError(final BGPError error, final String format, final Object... args)
82             throws BGPDocumentedException, BGPTreatAsWithdrawException {
83         throw reportError(error, null, format, args);
84     }
85 }