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 com.google.common.collect.Lists;
13 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.List;
18 import org.opendaylight.protocol.bgp.parser.AttributeFlags;
19 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
20 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
21 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
22 import org.opendaylight.protocol.concepts.Ipv4Util;
23 import org.opendaylight.protocol.util.ReferenceCache;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCase;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
34 public final class ExtendedCommunitiesAttributeParser implements AttributeParser,AttributeSerializer {
36 public static final int TYPE = 16;
38 private final ReferenceCache refCache;
40 public ExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
41 this.refCache = Preconditions.checkNotNull(refCache);
45 public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
46 final List<ExtendedCommunities> set = Lists.newArrayList();
47 while (buffer.isReadable()) {
48 final ExtendedCommunities comm = CommunitiesParser.parseExtendedCommunity(this.refCache, buffer.slice(buffer.readerIndex(), CommunitiesParser.EXTENDED_COMMUNITY_LENGTH));
49 buffer.skipBytes(CommunitiesParser.EXTENDED_COMMUNITY_LENGTH);
52 builder.setExtendedCommunities(set);
56 public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
57 PathAttributes pathAttributes = (PathAttributes) attribute;
58 List<ExtendedCommunities> communitiesList = pathAttributes.getExtendedCommunities();
59 if (communitiesList == null) {
62 ByteBuf extendedCommunitiesBuffer = Unpooled.buffer();
63 for (ExtendedCommunities extendedCommunities : communitiesList) {
64 if (extendedCommunities.getCommSubType() != null) {
65 extendedCommunitiesBuffer.writeShort(extendedCommunities.getCommSubType());
67 if (extendedCommunities.getExtendedCommunity() instanceof AsSpecificExtendedCommunityCase) {
68 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 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 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 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 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 byteAggregator.writeByte(AttributeFlags.OPTIONAL);
113 byteAggregator.writeByte(TYPE);
114 byteAggregator.writeByte(extendedCommunitiesBuffer.writerIndex());
115 byteAggregator.writeBytes(extendedCommunitiesBuffer);