Code clean up
[bgpcep.git] / bgp / 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.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;
27
28 /**
29  * PE Distinguisher Labels Attribute Handler.
30  * https://tools.ietf.org/html/rfc6514#section-5
31  *
32  * @author Claudio D. Gasparini
33  */
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();
39
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);
44         }
45     }
46
47     private static void serializeMpls(final MplsLabel mplsLabel, final ByteBuf body) {
48         if (mplsLabel == null) {
49             body.writeZero(MPLS_LENGTH);
50             return;
51         }
52         body.writeBytes(MplsLabelUtil.byteBufForMplsLabel(mplsLabel));
53     }
54
55     private static void serializeFlag(final PmsiTunnel pmsiTunnelAttribute, final ByteBuf body) {
56         body.writeBoolean(pmsiTunnelAttribute.isLeafInformationRequired());
57     }
58
59     @Override
60     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) {
61         if (!buffer.isReadable()) {
62             return;
63         }
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);
71         }
72         builder.addAugmentation(PmsiTunnelAugmentation.class, new PmsiTunnelAugmentationBuilder()
73                 .setPmsiTunnel(pmsiTunnelBuilder.build()).build());
74     }
75
76     public int getType() {
77         return PMSI_ATTRIBUTE;
78     }
79
80     @Override
81     public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
82         final PmsiTunnelAugmentation pmsiTunnelAugmentation = attribute
83                 .augmentation(PmsiTunnelAugmentation.class);
84         if (pmsiTunnelAugmentation == null) {
85             return;
86         }
87
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);
98     }
99
100     public Class<? extends DataObject> getClazz() {
101         return org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.PmsiTunnel.class;
102     }
103 }