Bump versions by x.y.(z+1)
[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.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;
23
24 /**
25  * Utility class for of CNextHop serialization and parsing.
26  */
27 public final class NextHopUtil {
28
29     private NextHopUtil() {
30         throw new UnsupportedOperationException();
31     }
32
33     /**
34      * Writes serialized cnextHop IP address as byte value into ByteBuf.
35      *
36      * @param cnextHop next hop to be serialized
37      * @param byteAggregator where the next hop will be written
38      */
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()));
48             }
49         } else if (!(cnextHop instanceof EmptyNextHopCase)) {
50             throw new IllegalArgumentException("Cannot serialize NEXT_HOP. Class not supported: " + cnextHop);
51         }
52     }
53
54     /**
55      * Parses CNextHop IP address from given ByteBuf.
56      *
57      * @param buffer contains byte array representation of CNextHop
58      * @return CNexthop object
59      */
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();
69         default:
70             throw new IllegalArgumentException("Cannot parse NEXT_HOP attribute. Wrong bytes length: " + buffer.writerIndex());
71         }
72     }
73 }