582f7acd106936f2660f8eb3c473ce229a9c6079
[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 package org.opendaylight.protocol.bgp.parser.impl.message;
9
10
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
16 import org.opendaylight.protocol.bgp.parser.BGPError;
17 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
19 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
20 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
21 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.protocol.util.Ipv4Util;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.Nlri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.NlriBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.WithdrawnRoutes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.WithdrawnRoutesBuilder;
32 import org.opendaylight.yangtools.yang.binding.Notification;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
38  *
39  * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
40  */
41 public final class BGPUpdateMessageParser implements MessageParser, MessageSerializer {
42
43     private static final Logger LOG = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
44
45     public static final int TYPE = 2;
46
47     private static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
48
49     private static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
50
51     private final AttributeRegistry reg;
52
53     public BGPUpdateMessageParser(final AttributeRegistry reg) {
54         this.reg = Preconditions.checkNotNull(reg);
55     }
56
57     @Override
58     public Update parseMessageBody(final ByteBuf buffer, final int messageLength) throws BGPDocumentedException {
59         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Buffer cannot be null or empty.");
60
61         final UpdateBuilder builder = new UpdateBuilder();
62
63         final int withdrawnRoutesLength = buffer.readUnsignedShort();
64         if (withdrawnRoutesLength > 0) {
65             final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util.prefixListForBytes(ByteArray.readBytes(buffer, withdrawnRoutesLength));
66             builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(withdrawnRoutes).build());
67         }
68         final int totalPathAttrLength = buffer.readUnsignedShort();
69
70         if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
71             return builder.build();
72         }
73         if (totalPathAttrLength > 0) {
74             try {
75                 final Attributes pathAttributes = this.reg.parseAttributes(buffer.readSlice(totalPathAttrLength));
76                 builder.setAttributes(pathAttributes);
77             } catch (final BGPParsingException | RuntimeException e) {
78                 // Catch everything else and turn it into a BGPDocumentedException
79                 LOG.warn("Could not parse BGP attributes", e);
80                 throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
81             }
82         }
83         final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(buffer));
84         if (nlri != null && !nlri.isEmpty()) {
85             builder.setNlri(new NlriBuilder().setNlri(nlri).build());
86         }
87         final Update msg = builder.build();
88         LOG.debug("BGP Update message was parsed {}.", msg);
89         return msg;
90     }
91
92     @Override
93     public void serializeMessage(final Notification message, final ByteBuf bytes) {
94         Preconditions.checkArgument(message instanceof Update, "Message needs to be of type Update");
95         final Update update = (Update) message;
96
97         final ByteBuf messageBody = Unpooled.buffer();
98         final WithdrawnRoutes withdrawnRoutes = update.getWithdrawnRoutes();
99         if (withdrawnRoutes != null) {
100             final ByteBuf withdrawnRoutesBuf = Unpooled.buffer();
101             for (final Ipv4Prefix prefix : withdrawnRoutes.getWithdrawnRoutes()) {
102                 withdrawnRoutesBuf.writeBytes(Ipv4Util.bytesForPrefixBegin(prefix));
103             }
104             messageBody.writeShort(withdrawnRoutesBuf.writerIndex());
105             messageBody.writeBytes(withdrawnRoutesBuf);
106         } else {
107             messageBody.writeZero(WITHDRAWN_ROUTES_LENGTH_SIZE);
108         }
109         if (update.getAttributes() != null) {
110             final ByteBuf pathAttributesBuf = Unpooled.buffer();
111             this.reg.serializeAttribute(update.getAttributes(), pathAttributesBuf);
112             messageBody.writeShort(pathAttributesBuf.writerIndex());
113             messageBody.writeBytes(pathAttributesBuf);
114         } else {
115             messageBody.writeZero(TOTAL_PATH_ATTR_LENGTH_SIZE);
116         }
117         final Nlri nlri = update.getNlri();
118         if (nlri != null) {
119             for (final Ipv4Prefix prefix : nlri.getNlri()) {
120                 messageBody.writeBytes(Ipv4Util.bytesForPrefixBegin(prefix));
121             }
122         }
123         MessageUtil.formatMessage(TYPE, messageBody, bytes);
124     }
125 }