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.concepts;
10 import java.io.Serializable;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
14 import com.google.common.base.Preconditions;
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.
20 public final class Community implements Serializable {
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).
26 public static final Community NO_EXPORT = new Community(new AsNumber((long) 0xFFFF), 0xFF01);
28 * NO_ADVERTISE community. All routes received carrying a communities attribute containing this value MUST NOT be
29 * advertised to other BGP peers.
31 public static final Community NO_ADVERTISE = new Community(new AsNumber((long) 0xFFFF), 0xFF02);
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
37 public static final Community NO_EXPORT_SUBCONFED = new Community(new AsNumber((long) 0xFFFF), 0xFF03);
39 private static final long serialVersionUID = -944853598551415685L;
41 private final int semantics;
43 private final AsNumber as;
46 * Create a new community tag for a particular AS number and semantics.
48 * @param as Global semantics namespace identifier (usually the tagging Autonomous System)
49 * @param semantics Sematics identifier (specific meaning defined externally by the namespace)
51 public Community(final AsNumber as, final int semantics) {
52 Preconditions.checkArgument(semantics > 0 && semantics < 65535, "Invalid semantics specified");
53 this.semantics = semantics;
58 * Return semantics attribute of community.
60 * @return Semantics attribute
62 public int getSemantics() {
63 return this.semantics;
67 * Return ASNumber of community.
69 * @return {@link AsNumber}
71 public AsNumber getAs() {
76 public boolean equals(final Object o) {
77 if (!(o instanceof Community))
80 final Community c = (Community) o;
81 return this.as.equals(c.as) && this.semantics == c.semantics;
85 public int hashCode() {
86 return 7 * this.as.hashCode() + 13 * this.semantics;
90 public final String toString() {
91 final StringBuilder sb = new StringBuilder(this.as.toString());
93 sb.append(this.semantics);
98 * Creates a Community from its String representation.
100 * @param string String representation of a community
101 * @return new Community
103 public static Community valueOf(final String string) {
104 final String[] parts = string.split(":", 2);
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);