04e56ec2e7e1d8c1eb736e9bd2dd1e44091e90da
[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 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.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         throw new UnsupportedOperationException();
30     }
31
32     /**
33      * Writes serialized cnextHop IP address as byte value into ByteBuf.
34      *
35      * @param cnextHop next hop to be serialized
36      * @param byteAggregator where the next hop will be written
37      */
38     public static void serializeNextHop(final CNextHop cnextHop, final ByteBuf byteAggregator) {
39         if (cnextHop instanceof Ipv4NextHopCase) {
40             byteAggregator.writeBytes(Ipv4Util.bytesForAddress(((Ipv4NextHopCase) cnextHop).getIpv4NextHop().getGlobal()));
41         } else if (cnextHop instanceof Ipv6NextHopCase) {
42             final Ipv6NextHop nextHop = ((Ipv6NextHopCase) cnextHop).getIpv6NextHop();
43             Preconditions.checkArgument(nextHop.getGlobal() != null, "Ipv6 Next Hop is missing Global address.");
44             byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getGlobal()));
45             if (nextHop.getLinkLocal() != null) {
46                 byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getLinkLocal()));
47             }
48         }
49     }
50
51     /**
52      * Parses CNextHop IP address from given ByteBuf.
53      *
54      * @param buffer contains byte array representation of CNextHop
55      * @return CNexthop object
56      */
57     public static CNextHop parseNextHop(final ByteBuf buffer) {
58         switch (buffer.writerIndex()) {
59         case Ipv4Util.IP4_LENGTH:
60             return new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(Ipv4Util.addressForByteBuf(buffer)).build()).build();
61         case Ipv6Util.IPV6_LENGTH:
62             return new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder().setGlobal(Ipv6Util.addressForByteBuf(buffer)).build()).build();
63         case Ipv6Util.IPV6_LENGTH * 2:
64             return new Ipv6NextHopCaseBuilder().setIpv6NextHop(
65                     new Ipv6NextHopBuilder().setGlobal(Ipv6Util.addressForByteBuf(buffer)).setLinkLocal(Ipv6Util.addressForByteBuf(buffer)).build()).build();
66         default:
67             throw new IllegalArgumentException("Cannot parse NEXT_HOP attribute. Wrong bytes length: " + buffer.writerIndex());
68         }
69     }
70 }