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 com.google.common.base.Preconditions;
11 import org.opendaylight.protocol.util.NoopReferenceCache;
12 import org.opendaylight.protocol.util.ReferenceCache;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.CommunitiesBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
18 * Object representation of a RFC1997 Community tag. Communities are a way for the advertising entity to attach semantic
19 * meaning/policy to advertised objects.
21 public final class CommunityUtil {
23 * NO_EXPORT community. All routes received carrying a communities attribute containing this value MUST NOT be
24 * advertised outside a BGP confederation boundary (a stand-alone autonomous system that is not part of a
25 * confederation should be considered a confederation itself).
27 public static final Community NO_EXPORT = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF01);
29 * NO_ADVERTISE community. All routes received carrying a communities attribute containing this value MUST NOT be
30 * advertised to other BGP peers.
32 public static final Community NO_ADVERTISE = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF02);
34 * NO_EXPORT_SUBCONFED community. All routes received carrying a communities attribute containing this value MUST
35 * NOT be advertised to external BGP peers (this includes peers in other members autonomous systems inside a BGP
38 public static final Community NO_EXPORT_SUBCONFED = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF03);
40 private final ReferenceCache refCache;
42 public CommunityUtil(final ReferenceCache refCache) {
43 this.refCache = Preconditions.checkNotNull(refCache);
47 * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
50 * @param semantics int
51 * @return new Community
53 public Community create(final long asn, final int semantics) {
54 return create(this.refCache, asn, semantics);
58 * Creates a Community from its String representation.
60 * @param string String representation of a community
61 * @return new Community
63 public Community valueOf(final String string) {
64 return valueOf(this.refCache, string);
68 * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
70 * @param refCache reference cache to use
72 * @param semantics int
73 * @return new Community
75 public static Community create(final ReferenceCache refCache, final long asn, final int semantics) {
76 final CommunitiesBuilder builder = new CommunitiesBuilder();
77 builder.setAsNumber(refCache.getSharedReference(new AsNumber(asn)));
78 builder.setSemantics(refCache.getSharedReference(semantics));
79 return refCache.getSharedReference(builder.build());
83 * Creates a Community from its String representation.
85 * @param refCache reference cache to use
86 * @param string String representation of a community
87 * @return new Community
89 public static Community valueOf(final ReferenceCache refCache, final String string) {
90 final String[] parts = string.split(":", 2);
91 final CommunitiesBuilder builder = new CommunitiesBuilder();
92 builder.setAsNumber(refCache.getSharedReference(new AsNumber(Long.valueOf(parts[0]))));
93 builder.setSemantics(refCache.getSharedReference(Integer.valueOf(parts[1])));
94 return refCache.getSharedReference(builder.build());