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