BUG-731 : more warnings down
[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.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.primitives.UnsignedBytes;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPError;
19 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
20 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
21 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.protocol.util.Ipv4Util;
24 import org.opendaylight.protocol.util.ReferenceCache;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunitiesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunity;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.as.specific.extended.community._case.AsSpecificExtendedCommunityBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunity;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunity;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunityBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunity;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.origin.extended.community._case.RouteOriginExtendedCommunityBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunity;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.route.target.extended.community._case.RouteTargetExtendedCommunityBuilder;
51 import org.opendaylight.yangtools.yang.binding.DataObject;
52
53 public final class ExtendedCommunitiesAttributeParser implements AttributeParser,AttributeSerializer {
54
55     public static final int TYPE = 16;
56
57     private static final int EXTENDED_COMMUNITY_LENGTH = 8;
58
59     private static final int AS_LOCAL_ADMIN_LENGTH = 4;
60
61     private static final int INET_LOCAL_ADMIN_LENGTH = 2;
62
63     private static final short AS_TYPE_TRANS = 0;
64
65     private static final short AS_TYPE_NON_TRANS = 40;
66
67     private static final short INET_TYPE_TRANS = 1;
68
69     private static final short INET_TYPE_NON_TRANS = 41;
70
71     private static final short OPAQUE_TYPE_TRANS = 3;
72
73     private static final short OPAQUE_TYPE_NON_TRANS = 43;
74
75     private static final short ROUTE_TYPE_ONLY = 2;
76
77     private static final short ROUTE_TARGET_SUBTYPE = 2;
78
79     private static final short ROUTE_ORIGIN_SUBTYPE = 3;
80
81     private final ReferenceCache refCache;
82
83     public ExtendedCommunitiesAttributeParser(final ReferenceCache refCache) {
84         this.refCache = Preconditions.checkNotNull(refCache);
85     }
86
87     @Override
88     public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
89         final List<ExtendedCommunities> set = new ArrayList<>();
90         while (buffer.isReadable()) {
91             final ExtendedCommunities comm = parseExtendedCommunity(this.refCache, buffer.slice(buffer.readerIndex(), EXTENDED_COMMUNITY_LENGTH));
92             buffer.skipBytes(EXTENDED_COMMUNITY_LENGTH);
93             set.add(comm);
94         }
95         builder.setExtendedCommunities(set);
96     }
97
98     /**
99      * Parse Extended Community according to their type.
100      *
101      * @param bytes byte array to be parsed
102      * @return new Specific Extended Community
103      * @throws BGPDocumentedException if the type is not recognized
104      */
105     @VisibleForTesting
106     public static ExtendedCommunities parseExtendedCommunity(final ReferenceCache refCache, final ByteBuf buffer)
107             throws BGPDocumentedException {
108         final ExtendedCommunitiesBuilder comm = new ExtendedCommunitiesBuilder();
109         comm.setCommType((short) UnsignedBytes.toInt(buffer.readByte()));
110         comm.setCommSubType((short) UnsignedBytes.toInt(buffer.readByte()));
111         ExtendedCommunity c = null;
112         switch (comm.getCommType()) {
113         case AS_TYPE_TRANS:
114             c = parseAsTransCommunity(comm, buffer);
115             break;
116         case AS_TYPE_NON_TRANS:
117             ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
118             byte[] value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
119             c = new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
120                 new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
121             break;
122         case ROUTE_TYPE_ONLY:
123             as = new ShortAsNumber((long) buffer.readUnsignedShort());
124             value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
125             if (comm.getCommSubType() == ROUTE_TARGET_SUBTYPE) {
126                 c = new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
127             } else if (comm.getCommSubType() == ROUTE_ORIGIN_SUBTYPE) {
128                 c = new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
129             } else {
130                 throw new BGPDocumentedException("Could not parse Extended Community subtype: " + comm.getCommSubType(), BGPError.OPT_ATTR_ERROR);
131             }
132             break;
133         case INET_TYPE_TRANS:
134             c = parseInetTypeCommunity(comm, buffer);
135             break;
136         case INET_TYPE_NON_TRANS:
137             c = new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
138                 new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
139                         Ipv4Util.addressForByteBuf(buffer)).setLocalAdministrator(
140                         ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build();
141             break;
142         case OPAQUE_TYPE_TRANS:
143             c = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(ByteArray.readAllBytes(buffer)).build()).build();
144             break;
145         case OPAQUE_TYPE_NON_TRANS:
146             c = new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(ByteArray.readAllBytes(buffer)).build()).build();
147             break;
148         default:
149             throw new BGPDocumentedException("Could not parse Extended Community type: " + comm.getCommType(), BGPError.OPT_ATTR_ERROR);
150         }
151         return comm.setExtendedCommunity(c).build();
152     }
153
154     private static ExtendedCommunity parseAsTransCommunity(final ExtendedCommunitiesBuilder comm, final ByteBuf buffer) {
155         final ShortAsNumber as = new ShortAsNumber((long) buffer.readUnsignedShort());
156         final byte[] value = ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH);
157         if (comm.getCommSubType() == ROUTE_TARGET_SUBTYPE) {
158             return new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
159                 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
160         }
161         if (comm.getCommSubType() == ROUTE_ORIGIN_SUBTYPE) {
162             return new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
163                 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
164         }
165         return new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
166             new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(as).setLocalAdministrator(value).build()).build();
167     }
168
169     private static ExtendedCommunity parseInetTypeCommunity(final ExtendedCommunitiesBuilder comm, final ByteBuf buffer) {
170         if (comm.getCommSubType() == ROUTE_TARGET_SUBTYPE) {
171             return new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
172                 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
173                     .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build();
174         }
175         if (comm.getCommSubType() == ROUTE_ORIGIN_SUBTYPE) {
176             return new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
177                 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber((long) buffer.readUnsignedShort()))
178                     .setLocalAdministrator(ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build();
179         }
180         return new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
181             new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
182                     Ipv4Util.addressForByteBuf(buffer)).setLocalAdministrator(
183                     ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build();
184     }
185
186     @Override
187     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
188         Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
189         final List<ExtendedCommunities> communitiesList = ((PathAttributes) attribute).getExtendedCommunities();
190         if (communitiesList == null || communitiesList.isEmpty()) {
191             return;
192         }
193         final ByteBuf extendedCommunitiesBuffer = Unpooled.buffer();
194         for (final ExtendedCommunities extendedCommunities : communitiesList) {
195             serializeExtendedCommunity(extendedCommunities, extendedCommunitiesBuffer);
196             AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, extendedCommunitiesBuffer, byteAggregator);
197         }
198     }
199
200     @VisibleForTesting
201     public static void serializeExtendedCommunity(final ExtendedCommunities extendedCommunities, final ByteBuf extendedCommunitiesBuffer) {
202         if (extendedCommunities.getCommType() != null) {
203             extendedCommunitiesBuffer.writeByte(extendedCommunities.getCommType());
204         }
205         if (extendedCommunities.getCommSubType() != null) {
206             extendedCommunitiesBuffer.writeByte(extendedCommunities.getCommSubType());
207         }
208         final ExtendedCommunity ex = extendedCommunities.getExtendedCommunity();
209         if (ex instanceof AsSpecificExtendedCommunityCase) {
210             final AsSpecificExtendedCommunity asSpecificExtendedCommunity = ((AsSpecificExtendedCommunityCase) ex).getAsSpecificExtendedCommunity();
211             extendedCommunitiesBuffer.writeShort(asSpecificExtendedCommunity.getGlobalAdministrator().getValue().shortValue());
212             extendedCommunitiesBuffer.writeBytes(asSpecificExtendedCommunity.getLocalAdministrator());
213         }
214         else if (ex instanceof Inet4SpecificExtendedCommunityCase) {
215             final Inet4SpecificExtendedCommunity inet4SpecificExtendedCommunity = ((Inet4SpecificExtendedCommunityCase) ex).getInet4SpecificExtendedCommunity();
216             extendedCommunitiesBuffer.writeBytes(Ipv4Util.bytesForAddress(inet4SpecificExtendedCommunity.getGlobalAdministrator()));
217             extendedCommunitiesBuffer.writeBytes(inet4SpecificExtendedCommunity.getLocalAdministrator());
218         }
219         else if (ex instanceof OpaqueExtendedCommunityCase) {
220             final OpaqueExtendedCommunity opaqueExtendedCommunity = ((OpaqueExtendedCommunityCase) ex).getOpaqueExtendedCommunity();
221             extendedCommunitiesBuffer.writeBytes(opaqueExtendedCommunity.getValue());
222         }
223         else if (ex instanceof RouteTargetExtendedCommunityCase) {
224             final RouteTargetExtendedCommunity routeTargetExtendedCommunity = ((RouteTargetExtendedCommunityCase) ex).getRouteTargetExtendedCommunity();
225             extendedCommunitiesBuffer.writeShort(routeTargetExtendedCommunity.getGlobalAdministrator().getValue().shortValue());
226             extendedCommunitiesBuffer.writeBytes(routeTargetExtendedCommunity.getLocalAdministrator());
227         }
228         else if (ex instanceof RouteOriginExtendedCommunityCase) {
229             final RouteOriginExtendedCommunity routeOriginExtendedCommunity = ((RouteOriginExtendedCommunityCase) ex).getRouteOriginExtendedCommunity();
230             extendedCommunitiesBuffer.writeShort(routeOriginExtendedCommunity.getGlobalAdministrator().getValue().shortValue());
231             extendedCommunitiesBuffer.writeBytes(routeOriginExtendedCommunity.getLocalAdministrator());
232         }
233     }
234 }