Moved Ipv4Util and Ipv6Util from concepts to util
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / MPReachAttributeParser.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.update;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.bgp.parser.AttributeFlags;
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPError;
16 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
19 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
20 import org.opendaylight.protocol.util.Ipv4Util;
21 import org.opendaylight.protocol.util.Ipv6Util;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv4Case;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv6Case;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutes;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34
35
36 public final class MPReachAttributeParser implements AttributeParser, AttributeSerializer {
37
38     public static final int TYPE = 14;
39     public static final int ATTR_LENGTH = 2;
40     public static final int MAX_ATTR_LENGTH_FOR_SINGLE_BYTE = 127;
41     private final NlriRegistry reg;
42
43     public MPReachAttributeParser(final NlriRegistry reg) {
44         this.reg = Preconditions.checkNotNull(reg);
45     }
46
47     @Override
48     public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
49         try {
50             final PathAttributes1 a = new PathAttributes1Builder().setMpReachNlri(this.reg.parseMpReach(buffer)).build();
51             builder.addAugmentation(PathAttributes1.class, a);
52         } catch (final BGPParsingException e) {
53             throw new BGPDocumentedException("Could not parse MP_REACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
54         }
55     }
56
57     @Override
58     public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
59         PathAttributes pathAttributes = (PathAttributes) attribute;
60         PathAttributes1 pathAttributes1 = pathAttributes.getAugmentation(PathAttributes1.class);
61         if (pathAttributes1 == null) {
62             return;
63         }
64         MpReachNlri mpReachNlri = pathAttributes1.getMpReachNlri();
65         ByteBuf reachBuffer = Unpooled.buffer();
66         this.reg.serializeMpReach(mpReachNlri, reachBuffer);
67
68         serializeAdvertizedRoutes(mpReachNlri.getAdvertizedRoutes(), reachBuffer);
69         if (reachBuffer.writerIndex() > MAX_ATTR_LENGTH_FOR_SINGLE_BYTE) {
70             byteAggregator.writeByte(AttributeFlags.OPTIONAL | AttributeFlags.EXTENDED);
71             byteAggregator.writeByte(TYPE);
72             byteAggregator.writeShort(reachBuffer.writerIndex());
73         } else {
74             byteAggregator.writeByte(AttributeFlags.OPTIONAL);
75             byteAggregator.writeByte(TYPE);
76             byteAggregator.writeByte(reachBuffer.writerIndex());
77         }
78         byteAggregator.writeBytes(reachBuffer);
79
80
81     }
82
83     private void serializeAdvertizedRoutes(AdvertizedRoutes advertizedRoutes, ByteBuf byteAggregator) {
84         DestinationType destinationType = advertizedRoutes.getDestinationType();
85         if (destinationType instanceof DestinationIpv4Case) {
86             DestinationIpv4Case destinationIpv4Case = (DestinationIpv4Case) destinationType;
87             for (Ipv4Prefix ipv4Prefix : destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes()) {
88                 byteAggregator.writeByte(Ipv4Util.getPrefixLength(ipv4Prefix.getValue()));
89                 byteAggregator.writeBytes(Ipv4Util.bytesForPrefixByPrefixLength(ipv4Prefix));
90             }
91         } else if (destinationType instanceof DestinationIpv6Case) {
92             DestinationIpv6Case destinationIpv6Case = (DestinationIpv6Case) destinationType;
93             for (Ipv6Prefix ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
94                 byteAggregator.writeByte(Ipv4Util.getPrefixLength(ipv6Prefix.getValue()));
95                 byteAggregator.writeBytes(Ipv6Util.bytesForPrefixByPrefixLength(ipv6Prefix));
96             }
97         }
98     }
99 }