2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.parser.impl.message.update;
10 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
14 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
15 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
16 import org.opendaylight.protocol.concepts.Ipv4Util;
17 import org.opendaylight.protocol.concepts.Ipv6Util;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
25 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;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
28 public final class NextHopAttributeParser implements AttributeParser, AttributeSerializer {
30 public static final int TYPE = 3;
33 public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) {
34 Preconditions.checkArgument(buffer.readableBytes() == Ipv4Util.IP4_LENGTH, "Length of byte array for NEXT_HOP should be %s, but is %s", buffer.readableBytes(), Ipv4Util.IP4_LENGTH);
35 builder.setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(
36 new Ipv4NextHopBuilder().setGlobal(Ipv4Util.addressForBytes(ByteArray.readAllBytes(buffer))).build()).build());
40 public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
41 PathAttributes pathAttributes = (PathAttributes) attribute;
42 CNextHop cNextHop = pathAttributes.getCNextHop();
43 if (cNextHop == null) {
46 if (cNextHop instanceof Ipv4NextHopCase) {
47 Ipv4NextHopCase nextHop = (Ipv4NextHopCase) cNextHop;
48 byteAggregator.writeBytes(Ipv4Util.bytesForAddress(nextHop.getIpv4NextHop().getGlobal()));
49 } else if (cNextHop instanceof Ipv6NextHopCase) {
50 Ipv6NextHopCase nextHop = (Ipv6NextHopCase) cNextHop;
51 if (nextHop.getIpv6NextHop().getGlobal() != null) {
52 byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getIpv6NextHop().getGlobal()));
54 if (nextHop.getIpv6NextHop().getLinkLocal() != null) {
55 byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getIpv6NextHop().getLinkLocal()));