MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / evpn / src / main / java / org / opendaylight / protocol / bgp / evpn / impl / attributes / PMSITunnelAttributeHandler.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  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.evpn.impl.attributes;
10
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier.TunnelIdentifierHandler;
16 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
19 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
20 import org.opendaylight.protocol.util.MplsLabelUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.evpn.route.PmsiTunnelAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.routes.evpn.routes.evpn.route.PmsiTunnelAugmentationBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.PmsiTunnel;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.PmsiTunnelBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.TunnelIdentifier;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30
31 public final class PMSITunnelAttributeHandler implements AttributeParser, AttributeSerializer {
32     private static final int PMSI_ATTRIBUTE = 22;
33     private static final int MPLS_LENGTH = 3;
34     private final TunnelIdentifierHandler tunnelIdentifierHandler;
35
36     public PMSITunnelAttributeHandler(final AddressFamilyRegistry addressFamilyRegistry) {
37         this.tunnelIdentifierHandler = new TunnelIdentifierHandler(addressFamilyRegistry);
38     }
39
40     @Override
41     public void parseAttribute(@Nonnull final ByteBuf buffer, @Nonnull final AttributesBuilder builder) {
42         if (!buffer.isReadable()) {
43             return;
44         }
45         final PmsiTunnelBuilder pmsiTunnelBuilder = new PmsiTunnelBuilder();
46         pmsiTunnelBuilder.setLeafInformationRequired(buffer.readBoolean());
47         final int tunnelType = buffer.readUnsignedByte();
48         parseMpls(pmsiTunnelBuilder, buffer);
49         final TunnelIdentifier tunnelIdentifier = this.tunnelIdentifierHandler.parse(tunnelType, buffer);
50         if (tunnelIdentifier != null) {
51             pmsiTunnelBuilder.setTunnelIdentifier(tunnelIdentifier);
52         }
53         builder.addAugmentation(PmsiTunnelAugmentation.class, new PmsiTunnelAugmentationBuilder()
54                 .setPmsiTunnel(pmsiTunnelBuilder.build()).build());
55     }
56
57     private static void parseMpls(final PmsiTunnelBuilder pmsiTunnelBuilder, final ByteBuf buffer) {
58         final MplsLabel mpls = MplsLabelUtil.mplsLabelForByteBuf(buffer);
59         if (mpls.getValue() != 0) {
60             pmsiTunnelBuilder.setMplsLabel(mpls);
61         }
62     }
63
64     public int getType() {
65         return PMSI_ATTRIBUTE;
66     }
67
68     @Override
69     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
70         Preconditions.checkArgument(attribute instanceof Attributes,
71                 "Attribute parameter is not a PathAttribute object.");
72         final PmsiTunnelAugmentation pmsiTunnelAugmentation = ((Attributes) attribute)
73                 .getAugmentation(PmsiTunnelAugmentation.class);
74         if (pmsiTunnelAugmentation == null) {
75             return;
76         }
77
78         final PmsiTunnel pmsiTunnelAttribute = pmsiTunnelAugmentation.getPmsiTunnel();
79         final TunnelIdentifier tunnel = pmsiTunnelAttribute.getTunnelIdentifier();
80         final ByteBuf tunnelBuffer = Unpooled.buffer();
81         final int tunnelType = this.tunnelIdentifierHandler.serialize(tunnel, tunnelBuffer);
82         final ByteBuf body = Unpooled.buffer();
83         serializeFlag(pmsiTunnelAttribute, body);
84         body.writeByte(tunnelType);
85         serializeMpls(pmsiTunnelAttribute.getMplsLabel(), body);
86         body.writeBytes(tunnelBuffer);
87         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, getType(), body, byteAggregator);
88     }
89
90     private static void serializeMpls(final MplsLabel mplsLabel, final ByteBuf body) {
91         if (mplsLabel == null) {
92             body.writeZero(MPLS_LENGTH);
93         }
94         body.writeBytes(MplsLabelUtil.byteBufForMplsLabel(mplsLabel));
95     }
96
97     private static void serializeFlag(final PmsiTunnel pmsiTunnelAttribute, final ByteBuf body) {
98         body.writeBoolean(pmsiTunnelAttribute.isLeafInformationRequired());
99     }
100
101     public Class<? extends DataObject> getClazz() {
102         return org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.PmsiTunnel.class;
103     }
104 }