moved BGP Table type from concepts to parser-api.
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / BGPUpdateMessageParser.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
9 package org.opendaylight.protocol.bgp.parser.impl.message;
10
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPError;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.bgp.parser.BGPTableType;
20 import org.opendaylight.protocol.bgp.parser.BGPUpdateEvent;
21 import org.opendaylight.protocol.bgp.parser.BGPUpdateSynchronized;
22 import org.opendaylight.protocol.bgp.parser.impl.BGPMessageFactoryImpl;
23 import org.opendaylight.protocol.bgp.parser.impl.BGPUpdateEventBuilder;
24 import org.opendaylight.protocol.bgp.parser.impl.IPv6MP;
25 import org.opendaylight.protocol.bgp.parser.impl.PathAttribute;
26 import org.opendaylight.protocol.bgp.parser.impl.PathAttribute.TypeCode;
27 import org.opendaylight.protocol.bgp.parser.impl.message.update.PathAttributeParser;
28 import org.opendaylight.protocol.concepts.IPv4;
29 import org.opendaylight.protocol.concepts.IPv4Address;
30 import org.opendaylight.protocol.concepts.Prefix;
31 import org.opendaylight.protocol.util.ByteArray;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import com.google.common.collect.Lists;
40
41 /**
42  * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
43  * 
44  * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
45  * 
46  */
47 public class BGPUpdateMessageParser {
48
49         private static Logger logger = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
50
51         /**
52          * Size of the withdrawn_routes_length field, in bytes.
53          */
54         public static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
55
56         /**
57          * Size of the total_path_attr_length field, in bytes.
58          */
59         public static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
60
61         // Constructors -------------------------------------------------------
62
63         public BGPUpdateMessageParser() {
64
65         }
66
67         // Getters & setters --------------------------------------------------
68
69         public static BGPUpdateEvent parse(final byte[] bytes, final int msgLength) throws BGPDocumentedException {
70                 if (bytes == null || bytes.length == 0) {
71                         throw new IllegalArgumentException("Byte array cannot be null or empty.");
72                 }
73                 logger.trace("Started parsing of update message: {}", Arrays.toString(bytes));
74
75                 int byteOffset = 0;
76
77                 final int withdrawnRoutesLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, WITHDRAWN_ROUTES_LENGTH_SIZE));
78                 byteOffset += WITHDRAWN_ROUTES_LENGTH_SIZE;
79
80                 final BGPUpdateEventBuilder eventBuilder = new BGPUpdateEventBuilder();
81                 eventBuilder.setWithdrawnRoutesLength(withdrawnRoutesLength);
82
83                 Set<Prefix<IPv4Address>> withdrawnRoutes;
84                 if (withdrawnRoutesLength > 0) {
85                         withdrawnRoutes = IPv4.FAMILY.prefixListForBytes(ByteArray.subByte(bytes, byteOffset, withdrawnRoutesLength));
86                         byteOffset += withdrawnRoutesLength;
87                 } else {
88                         withdrawnRoutes = Collections.emptySet();
89                 }
90                 eventBuilder.setWithdrawnRoutes(withdrawnRoutes);
91
92                 final int totalPathAttrLength = ByteArray.bytesToInt(ByteArray.subByte(bytes, byteOffset, TOTAL_PATH_ATTR_LENGTH_SIZE));
93                 byteOffset += TOTAL_PATH_ATTR_LENGTH_SIZE;
94                 eventBuilder.setTotalPathAttrLength(totalPathAttrLength);
95
96                 if (withdrawnRoutesLength + totalPathAttrLength + BGPMessageFactoryImpl.COMMON_HEADER_LENGTH > msgLength) {
97                         throw new BGPDocumentedException("Message length inconsistent with withdrawn router length.", BGPError.MALFORMED_ATTR_LIST);
98                 }
99
100                 if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
101                         final BGPUpdateSynchronized event = new BGPUpdateSynchronized() {
102                                 @Override
103                                 public BGPTableType getTableType() {
104                                         return new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
105                                 }
106                         };
107                         return event;
108                 }
109
110                 List<PathAttribute> pathAttributes;
111                 if (totalPathAttrLength > 0) {
112                         pathAttributes = parsePathAttributes(ByteArray.subByte(bytes, byteOffset, totalPathAttrLength));
113                         byteOffset += totalPathAttrLength;
114                         if (pathAttributes.get(0).getType() == TypeCode.MP_UNREACH_NLRI && totalPathAttrLength == 6) {
115                                 if (pathAttributes.get(0).getValue() instanceof IPv6MP) {
116                                         final BGPUpdateEvent event = new BGPUpdateSynchronized() {
117                                                 @Override
118                                                 public BGPTableType getTableType() {
119                                                         return new BGPTableType(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class);
120                                                 }
121                                         };
122                                         return event;
123                                 } else if (pathAttributes.get(0).getValue() == null) {
124                                         final BGPUpdateSynchronized event = new BGPUpdateSynchronized() {
125                                                 @Override
126                                                 public BGPTableType getTableType() {
127                                                         return new BGPTableType(LinkstateAddressFamily.class, UnicastSubsequentAddressFamily.class);
128                                                 }
129                                         };
130                                         return event;
131                                 }
132                         }
133                 } else {
134                         pathAttributes = Collections.emptyList();
135                 }
136                 eventBuilder.setPathAttributes(pathAttributes);
137
138                 final Set<Prefix<IPv4Address>> nlri = IPv4.FAMILY.prefixListForBytes(ByteArray.subByte(bytes, byteOffset, bytes.length - byteOffset));
139                 eventBuilder.setNlri(nlri);
140
141                 try {
142                         logger.trace("Update message was parsed.");
143                         return eventBuilder.buildEvent();
144                 } catch (final BGPParsingException e) {
145                         throw new BGPDocumentedException("Parsing unsuccessful: {}" + e.getMessage(), BGPError.MALFORMED_ATTR_LIST);
146                 }
147         }
148
149         /**
150          * Parse different Path Attributes from given bytes.
151          * 
152          * @param bytes byte array to be parsed
153          * @return list of Path Attributes
154          * @throws BGPParsingException
155          */
156         private static List<PathAttribute> parsePathAttributes(byte[] bytes) throws BGPDocumentedException {
157                 if (bytes.length == 0) {
158                         return Collections.emptyList();
159                 }
160                 final List<PathAttribute> list = Lists.newArrayList();
161                 while (bytes.length != 0) {
162                         PathAttribute attr;
163                         try {
164                                 attr = PathAttributeParser.parseAttribute(bytes);
165                                 bytes = ByteArray.cutBytes(bytes,
166                                                 PathAttribute.ATTR_FLAGS_SIZE + PathAttribute.ATTR_TYPE_CODE_SIZE + attr.getAttrLengthSize() + attr.getLength());
167                                 list.add(attr);
168                         } catch (final BGPParsingException e) {
169                                 logger.warn("Could not parse BGP attributes: {}", e.getMessage(), e);
170                                 throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST);
171                         }
172                 }
173                 return list;
174         }
175 }