Move ipv4/ipv6 ByteBuf utilities to Ipv{4,6}Util
[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 package org.opendaylight.protocol.bgp.parser.impl.message.update.extended.communities;
9
10 import static com.google.common.base.Preconditions.checkArgument;
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 io.netty.buffer.ByteBuf;
15 import org.opendaylight.protocol.bgp.parser.spi.extended.community.AbstractIpv4ExtendedCommunity;
16 import org.opendaylight.protocol.util.Ipv4Util;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.Inet4SpecificExtendedCommunityCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.Inet4SpecificExtendedCommunityCaseBuilder;
20 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;
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.Inet4SpecificExtendedCommunityBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.inet4.specific.extended.community.common.Inet4SpecificExtendedCommunityCommon;
23
24 public final class Ipv4SpecificEcHandler extends AbstractIpv4ExtendedCommunity {
25     private static final int SUBTYPE = 0;
26
27     @Override
28     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) {
29         return new Inet4SpecificExtendedCommunityCaseBuilder().setInet4SpecificExtendedCommunity(
30                 new Inet4SpecificExtendedCommunityBuilder()
31                         .setInet4SpecificExtendedCommunityCommon(parseCommon(buffer))
32                         .build()).build();
33     }
34
35     @Override
36     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
37         checkArgument(extendedCommunity instanceof Inet4SpecificExtendedCommunityCase,
38                 "The extended community %s is not Inet4SpecificExtendedCommunityCase type.", extendedCommunity);
39         final Inet4SpecificExtendedCommunity inet4SpecificExtendedCommunity
40                 = ((Inet4SpecificExtendedCommunityCase) extendedCommunity).getInet4SpecificExtendedCommunity();
41
42         final Inet4SpecificExtendedCommunityCommon common
43                 = inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunityCommon();
44         if (common != null) {
45             serializeCommon(inet4SpecificExtendedCommunity.getInet4SpecificExtendedCommunityCommon(), byteAggregator);
46         } else {
47             Ipv4Util.writeIpv4Address(inet4SpecificExtendedCommunity.getGlobalAdministrator(), byteAggregator);
48             byteAggregator.writeBytes(inet4SpecificExtendedCommunity.getLocalAdministrator());
49         }
50     }
51
52     @Override
53     public int getSubType() {
54         return SUBTYPE;
55     }
56 }