Merge "Fix junit dependencies in poms. Reuse existing from parent, add missing ones."
[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.List;
13
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPError;
16 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
18 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
19 import org.opendaylight.protocol.concepts.Ipv4Util;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Update;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.UpdateBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.NlriBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.WithdrawnRoutesBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.base.Preconditions;
31
32 /**
33  * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
34  * 
35  * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
36  * 
37  */
38 public class BGPUpdateMessageParser implements MessageParser {
39         public static final int TYPE = 2;
40
41         private static Logger logger = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
42
43         /**
44          * Size of the withdrawn_routes_length field, in bytes.
45          */
46         public static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
47
48         /**
49          * Size of the total_path_attr_length field, in bytes.
50          */
51         public static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
52
53         private final AttributeRegistry reg;
54
55         // Constructors -------------------------------------------------------
56         public BGPUpdateMessageParser(final AttributeRegistry reg) {
57                 this.reg = Preconditions.checkNotNull(reg);
58         }
59
60         // Getters & setters --------------------------------------------------
61
62         @Override
63         public Update parseMessageBody(final byte[] body, final int messageLength) throws BGPDocumentedException {
64                 if (body == null || body.length == 0) {
65                         throw new IllegalArgumentException("Byte array cannot be null or empty.");
66                 }
67                 logger.trace("Started parsing of update message: {}", Arrays.toString(body));
68
69                 int byteOffset = 0;
70
71                 final int withdrawnRoutesLength = ByteArray.bytesToInt(ByteArray.subByte(body, byteOffset, WITHDRAWN_ROUTES_LENGTH_SIZE));
72                 byteOffset += WITHDRAWN_ROUTES_LENGTH_SIZE;
73
74                 final UpdateBuilder eventBuilder = new UpdateBuilder();
75
76                 if (withdrawnRoutesLength > 0) {
77                         final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util.prefixListForBytes(ByteArray.subByte(body, byteOffset, withdrawnRoutesLength));
78                         byteOffset += withdrawnRoutesLength;
79                         eventBuilder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(withdrawnRoutes).build());
80                 }
81
82                 final int totalPathAttrLength = ByteArray.bytesToInt(ByteArray.subByte(body, byteOffset, TOTAL_PATH_ATTR_LENGTH_SIZE));
83                 byteOffset += TOTAL_PATH_ATTR_LENGTH_SIZE;
84
85                 if (withdrawnRoutesLength + totalPathAttrLength > body.length) {
86                         throw new BGPDocumentedException("Message length inconsistent with withdrawn router length.", BGPError.MALFORMED_ATTR_LIST);
87                 }
88
89                 if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
90                         return eventBuilder.build();
91                 }
92
93                 if (totalPathAttrLength > 0) {
94                         try {
95                                 final PathAttributes pathAttributes = reg.parseAttributes(ByteArray.subByte(body, byteOffset, totalPathAttrLength));
96                                 byteOffset += totalPathAttrLength;
97                                 eventBuilder.setPathAttributes(pathAttributes);
98                         } catch (final BGPParsingException e) {
99                                 logger.warn("Could not parse BGP attributes", e);
100                                 throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
101                         }
102                 }
103
104                 final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.subByte(body, byteOffset, body.length - byteOffset));
105                 if (nlri != null && !nlri.isEmpty()) {
106                         eventBuilder.setNlri(new NlriBuilder().setNlri(nlri).build());
107                 }
108                 logger.trace("Update message was parsed.");
109                 return eventBuilder.build();
110         }
111 }