Moved Ipv4Util and Ipv6Util from concepts to util
[bgpcep.git] / bgp / concepts / src / main / java / org / opendaylight / protocol / bgp / concepts / util / NextHopUtil.java
1 /*
2  * Copyright (c) 2014 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.concepts.util;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
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.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;
18
19 /**
20  * Utility class for of CNextHop attribute serialization.
21  */
22 public class NextHopUtil {
23
24     private NextHopUtil() {
25     }
26
27     public static void serializeNextHop(DataObject attribute, ByteBuf byteAggregator) {
28         serializeNextHop((CNextHop) attribute, byteAggregator);
29     }
30
31     /**
32      * Writes serialized cnextHop attribute ip address as byte value into byteAggregator without serializing attributes
33      * length.
34      *
35      * @param cnextHop
36      * @param byteAggregator
37      */
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);
45         }
46     }
47
48     /**
49      * Writes serialized cnextHop attribute ip address as byte value into byteAggregator with serializing attributes
50      * length.
51      *
52      * @param cnextHop
53      * @param byteAggregator
54      */
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);
60     }
61
62     /**
63      * Writes nextHopCase attributes ipv4 address as bytes to byteAggregator.
64      *
65      * @param nextHopCase
66      * @param byteAggregator
67      */
68     public static void serializeIpv4NextHop(Ipv4NextHopCase nextHopCase, ByteBuf byteAggregator) {
69         byteAggregator.writeBytes(Ipv4Util.bytesForAddress(nextHopCase.getIpv4NextHop().getGlobal()));
70     }
71
72     /**
73      * Writes nextHopCase attributes ipv6 address as bytes to byteAggregator.
74      *
75      * @param nextHopCase
76      * @param byteAggregator
77      */
78     public static void serializeIpv6NextHop(Ipv6NextHopCase nextHopCase, ByteBuf byteAggregator) {
79         if (nextHopCase.getIpv6NextHop().getGlobal() != null) {
80             byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHopCase.getIpv6NextHop().getGlobal()));
81         }
82         if (nextHopCase.getIpv6NextHop().getLinkLocal() != null) {
83             byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHopCase.getIpv6NextHop().getLinkLocal()));
84         }
85
86     }
87 }