b975a9f24b6c906adaf11d780841c2096983db61
[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 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 DataObject attribute, final ByteBuf byteAggregator) {
78         Preconditions.checkArgument(attribute instanceof Attributes,
79                 "Attribute parameter is not a PathAttribute object.");
80         final PeDistinguisherLabelsAttributeAugmentation ipv4Att =
81                 ((Attributes) attribute).getAugmentation(PeDistinguisherLabelsAttributeAugmentation.class);
82
83         if (ipv4Att == null) {
84             return;
85         }
86
87         final List<PeDistinguisherLabelAttribute> distinguishers
88                 = ipv4Att.getPeDistinguisherLabelsAttribute().getPeDistinguisherLabelAttribute();
89         final ByteBuf buffer = Unpooled.buffer();
90         for (final PeDistinguisherLabelAttribute peDist : distinguishers) {
91             if (peDist.getPeAddress().getIpv4Address() != null) {
92                 buffer.writeBytes(Ipv4Util.bytesForAddress(peDist.getPeAddress().getIpv4Address()));
93             } else {
94                 buffer.writeBytes(Ipv6Util.bytesForAddress(peDist.getPeAddress().getIpv6Address()));
95             }
96             buffer.writeBytes(MplsLabelUtil.byteBufForMplsLabel(peDist.getMplsLabel()));
97         }
98         formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, TYPE, buffer, byteAggregator);
99     }
100
101     public int getType() {
102         return TYPE;
103     }
104
105     public Class<? extends DataObject> getClazz() {
106         return PeDistinguisherLabelsAttribute.class;
107     }
108 }