Moved Ipv4Util and Ipv6Util from concepts to util
[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 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import org.opendaylight.protocol.bgp.parser.AttributeFlags;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
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;
30
31 public final class ExtendedCommunitiesAttributeParser implements AttributeParser,AttributeSerializer {
32
33     public static final int TYPE = 16;
34
35     private final ReferenceCache refCache;
36
37     public ExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
38         this.refCache = Preconditions.checkNotNull(refCache);
39     }
40
41     @Override
42     public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
43         final List<ExtendedCommunities> set = Lists.newArrayList();
44         while (buffer.isReadable()) {
45             final ExtendedCommunities comm = CommunitiesParser.parseExtendedCommunity(this.refCache, buffer.slice(buffer.readerIndex(), CommunitiesParser.EXTENDED_COMMUNITY_LENGTH));
46             buffer.skipBytes(CommunitiesParser.EXTENDED_COMMUNITY_LENGTH);
47             set.add(comm);
48         }
49         builder.setExtendedCommunities(set);
50     }
51
52     @Override
53     public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
54         PathAttributes pathAttributes = (PathAttributes) attribute;
55         List<ExtendedCommunities> communitiesList = pathAttributes.getExtendedCommunities();
56         if (communitiesList == null) {
57             return;
58         }
59         ByteBuf extendedCommunitiesBuffer = Unpooled.buffer();
60         for (ExtendedCommunities extendedCommunities : communitiesList) {
61             if (extendedCommunities.getCommSubType() != null) {
62                 extendedCommunitiesBuffer.writeShort(extendedCommunities.getCommSubType());
63             }
64             if (extendedCommunities.getExtendedCommunity() instanceof AsSpecificExtendedCommunityCase) {
65                 AsSpecificExtendedCommunityCase asSpecificExtendedCommunity = (AsSpecificExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
66
67                 //TODO resolve types correctly
68                 extendedCommunitiesBuffer.writeByte(0);
69                 extendedCommunitiesBuffer.writeByte(1);
70
71                 extendedCommunitiesBuffer.writeShort(asSpecificExtendedCommunity.getAsSpecificExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
72                 extendedCommunitiesBuffer.writeBytes(asSpecificExtendedCommunity.getAsSpecificExtendedCommunity().getLocalAdministrator());
73             }
74             if (extendedCommunities.getExtendedCommunity() instanceof Inet4SpecificExtendedCommunityCase) {
75                 Inet4SpecificExtendedCommunityCase inet4SpecificExtendedCommunity = (Inet4SpecificExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
76
77                 //TODO resolve types correctly
78                 extendedCommunitiesBuffer.writeByte(1);
79                 extendedCommunitiesBuffer.writeByte(4);
80
81                 extendedCommunitiesBuffer.writeBytes(Ipv4Util.bytesForAddress(inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunity().getGlobalAdministrator()));
82                 extendedCommunitiesBuffer.writeBytes(inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunity().getLocalAdministrator());
83             }
84             if (extendedCommunities.getExtendedCommunity() instanceof OpaqueExtendedCommunityCase) {
85                 OpaqueExtendedCommunityCase opaqueExtendedCommunity = (OpaqueExtendedCommunityCase) extendedCommunities.getExtendedCommunity();
86                 //TODO resolve types correctly
87                 extendedCommunitiesBuffer.writeByte(3);
88                 extendedCommunitiesBuffer.writeByte(4);
89
90                 extendedCommunitiesBuffer.writeBytes(opaqueExtendedCommunity.getOpaqueExtendedCommunity().getValue());
91             }
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                 extendedCommunitiesBuffer.writeByte(0);
96                 extendedCommunitiesBuffer.writeByte(2);
97
98                 extendedCommunitiesBuffer.writeShort(routeTargetExtendedCommunity.getRouteTargetExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
99                 extendedCommunitiesBuffer.writeBytes(routeTargetExtendedCommunity.getRouteTargetExtendedCommunity().getLocalAdministrator());
100             }
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                 extendedCommunitiesBuffer.writeByte(2);
105                 extendedCommunitiesBuffer.writeByte(3);
106                 extendedCommunitiesBuffer.writeShort(routeOriginExtendedCommunity.getRouteOriginExtendedCommunity().getGlobalAdministrator().getValue().shortValue());
107                 extendedCommunitiesBuffer.writeBytes(routeOriginExtendedCommunity.getRouteOriginExtendedCommunity().getLocalAdministrator());
108             }
109             byteAggregator.writeByte(AttributeFlags.OPTIONAL);
110             byteAggregator.writeByte(TYPE);
111             byteAggregator.writeByte(extendedCommunitiesBuffer.writerIndex());
112             byteAggregator.writeBytes(extendedCommunitiesBuffer);
113         }
114     }
115 }