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.impl.message.update;
10 import static java.util.Objects.requireNonNull;
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;
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.
24 public final class CommunityUtil {
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).
30 public static final Community NO_EXPORT
31 = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF01);
33 * NO_ADVERTISE community. All routes received carrying a communities attribute containing this value MUST NOT be
34 * advertised to other BGP peers.
36 public static final Community NO_ADVERTISE
37 = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF02);
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
43 public static final Community NO_EXPORT_SUBCONFED
44 = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF03);
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.
51 public static final Community LLGR_STALE
52 = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0x0006);
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.
58 public static final Community NO_LLGR
59 = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0x0007);
61 private final ReferenceCache refCache;
63 public CommunityUtil(final ReferenceCache refCache) {
64 this.refCache = requireNonNull(refCache);
68 * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
71 * @param semantics int
72 * @return new Community
74 public Community create(final long asn, final int semantics) {
75 return create(this.refCache, asn, semantics);
79 * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
81 * @param refCache reference cache to use
83 * @param semantics int
84 * @return new Community
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());
96 * Creates a Community from its String representation.
98 * @param string String representation of a community
99 * @return new Community
101 public Community valueOf(final String string) {
102 return valueOf(this.refCache, string);
106 * Creates a Community from its String representation.
108 * @param refCache reference cache to use
109 * @param string String representation of a community
110 * @return new Community
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());