Redice use of ByteBufWriteUtil in flowspec
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / extended / communities / RedirectIpNextHopEcHandler.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.ByteBufWriteUtil;
16 import org.opendaylight.protocol.util.Ipv4Util;
17 import org.opendaylight.protocol.util.Ipv6Util;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.ip.nh.extended.community.RedirectIpNhExtendedCommunity;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.ip.nh.extended.community.RedirectIpNhExtendedCommunityBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.extended.communities.extended.community.RedirectIpNhExtendedCommunityCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
23
24 public class RedirectIpNextHopEcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
25     //https://tools.ietf.org/html/draft-ietf-idr-flowspec-redirect-ip-00#section-7
26     private static final int TYPE = 8;
27     private static final int SUBTYPE = 0;
28     private static final byte COPY = 0x1;
29
30     @Override
31     public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
32         checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
33             .flowspec.rev180329.RedirectIpNhExtendedCommunity,
34                 "The extended community %s is not RedirectIpNhExtendedCommunityCase type.", extendedCommunity);
35         final RedirectIpNhExtendedCommunity redirect = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
36                 .yang.bgp.flowspec.rev180329.RedirectIpNhExtendedCommunity) extendedCommunity)
37                 .getRedirectIpNhExtendedCommunity();
38         final IpAddress nextHopAddress = redirect.getNextHopAddress();
39         if (nextHopAddress.getIpv4Address() != null) {
40             ByteBufWriteUtil.writeIpv4Address(nextHopAddress.getIpv4Address(), byteAggregator);
41         } else {
42             ByteBufWriteUtil.writeIpv6Address(nextHopAddress.getIpv6Address(), byteAggregator);
43         }
44         byteAggregator.writeShort(Boolean.TRUE.equals(redirect.isCopy()) ? 1 : 0);
45     }
46
47     @Override
48     public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) {
49         final RedirectIpNhExtendedCommunityBuilder builder = new RedirectIpNhExtendedCommunityBuilder();
50         if (buffer.readableBytes() > Ipv6Util.IPV6_LENGTH) {
51             builder.setNextHopAddress(new IpAddress(Ipv6Util.addressForByteBuf(buffer)));
52         } else {
53             builder.setNextHopAddress(new IpAddress(Ipv4Util.addressForByteBuf(buffer)));
54         }
55         builder.setCopy((buffer.readUnsignedShort() & COPY) == 1);
56         return new RedirectIpNhExtendedCommunityCaseBuilder().setRedirectIpNhExtendedCommunity(builder.build()).build();
57     }
58
59     @Override
60     public int getType(final boolean isTransitive) {
61         return TYPE;
62     }
63
64     @Override
65     public int getSubType() {
66         return SUBTYPE;
67     }
68 }