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.spi.AttributeParser;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
19 import org.opendaylight.protocol.util.Ipv4Util;
20 import org.opendaylight.protocol.util.ReferenceCache;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
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.update.PathAttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCase;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
31 public final class ExtendedCommunitiesAttributeParser implements AttributeParser,AttributeSerializer {
33 public static final int TYPE = 16;
35 private static final int EXTENDED_COMMUNITY_LENGTH = 8;
37 private final ReferenceCache refCache;
39 public ExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
40 this.refCache = Preconditions.checkNotNull(refCache);
44 public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
45 final List<ExtendedCommunities> set = new ArrayList<>();
46 while (buffer.isReadable()) {
47 final ExtendedCommunities comm = CommunitiesParser.parseExtendedCommunity(this.refCache, buffer.slice(buffer.readerIndex(), EXTENDED_COMMUNITY_LENGTH));
48 buffer.skipBytes(EXTENDED_COMMUNITY_LENGTH);
51 builder.setExtendedCommunities(set);
55 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
56 Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
57 final PathAttributes pathAttributes = (PathAttributes) attribute;
58 final List<ExtendedCommunities> communitiesList = pathAttributes.getExtendedCommunities();
59 if (communitiesList == null || communitiesList.isEmpty()) {
62 final ByteBuf extendedCommunitiesBuffer = Unpooled.buffer();
63 for (final ExtendedCommunities extendedCommunities : communitiesList) {
64 if (extendedCommunities.getCommSubType() != null) {
65 extendedCommunitiesBuffer.writeShort(extendedCommunities.getCommSubType());
67 if (extendedCommunities.getExtendedCommunity() instanceof AsSpecificExtendedCommunityCase) {
68 final AsSpecificExtendedCommunityCase asSpecificExtendedCommunity = (AsSpecificExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
70 //TODO resolve types correctly
71 extendedCommunitiesBuffer.writeByte(0);
72 extendedCommunitiesBuffer.writeByte(1);
74 extendedCommunitiesBuffer.writeShort(asSpecificExtendedCommunity.getAsSpecificExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
75 extendedCommunitiesBuffer.writeBytes(asSpecificExtendedCommunity.getAsSpecificExtendedCommunity().getLocalAdministrator());
77 if (extendedCommunities.getExtendedCommunity() instanceof Inet4SpecificExtendedCommunityCase) {
78 final Inet4SpecificExtendedCommunityCase inet4SpecificExtendedCommunity = (Inet4SpecificExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
80 //TODO resolve types correctly
81 extendedCommunitiesBuffer.writeByte(1);
82 extendedCommunitiesBuffer.writeByte(4);
84 extendedCommunitiesBuffer.writeBytes(Ipv4Util.bytesForAddress(inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunity().getGlobalAdministrator()));
85 extendedCommunitiesBuffer.writeBytes(inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunity().getLocalAdministrator());
87 if (extendedCommunities.getExtendedCommunity() instanceof OpaqueExtendedCommunityCase) {
88 final OpaqueExtendedCommunityCase opaqueExtendedCommunity = (OpaqueExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
89 //TODO resolve types correctly
90 extendedCommunitiesBuffer.writeByte(3);
91 extendedCommunitiesBuffer.writeByte(4);
93 extendedCommunitiesBuffer.writeBytes(opaqueExtendedCommunity.getOpaqueExtendedCommunity().getValue());
95 if (extendedCommunities.getExtendedCommunity() instanceof RouteTargetExtendedCommunityCase) {
96 final RouteTargetExtendedCommunityCase routeTargetExtendedCommunity = (RouteTargetExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
97 //TODO how to determine, which numbering space global administrator number is originated from
98 extendedCommunitiesBuffer.writeByte(0);
99 extendedCommunitiesBuffer.writeByte(2);
101 extendedCommunitiesBuffer.writeShort(routeTargetExtendedCommunity.getRouteTargetExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
102 extendedCommunitiesBuffer.writeBytes(routeTargetExtendedCommunity.getRouteTargetExtendedCommunity().getLocalAdministrator());
104 if (extendedCommunities.getExtendedCommunity() instanceof RouteOriginExtendedCommunityCase) {
105 final RouteOriginExtendedCommunityCase routeOriginExtendedCommunity = (RouteOriginExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
106 //TODO how to determine, which numbering space global administrator number is originated from
107 extendedCommunitiesBuffer.writeByte(2);
108 extendedCommunitiesBuffer.writeByte(3);
109 extendedCommunitiesBuffer.writeShort(routeOriginExtendedCommunity.getRouteOriginExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
110 extendedCommunitiesBuffer.writeBytes(routeOriginExtendedCommunity.getRouteOriginExtendedCommunity().getLocalAdministrator());
112 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, extendedCommunitiesBuffer, byteAggregator);