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