38ec9e7759ab8819a27e30ce373eb2d227435bab
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / CommunitiesParser.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.primitives.UnsignedBytes;
12
13 import io.netty.buffer.ByteBuf;
14
15 import java.util.Arrays;
16
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPError;
19 import org.opendaylight.protocol.concepts.Ipv4Util;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.protocol.util.ReferenceCache;
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.path.attributes.ExtendedCommunitiesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.AsSpecificExtendedCommunityCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.Inet4SpecificExtendedCommunityCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.OpaqueExtendedCommunityCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteOriginExtendedCommunityCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.RouteTargetExtendedCommunityCaseBuilder;
31 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;
32 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;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.extended.community.opaque.extended.community._case.OpaqueExtendedCommunityBuilder;
34 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;
35 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;
36
37 /**
38  * Parser for Extended Communities Path Attribute.
39  */
40 public final class CommunitiesParser {
41
42     protected static final int EXTENDED_COMMUNITY_LENGTH = 8;
43
44     protected static final int COMMUNITY_LENGTH = 4;
45
46     private static final int AS_LOCAL_ADMIN_LENGTH = 4;
47
48     private static final int INET_LOCAL_ADMIN_LENGTH = 2;
49
50     protected static final short AS_TYPE_TRANS = 0;
51
52     protected static final short AS_TYPE_NON_TRANS = 40;
53
54     protected static final short INET_TYPE_TRANS = 1;
55
56     protected static final short INET_TYPE_NON_TRANS = 41;
57
58     protected static final short OPAQUE_TYPE_TRANS = 3;
59
60     protected static final short OPAQUE_TYPE_NON_TRANS = 43;
61
62     protected static final short ROUTE_TYPE_ONLY = 2;
63
64     protected static final short ROUTE_TARGET_SUBTYPE = 2;
65
66     protected static final short ROUTE_ORIGIN_SUBTYPE = 3;
67
68     private static final byte[] NO_EXPORT = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x01 };
69
70     private static final byte[] NO_ADVERTISE = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x02 };
71
72     private static final byte[] NO_EXPORT_SUBCONFED = new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x03 };
73
74     private CommunitiesParser() {
75
76     }
77
78     /**
79      * Parse known Community, if unknown, a new one will be created.
80      *
81      * @param refCache
82      *
83      * @param bytes byte array to be parsed
84      * @return new Community
85      * @throws BGPDocumentedException
86      */
87     static Community parseCommunity(final ReferenceCache refCache, final ByteBuf buffer) throws BGPDocumentedException {
88         if (buffer.readableBytes() != COMMUNITY_LENGTH) {
89             throw new BGPDocumentedException("Community with wrong length: " + buffer.readableBytes(), BGPError.OPT_ATTR_ERROR);
90         }
91         byte[] body = ByteArray.getBytes(buffer, COMMUNITY_LENGTH);
92         if (Arrays.equals(body, NO_EXPORT)) {
93             return CommunityUtil.NO_EXPORT;
94         } else if (Arrays.equals(body, NO_ADVERTISE)) {
95             return CommunityUtil.NO_ADVERTISE;
96         } else if (Arrays.equals(body, NO_EXPORT_SUBCONFED)) {
97             return CommunityUtil.NO_EXPORT_SUBCONFED;
98         }
99         return CommunityUtil.create(refCache, buffer.readUnsignedShort(), buffer.readUnsignedShort());
100     }
101
102     /**
103      * Parse Extended Community according to their type.
104      *
105      * @param bytes byte array to be parsed
106      * @return new Specific Extended Community
107      * @throws BGPDocumentedException if the type is not recognized
108      */
109     @VisibleForTesting
110     public static ExtendedCommunities parseExtendedCommunity(final ReferenceCache refCache, final ByteBuf buffer)
111             throws BGPDocumentedException {
112         final int type = UnsignedBytes.toInt(buffer.readByte());
113         final int subType = UnsignedBytes.toInt(buffer.readByte());
114
115         final ExtendedCommunitiesBuilder comm = new ExtendedCommunitiesBuilder();
116         switch (type) {
117         case AS_TYPE_TRANS:
118             comm.setCommType(AS_TYPE_TRANS);
119             if (subType == ROUTE_TARGET_SUBTYPE) {
120                 comm.setCommSubType(ROUTE_TARGET_SUBTYPE).setExtendedCommunity(
121                         new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
122                                 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(
123                                         new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
124                                         ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
125             } else if (subType == ROUTE_ORIGIN_SUBTYPE) {
126                 comm.setCommSubType(ROUTE_ORIGIN_SUBTYPE).setExtendedCommunity(
127                         new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
128                                 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(
129                                         new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
130                                         ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
131             } else {
132                 comm.setExtendedCommunity(new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
133                         new AsSpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
134                                 new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
135                                 ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
136             }
137             break;
138         case AS_TYPE_NON_TRANS:
139             comm.setCommType(AS_TYPE_NON_TRANS).setExtendedCommunity(
140                     new AsSpecificExtendedCommunityCaseBuilder().setAsSpecificExtendedCommunity(
141                             new AsSpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
142                                     new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
143                                     ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
144             break;
145         case ROUTE_TYPE_ONLY:
146             comm.setCommType(ROUTE_TYPE_ONLY);
147             if (subType == ROUTE_TARGET_SUBTYPE) {
148                 comm.setCommSubType(ROUTE_TARGET_SUBTYPE).setExtendedCommunity(
149                         new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
150                                 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(
151                                         new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
152                                         ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
153             } else if (subType == ROUTE_ORIGIN_SUBTYPE) {
154                 comm.setCommSubType(ROUTE_ORIGIN_SUBTYPE).setExtendedCommunity(
155                         new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
156                                 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(
157                                         new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
158                                         ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
159             } else {
160                 throw new BGPDocumentedException("Could not parse Extended Community subtype: " + subType, BGPError.OPT_ATTR_ERROR);
161             }
162             break;
163         case INET_TYPE_TRANS:
164             comm.setCommType(INET_TYPE_TRANS);
165             if (subType == ROUTE_TARGET_SUBTYPE) {
166                 comm.setCommSubType(ROUTE_TARGET_SUBTYPE).setExtendedCommunity(
167                         new RouteTargetExtendedCommunityCaseBuilder().setRouteTargetExtendedCommunity(
168                                 new RouteTargetExtendedCommunityBuilder().setGlobalAdministrator(
169                                         new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
170                                         ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
171             } else if (subType == ROUTE_ORIGIN_SUBTYPE) {
172                 comm.setCommSubType(ROUTE_ORIGIN_SUBTYPE).setExtendedCommunity(
173                         new RouteOriginExtendedCommunityCaseBuilder().setRouteOriginExtendedCommunity(
174                                 new RouteOriginExtendedCommunityBuilder().setGlobalAdministrator(
175                                         new ShortAsNumber((long) buffer.readUnsignedShort())).setLocalAdministrator(
176                                         ByteArray.readBytes(buffer, AS_LOCAL_ADMIN_LENGTH)).build()).build());
177             } else {
178                 comm.setExtendedCommunity(new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
179                         new Inet4SpecificExtendedCommunityBuilder().setTransitive(false).setGlobalAdministrator(
180                                 Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))).setLocalAdministrator(
181                                 ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build());
182             }
183             break;
184         case INET_TYPE_NON_TRANS:
185             comm.setCommType(INET_TYPE_NON_TRANS).setExtendedCommunity(
186                     new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
187                             new Inet4SpecificExtendedCommunityBuilder().setTransitive(true).setGlobalAdministrator(
188                                     Ipv4Util.addressForBytes(ByteArray.readBytes(buffer, Ipv4Util.IP4_LENGTH))).setLocalAdministrator(
189                                     ByteArray.readBytes(buffer, INET_LOCAL_ADMIN_LENGTH)).build()).build());
190             break;
191         case OPAQUE_TYPE_TRANS:
192             comm.setCommType(OPAQUE_TYPE_TRANS).setExtendedCommunity(
193                     new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
194                             new OpaqueExtendedCommunityBuilder().setTransitive(false).setValue(ByteArray.readAllBytes(buffer)).build()).build());
195             break;
196         case OPAQUE_TYPE_NON_TRANS:
197             comm.setCommType(OPAQUE_TYPE_NON_TRANS).setExtendedCommunity(
198                     new OpaqueExtendedCommunityCaseBuilder().setOpaqueExtendedCommunity(
199                             new OpaqueExtendedCommunityBuilder().setTransitive(true).setValue(ByteArray.readAllBytes(buffer)).build()).build());
200             break;
201         default:
202             throw new BGPDocumentedException("Could not parse Extended Community type: " + type, BGPError.OPT_ATTR_ERROR);
203         }
204
205         return comm.build();
206     }
207 }