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.rev130919.path.attributes.attributes.CommunitiesBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.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 = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF01);
30 * NO_ADVERTISE community. All routes received carrying a communities attribute containing this value MUST NOT be
31 * advertised to other BGP peers.
33 public static final Community NO_ADVERTISE = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF02);
35 * NO_EXPORT_SUBCONFED community. All routes received carrying a communities attribute containing this value MUST
36 * NOT be advertised to external BGP peers (this includes peers in other members autonomous systems inside a BGP
39 public static final Community NO_EXPORT_SUBCONFED = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF03);
41 private final ReferenceCache refCache;
43 public CommunityUtil(final ReferenceCache refCache) {
44 this.refCache = requireNonNull(refCache);
48 * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
51 * @param semantics int
52 * @return new Community
54 public Community create(final long asn, final int semantics) {
55 return create(this.refCache, asn, semantics);
59 * Creates a Community from its String representation.
61 * @param string String representation of a community
62 * @return new Community
64 public Community valueOf(final String string) {
65 return valueOf(this.refCache, string);
69 * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
71 * @param refCache reference cache to use
73 * @param semantics int
74 * @return new Community
76 public static Community create(final ReferenceCache refCache, final long asn, final int semantics) {
77 final CommunitiesBuilder builder = new CommunitiesBuilder();
78 builder.setAsNumber(refCache.getSharedReference(new AsNumber(asn)));
79 builder.setSemantics(refCache.getSharedReference(semantics));
80 return refCache.getSharedReference(builder.build());
84 * Creates a Community from its String representation.
86 * @param refCache reference cache to use
87 * @param string String representation of a community
88 * @return new Community
90 public static Community valueOf(final ReferenceCache refCache, final String string) {
91 final String[] parts = string.split(":", 2);
92 final CommunitiesBuilder builder = new CommunitiesBuilder();
93 builder.setAsNumber(refCache.getSharedReference(new AsNumber(Long.valueOf(parts[0]))));
94 builder.setSemantics(refCache.getSharedReference(Integer.valueOf(parts[1])));
95 return refCache.getSharedReference(builder.build());