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 static org.opendaylight.protocol.util.Ipv4Util.IP4_LENGTH;
11 import static org.opendaylight.protocol.util.Ipv6Util.IPV6_LENGTH;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.bgp.concepts.NextHopUtil;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPError;
18 import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
19 import org.opendaylight.protocol.bgp.parser.spi.AbstractAttributeParser;
20 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
21 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
22 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
23 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.CNextHop;
28 public final class NextHopAttributeParser extends AbstractAttributeParser implements AttributeSerializer {
29 public static final int TYPE = 3;
32 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder,
33 final RevisedErrorHandling errorHandling, final PeerSpecificParserConstraint constraint)
34 throws BGPDocumentedException, BGPTreatAsWithdrawException {
35 final int readable = buffer.readableBytes();
36 final CNextHop nextHop;
39 nextHop = NextHopUtil.parseNextHopIpv4(buffer);
42 nextHop = NextHopUtil.parseNextHopIpv6(buffer);
45 nextHop = NextHopUtil.parseNextHopFullIpv6(buffer);
48 throw errorHandling.reportError(BGPError.ATTR_LENGTH_ERROR,
49 "NEXT_HOP attribute is expected to have length of {%s, %s, %s} but has %s", IP4_LENGTH, IPV6_LENGTH,
50 IPV6_LENGTH * 2, readable);
53 builder.setCNextHop(nextHop);
57 public void serializeAttribute(final Attributes attribute, final ByteBuf byteAggregator) {
58 final CNextHop cNextHop = attribute.getCNextHop();
59 if (cNextHop != null) {
60 final ByteBuf nextHopBuffer = Unpooled.buffer();
61 NextHopUtil.serializeNextHop(cNextHop, nextHopBuffer);
62 AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE, TYPE, nextHopBuffer, byteAggregator);