Bump versions to 0.21.6-SNAPSHOT
[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 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Streams;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Optional;
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.BGPTreatAsWithdrawException;
24 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
25 import org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathAttributeParser;
26 import org.opendaylight.protocol.bgp.parser.impl.message.update.NextHopAttributeParser;
27 import org.opendaylight.protocol.bgp.parser.impl.message.update.OriginAttributeParser;
28 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
29 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
30 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
31 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
32 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil;
33 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
34 import org.opendaylight.protocol.bgp.parser.spi.ParsedAttributes;
35 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
36 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
37 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
38 import org.opendaylight.protocol.util.Ipv4Util;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Update;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.UpdateBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.Nlri;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.NlriBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.WithdrawnRoutes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.update.message.WithdrawnRoutesBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesReach;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesUnreach;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.AttributesUnreachBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlri;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlri;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.graceful.restart.capability.TablesKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
57 import org.opendaylight.yangtools.yang.binding.Notification;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 /**
62  * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
63  *
64  * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
65  */
66 public final class BGPUpdateMessageParser implements MessageParser, MessageSerializer {
67     private static final Logger LOG = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
68
69     public static final int TYPE = 2;
70
71     private final AttributeRegistry attrReg;
72     private final NlriRegistry nlriReg;
73
74     public BGPUpdateMessageParser(final AttributeRegistry attrReg, final NlriRegistry nlriReg) {
75         this.attrReg = requireNonNull(attrReg);
76         this.nlriReg = requireNonNull(nlriReg);
77     }
78
79     @Override
80     public void serializeMessage(final Notification<?> message, final ByteBuf bytes) {
81         checkArgument(message instanceof Update, "Message needs to be of type Update");
82         final Update update = (Update) message;
83
84         final ByteBuf messageBody = Unpooled.buffer();
85         final List<WithdrawnRoutes> withdrawnRoutes = update.getWithdrawnRoutes();
86         if (withdrawnRoutes != null) {
87             final ByteBuf withdrawnRoutesBuf = Unpooled.buffer();
88             withdrawnRoutes.forEach(withdrawnRoute -> writePathIdPrefix(withdrawnRoutesBuf, withdrawnRoute.getPathId(),
89                     withdrawnRoute.getPrefix()));
90             messageBody.writeShort(withdrawnRoutesBuf.writerIndex());
91             messageBody.writeBytes(withdrawnRoutesBuf);
92         } else {
93             messageBody.writeShort(0);
94         }
95         if (update.getAttributes() != null) {
96             final ByteBuf pathAttributesBuf = Unpooled.buffer();
97             attrReg.serializeAttribute(update.getAttributes(), pathAttributesBuf);
98             messageBody.writeShort(pathAttributesBuf.writerIndex());
99             messageBody.writeBytes(pathAttributesBuf);
100         } else {
101             messageBody.writeShort(0);
102         }
103         final List<Nlri> nlris = update.getNlri();
104         if (nlris != null) {
105             nlris.forEach(nlri -> writePathIdPrefix(messageBody, nlri.getPathId(), nlri.getPrefix()));
106         }
107         MessageUtil.formatMessage(TYPE, messageBody, bytes);
108     }
109
110     private static void writePathIdPrefix(final ByteBuf byteBuf, final PathId pathId, final Ipv4Prefix ipv4Prefix) {
111         PathIdUtil.writePathId(pathId, byteBuf);
112         Ipv4Util.writeMinimalPrefix(ipv4Prefix, byteBuf);
113     }
114
115     /**
116      * Parse Update message from buffer. Calls {@link #checkMandatoryAttributesPresence(Update, RevisedErrorHandling)}
117      * to check for presence of mandatory attributes.
118      *
119      * @param buffer Encoded BGP message in ByteBuf
120      * @param messageLength Length of the BGP message
121      * @param constraint Peer specific constraints
122      * @return Parsed Update message body
123      */
124     @Override
125     public Update parseMessageBody(final ByteBuf buffer, final int messageLength,
126             final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
127         checkArgument(buffer != null && buffer.isReadable(),"Buffer cannot be null or empty.");
128
129         final UpdateBuilder builder = new UpdateBuilder();
130         final boolean isMultiPathSupported = MultiPathSupportUtil.isTableTypeSupported(constraint,
131                 new BgpTableTypeImpl(Ipv4AddressFamily.VALUE, UnicastSubsequentAddressFamily.VALUE));
132         final RevisedErrorHandling errorHandling = RevisedErrorHandling.from(constraint);
133
134         final int withdrawnRoutesLength = buffer.readUnsignedShort();
135         if (withdrawnRoutesLength > 0) {
136             final List<WithdrawnRoutes> withdrawnRoutes = new ArrayList<>();
137             final ByteBuf withdrawnRoutesBuffer = buffer.readSlice(withdrawnRoutesLength);
138             while (withdrawnRoutesBuffer.isReadable()) {
139                 final WithdrawnRoutesBuilder withdrawnRoutesBuilder = new WithdrawnRoutesBuilder();
140                 if (isMultiPathSupported) {
141                     withdrawnRoutesBuilder.setPathId(PathIdUtil.readPathId(withdrawnRoutesBuffer));
142                 }
143                 withdrawnRoutesBuilder.setPrefix(readPrefix(withdrawnRoutesBuffer, errorHandling, "Withdrawn Routes"));
144                 withdrawnRoutes.add(withdrawnRoutesBuilder.build());
145             }
146             builder.setWithdrawnRoutes(withdrawnRoutes);
147         }
148         final int totalPathAttrLength = buffer.readUnsignedShort();
149         if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
150             return builder.build();
151         }
152
153         Optional<BGPTreatAsWithdrawException> withdrawCauseOpt;
154         if (totalPathAttrLength > 0) {
155             final ParsedAttributes attributes = parseAttributes(buffer, totalPathAttrLength, constraint);
156             builder.setAttributes(attributes.getAttributes());
157             withdrawCauseOpt = attributes.getWithdrawCause();
158         } else {
159             withdrawCauseOpt = Optional.empty();
160         }
161
162         final List<Nlri> nlri = new ArrayList<>();
163         while (buffer.isReadable()) {
164             final NlriBuilder nlriBuilder = new NlriBuilder();
165             if (isMultiPathSupported) {
166                 nlriBuilder.setPathId(PathIdUtil.readPathId(buffer));
167             }
168             nlriBuilder.setPrefix(readPrefix(buffer, errorHandling, "NLRI"));
169             nlri.add(nlriBuilder.build());
170         }
171         if (!nlri.isEmpty()) {
172             builder.setNlri(nlri);
173         }
174
175         try {
176             checkMandatoryAttributesPresence(builder.build(), errorHandling);
177         } catch (BGPTreatAsWithdrawException e) {
178             LOG.debug("Well-known mandatory attributes missing", e);
179             if (withdrawCauseOpt.isPresent()) {
180                 final BGPTreatAsWithdrawException exception = withdrawCauseOpt.orElseThrow();
181                 exception.addSuppressed(e);
182                 withdrawCauseOpt = Optional.of(exception);
183             } else {
184                 withdrawCauseOpt = Optional.of(e);
185             }
186         }
187
188         Update msg = builder.build();
189         if (withdrawCauseOpt.isPresent()) {
190             // Attempt to apply treat-as-withdraw
191             msg = withdrawUpdate(msg, errorHandling, withdrawCauseOpt.orElseThrow());
192         }
193
194         LOG.debug("BGP Update message was parsed {}.", msg);
195         return msg;
196     }
197
198     @SuppressWarnings("checkstyle:illegalCatch")
199     private ParsedAttributes parseAttributes(final ByteBuf buffer, final int totalPathAttrLength,
200             final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
201         try {
202             return attrReg.parseAttributes(buffer.readSlice(totalPathAttrLength), constraint);
203         } catch (final RuntimeException | BGPParsingException e) {
204             // Catch everything else and turn it into a BGPDocumentedException
205             throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
206         }
207     }
208
209     private static Ipv4Prefix readPrefix(final ByteBuf buf, final RevisedErrorHandling errorHandling,
210             final String fieldName) throws BGPDocumentedException {
211         final int prefixLength = buf.readUnsignedByte();
212         if (errorHandling != RevisedErrorHandling.NONE) {
213             // https://tools.ietf.org/html/rfc7606#section-5.3
214             if (prefixLength > 32) {
215                 throw new BGPDocumentedException(fieldName + " length " + prefixLength + " exceeds 32 bytes",
216                     BGPError.ATTR_LENGTH_ERROR);
217             }
218             if (prefixLength > buf.readableBytes() * 8) {
219                 throw new BGPDocumentedException(fieldName + " length " + prefixLength
220                     + " exceeds unconsumed field space", BGPError.ATTR_LENGTH_ERROR);
221             }
222         }
223
224         return Ipv4Util.prefixForByteBuf(buf, prefixLength);
225     }
226
227     /**
228      * Check for presence of well known mandatory path attributes ORIGIN, AS_PATH and NEXT_HOP in Update message.
229      *
230      * @param message Update message
231      * @param errorHandling Error handling type
232      */
233     private static void checkMandatoryAttributesPresence(final Update message,
234             final RevisedErrorHandling errorHandling) throws BGPDocumentedException, BGPTreatAsWithdrawException {
235         requireNonNull(message, "Update message cannot be null");
236
237         final Attributes attrs = message.getAttributes();
238         if (message.getNlri() != null && (attrs == null || attrs.getCNextHop() == null)) {
239             throw reportMissingAttribute(errorHandling, "NEXT_HOP", NextHopAttributeParser.TYPE);
240         }
241
242         if (MessageUtil.isAnyNlriPresent(message)) {
243             if (attrs == null || attrs.getOrigin() == null) {
244                 throw reportMissingAttribute(errorHandling, "ORIGIN", OriginAttributeParser.TYPE);
245             }
246             if (attrs.getAsPath() == null) {
247                 throw reportMissingAttribute(errorHandling, "AS_PATH", AsPathAttributeParser.TYPE);
248             }
249         }
250     }
251
252     private static BGPDocumentedException reportMissingAttribute(final RevisedErrorHandling errorHandling,
253             final String attrName, final int attrType) throws BGPDocumentedException, BGPTreatAsWithdrawException {
254         return errorHandling.reportError(BGPError.WELL_KNOWN_ATTR_MISSING, new byte[] { (byte) attrType },
255             "Well known mandatory attribute missing: %s", attrName);
256     }
257
258     private Update withdrawUpdate(final Update parsed, final RevisedErrorHandling errorHandling,
259             final BGPTreatAsWithdrawException withdrawCause) throws BGPDocumentedException {
260         if (errorHandling == RevisedErrorHandling.NONE) {
261             throw new BGPDocumentedException(withdrawCause);
262         }
263
264         // TODO: additional checks as per RFC7606 section 5.2
265
266         LOG.debug("Converting BGP Update message {} to withdraw", parsed, withdrawCause);
267         final UpdateBuilder builder = new UpdateBuilder();
268
269         final List<Nlri> nlris = parsed.getNlri();
270         final List<WithdrawnRoutes> withdrawn;
271         if (nlris != null && !nlris.isEmpty()) {
272             withdrawn = Streams.concat(parsed.nonnullWithdrawnRoutes().stream(),
273                 nlris.stream().map(nlri -> new WithdrawnRoutesBuilder(nlri).build()))
274                     .collect(ImmutableList.toImmutableList());
275         } else {
276             withdrawn = parsed.getWithdrawnRoutes();
277         }
278         builder.setWithdrawnRoutes(withdrawn);
279
280         final Attributes attributes = parsed.getAttributes();
281         if (attributes != null) {
282             builder.setAttributes(withdrawAttributes(attributes, withdrawCause));
283         }
284
285         return builder.build();
286     }
287
288     private Attributes withdrawAttributes(final Attributes parsed,
289             final BGPTreatAsWithdrawException withdrawCause) throws BGPDocumentedException {
290         final AttributesBuilder builder = new AttributesBuilder();
291         final MpReachNlri mpReachNlri = getMpReach(parsed);
292         if (mpReachNlri == null) {
293             // No MP_REACH attribute, just reuse MP_UNREACH if it is present.
294             final AttributesUnreach attrs2 = parsed.augmentation(AttributesUnreach.class);
295             if (attrs2 != null) {
296                 builder.addAugmentation(attrs2);
297             }
298             return builder.build();
299         }
300
301         final MpUnreachNlri unreachNlri = getMpUnreach(parsed);
302         if (unreachNlri != null) {
303             final TablesKey reachKey = new TablesKey(mpReachNlri.getAfi(), mpReachNlri.getSafi());
304             final TablesKey unreachKey = new TablesKey(unreachNlri.getAfi(), unreachNlri.getSafi());
305             if (!reachKey.equals(unreachKey)) {
306                 LOG.warn("Unexpected mismatch between MP_REACH ({}) and MP_UNREACH ({})", reachKey, unreachKey,
307                     withdrawCause);
308                 throw new BGPDocumentedException(withdrawCause);
309             }
310         }
311
312         final MpUnreachNlri converted = nlriReg.convertMpReachToMpUnReach(mpReachNlri, unreachNlri)
313                 .orElseThrow(() -> {
314                     LOG.warn("Could not convert attributes {} to withdraw attributes", parsed, withdrawCause);
315                     return new BGPDocumentedException(withdrawCause);
316                 });
317
318         builder.addAugmentation(new AttributesUnreachBuilder().setMpUnreachNlri(converted).build());
319         return builder.build();
320     }
321
322     private static MpReachNlri getMpReach(final Attributes attrs) {
323         final AttributesReach reachAttr = attrs.augmentation(AttributesReach.class);
324         return reachAttr == null ? null : reachAttr.getMpReachNlri();
325     }
326
327     private static MpUnreachNlri getMpUnreach(final Attributes attrs) {
328         final AttributesUnreach unreachAttr = attrs.augmentation(AttributesUnreach.class);
329         return unreachAttr == null ? null : unreachAttr.getMpUnreachNlri();
330     }
331 }