MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / extended / communities / Ipv4SpecificEcHandler.java
1 /*
2  * Copyright (c) 2015 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.parser.impl.message.update.extended.communities;
10
11 import static org.opendaylight.protocol.bgp.parser.spi.extended.community.Inet4SpecificExtendedCommunityCommonUtil.parseCommon;
12 import static org.opendaylight.protocol.bgp.parser.spi.extended.community.Inet4SpecificExtendedCommunityCommonUtil.serializeCommon;
13
14 import com.google.common.base.Preconditions;
15 import io.netty.buffer.ByteBuf;
16 import org.opendaylight.protocol.bgp.parser.spi.extended.community.AbstractIpv4ExtendedCommunity;
17 import org.opendaylight.protocol.util.ByteBufWriteUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.Inet4SpecificExtendedCommunityCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.Inet4SpecificExtendedCommunityCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunity;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.inet4.specific.extended.community._case.Inet4SpecificExtendedCommunityBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.inet4.specific.extended.community.common.Inet4SpecificExtendedCommunityCommon;
24
25 public final class Ipv4SpecificEcHandler extends AbstractIpv4ExtendedCommunity {
26
27     private static final int SUBTYPE = 0;
28
29     @Override
30     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) {
31         return new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
32                 new Inet4SpecificExtendedCommunityBuilder()
33                         .setInet4SpecificExtendedCommunityCommon(parseCommon(buffer))
34                         .build()).build();
35     }
36
37     @Override
38     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
39         Preconditions.checkArgument(extendedCommunity instanceof Inet4SpecificExtendedCommunityCase,
40                 "The extended community %s is not Inet4SpecificExtendedCommunityCase type.",
41                 extendedCommunity);
42         final Inet4SpecificExtendedCommunity inet4SpecificExtendedCommunity
43                 = ((Inet4SpecificExtendedCommunityCase) extendedCommunity).getInet4SpecificExtendedCommunity();
44
45         final Inet4SpecificExtendedCommunityCommon common
46                 = inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunityCommon();
47         if (common != null) {
48             serializeCommon(inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunityCommon(), byteAggregator);
49         } else {
50             ByteBufWriteUtil.writeIpv4Address(inet4SpecificExtendedCommunity.getGlobalAdministrator(), byteAggregator);
51             byteAggregator.writeBytes(inet4SpecificExtendedCommunity.getLocalAdministrator());
52         }
53     }
54
55     @Override
56     public int getSubType() {
57         return SUBTYPE;
58     }
59
60 }