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
8 package org.opendaylight.protocol.bgp.mvpn.impl.attributes;
10 import static org.opendaylight.protocol.bgp.parser.spi.AttributeUtil.formatAttribute;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.protocol.bgp.parser.BGPError;
17 import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
18 import org.opendaylight.protocol.bgp.parser.spi.AbstractAttributeParser;
19 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
20 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
21 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
22 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
23 import org.opendaylight.protocol.util.Ipv4Util;
24 import org.opendaylight.protocol.util.Ipv6Util;
25 import org.opendaylight.protocol.util.MplsLabelUtil;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressNoZone;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.bgp.rib.route.PeDistinguisherLabelsAttributeAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.bgp.rib.route.PeDistinguisherLabelsAttributeAugmentationBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.pe.distinguisher.labels.attribute.PeDistinguisherLabelsAttribute;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.pe.distinguisher.labels.attribute.PeDistinguisherLabelsAttributeBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.pe.distinguisher.labels.attribute.pe.distinguisher.labels.attribute.PeDistinguisherLabelAttribute;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.pe.distinguisher.labels.attribute.pe.distinguisher.labels.attribute.PeDistinguisherLabelAttributeBuilder;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
38 * PE Distinguisher Labels Attribute Handler.
39 * https://tools.ietf.org/html/rfc6514#section-8
41 * @author Claudio D. Gasparini
43 public final class PEDistinguisherLabelsAttributeHandler extends AbstractAttributeParser
44 implements AttributeSerializer {
46 private static final int TYPE = 27;
48 public PEDistinguisherLabelsAttributeHandler() {
52 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder,
53 final RevisedErrorHandling errorHandling, final PeerSpecificParserConstraint constraint)
54 throws BGPTreatAsWithdrawException {
55 final int readable = buffer.readableBytes();
62 if (readable % 7 == 0) {
65 } else if (readable % 19 == 0) {
66 count = readable / 19;
70 // When a router that receives a BGP Update that contains the PE
71 // Distinguisher Labels attribute with its Partial bit set determines
72 // that the attribute is malformed, the router SHOULD treat this Update
73 // as though all the routes contained in this Update had been withdrawn.
74 throw new BGPTreatAsWithdrawException(BGPError.MALFORMED_ATTR_LIST,
75 "PE Distinguisher Labels has incorrect length %s", readable);
78 final List<PeDistinguisherLabelAttribute> list = new ArrayList<>(count);
79 for (int i = 0; i < count; ++i) {
80 final PeDistinguisherLabelAttributeBuilder attribute = new PeDistinguisherLabelAttributeBuilder();
82 attribute.setPeAddress(new IpAddressNoZone(Ipv4Util.addressForByteBuf(buffer)));
84 attribute.setPeAddress(new IpAddressNoZone(Ipv6Util.addressForByteBuf(buffer)));
86 attribute.setMplsLabel(MplsLabelUtil.mplsLabelForByteBuf(buffer));
87 list.add(attribute.build());
90 builder.addAugmentation(PeDistinguisherLabelsAttributeAugmentation.class,
91 new PeDistinguisherLabelsAttributeAugmentationBuilder()
92 .setPeDistinguisherLabelsAttribute(new PeDistinguisherLabelsAttributeBuilder()
93 .setPeDistinguisherLabelAttribute(list).build()).build());
97 public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
98 final PeDistinguisherLabelsAttributeAugmentation att =
99 attribute.augmentation(PeDistinguisherLabelsAttributeAugmentation.class);
105 final List<PeDistinguisherLabelAttribute> distinguishers
106 = att.getPeDistinguisherLabelsAttribute().getPeDistinguisherLabelAttribute();
107 final ByteBuf buffer = Unpooled.buffer();
108 for (final PeDistinguisherLabelAttribute peDist : distinguishers) {
109 if (peDist.getPeAddress().getIpv4AddressNoZone() != null) {
110 buffer.writeBytes(Ipv4Util.bytesForAddress(peDist.getPeAddress().getIpv4AddressNoZone()));
112 buffer.writeBytes(Ipv6Util.bytesForAddress(peDist.getPeAddress().getIpv6AddressNoZone()));
114 buffer.writeBytes(MplsLabelUtil.byteBufForMplsLabel(peDist.getMplsLabel()));
116 formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, buffer, byteAggregator);
119 public int getType() {
123 public Class<? extends DataObject> getClazz() {
124 return PeDistinguisherLabelsAttribute.class;