BUG-730 : added test for bgp/concepts
[bgpcep.git] / bgp / concepts / src / main / java / org / opendaylight / bgp / concepts / 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.bgp.concepts;
9
10 import io.netty.buffer.ByteBuf;
11
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.Ipv4NextHopCaseBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
19 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;
20 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;
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.Ipv6NextHopBuilder;
22
23 /**
24  * Utility class for of CNextHop serialization and parsing.
25  */
26 public final class NextHopUtil {
27
28     private NextHopUtil() {
29     }
30
31     /**
32      * Writes serialized cnextHop IP address as byte value into ByteBuf.
33      *
34      * @param cnextHop next hop to be serialized
35      * @param byteAggregator where the next hop will be written
36      */
37     public static void serializeNextHop(final CNextHop cnextHop, final ByteBuf byteAggregator) {
38         if (cnextHop instanceof Ipv4NextHopCase) {
39             byteAggregator.writeBytes(Ipv4Util.bytesForAddress(((Ipv4NextHopCase) cnextHop).getIpv4NextHop().getGlobal()));
40         } else if (cnextHop instanceof Ipv6NextHopCase) {
41             final Ipv6NextHop nextHop = ((Ipv6NextHopCase) cnextHop).getIpv6NextHop();
42             if (nextHop.getGlobal() == null) {
43                 throw new IllegalArgumentException("Ipv6 Next Hop is missing Global address.");
44             }
45             byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getGlobal()));
46             if (nextHop.getLinkLocal() != null) {
47                 byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getLinkLocal()));
48             }
49         }
50     }
51
52     /**
53      * Parses CNextHop IP address from given ByteBuf.
54      *
55      * @param buffer contains byte array representation of CNextHop
56      * @return CNexthop object
57      */
58     public static CNextHop parseNextHop(final ByteBuf buffer) {
59         switch (buffer.writerIndex()) {
60         case Ipv4Util.IP4_LENGTH:
61             return new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(Ipv4Util.addressForByteBuf(buffer)).build()).build();
62         case Ipv6Util.IPV6_LENGTH:
63             return new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder().setGlobal(Ipv6Util.addressForByteBuf(buffer)).build()).build();
64         case Ipv6Util.IPV6_LENGTH * 2:
65             return new Ipv6NextHopCaseBuilder().setIpv6NextHop(
66                     new Ipv6NextHopBuilder().setGlobal(Ipv6Util.addressForByteBuf(buffer)).setLinkLocal(Ipv6Util.addressForByteBuf(buffer)).build()).build();
67         default:
68             throw new IllegalArgumentException("Cannot parse NEXT_HOP attribute. Wrong bytes length: " + buffer.writerIndex());
69         }
70     }
71 }