YANG revision dates mass-update
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / NextHopAttributeParser.java
1 /*
2  * Copyright (c) 2013 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.protocol.bgp.parser.impl.message.update;
9
10 import static org.opendaylight.protocol.util.Ipv4Util.IP4_LENGTH;
11 import static org.opendaylight.protocol.util.Ipv6Util.IPV6_LENGTH;
12
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;
27
28 public final class NextHopAttributeParser extends AbstractAttributeParser implements AttributeSerializer {
29     public static final int TYPE = 3;
30
31     @Override
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;
37         switch (readable) {
38             case IP4_LENGTH:
39                 nextHop = NextHopUtil.parseNextHopIpv4(buffer);
40                 break;
41             case IPV6_LENGTH:
42                 nextHop = NextHopUtil.parseNextHopIpv6(buffer);
43                 break;
44             case IPV6_LENGTH * 2:
45                 nextHop = NextHopUtil.parseNextHopFullIpv6(buffer);
46                 break;
47             default:
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);
51         }
52
53         builder.setCNextHop(nextHop);
54     }
55
56     @Override
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);
63         }
64     }
65 }