Merge "Add EPL license declaration"
[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 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.CommunitiesBuilder;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
13
14 /**
15  * Object representation of a RFC1997 Community tag. Communities are a way for the advertising entity to attach semantic
16  * meaning/policy to advertised objects.
17  */
18 public final class CommunityUtil {
19         /**
20          * NO_EXPORT community. All routes received carrying a communities attribute containing this value MUST NOT be
21          * advertised outside a BGP confederation boundary (a stand-alone autonomous system that is not part of a
22          * confederation should be considered a confederation itself).
23          */
24         public static final Community NO_EXPORT = CommunityUtil.create(0xFFFF, 0xFF01);
25         /**
26          * NO_ADVERTISE community. All routes received carrying a communities attribute containing this value MUST NOT be
27          * advertised to other BGP peers.
28          */
29         public static final Community NO_ADVERTISE = CommunityUtil.create(0xFFFF, 0xFF02);
30         /**
31          * NO_EXPORT_SUBCONFED community. All routes received carrying a communities attribute containing this value MUST
32          * NOT be advertised to external BGP peers (this includes peers in other members autonomous systems inside a BGP
33          * confederation).
34          */
35         public static final Community NO_EXPORT_SUBCONFED = CommunityUtil.create(0xFFFF, 0xFF03);
36
37         private CommunityUtil() {
38
39         }
40
41         /**
42          * Creates a new Community given AS number value and semantics using generated CommunitiesBuilder.
43          * 
44          * @param asn long
45          * @param semantics long
46          * @return new Community
47          */
48         public static Community create(final long asn, final int semantics) {
49                 final CommunitiesBuilder builder = new CommunitiesBuilder();
50                 builder.setAsNumber(new AsNumber(asn));
51                 builder.setSemantics(semantics);
52                 return builder.build();
53         }
54
55         /**
56          * Creates a Community from its String representation.
57          * 
58          * @param string String representation of a community
59          * @return new Community
60          */
61         public static Community valueOf(final String string) {
62                 final String[] parts = string.split(":", 2);
63                 final CommunitiesBuilder builder = new CommunitiesBuilder();
64                 builder.setAsNumber(new AsNumber(Long.valueOf(parts[0])));
65                 builder.setSemantics(Integer.valueOf(parts[1]));
66                 return builder.build();
67         }
68 }