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 / RouteOriginIpv4EcHandler.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
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
14 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
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.RouteOriginIpv4Case;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteOriginIpv4CaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.route.origin.ipv4._case.RouteOriginIpv4;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.route.origin.ipv4._case.RouteOriginIpv4Builder;
22 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
23
24 public final class RouteOriginIpv4EcHandler extends AbstractIpv4ExtendedCommunity {
25     private static final int SUBTYPE = 3;
26
27     @Override
28     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
29         checkArgument(extendedCommunity instanceof RouteOriginIpv4Case,
30                 "The extended community %s is not RouteOriginIpv4Case type.", extendedCommunity);
31         final RouteOriginIpv4 routeTarget = ((RouteOriginIpv4Case) extendedCommunity).getRouteOriginIpv4();
32         Ipv4Util.writeIpv4Address(routeTarget.getGlobalAdministrator(), byteAggregator);
33         ByteBufUtils.writeOrZero(byteAggregator, routeTarget.getLocalAdministrator());
34     }
35
36     @Override
37     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer)
38             throws BGPDocumentedException, BGPParsingException {
39         return new RouteOriginIpv4CaseBuilder()
40                 .setRouteOriginIpv4(new RouteOriginIpv4Builder()
41                     .setGlobalAdministrator(Ipv4Util.addressForByteBuf(buffer))
42                     .setLocalAdministrator(ByteBufUtils.readUint16(buffer))
43                     .build())
44                 .build();
45     }
46
47     @Override
48     public int getSubType() {
49         return SUBTYPE;
50     }
51 }