Moved Ipv4Util and Ipv6Util from concepts to util
[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.ByteBufUtil;
14 import io.netty.buffer.Unpooled;
15 import java.util.Arrays;
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.ClusterIdAttributeParser;
21 import org.opendaylight.protocol.bgp.parser.impl.message.update.OriginatorIdAttributeParser;
22 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
23 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
24 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
25 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.protocol.util.Ipv4Util;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.Nlri;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.NlriBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutesBuilder;
36 import org.opendaylight.yangtools.yang.binding.Notification;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * LENGTH fields, that denote the length of the fields with variable length, have fixed SIZE.
42  *
43  * @see <a href="http://tools.ietf.org/html/rfc4271#section-4.3">BGP-4 Update Message Format</a>
44  */
45 public class BGPUpdateMessageParser implements MessageParser, MessageSerializer {
46     public static final int TYPE = 2;
47
48     private static final Logger LOG = LoggerFactory.getLogger(BGPUpdateMessageParser.class);
49
50     /**
51      * Size of the withdrawn_routes_length field, in bytes.
52      */
53     public static final int WITHDRAWN_ROUTES_LENGTH_SIZE = 2;
54     /**
55      * Size of the total_path_attr_length field, in bytes.
56      */
57     public static final int TOTAL_PATH_ATTR_LENGTH_SIZE = 2;
58
59     private final AttributeRegistry reg;
60
61     // Constructors -------------------------------------------------------
62     public BGPUpdateMessageParser(final AttributeRegistry reg) {
63         this.reg = Preconditions.checkNotNull(reg);
64     }
65
66     // Getters & setters --------------------------------------------------
67
68     @Override
69     public Update parseMessageBody(final ByteBuf buffer, final int messageLength) throws BGPDocumentedException {
70         Preconditions.checkArgument(buffer != null && buffer.readableBytes() != 0, "Byte array cannot be null or empty.");
71         LOG.trace("Started parsing of update message: {}", Arrays.toString(ByteArray.getAllBytes(buffer)));
72
73         final int withdrawnRoutesLength = buffer.readUnsignedShort();
74         final UpdateBuilder eventBuilder = new UpdateBuilder();
75
76         if (withdrawnRoutesLength > 0) {
77             final List<Ipv4Prefix> withdrawnRoutes = Ipv4Util.prefixListForBytes(ByteArray.readBytes(buffer, withdrawnRoutesLength));
78             eventBuilder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setWithdrawnRoutes(withdrawnRoutes).build());
79         }
80         final int totalPathAttrLength = buffer.readUnsignedShort();
81
82         if (withdrawnRoutesLength == 0 && totalPathAttrLength == 0) {
83             return eventBuilder.build();
84         }
85         if (totalPathAttrLength > 0) {
86             try {
87                 final PathAttributes pathAttributes = this.reg.parseAttributes(buffer.slice(buffer.readerIndex(), totalPathAttrLength));
88                 buffer.skipBytes(totalPathAttrLength);
89                 eventBuilder.setPathAttributes(pathAttributes);
90             } catch (final BGPParsingException | RuntimeException e) {
91                 // Catch everything else and turn it into a BGPDocumentedException
92                 LOG.warn("Could not parse BGP attributes", e);
93                 throw new BGPDocumentedException("Could not parse BGP attributes.", BGPError.MALFORMED_ATTR_LIST, e);
94             }
95         }
96         final List<Ipv4Prefix> nlri = Ipv4Util.prefixListForBytes(ByteArray.readAllBytes(buffer));
97         if (nlri != null && !nlri.isEmpty()) {
98             eventBuilder.setNlri(new NlriBuilder().setNlri(nlri).build());
99         }
100         Update msg = eventBuilder.build();
101         LOG.debug("BGP Update message was parsed {}.", msg);
102         return msg;
103     }
104
105     @Override
106     public void serializeMessage(Notification message, ByteBuf bytes) {
107         if (message == null) {
108             throw new IllegalArgumentException("BGPUpdate message cannot be null");
109         }
110         LOG.trace("Started serializing update message: {}", message);
111         final Update update = (Update) message;
112
113         ByteBuf messageBody = Unpooled.buffer();
114         WithdrawnRoutes withdrawnRoutes = update.getWithdrawnRoutes();
115         if (withdrawnRoutes != null) {
116             ByteBuf withdrawnRoutesBuf = Unpooled.buffer();
117             for (Ipv4Prefix withdrawnRoutePrefix : withdrawnRoutes.getWithdrawnRoutes()) {
118                 int prefixBits = Ipv4Util.getPrefixLength(withdrawnRoutePrefix.getValue());
119                 byte[] prefixBytes = ByteArray.subByte(Ipv4Util.bytesForPrefix(withdrawnRoutePrefix), 0,
120                         Ipv4Util.getPrefixLengthBytes(withdrawnRoutePrefix.getValue()));
121                 withdrawnRoutesBuf.writeByte(prefixBits);
122                 withdrawnRoutesBuf.writeBytes(prefixBytes);
123             }
124             messageBody.writeShort(withdrawnRoutesBuf.writerIndex());
125             messageBody.writeBytes(withdrawnRoutesBuf);
126
127         } else {
128             messageBody.writeZero(2);
129         }
130         if (update.getPathAttributes() != null) {
131             ByteBuf pathAttributesBuf = Unpooled.buffer();
132             this.reg.serializeAttribute(update.getPathAttributes(), pathAttributesBuf);
133             ClusterIdAttributeParser clusterIdAttributeParser = new ClusterIdAttributeParser();
134             clusterIdAttributeParser.serializeAttribute(update.getPathAttributes(), pathAttributesBuf);
135
136             OriginatorIdAttributeParser originatorIdAttributeParser = new OriginatorIdAttributeParser();
137             originatorIdAttributeParser.serializeAttribute(update.getPathAttributes(), pathAttributesBuf);
138
139             messageBody.writeShort(pathAttributesBuf.writerIndex());
140             messageBody.writeBytes(pathAttributesBuf);
141         } else {
142             messageBody.writeZero(2);
143         }
144         Nlri nlri = update.getNlri();
145         if (nlri != null) {
146             for (Ipv4Prefix ipv4Prefix : nlri.getNlri()) {
147                 int prefixBits = Ipv4Util.getPrefixLength(ipv4Prefix.getValue());
148                 byte[] prefixBytes = ByteArray.subByte(Ipv4Util.bytesForPrefix(ipv4Prefix), 0,
149                         Ipv4Util.getPrefixLengthBytes(ipv4Prefix.getValue()));
150                 messageBody.writeByte(prefixBits);
151                 messageBody.writeBytes(prefixBytes);
152             }
153         }
154
155         LOG.trace("Update message serialized to {}", ByteBufUtil.hexDump(messageBody));
156         //FIXME: switch to ByteBuf
157         bytes.writeBytes(MessageUtil.formatMessage(TYPE, ByteArray.getAllBytes(messageBody)));
158     }
159 }