2 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.mvpn.impl.attributes;
11 import static org.opendaylight.protocol.bgp.parser.spi.AttributeUtil.formatAttribute;
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;
36 * PE Distinguisher Labels Attribute Handler.
37 * https://tools.ietf.org/html/rfc6514#section-8
39 * @author Claudio D. Gasparini
41 public final class PEDistinguisherLabelsAttributeHandler implements AttributeParser, AttributeSerializer {
43 private static final int TYPE = 27;
45 public PEDistinguisherLabelsAttributeHandler() {
49 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) {
50 if (!buffer.isReadable()) {
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");
58 final List<PeDistinguisherLabelAttribute> list = Lists.newArrayList();
59 while (buffer.isReadable()) {
60 final PeDistinguisherLabelAttributeBuilder attribute = new PeDistinguisherLabelAttributeBuilder();
62 attribute.setPeAddress(new IpAddress(Ipv4Util.addressForByteBuf(buffer)));
64 attribute.setPeAddress(new IpAddress(Ipv6Util.addressForByteBuf(buffer)));
66 attribute.setMplsLabel(MplsLabelUtil.mplsLabelForByteBuf(buffer));
67 list.add(attribute.build());
70 builder.addAugmentation(PeDistinguisherLabelsAttributeAugmentation.class,
71 new PeDistinguisherLabelsAttributeAugmentationBuilder()
72 .setPeDistinguisherLabelsAttribute(new PeDistinguisherLabelsAttributeBuilder()
73 .setPeDistinguisherLabelAttribute(list).build()).build());
77 public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
78 final PeDistinguisherLabelsAttributeAugmentation att =
79 attribute.augmentation(PeDistinguisherLabelsAttributeAugmentation.class);
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()));
92 buffer.writeBytes(Ipv6Util.bytesForAddress(peDist.getPeAddress().getIpv6Address()));
94 buffer.writeBytes(MplsLabelUtil.byteBufForMplsLabel(peDist.getMplsLabel()));
96 formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, buffer, byteAggregator);
99 public int getType() {
103 public Class<? extends DataObject> getClazz() {
104 return PeDistinguisherLabelsAttribute.class;