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.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
16 import org.opendaylight.protocol.bgp.parser.BGPError;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
19 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.protocol.util.ByteBufWriteUtil;
22 import org.opendaylight.protocol.util.Ipv4Util;
23 import org.opendaylight.protocol.util.ReferenceCache;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunitiesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunity;
41 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;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunity;
43 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;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunity;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunityBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunity;
47 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;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunity;
49 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;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
52 public class ExtendedCommunitiesAttributeParser implements AttributeParser,AttributeSerializer {
54 public static final int TYPE = 16;
56 private static final int EXTENDED_COMMUNITY_LENGTH = 6;
58 private static final int AS_LOCAL_ADMIN_LENGTH = 4;
60 private static final int INET_LOCAL_ADMIN_LENGTH = 2;
62 private static final short AS_TYPE_TRANS = 0;
64 private static final short AS_TYPE_NON_TRANS = 40;
66 private static final short INET_TYPE_TRANS = 1;
68 private static final short INET_TYPE_NON_TRANS = 41;
70 private static final short OPAQUE_TYPE_TRANS = 3;
72 private static final short OPAQUE_TYPE_NON_TRANS = 43;
74 private static final short ROUTE_TYPE_ONLY = 2;
76 private static final short ROUTE_TARGET_SUBTYPE = 2;
78 private static final short ROUTE_ORIGIN_SUBTYPE = 3;
80 private final ReferenceCache refCache;
82 public ExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
83 this.refCache = Preconditions.checkNotNull(refCache);
87 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException {
88 final List<ExtendedCommunities> set = new ArrayList<>();
89 while (buffer.isReadable()) {
90 final ExtendedCommunitiesBuilder exBuilder = new ExtendedCommunitiesBuilder();
91 parseHeader(exBuilder, buffer);
92 final ExtendedCommunities comm = parseExtendedCommunity(this.refCache, exBuilder, buffer.readSlice(EXTENDED_COMMUNITY_LENGTH));
95 builder.setExtendedCommunities(set);
98 protected void parseHeader(final ExtendedCommunitiesBuilder exBuilder, final ByteBuf buffer) {
99 exBuilder.setCommType(buffer.readUnsignedByte());
100 exBuilder.setCommSubType(buffer.readUnsignedByte());
104 * Parse Extended Community according to their type.
107 * @param comm ExtendedCommunitiesBuilder based on which community type will be new ExtendedCommunity created
108 * @param buffer byte array to be parsed
109 * @return new Specific Extended Community
110 * @throws BGPDocumentedException if the type is not recognized
112 public ExtendedCommunities parseExtendedCommunity(final ReferenceCache refCache, final ExtendedCommunitiesBuilder comm, final ByteBuf buffer)
113 throws BGPDocumentedException {
114 ExtendedCommunity c = null;
115 switch (comm.getCommType()) {
117 c = parseAsTransCommunity(comm, buffer);
119 case AS_TYPE_NON_TRANS:
120 final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
121 final byte[] value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
122 c = new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
123 new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
125 case ROUTE_TYPE_ONLY:
126 c = parseRouteTypeOnlyCommunity(buffer, comm.getCommSubType());
128 case INET_TYPE_TRANS:
129 c = parseInetTypeCommunity(comm, buffer);
131 case INET_TYPE_NON_TRANS:
132 c = new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
133 new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
134 Ipv4Util.addressForByteBuf(buffer)).setLocalAdministrator(
135 ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build();
137 case OPAQUE_TYPE_TRANS:
138 c = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(ByteArray.readAllBytes(buffer)).build()).build();
140 case OPAQUE_TYPE_NON_TRANS:
141 c = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(ByteArray.readAllBytes(buffer)).build()).build();
144 throw new BGPDocumentedException("Could not parse Extended Community type: " + comm.getCommType(), BGPError.OPT_ATTR_ERROR);
146 return comm.setExtendedCommunity(c).build();
149 private static ExtendedCommunity parseRouteTypeOnlyCommunity(final ByteBuf buffer, final int subtype) throws BGPDocumentedException {
150 final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
151 final byte[] value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
152 if (subtype == ROUTE_TARGET_SUBTYPE) {
153 return new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
154 } else if (subtype == ROUTE_ORIGIN_SUBTYPE) {
155 return new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
157 throw new BGPDocumentedException("Could not parse Extended Community subtype: " + subtype, BGPError.OPT_ATTR_ERROR);
161 private static ExtendedCommunity parseAsTransCommunity(final ExtendedCommunitiesBuilder comm, final ByteBuf buffer) {
162 final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
163 final byte[] value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
164 if (comm.getCommSubType() == ROUTE_TARGET_SUBTYPE) {
165 return new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
166 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
168 if (comm.getCommSubType() == ROUTE_ORIGIN_SUBTYPE) {
169 return new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
170 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
172 return new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
173 new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
176 private static ExtendedCommunity parseInetTypeCommunity(final ExtendedCommunitiesBuilder comm, final ByteBuf buffer) {
177 if (comm.getCommSubType() == ROUTE_TARGET_SUBTYPE) {
178 return new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
179 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
180 .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build();
182 if (comm.getCommSubType() == ROUTE_ORIGIN_SUBTYPE) {
183 return new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
184 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
185 .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build();
187 return new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
188 new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
189 Ipv4Util.addressForByteBuf(buffer)).setLocalAdministrator(
190 ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build();
194 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
195 Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
196 final List<ExtendedCommunities> communitiesList = ((Attributes) attribute).getExtendedCommunities();
197 if (communitiesList == null) {
200 final ByteBuf extendedCommunitiesBuffer = Unpooled.buffer();
201 for (final ExtendedCommunities extendedCommunities : communitiesList) {
202 serializeHeader(extendedCommunities, extendedCommunitiesBuffer);
203 serializeExtendedCommunity(extendedCommunities, extendedCommunitiesBuffer);
205 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, extendedCommunitiesBuffer, byteAggregator);
208 protected void serializeHeader(final ExtendedCommunities extendedCommunities, final ByteBuf extendedCommunitiesBuffer) {
209 ByteBufWriteUtil.writeUnsignedByte(extendedCommunities.getCommType(), extendedCommunitiesBuffer);
210 ByteBufWriteUtil.writeUnsignedByte(extendedCommunities.getCommSubType(), extendedCommunitiesBuffer);
213 public void serializeExtendedCommunity(final ExtendedCommunities extendedCommunities, final ByteBuf buffer) {
214 final ExtendedCommunity ex = extendedCommunities.getExtendedCommunity();
215 if (ex instanceof AsSpecificExtendedCommunityCase) {
216 final AsSpecificExtendedCommunity asSpecificExtendedCommunity = ((AsSpecificExtendedCommunityCase) ex).getAsSpecificExtendedCommunity();
217 ByteBufWriteUtil.writeUnsignedShort(asSpecificExtendedCommunity.getGlobalAdministrator().getValue().intValue(), buffer);
218 buffer.writeBytes(asSpecificExtendedCommunity.getLocalAdministrator());
219 } else if (ex instanceof Inet4SpecificExtendedCommunityCase) {
220 final Inet4SpecificExtendedCommunity inet4SpecificExtendedCommunity = ((Inet4SpecificExtendedCommunityCase) ex).getInet4SpecificExtendedCommunity();
221 ByteBufWriteUtil.writeIpv4Address(inet4SpecificExtendedCommunity.getGlobalAdministrator(), buffer);
222 buffer.writeBytes(inet4SpecificExtendedCommunity.getLocalAdministrator());
223 } else if (ex instanceof OpaqueExtendedCommunityCase) {
224 final OpaqueExtendedCommunity opaqueExtendedCommunity = ((OpaqueExtendedCommunityCase) ex).getOpaqueExtendedCommunity();
225 buffer.writeBytes(opaqueExtendedCommunity.getValue());
226 } else if (ex instanceof RouteTargetExtendedCommunityCase) {
227 final RouteTargetExtendedCommunity routeTarget = ((RouteTargetExtendedCommunityCase) ex).getRouteTargetExtendedCommunity();
228 ByteBufWriteUtil.writeUnsignedShort(routeTarget.getGlobalAdministrator().getValue().intValue(), buffer);
229 buffer.writeBytes(routeTarget.getLocalAdministrator());
230 } else if (ex instanceof RouteOriginExtendedCommunityCase) {
231 final RouteOriginExtendedCommunity routeOriginExtendedCommunity = ((RouteOriginExtendedCommunityCase) ex).getRouteOriginExtendedCommunity();
232 ByteBufWriteUtil.writeUnsignedShort(routeOriginExtendedCommunity.getGlobalAdministrator().getValue().intValue(), buffer);
233 buffer.writeBytes(routeOriginExtendedCommunity.getLocalAdministrator());