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.protocol.bgp.concepts.util;
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import org.opendaylight.protocol.concepts.Ipv4Util;
13 import org.opendaylight.protocol.concepts.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.Ipv4NextHopCase;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
20 * Utility class for of CNextHop attribute serialization.
22 public class NextHopUtil {
24 private NextHopUtil() {
27 public static void serializeNextHop(DataObject attribute, ByteBuf byteAggregator) {
28 serializeNextHop((CNextHop) attribute, byteAggregator);
32 * Writes serialized cnextHop attribute ip address as byte value into byteAggregator without serializing attributes
36 * @param byteAggregator
38 public static void serializeNextHopSimple(CNextHop cnextHop, ByteBuf byteAggregator) {
39 if (cnextHop instanceof Ipv4NextHopCase) {
40 Ipv4NextHopCase nextHop = (Ipv4NextHopCase) cnextHop;
41 serializeIpv4NextHop(nextHop, byteAggregator);
42 } else if (cnextHop instanceof Ipv6NextHopCase) {
43 Ipv6NextHopCase nextHop = (Ipv6NextHopCase) cnextHop;
44 serializeIpv6NextHop(nextHop, byteAggregator);
49 * Writes serialized cnextHop attribute ip address as byte value into byteAggregator with serializing attributes
53 * @param byteAggregator
55 public static void serializeNextHop(CNextHop cnextHop, ByteBuf byteAggregator) {
56 ByteBuf nextHopBuffer = Unpooled.buffer();
57 serializeNextHopSimple(cnextHop, nextHopBuffer);
58 byteAggregator.writeByte(nextHopBuffer.writerIndex());
59 byteAggregator.writeBytes(nextHopBuffer);
63 * Writes nextHopCase attributes ipv4 address as bytes to byteAggregator.
66 * @param byteAggregator
68 public static void serializeIpv4NextHop(Ipv4NextHopCase nextHopCase, ByteBuf byteAggregator) {
69 byteAggregator.writeBytes(Ipv4Util.bytesForAddress(nextHopCase.getIpv4NextHop().getGlobal()));
73 * Writes nextHopCase attributes ipv6 address as bytes to byteAggregator.
76 * @param byteAggregator
78 public static void serializeIpv6NextHop(Ipv6NextHopCase nextHopCase, ByteBuf byteAggregator) {
79 if (nextHopCase.getIpv6NextHop().getGlobal() != null) {
80 byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHopCase.getIpv6NextHop().getGlobal()));
82 if (nextHopCase.getIpv6NextHop().getLinkLocal() != null) {
83 byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHopCase.getIpv6NextHop().getLinkLocal()));