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