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 java.util.List;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
19 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
20 import org.opendaylight.protocol.concepts.Ipv4Util;
21 import org.opendaylight.protocol.util.ReferenceCache;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCase;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
32 public final class ExtendedCommunitiesAttributeParser implements AttributeParser,AttributeSerializer {
34 public static final int TYPE = 16;
36 private final ReferenceCache refCache;
38 public ExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
39 this.refCache = Preconditions.checkNotNull(refCache);
43 public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
44 final List<ExtendedCommunities> set = Lists.newArrayList();
45 while (buffer.isReadable()) {
46 final ExtendedCommunities comm = CommunitiesParser.parseExtendedCommunity(this.refCache, buffer.slice(buffer.readerIndex(), CommunitiesParser.EXTENDED_COMMUNITY_LENGTH));
47 buffer.skipBytes(CommunitiesParser.EXTENDED_COMMUNITY_LENGTH);
50 builder.setExtendedCommunities(set);
54 public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
55 PathAttributes pathAttributes = (PathAttributes) attribute;
56 List<ExtendedCommunities> communitiesList = pathAttributes.getExtendedCommunities();
57 if (communitiesList == null) {
60 for (ExtendedCommunities extendedCommunities : communitiesList) {
61 if (extendedCommunities.getCommSubType() != null) {
62 byteAggregator.writeShort(extendedCommunities.getCommSubType());
64 if (extendedCommunities.getExtendedCommunity() instanceof AsSpecificExtendedCommunityCase) {
65 AsSpecificExtendedCommunityCase asSpecificExtendedCommunity = (AsSpecificExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
67 //TODO resolve types correctly
68 byteAggregator.writeByte(0);
69 byteAggregator.writeByte(1);
71 byteAggregator.writeShort(asSpecificExtendedCommunity.getAsSpecificExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
72 byteAggregator.writeBytes(asSpecificExtendedCommunity.getAsSpecificExtendedCommunity().getLocalAdministrator());
74 if (extendedCommunities.getExtendedCommunity() instanceof Inet4SpecificExtendedCommunityCase) {
75 Inet4SpecificExtendedCommunityCase inet4SpecificExtendedCommunity = (Inet4SpecificExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
77 //TODO resolve types correctly
78 byteAggregator.writeByte(1);
79 byteAggregator.writeByte(4);
81 byteAggregator.writeBytes(Ipv4Util.bytesForAddress(inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunity().getGlobalAdministrator()));
82 byteAggregator.writeBytes(inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunity().getLocalAdministrator());
84 if (extendedCommunities.getExtendedCommunity() instanceof OpaqueExtendedCommunityCase) {
85 OpaqueExtendedCommunityCase opaqueExtendedCommunity = (OpaqueExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
86 //TODO resolve types correctly
87 byteAggregator.writeByte(3);
88 byteAggregator.writeByte(4);
90 byteAggregator.writeBytes(opaqueExtendedCommunity.getOpaqueExtendedCommunity().getValue());
92 if (extendedCommunities.getExtendedCommunity() instanceof RouteTargetExtendedCommunityCase) {
93 RouteTargetExtendedCommunityCase routeTargetExtendedCommunity = (RouteTargetExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
94 //TODO how to determine, which numbering space global administrator number is originated from
95 byteAggregator.writeByte(0);
96 byteAggregator.writeByte(2);
98 byteAggregator.writeShort(routeTargetExtendedCommunity.getRouteTargetExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
99 byteAggregator.writeBytes(routeTargetExtendedCommunity.getRouteTargetExtendedCommunity().getLocalAdministrator());
101 if (extendedCommunities.getExtendedCommunity() instanceof RouteOriginExtendedCommunityCase) {
102 RouteOriginExtendedCommunityCase routeOriginExtendedCommunity = (RouteOriginExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
103 //TODO how to determine, which numbering space global administrator number is originated from
104 byteAggregator.writeByte(2);
105 byteAggregator.writeByte(3);
106 byteAggregator.writeShort(routeOriginExtendedCommunity.getRouteOriginExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
107 byteAggregator.writeBytes(routeOriginExtendedCommunity.getRouteOriginExtendedCommunity().getLocalAdministrator());