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