5599a66ffb6d7b216568b74fbd97f90ba1c3b31b
[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 com.google.common.base.Preconditions;
12
13 import io.netty.buffer.ByteBuf;
14
15 import java.util.Arrays;
16 import java.util.List;
17
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPError;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
22 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
23 import org.opendaylight.protocol.concepts.Ipv4Util;
24 import org.opendaylight.protocol.util.ByteArray;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.NlriBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutesBuilder;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
36  *
37  * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
38  *
39  */
40 public class BGPUpdateMessageParser implements MessageParser {
41     public static final int TYPE = 2;
42
43     private static final Logger LOG = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
44
45     /**
46      * Size of the withdrawn_routes_length field, in bytes.
47      */
48     public static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
49
50     /**
51      * Size of the total_path_attr_length field, in bytes.
52      */
53     public static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
54
55     private final AttributeRegistry reg;
56
57     // Constructors -------------------------------------------------------
58     public BGPUpdateMessageParser(final AttributeRegistry reg) {
59         this.reg = Preconditions.checkNotNull(reg);
60     }
61
62     // Getters & setters --------------------------------------------------
63
64     @Override
65     public Update parseMessageBody(final ByteBuf buffer, final int messageLength) throws BGPDocumentedException {
66         Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty.");
67         LOG.trace("Started parsing of update message: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
68
69         final int withdrawnRoutesLength = buffer.readUnsignedShort();
70         final UpdateBuilder eventBuilder = new UpdateBuilder();
71
72         if (withdrawnRoutesLength > 0) {
73             final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util.prefixListForBytes(ByteArray.readBytes(buffer, withdrawnRoutesLength));
74             eventBuilder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(withdrawnRoutes).build());
75         }
76         final int totalPathAttrLength = buffer.readUnsignedShort();
77
78         if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
79             return eventBuilder.build();
80         }
81         if (totalPathAttrLength > 0) {
82             try {
83                 final PathAttributes pathAttributes = this.reg.parseAttributes(buffer.slice(buffer.readerIndex(), totalPathAttrLength));
84                 buffer.skipBytes(totalPathAttrLength);
85                 eventBuilder.setPathAttributes(pathAttributes);
86             } catch (final BGPParsingException | RuntimeException e) {
87                 // Catch everything else and turn it into a BGPDocumentedException
88                 LOG.warn("Could not parse BGP attributes", e);
89                 throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
90             }
91         }
92         final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(buffer));
93         if (nlri != null && !nlri.isEmpty()) {
94             eventBuilder.setNlri(new NlriBuilder().setNlri(nlri).build());
95         }
96         Update msg = eventBuilder.build();
97         LOG.debug("BGP Update message was parsed {}.", msg);
98         return msg;
99     }
100 }