2cb3782ac9552de4285f6a7213e5795b3237e6cc
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / ExtendedCommunitiesAttributeParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.parser.impl.message.update;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12
13 import io.netty.buffer.ByteBuf;
14
15 import io.netty.buffer.Unpooled;
16 import java.util.List;
17
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;
33
34 public final class ExtendedCommunitiesAttributeParser implements AttributeParser,AttributeSerializer {
35
36     public static final int TYPE = 16;
37
38     private final ReferenceCache refCache;
39
40     public ExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
41         this.refCache = Preconditions.checkNotNull(refCache);
42     }
43
44     @Override
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);
50             set.add(comm);
51         }
52         builder.setExtendedCommunities(set);
53     }
54
55     @Override
56     public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
57         PathAttributes pathAttributes = (PathAttributes) attribute;
58         List<ExtendedCommunities> communitiesList = pathAttributes.getExtendedCommunities();
59         if (communitiesList == null) {
60             return;
61         }
62         ByteBuf extendedCommunitiesBuffer = Unpooled.buffer();
63         for (ExtendedCommunities extendedCommunities : communitiesList) {
64             if (extendedCommunities.getCommSubType() != null) {
65                 extendedCommunitiesBuffer.writeShort(extendedCommunities.getCommSubType());
66             }
67             if (extendedCommunities.getExtendedCommunity() instanceof AsSpecificExtendedCommunityCase) {
68                 AsSpecificExtendedCommunityCase asSpecificExtendedCommunity = (AsSpecificExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
69
70                 //TODO resolve types correctly
71                 extendedCommunitiesBuffer.writeByte(0);
72                 extendedCommunitiesBuffer.writeByte(1);
73
74                 extendedCommunitiesBuffer.writeShort(asSpecificExtendedCommunity.getAsSpecificExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
75                 extendedCommunitiesBuffer.writeBytes(asSpecificExtendedCommunity.getAsSpecificExtendedCommunity().getLocalAdministrator());
76             }
77             if (extendedCommunities.getExtendedCommunity() instanceof Inet4SpecificExtendedCommunityCase) {
78                 Inet4SpecificExtendedCommunityCase inet4SpecificExtendedCommunity = (Inet4SpecificExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
79
80                 //TODO resolve types correctly
81                 extendedCommunitiesBuffer.writeByte(1);
82                 extendedCommunitiesBuffer.writeByte(4);
83
84                 extendedCommunitiesBuffer.writeBytes(Ipv4Util.bytesForAddress(inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunity().getGlobalAdministrator()));
85                 extendedCommunitiesBuffer.writeBytes(inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunity().getLocalAdministrator());
86             }
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);
92
93                 extendedCommunitiesBuffer.writeBytes(opaqueExtendedCommunity.getOpaqueExtendedCommunity().getValue());
94             }
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);
100
101                 extendedCommunitiesBuffer.writeShort(routeTargetExtendedCommunity.getRouteTargetExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
102                 extendedCommunitiesBuffer.writeBytes(routeTargetExtendedCommunity.getRouteTargetExtendedCommunity().getLocalAdministrator());
103             }
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());
111             }
112             byteAggregator.writeByte(AttributeFlags.OPTIONAL);
113             byteAggregator.writeByte(TYPE);
114             byteAggregator.writeByte(extendedCommunitiesBuffer.writerIndex());
115             byteAggregator.writeBytes(extendedCommunitiesBuffer);
116         }
117     }
118 }