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.parser.impl.message.update;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.primitives.UnsignedBytes;
13 import io.netty.buffer.ByteBuf;
15 import java.util.Arrays;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPError;
19 import org.opendaylight.protocol.concepts.Ipv4Util;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.protocol.util.ReferenceCache;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunitiesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunityBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunityBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunityBuilder;
38 * Parser for Extended Communities Path Attribute.
40 public final class CommunitiesParser {
42 protected static final int EXTENDED_COMMUNITY_LENGTH = 8;
44 protected static final int COMMUNITY_LENGTH = 4;
46 private static final int AS_LOCAL_ADMIN_LENGTH = 4;
48 private static final int INET_LOCAL_ADMIN_LENGTH = 2;
50 protected static final short AS_TYPE_TRANS = 0;
52 protected static final short AS_TYPE_NON_TRANS = 40;
54 protected static final short INET_TYPE_TRANS = 1;
56 protected static final short INET_TYPE_NON_TRANS = 41;
58 protected static final short OPAQUE_TYPE_TRANS = 3;
60 protected static final short OPAQUE_TYPE_NON_TRANS = 43;
62 protected static final short ROUTE_TYPE_ONLY = 2;
64 protected static final short ROUTE_TARGET_SUBTYPE = 2;
66 protected static final short ROUTE_ORIGIN_SUBTYPE = 3;
68 private static final byte[] NO_EXPORT = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x01 };
70 private static final byte[] NO_ADVERTISE = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x02 };
72 private static final byte[] NO_EXPORT_SUBCONFED = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x03 };
74 private CommunitiesParser() {
79 * Parse known Community, if unknown, a new one will be created.
83 * @param bytes byte array to be parsed
84 * @return new Community
85 * @throws BGPDocumentedException
87 static Community parseCommunity(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException {
88 if (buffer.readableBytes() != COMMUNITY_LENGTH) {
89 throw new BGPDocumentedException("Community with wrong length: " + buffer.readableBytes(), BGPError.OPT_ATTR_ERROR);
91 byte[] body = ByteArray.getBytes(buffer, COMMUNITY_LENGTH);
92 if (Arrays.equals(body, NO_EXPORT)) {
93 return CommunityUtil.NO_EXPORT;
94 } else if (Arrays.equals(body, NO_ADVERTISE)) {
95 return CommunityUtil.NO_ADVERTISE;
96 } else if (Arrays.equals(body, NO_EXPORT_SUBCONFED)) {
97 return CommunityUtil.NO_EXPORT_SUBCONFED;
99 return CommunityUtil.create(refCache, buffer.readUnsignedShort(), buffer.readUnsignedShort());
103 * Parse Extended Community according to their type.
105 * @param bytes byte array to be parsed
106 * @return new Specific Extended Community
107 * @throws BGPDocumentedException if the type is not recognized
110 public static ExtendedCommunities parseExtendedCommunity(final ReferenceCache refCache, final ByteBuf buffer)
111 throws BGPDocumentedException {
112 final int type = UnsignedBytes.toInt(buffer.readByte());
113 final int subType = UnsignedBytes.toInt(buffer.readByte());
115 final ExtendedCommunitiesBuilder comm = new ExtendedCommunitiesBuilder();
118 comm.setCommType(AS_TYPE_TRANS);
119 if (subType == ROUTE_TARGET_SUBTYPE) {
120 comm.setCommSubType(ROUTE_TARGET_SUBTYPE).setExtendedCommunity(
121 new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
122 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(
123 new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
124 ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
125 } else if (subType == ROUTE_ORIGIN_SUBTYPE) {
126 comm.setCommSubType(ROUTE_ORIGIN_SUBTYPE).setExtendedCommunity(
127 new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
128 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(
129 new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
130 ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
132 comm.setExtendedCommunity(new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
133 new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
134 new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
135 ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
138 case AS_TYPE_NON_TRANS:
139 comm.setCommType(AS_TYPE_NON_TRANS).setExtendedCommunity(
140 new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
141 new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
142 new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
143 ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
145 case ROUTE_TYPE_ONLY:
146 comm.setCommType(ROUTE_TYPE_ONLY);
147 if (subType == ROUTE_TARGET_SUBTYPE) {
148 comm.setCommSubType(ROUTE_TARGET_SUBTYPE).setExtendedCommunity(
149 new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
150 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(
151 new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
152 ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
153 } else if (subType == ROUTE_ORIGIN_SUBTYPE) {
154 comm.setCommSubType(ROUTE_ORIGIN_SUBTYPE).setExtendedCommunity(
155 new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
156 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(
157 new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
158 ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
160 throw new BGPDocumentedException("Could not parse Extended Community subtype: " + subType, BGPError.OPT_ATTR_ERROR);
163 case INET_TYPE_TRANS:
164 comm.setCommType(INET_TYPE_TRANS);
165 if (subType == ROUTE_TARGET_SUBTYPE) {
166 comm.setCommSubType(ROUTE_TARGET_SUBTYPE).setExtendedCommunity(
167 new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
168 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(
169 new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
170 ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
171 } else if (subType == ROUTE_ORIGIN_SUBTYPE) {
172 comm.setCommSubType(ROUTE_ORIGIN_SUBTYPE).setExtendedCommunity(
173 new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
174 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(
175 new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
176 ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
178 comm.setExtendedCommunity(new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
179 new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
180 Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))).setLocalAdministrator(
181 ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build());
184 case INET_TYPE_NON_TRANS:
185 comm.setCommType(INET_TYPE_NON_TRANS).setExtendedCommunity(
186 new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
187 new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
188 Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))).setLocalAdministrator(
189 ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build());
191 case OPAQUE_TYPE_TRANS:
192 comm.setCommType(OPAQUE_TYPE_TRANS).setExtendedCommunity(
193 new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
194 new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(ByteArray.readAllBytes(buffer)).build()).build());
196 case OPAQUE_TYPE_NON_TRANS:
197 comm.setCommType(OPAQUE_TYPE_NON_TRANS).setExtendedCommunity(
198 new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
199 new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(ByteArray.readAllBytes(buffer)).build()).build());
202 throw new BGPDocumentedException("Could not parse Extended Community type: " + type, BGPError.OPT_ATTR_ERROR);