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