a2a6c22cba7bbcc4a8be1e248bd4b24295395122
[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)
42                     .getIpv4NextHop().getGlobal()));
43         } else if (cnextHop instanceof Ipv6NextHopCase) {
44             final Ipv6NextHop nextHop = ((Ipv6NextHopCase) cnextHop).getIpv6NextHop();
45             Preconditions.checkArgument(nextHop.getGlobal() != null,
46                     "Ipv6 Next Hop is missing Global address.");
47             byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getGlobal()));
48             if (nextHop.getLinkLocal() != null) {
49                 byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getLinkLocal()));
50             }
51         } else if (!(cnextHop instanceof EmptyNextHopCase)) {
52             throw new IllegalArgumentException("Cannot serialize NEXT_HOP. Class not supported: " + cnextHop);
53         }
54     }
55
56     /**
57      * Parses CNextHop IP address from given ByteBuf.
58      *
59      * @param buffer contains byte array representation of CNextHop
60      * @return CNexthop object
61      */
62     public static CNextHop parseNextHop(final ByteBuf buffer) {
63         switch (buffer.writerIndex()) {
64             case Ipv4Util.IP4_LENGTH:
65                 return new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder()
66                         .setGlobal(Ipv4Util.addressForByteBuf(buffer)).build()).build();
67             case Ipv6Util.IPV6_LENGTH:
68                 return new Ipv6NextHopCaseBuilder().setIpv6NextHop(new Ipv6NextHopBuilder()
69                         .setGlobal(Ipv6Util.addressForByteBuf(buffer)).build()).build();
70             case Ipv6Util.IPV6_LENGTH * 2:
71                 return new Ipv6NextHopCaseBuilder().setIpv6NextHop(
72                         new Ipv6NextHopBuilder().setGlobal(Ipv6Util.addressForByteBuf(buffer))
73                                 .setLinkLocal(Ipv6Util.addressForByteBuf(buffer)).build()).build();
74             default:
75                 throw new IllegalArgumentException("Cannot parse NEXT_HOP attribute. Wrong bytes length: "
76                         + buffer.writerIndex());
77         }
78     }
79 }