Move ipv4/ipv6 ByteBuf utilities to Ipv{4,6}Util
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / extended / communities / RedirectIpv4EcHandler.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.flowspec.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.spi.extended.community.ExtendedCommunityParser;
14 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
15 import org.opendaylight.protocol.util.Ipv4Util;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.ipv4.extended.community.RedirectIpv4;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.ipv4.extended.community.RedirectIpv4Builder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.extended.communities.extended.community.RedirectIpv4ExtendedCommunityCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
20 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
21
22 public class RedirectIpv4EcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
23     private static final int TYPE = 129;
24     private static final int SUBTYPE = 8;
25
26     @Override
27     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
28         checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
29             .flowspec.rev180329.RedirectIpv4ExtendedCommunity,
30                 "The extended community %s is not RedirectIpv4ExtendedCommunityCase type.", extendedCommunity);
31         final RedirectIpv4 redirect = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec
32                 .rev180329.RedirectIpv4ExtendedCommunity) extendedCommunity).getRedirectIpv4();
33         Ipv4Util.writeIpv4Address(redirect.getGlobalAdministrator(), byteAggregator);
34         ByteBufUtils.writeOrZero(byteAggregator, redirect.getLocalAdministrator());
35     }
36
37     @Override
38     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) {
39         return new RedirectIpv4ExtendedCommunityCaseBuilder()
40                 .setRedirectIpv4(new RedirectIpv4Builder()
41                     .setGlobalAdministrator(Ipv4Util.addressForByteBuf(buffer))
42                     .setLocalAdministrator(ByteBufUtils.readUint16(buffer))
43                     .build())
44                 .build();
45     }
46
47     @Override
48     public int getType(final boolean isTransitive) {
49         return TYPE;
50     }
51
52     @Override
53     public int getSubType() {
54         return SUBTYPE;
55     }
56 }