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