6b58ca572f7999e581e0902125414b9d1d4aea14
[bgpcep.git] / bgp / mvpn / src / main / java / org / opendaylight / protocol / bgp / mvpn / impl / attributes / PEDistinguisherLabelsAttributeHandler.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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
9 package org.opendaylight.protocol.bgp.mvpn.impl.attributes;
10
11 import static org.opendaylight.protocol.bgp.parser.spi.AttributeUtil.formatAttribute;
12
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Lists;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.List;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
19 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
20 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
21 import org.opendaylight.protocol.util.Ipv4Util;
22 import org.opendaylight.protocol.util.Ipv6Util;
23 import org.opendaylight.protocol.util.MplsLabelUtil;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.bgp.rib.route.PeDistinguisherLabelsAttributeAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.bgp.rib.route.PeDistinguisherLabelsAttributeAugmentationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.pe.distinguisher.labels.attribute.PeDistinguisherLabelsAttribute;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.pe.distinguisher.labels.attribute.PeDistinguisherLabelsAttributeBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.pe.distinguisher.labels.attribute.pe.distinguisher.labels.attribute.PeDistinguisherLabelAttribute;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.pe.distinguisher.labels.attribute.pe.distinguisher.labels.attribute.PeDistinguisherLabelAttributeBuilder;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34
35 /**
36  * PE Distinguisher Labels Attribute Handler.
37  * https://tools.ietf.org/html/rfc6514#section-8
38  *
39  * @author Claudio D. Gasparini
40  */
41 public final class PEDistinguisherLabelsAttributeHandler implements AttributeParser, AttributeSerializer {
42
43     private static final int TYPE = 27;
44
45     public PEDistinguisherLabelsAttributeHandler() {
46     }
47
48     @Override
49     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) {
50         if (!buffer.isReadable()) {
51             return;
52         }
53         final boolean isIpv4 = buffer.readableBytes() % 7 == 0;
54         final boolean isIpv6 = buffer.readableBytes() % 19 == 0;
55         Preconditions.checkArgument(isIpv4 || isIpv6,
56                 "Length of byte array should be multiple of 7 or multiple of 19");
57
58         final List<PeDistinguisherLabelAttribute> list = Lists.newArrayList();
59         while (buffer.isReadable()) {
60             final PeDistinguisherLabelAttributeBuilder attribute = new PeDistinguisherLabelAttributeBuilder();
61             if (isIpv4) {
62                 attribute.setPeAddress(new IpAddress(Ipv4Util.addressForByteBuf(buffer)));
63             } else {
64                 attribute.setPeAddress(new IpAddress(Ipv6Util.addressForByteBuf(buffer)));
65             }
66             attribute.setMplsLabel(MplsLabelUtil.mplsLabelForByteBuf(buffer));
67             list.add(attribute.build());
68         }
69
70         builder.addAugmentation(PeDistinguisherLabelsAttributeAugmentation.class,
71                 new PeDistinguisherLabelsAttributeAugmentationBuilder()
72                         .setPeDistinguisherLabelsAttribute(new PeDistinguisherLabelsAttributeBuilder()
73                                 .setPeDistinguisherLabelAttribute(list).build()).build());
74     }
75
76     @Override
77     public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
78         final PeDistinguisherLabelsAttributeAugmentation att =
79                 attribute.augmentation(PeDistinguisherLabelsAttributeAugmentation.class);
80
81         if (att == null) {
82             return;
83         }
84
85         final List<PeDistinguisherLabelAttribute> distinguishers
86                 = att.getPeDistinguisherLabelsAttribute().getPeDistinguisherLabelAttribute();
87         final ByteBuf buffer = Unpooled.buffer();
88         for (final PeDistinguisherLabelAttribute peDist : distinguishers) {
89             if (peDist.getPeAddress().getIpv4Address() != null) {
90                 buffer.writeBytes(Ipv4Util.bytesForAddress(peDist.getPeAddress().getIpv4Address()));
91             } else {
92                 buffer.writeBytes(Ipv6Util.bytesForAddress(peDist.getPeAddress().getIpv6Address()));
93             }
94             buffer.writeBytes(MplsLabelUtil.byteBufForMplsLabel(peDist.getMplsLabel()));
95         }
96         formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, buffer, byteAggregator);
97     }
98
99     public int getType() {
100         return TYPE;
101     }
102
103     public Class<? extends DataObject> getClazz() {
104         return PeDistinguisherLabelsAttribute.class;
105     }
106 }