Fix most bgp-parser-impl checkstyle
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / CommunityUtil.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.impl.message.update;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.opendaylight.protocol.util.NoopReferenceCache;
13 import org.opendaylight.protocol.util.ReferenceCache;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.CommunitiesBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Community;
17
18 /**
19  * Object representation of a RFC1997 Community tag. Communities are a way for the advertising entity to attach semantic
20  * meaning/policy to advertised objects.
21  */
22 public final class CommunityUtil {
23     /**
24      * NO_EXPORT community. All routes received carrying a communities attribute containing this value MUST NOT be
25      * advertised outside a BGP confederation boundary (a stand-alone autonomous system that is not part of a
26      * confederation should be considered a confederation itself).
27      */
28     public static final Community NO_EXPORT
29             = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF01);
30     /**
31      * NO_ADVERTISE community. All routes received carrying a communities attribute containing this value MUST NOT be
32      * advertised to other BGP peers.
33      */
34     public static final Community NO_ADVERTISE
35             = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF02);
36     /**
37      * NO_EXPORT_SUBCONFED community. All routes received carrying a communities attribute containing this value MUST
38      * NOT be advertised to external BGP peers (this includes peers in other members autonomous systems inside a BGP
39      * confederation).
40      */
41     public static final Community NO_EXPORT_SUBCONFED
42             = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF03);
43
44     /**
45      * LLGR_STALE community can be used to mark stale routes retained for a longer period of time.
46      * Such long-lived stale routes are to be handled according to the procedures specified in
47      * https://tools.ietf.org/html/draft-uttaro-idr-bgp-persistence-04#section-4.
48      */
49     public static final Community LLGR_STALE
50             = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0x0006);
51
52     /**
53      * NO_LLGR community can be used to mark routes which a BGP speaker does not want treated according
54      * to procedures, as detailed in https://tools.ietf.org/html/draft-uttaro-idr-bgp-persistence-04#section-4.
55      */
56     public static final Community NO_LLGR
57             = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0x0007);
58
59     private final ReferenceCache refCache;
60
61     public CommunityUtil(final ReferenceCache refCache) {
62         this.refCache = requireNonNull(refCache);
63     }
64
65     /**
66      * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
67      *
68      * @param asn long
69      * @param semantics int
70      * @return new Community
71      */
72     public Community create(final long asn, final int semantics) {
73         return create(this.refCache, asn, semantics);
74     }
75
76     /**
77      * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
78      *
79      * @param refCache reference cache to use
80      * @param asn long
81      * @param semantics int
82      * @return new Community
83      */
84     public static Community create(final ReferenceCache refCache, final long asn, final int semantics) {
85         final CommunitiesBuilder builder = new CommunitiesBuilder();
86         builder.setAsNumber(refCache.getSharedReference(new AsNumber(asn)));
87         builder.setSemantics(refCache.getSharedReference(semantics));
88         return refCache.getSharedReference(builder.build());
89     }
90
91     /**
92      * Creates a Community from its String representation.
93      *
94      * @param string String representation of a community
95      * @return new Community
96      */
97     public Community valueOf(final String string) {
98         return valueOf(this.refCache, string);
99     }
100
101     /**
102      * Creates a Community from its String representation.
103      *
104      * @param refCache reference cache to use
105      * @param string String representation of a community
106      * @return new Community
107      */
108     public static Community valueOf(final ReferenceCache refCache, final String string) {
109         final String[] parts = string.split(":", 2);
110         final CommunitiesBuilder builder = new CommunitiesBuilder();
111         builder.setAsNumber(refCache.getSharedReference(new AsNumber(Long.valueOf(parts[0]))));
112         builder.setSemantics(refCache.getSharedReference(Integer.valueOf(parts[1])));
113         return refCache.getSharedReference(builder.build());
114     }
115 }