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