f18496861e9d2335178ccfde5accebddaf341c5d
[bgpcep.git] / bgp / concepts / src / main / java / org / opendaylight / protocol / bgp / concepts / Community.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.concepts;
9
10 import java.io.Serializable;
11
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
13
14 import com.google.common.base.Preconditions;
15
16 /**
17  * Object representation of a RFC1997 Community tag. Communities are a way for the advertising entitiy to attach
18  * semantic meaning/policy to advertised objects.
19  */
20 public final class Community implements Serializable {
21         /**
22          * NO_EXPORT community. All routes received carrying a communities attribute containing this value MUST NOT be
23          * advertised outside a BGP confederation boundary (a stand-alone autonomous system that is not part of a
24          * confederation should be considered a confederation itself).
25          */
26         public static final Community NO_EXPORT = new Community(new AsNumber((long) 0xFFFF), 0xFF01);
27         /**
28          * NO_ADVERTISE community. All routes received carrying a communities attribute containing this value MUST NOT be
29          * advertised to other BGP peers.
30          */
31         public static final Community NO_ADVERTISE = new Community(new AsNumber((long) 0xFFFF), 0xFF02);
32         /**
33          * NO_EXPORT_SUBCONFED community. All routes received carrying a communities attribute containing this value MUST
34          * NOT be advertised to external BGP peers (this includes peers in other members autonomous systems inside a BGP
35          * confederation).
36          */
37         public static final Community NO_EXPORT_SUBCONFED = new Community(new AsNumber((long) 0xFFFF), 0xFF03);
38
39         private static final long serialVersionUID = -944853598551415685L;
40
41         private final int semantics;
42
43         private final AsNumber as;
44
45         /**
46          * Create a new community tag for a particular AS number and semantics.
47          * 
48          * @param as Global semantics namespace identifier (usually the tagging Autonomous System)
49          * @param semantics Sematics identifier (specific meaning defined externally by the namespace)
50          */
51         public Community(final AsNumber as, final int semantics) {
52                 Preconditions.checkArgument(semantics > 0 && semantics < 65535, "Invalid semantics specified");
53                 this.semantics = semantics;
54                 this.as = as;
55         }
56
57         /**
58          * Return semantics attribute of community.
59          * 
60          * @return Semantics attribute
61          */
62         public int getSemantics() {
63                 return this.semantics;
64         }
65
66         /**
67          * Return ASNumber of community.
68          * 
69          * @return {@link AsNumber}
70          */
71         public AsNumber getAs() {
72                 return this.as;
73         }
74
75         @Override
76         public boolean equals(final Object o) {
77                 if (!(o instanceof Community))
78                         return false;
79
80                 final Community c = (Community) o;
81                 return this.as.equals(c.as) && this.semantics == c.semantics;
82         }
83
84         @Override
85         public int hashCode() {
86                 return 7 * this.as.hashCode() + 13 * this.semantics;
87         }
88
89         @Override
90         public final String toString() {
91                 final StringBuilder sb = new StringBuilder(this.as.toString());
92                 sb.append(':');
93                 sb.append(this.semantics);
94                 return sb.toString();
95         }
96
97         /**
98          * Creates a Community from its String representation.
99          * 
100          * @param string String representation of a community
101          * @return new Community
102          */
103         public static Community valueOf(final String string) {
104                 final String[] parts = string.split(":", 2);
105
106                 final int asn = Integer.valueOf(parts[0]);
107                 final int sem = Integer.valueOf(parts[1]);
108                 return new Community(new AsNumber((long) asn), sem);
109         }
110 }