Replace Preconditions.CheckNotNull per RequireNonNull
[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 static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.List;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPError;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathAttributeParser;
21 import org.opendaylight.protocol.bgp.parser.impl.message.update.NextHopAttributeParser;
22 import org.opendaylight.protocol.bgp.parser.impl.message.update.OriginAttributeParser;
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.bgp.parser.spi.PeerSpecificParserConstraint;
28 import org.opendaylight.protocol.util.ByteArray;
29 import org.opendaylight.protocol.util.ByteBufWriteUtil;
30 import org.opendaylight.protocol.util.Ipv4Util;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.Nlri;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.NlriBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.WithdrawnRoutes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.WithdrawnRoutesBuilder;
39 import org.opendaylight.yangtools.yang.binding.Notification;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
45  *
46  * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
47  */
48 public final class BGPUpdateMessageParser implements MessageParser, MessageSerializer {
49
50     private static final Logger LOG = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
51
52     public static final int TYPE = 2;
53
54     private static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
55
56     private static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
57
58     private final AttributeRegistry reg;
59
60     public BGPUpdateMessageParser(final AttributeRegistry reg) {
61         this.reg = requireNonNull(reg);
62     }
63
64     @Override
65     public Update parseMessageBody(final ByteBuf buffer, final int messageLength) throws BGPDocumentedException {
66         return parseMessageBody(buffer, messageLength, null);
67     }
68
69     @Override
70     public void serializeMessage(final Notification message, final ByteBuf bytes) {
71         Preconditions.checkArgument(message instanceof Update, "Message needs to be of type Update");
72         final Update update = (Update) message;
73
74         final ByteBuf messageBody = Unpooled.buffer();
75         final WithdrawnRoutes withdrawnRoutes = update.getWithdrawnRoutes();
76         if (withdrawnRoutes != null) {
77             final ByteBuf withdrawnRoutesBuf = Unpooled.buffer();
78             for (final Ipv4Prefix prefix : withdrawnRoutes.getWithdrawnRoutes()) {
79                 ByteBufWriteUtil.writeMinimalPrefix(prefix, withdrawnRoutesBuf);
80             }
81             messageBody.writeShort(withdrawnRoutesBuf.writerIndex());
82             messageBody.writeBytes(withdrawnRoutesBuf);
83         } else {
84             messageBody.writeZero(WITHDRAWN_ROUTES_LENGTH_SIZE);
85         }
86         if (update.getAttributes() != null) {
87             final ByteBuf pathAttributesBuf = Unpooled.buffer();
88             this.reg.serializeAttribute(update.getAttributes(), pathAttributesBuf);
89             messageBody.writeShort(pathAttributesBuf.writerIndex());
90             messageBody.writeBytes(pathAttributesBuf);
91         } else {
92             messageBody.writeZero(TOTAL_PATH_ATTR_LENGTH_SIZE);
93         }
94         final Nlri nlri = update.getNlri();
95         if (nlri != null && nlri.getNlri() !=null) {
96             for (final Ipv4Prefix prefix : nlri.getNlri()) {
97                 ByteBufWriteUtil.writeMinimalPrefix(prefix, messageBody);
98             }
99         }
100         MessageUtil.formatMessage(TYPE, messageBody, bytes);
101     }
102
103     /**
104      * Parse Update message from buffer.
105      * Calls {@link #checkMandatoryAttributesPresence(Update)} to check for presence of mandatory attributes.
106      *
107      * @param buffer Encoded BGP message in ByteBuf
108      * @param messageLength Length of the BGP message
109      * @param constraint Peer specific constraints
110      * @return Parsed Update message body
111      * @throws BGPDocumentedException
112      */
113     @Override
114     public Update parseMessageBody(final ByteBuf buffer, final int messageLength, final PeerSpecificParserConstraint constraint)
115             throws BGPDocumentedException {
116         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Buffer cannot be null or empty.");
117
118         final UpdateBuilder builder = new UpdateBuilder();
119
120         final int withdrawnRoutesLength = buffer.readUnsignedShort();
121         if (withdrawnRoutesLength > 0) {
122             // TODO handle NLRI with multiple paths - requires modified yang data model
123             final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util.prefixListForBytes(ByteArray.readBytes(buffer, withdrawnRoutesLength));
124             builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(withdrawnRoutes).build());
125         }
126         final int totalPathAttrLength = buffer.readUnsignedShort();
127
128         if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
129             return builder.build();
130         }
131         if (totalPathAttrLength > 0) {
132             try {
133                 final Attributes attributes = this.reg.parseAttributes(buffer.readSlice(totalPathAttrLength), constraint);
134                 builder.setAttributes(attributes);
135             } catch (final RuntimeException | BGPParsingException e) {
136                 // Catch everything else and turn it into a BGPDocumentedException
137                 throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
138             }
139         }
140         final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(buffer));
141         if (!nlri.isEmpty()) {
142             // TODO handle NLRI with multiple paths - requires modified yang data model
143             builder.setNlri(new NlriBuilder().setNlri(nlri).build());
144         }
145         final Update msg = builder.build();
146         checkMandatoryAttributesPresence(msg);
147         LOG.debug("BGP Update message was parsed {}.", msg);
148         return msg;
149     }
150
151     /**
152      * Check for presence of well known mandatory path attributes
153      * ORIGIN, AS_PATH and NEXT_HOP in Update message
154      *
155      * @param message Update message
156      * @throws BGPDocumentedException
157      */
158     private static void checkMandatoryAttributesPresence(final Update message) throws BGPDocumentedException {
159         requireNonNull(message, "Update message cannot be null");
160
161         final Attributes attrs = message.getAttributes();
162
163         if (message.getNlri() != null) {
164             if (attrs == null || attrs.getCNextHop() == null) {
165                 throw new BGPDocumentedException(BGPError.MANDATORY_ATTR_MISSING_MSG + "NEXT_HOP",
166                         BGPError.WELL_KNOWN_ATTR_MISSING,
167                         new byte[] { NextHopAttributeParser.TYPE });
168             }
169         }
170
171         if (MessageUtil.isAnyNlriPresent(message)) {
172             if (attrs == null || attrs.getOrigin() == null) {
173                 throw new BGPDocumentedException(BGPError.MANDATORY_ATTR_MISSING_MSG + "ORIGIN",
174                         BGPError.WELL_KNOWN_ATTR_MISSING,
175                         new byte[] { OriginAttributeParser.TYPE });
176             }
177
178             if (attrs == null || attrs.getAsPath() == null) {
179                 throw new BGPDocumentedException(BGPError.MANDATORY_ATTR_MISSING_MSG + "AS_PATH",
180                         BGPError.WELL_KNOWN_ATTR_MISSING,
181                         new byte[] { AsPathAttributeParser.TYPE });
182             }
183         }
184     }
185 }