Bug-6562: Support add-path in base BGP NLRI
[bgpcep.git] / bgp / evpn / src / main / java / org / opendaylight / protocol / bgp / evpn / impl / extended / communities / ESILabelExtCom.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 package org.opendaylight.protocol.bgp.evpn.impl.extended.communities;
9
10 import static org.opendaylight.protocol.util.MplsLabelUtil.byteBufForMplsLabel;
11 import static org.opendaylight.protocol.util.MplsLabelUtil.mplsLabelForByteBuf;
12
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
16 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171207.esi.label.extended.community.EsiLabelExtendedCommunity;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171207.esi.label.extended.community.EsiLabelExtendedCommunityBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171207.evpn.routes.evpn.routes.evpn.route.attributes.extended.communities.extended.community.EsiLabelExtendedCommunityCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171207.evpn.routes.evpn.routes.evpn.route.attributes.extended.communities.extended.community.EsiLabelExtendedCommunityCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
23
24 public final class ESILabelExtCom extends AbstractExtendedCommunities {
25     private static final int SUBTYPE = 1;
26     private static final int RESERVED = 2;
27
28     @Override
29     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
30         final boolean singleActive = buffer.readBoolean();
31         buffer.skipBytes(RESERVED);
32         final MplsLabel label = mplsLabelForByteBuf(buffer);
33         return new EsiLabelExtendedCommunityCaseBuilder().setEsiLabelExtendedCommunity(
34             new EsiLabelExtendedCommunityBuilder().setEsiLabel(label).setSingleActiveMode(singleActive).build()).build();
35     }
36
37     @Override
38     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
39         Preconditions.checkArgument(extendedCommunity instanceof EsiLabelExtendedCommunityCase,
40             "The extended community %s is not EsiLabelExtendedCommunityCaseCase type.", extendedCommunity);
41         final EsiLabelExtendedCommunity extCom = ((EsiLabelExtendedCommunityCase) extendedCommunity).getEsiLabelExtendedCommunity();
42         byteAggregator.writeBoolean(extCom.isSingleActiveMode());
43         byteAggregator.writeZero(RESERVED);
44         byteAggregator.writeBytes(byteBufForMplsLabel(extCom.getEsiLabel()));
45     }
46
47     @Override
48     public int getSubType() {
49         return SUBTYPE;
50     }
51 }