2 * Copyright (c) 2014 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.bgp.concepts;
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.protocol.util.Ipv4Util;
13 import org.opendaylight.protocol.util.Ipv6Util;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.EmptyNextHopCase;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHop;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
25 * Utility class for of CNextHop serialization and parsing.
27 public final class NextHopUtil {
29 private NextHopUtil() {
30 throw new UnsupportedOperationException();
34 * Writes serialized cnextHop IP address as byte value into ByteBuf.
36 * @param cnextHop next hop to be serialized
37 * @param byteAggregator where the next hop will be written
39 public static void serializeNextHop(final CNextHop cnextHop, final ByteBuf byteAggregator) {
40 if (cnextHop instanceof Ipv4NextHopCase) {
41 byteAggregator.writeBytes(Ipv4Util.bytesForAddress(((Ipv4NextHopCase) cnextHop).getIpv4NextHop().getGlobal()));
42 } else if (cnextHop instanceof Ipv6NextHopCase) {
43 final Ipv6NextHop nextHop = ((Ipv6NextHopCase) cnextHop).getIpv6NextHop();
44 Preconditions.checkArgument(nextHop.getGlobal() != null, "Ipv6 Next Hop is missing Global address.");
45 byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getGlobal()));
46 if (nextHop.getLinkLocal() != null) {
47 byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getLinkLocal()));
49 } else if (!(cnextHop instanceof EmptyNextHopCase)) {
50 throw new IllegalArgumentException("Cannot serialize NEXT_HOP. Class not supported: " + cnextHop);
55 * Parses CNextHop IP address from given ByteBuf.
57 * @param buffer contains byte array representation of CNextHop
58 * @return CNexthop object
60 public static CNextHop parseNextHop(final ByteBuf buffer) {
61 switch (buffer.writerIndex()) {
62 case Ipv4Util.IP4_LENGTH:
63 return new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(Ipv4Util.addressForByteBuf(buffer)).build()).build();
64 case Ipv6Util.IPV6_LENGTH:
65 return new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder().setGlobal(Ipv6Util.addressForByteBuf(buffer)).build()).build();
66 case Ipv6Util.IPV6_LENGTH * 2:
67 return new Ipv6NextHopCaseBuilder().setIpv6NextHop(
68 new Ipv6NextHopBuilder().setGlobal(Ipv6Util.addressForByteBuf(buffer)).setLinkLocal(Ipv6Util.addressForByteBuf(buffer)).build()).build();
70 throw new IllegalArgumentException("Cannot parse NEXT_HOP attribute. Wrong bytes length: " + buffer.writerIndex());