Moved Ipv4Util and Ipv6Util from concepts to util
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / MPUnreachAttributeParser.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.PathAttributes2;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv4Case;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv6Case;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32
33 public final class MPUnreachAttributeParser implements AttributeParser, AttributeSerializer {
34     public static final int TYPE = 15;
35     public static final int MAX_ATTR_LENGTH_FOR_SINGLE_BYTE = 127;
36
37     private final NlriRegistry reg;
38
39     public MPUnreachAttributeParser(final NlriRegistry reg) {
40         this.reg = Preconditions.checkNotNull(reg);
41     }
42
43     @Override
44     public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
45         try {
46             final PathAttributes2 a = new PathAttributes2Builder().setMpUnreachNlri(this.reg.parseMpUnreach(buffer)).build();
47             builder.addAugmentation(PathAttributes2.class, a);
48         } catch (final BGPParsingException e) {
49             throw new BGPDocumentedException("Could not parse MP_UNREACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
50         }
51     }
52
53     @Override
54     public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
55         PathAttributes pathAttributes = (PathAttributes) attribute;
56         PathAttributes2 pathAttributes2 = pathAttributes.getAugmentation(PathAttributes2.class);
57         if (pathAttributes2 == null) {
58             return;
59         }
60         MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
61         ByteBuf unreachBuffer = Unpooled.buffer();
62         this.reg.serializeMpUnReach(mpUnreachNlri, unreachBuffer);
63
64         if (mpUnreachNlri.getWithdrawnRoutes() != null) {
65             if (mpUnreachNlri.getWithdrawnRoutes().getDestinationType() instanceof DestinationIpv4Case) {
66                 DestinationIpv4Case destinationIpv4Case = (DestinationIpv4Case) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
67                 if (destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes() != null) {
68                     for (Ipv4Prefix ipv4Prefix : destinationIpv4Case.getDestinationIpv4().getIpv4Prefixes()) {
69                         unreachBuffer.writeByte(Ipv4Util.getPrefixLength(ipv4Prefix.getValue()));
70                         unreachBuffer.writeBytes(Ipv4Util.bytesForPrefixByPrefixLength(ipv4Prefix));
71                     }
72                 }
73             }
74             if (mpUnreachNlri.getWithdrawnRoutes().getDestinationType() instanceof DestinationIpv6Case) {
75                 DestinationIpv6Case destinationIpv6Case = (DestinationIpv6Case) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
76                 if (destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes() != null) {
77                     for (Ipv6Prefix ipv6Prefix : destinationIpv6Case.getDestinationIpv6().getIpv6Prefixes()) {
78                         unreachBuffer.writeByte(Ipv4Util.getPrefixLength(ipv6Prefix.getValue()));
79                         unreachBuffer.writeBytes(Ipv6Util.bytesForPrefixByPrefixLength(ipv6Prefix));
80                     }
81                 }
82             }
83         }
84         if (unreachBuffer.writerIndex() > MAX_ATTR_LENGTH_FOR_SINGLE_BYTE) {
85             byteAggregator.writeByte(AttributeFlags.OPTIONAL | AttributeFlags.EXTENDED);
86             byteAggregator.writeByte(TYPE);
87             byteAggregator.writeShort(unreachBuffer.writerIndex());
88         } else {
89             byteAggregator.writeByte(AttributeFlags.OPTIONAL);
90             byteAggregator.writeByte(TYPE);
91             byteAggregator.writeByte(unreachBuffer.writerIndex());
92         }
93         byteAggregator.writeBytes(unreachBuffer);
94     }
95 }