Cleanup deprecation warnings in bgp/extensions/mvpn
[bgpcep.git] / bgp / extensions / mvpn / src / main / java / org / opendaylight / protocol / bgp / mvpn / impl / attributes / PMSITunnelAttributeHandler.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 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.bgp.parser.spi.PeerSpecificParserConstraint;
18 import org.opendaylight.protocol.util.MplsLabelUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev200120.bgp.rib.route.PmsiTunnelAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev200120.bgp.rib.route.PmsiTunnelAugmentationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev200120.pmsi.tunnel.PmsiTunnel;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev200120.pmsi.tunnel.PmsiTunnelBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev200120.pmsi.tunnel.pmsi.tunnel.TunnelIdentifier;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28
29 /**
30  * PE Distinguisher Labels Attribute Handler.
31  * https://tools.ietf.org/html/rfc6514#section-5
32  *
33  * @author Claudio D. Gasparini
34  */
35 public final class PMSITunnelAttributeHandler implements AttributeParser, AttributeSerializer {
36     private static final int PMSI_ATTRIBUTE = 22;
37     private static final int MPLS_LENGTH = 3;
38     private final SimpleTunnelIdentifierRegistry tunnelIdentifierHandler =
39             SimpleTunnelIdentifierRegistry.getInstance();
40
41     private static void parseMpls(final PmsiTunnelBuilder pmsiTunnelBuilder, final ByteBuf buffer) {
42         final MplsLabel mpls = MplsLabelUtil.mplsLabelForByteBuf(buffer);
43         if (mpls.getValue().toJava() != 0) {
44             pmsiTunnelBuilder.setMplsLabel(mpls);
45         }
46     }
47
48     private static void serializeMpls(final MplsLabel mplsLabel, final ByteBuf body) {
49         if (mplsLabel == null) {
50             body.writeZero(MPLS_LENGTH);
51             return;
52         }
53         body.writeBytes(MplsLabelUtil.byteBufForMplsLabel(mplsLabel));
54     }
55
56     private static void serializeFlag(final PmsiTunnel pmsiTunnelAttribute, final ByteBuf body) {
57         body.writeBoolean(pmsiTunnelAttribute.isLeafInformationRequired());
58     }
59
60     @Override
61     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder,
62             final PeerSpecificParserConstraint constraint) {
63         // FIXME: BGPCEP-359: what is the error handling here?
64         if (!buffer.isReadable()) {
65             return;
66         }
67         final PmsiTunnelBuilder pmsiTunnelBuilder = new PmsiTunnelBuilder();
68         pmsiTunnelBuilder.setLeafInformationRequired(buffer.readBoolean());
69         final int tunnelType = buffer.readUnsignedByte();
70         parseMpls(pmsiTunnelBuilder, buffer);
71         final TunnelIdentifier tunnelIdentifier = this.tunnelIdentifierHandler.parse(tunnelType, buffer);
72         if (tunnelIdentifier != null) {
73             pmsiTunnelBuilder.setTunnelIdentifier(tunnelIdentifier);
74         }
75         builder.addAugmentation(new PmsiTunnelAugmentationBuilder().setPmsiTunnel(pmsiTunnelBuilder.build()).build());
76     }
77
78     public int getType() {
79         return PMSI_ATTRIBUTE;
80     }
81
82     @Override
83     public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
84         final PmsiTunnelAugmentation pmsiTunnelAugmentation = attribute
85                 .augmentation(PmsiTunnelAugmentation.class);
86         if (pmsiTunnelAugmentation == null) {
87             return;
88         }
89
90         final PmsiTunnel pmsiTunnelAttribute = pmsiTunnelAugmentation.getPmsiTunnel();
91         final TunnelIdentifier tunnel = pmsiTunnelAttribute.getTunnelIdentifier();
92         final ByteBuf tunnelBuffer = Unpooled.buffer();
93         final int tunnelType = this.tunnelIdentifierHandler.serialize(tunnel, tunnelBuffer);
94         final ByteBuf body = Unpooled.buffer();
95         serializeFlag(pmsiTunnelAttribute, body);
96         body.writeByte(tunnelType);
97         serializeMpls(pmsiTunnelAttribute.getMplsLabel(), body);
98         body.writeBytes(tunnelBuffer);
99         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL | AttributeUtil.TRANSITIVE, getType(), body,
100                 byteAggregator);
101     }
102
103     public Class<? extends DataObject> getClazz() {
104         return org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev200120.PmsiTunnel.class;
105     }
106 }