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.rev180329.path.attributes.attributes.CommunitiesBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Community;
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.
22 public final class CommunityUtil {
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).
28 public static final Community NO_EXPORT
29 = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF01);
31 * NO_ADVERTISE community. All routes received carrying a communities attribute containing this value MUST NOT be
32 * advertised to other BGP peers.
34 public static final Community NO_ADVERTISE
35 = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF02);
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
41 public static final Community NO_EXPORT_SUBCONFED
42 = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF03);
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.
49 public static final Community LLGR_STALE
50 = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0x0006);
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.
56 public static final Community NO_LLGR
57 = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0x0007);
59 private final ReferenceCache refCache;
61 public CommunityUtil(final ReferenceCache refCache) {
62 this.refCache = requireNonNull(refCache);
66 * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
69 * @param semantics int
70 * @return new Community
72 public Community create(final long asn, final int semantics) {
73 return create(this.refCache, asn, semantics);
77 * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
79 * @param refCache reference cache to use
81 * @param semantics int
82 * @return new Community
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());
92 * Creates a Community from its String representation.
94 * @param string String representation of a community
95 * @return new Community
97 public Community valueOf(final String string) {
98 return valueOf(this.refCache, string);
102 * Creates a Community from its String representation.
104 * @param refCache reference cache to use
105 * @param string String representation of a community
106 * @return new Community
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());