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 io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.bgp.mvpn.spi.pojo.attributes.tunnel.identifier.SimpleTunnelIdentifierRegistry;
14 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
15 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
16 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
17 import org.opendaylight.protocol.util.MplsLabelUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.bgp.rib.route.PmsiTunnelAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.bgp.rib.route.PmsiTunnelAugmentationBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.PmsiTunnel;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.PmsiTunnelBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.TunnelIdentifier;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
29 * PE Distinguisher Labels Attribute Handler.
30 * https://tools.ietf.org/html/rfc6514#section-5
32 * @author Claudio D. Gasparini
34 public final class PMSITunnelAttributeHandler implements AttributeParser, AttributeSerializer {
35 private static final int PMSI_ATTRIBUTE = 22;
36 private static final int MPLS_LENGTH = 3;
37 private final SimpleTunnelIdentifierRegistry tunnelIdentifierHandler =
38 SimpleTunnelIdentifierRegistry.getInstance();
40 private static void parseMpls(final PmsiTunnelBuilder pmsiTunnelBuilder, final ByteBuf buffer) {
41 final MplsLabel mpls = MplsLabelUtil.mplsLabelForByteBuf(buffer);
42 if (mpls.getValue() != 0) {
43 pmsiTunnelBuilder.setMplsLabel(mpls);
47 private static void serializeMpls(final MplsLabel mplsLabel, final ByteBuf body) {
48 if (mplsLabel == null) {
49 body.writeZero(MPLS_LENGTH);
52 body.writeBytes(MplsLabelUtil.byteBufForMplsLabel(mplsLabel));
55 private static void serializeFlag(final PmsiTunnel pmsiTunnelAttribute, final ByteBuf body) {
56 body.writeBoolean(pmsiTunnelAttribute.isLeafInformationRequired());
60 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) {
61 if (!buffer.isReadable()) {
64 final PmsiTunnelBuilder pmsiTunnelBuilder = new PmsiTunnelBuilder();
65 pmsiTunnelBuilder.setLeafInformationRequired(buffer.readBoolean());
66 final int tunnelType = buffer.readUnsignedByte();
67 parseMpls(pmsiTunnelBuilder, buffer);
68 final TunnelIdentifier tunnelIdentifier = this.tunnelIdentifierHandler.parse(tunnelType, buffer);
69 if (tunnelIdentifier != null) {
70 pmsiTunnelBuilder.setTunnelIdentifier(tunnelIdentifier);
72 builder.addAugmentation(PmsiTunnelAugmentation.class, new PmsiTunnelAugmentationBuilder()
73 .setPmsiTunnel(pmsiTunnelBuilder.build()).build());
76 public int getType() {
77 return PMSI_ATTRIBUTE;
81 public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
82 final PmsiTunnelAugmentation pmsiTunnelAugmentation = attribute
83 .augmentation(PmsiTunnelAugmentation.class);
84 if (pmsiTunnelAugmentation == null) {
88 final PmsiTunnel pmsiTunnelAttribute = pmsiTunnelAugmentation.getPmsiTunnel();
89 final TunnelIdentifier tunnel = pmsiTunnelAttribute.getTunnelIdentifier();
90 final ByteBuf tunnelBuffer = Unpooled.buffer();
91 final int tunnelType = this.tunnelIdentifierHandler.serialize(tunnel, tunnelBuffer);
92 final ByteBuf body = Unpooled.buffer();
93 serializeFlag(pmsiTunnelAttribute, body);
94 body.writeByte(tunnelType);
95 serializeMpls(pmsiTunnelAttribute.getMplsLabel(), body);
96 body.writeBytes(tunnelBuffer);
97 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, getType(), body, byteAggregator);
100 public Class<? extends DataObject> getClazz() {
101 return org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.PmsiTunnel.class;