2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.flowspec.extended.communities;
10 import static com.google.common.base.Preconditions.checkArgument;
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.protocol.util.Ipv6Util;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.ip.nh.extended.community.RedirectIpNhExtendedCommunity;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.redirect.ip.nh.extended.community.RedirectIpNhExtendedCommunityBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.update.attributes.extended.communities.extended.community.RedirectIpNhExtendedCommunityCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
23 public class RedirectIpNextHopEcHandler implements ExtendedCommunityParser, ExtendedCommunitySerializer {
24 //https://tools.ietf.org/html/draft-ietf-idr-flowspec-redirect-ip-00#section-7
25 private static final int TYPE = 8;
26 private static final int SUBTYPE = 0;
27 private static final byte COPY = 0x1;
30 public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity, final ByteBuf byteAggregator) {
31 checkArgument(extendedCommunity instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
32 .flowspec.rev180329.RedirectIpNhExtendedCommunity,
33 "The extended community %s is not RedirectIpNhExtendedCommunityCase type.", extendedCommunity);
34 final RedirectIpNhExtendedCommunity redirect = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
35 .yang.bgp.flowspec.rev180329.RedirectIpNhExtendedCommunity) extendedCommunity)
36 .getRedirectIpNhExtendedCommunity();
37 final IpAddress nextHopAddress = redirect.getNextHopAddress();
38 if (nextHopAddress.getIpv4Address() != null) {
39 Ipv4Util.writeIpv4Address(nextHopAddress.getIpv4Address(), byteAggregator);
41 Ipv6Util.writeIpv6Address(nextHopAddress.getIpv6Address(), byteAggregator);
43 byteAggregator.writeShort(Boolean.TRUE.equals(redirect.isCopy()) ? 1 : 0);
47 public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) {
48 final RedirectIpNhExtendedCommunityBuilder builder = new RedirectIpNhExtendedCommunityBuilder();
49 if (buffer.readableBytes() > Ipv6Util.IPV6_LENGTH) {
50 builder.setNextHopAddress(new IpAddress(Ipv6Util.addressForByteBuf(buffer)));
52 builder.setNextHopAddress(new IpAddress(Ipv4Util.addressForByteBuf(buffer)));
54 builder.setCopy((buffer.readUnsignedShort() & COPY) == 1);
55 return new RedirectIpNhExtendedCommunityCaseBuilder().setRedirectIpNhExtendedCommunity(builder.build()).build();
59 public int getType(final boolean isTransitive) {
64 public int getSubType() {