b696d4c4f06d81fb19d9a3ee128e0133fc962ec9
[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 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;
16
17 /**
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.
20  */
21 public final class CommunityUtil {
22     /**
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).
26      */
27     public static final Community NO_EXPORT = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF01);
28     /**
29      * NO_ADVERTISE community. All routes received carrying a communities attribute containing this value MUST NOT be
30      * advertised to other BGP peers.
31      */
32     public static final Community NO_ADVERTISE = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF02);
33     /**
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
36      * confederation).
37      */
38     public static final Community NO_EXPORT_SUBCONFED = CommunityUtil.create(NoopReferenceCache.getInstance(), 0xFFFF, 0xFF03);
39
40     private final ReferenceCache refCache;
41
42     public CommunityUtil(final ReferenceCache refCache) {
43         this.refCache = Preconditions.checkNotNull(refCache);
44     }
45
46     /**
47      * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
48      *
49      * @param asn long
50      * @param semantics int
51      * @return new Community
52      */
53     public Community create(final long asn, final int semantics) {
54         return create(this.refCache, asn, semantics);
55     }
56
57     /**
58      * Creates a Community from its String representation.
59      *
60      * @param string String representation of a community
61      * @return new Community
62      */
63     public Community valueOf(final String string) {
64         return valueOf(this.refCache, string);
65     }
66
67     /**
68      * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
69      *
70      * @param refCache reference cache to use
71      * @param asn long
72      * @param semantics int
73      * @return new Community
74      */
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());
80     }
81
82     /**
83      * Creates a Community from its String representation.
84      *
85      * @param refCache reference cache to use
86      * @param string String representation of a community
87      * @return new Community
88      */
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());
95     }
96 }