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;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.bgp.parser.AttributeFlags;
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPError;
16 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
19 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
20 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
29 public final class MPReachAttributeParser implements AttributeParser, AttributeSerializer {
31 public static final int TYPE = 14;
32 public static final int ATTR_LENGTH = 2;
33 public static final int MAX_ATTR_LENGTH_FOR_SINGLE_BYTE = 127;
34 private final NlriRegistry reg;
36 public MPReachAttributeParser(final NlriRegistry reg) {
37 this.reg = Preconditions.checkNotNull(reg);
41 public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
43 final PathAttributes1 a = new PathAttributes1Builder().setMpReachNlri(this.reg.parseMpReach(buffer)).build();
44 builder.addAugmentation(PathAttributes1.class, a);
45 } catch (final BGPParsingException e) {
46 throw new BGPDocumentedException("Could not parse MP_REACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
51 public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
52 PathAttributes pathAttributes = (PathAttributes) attribute;
53 PathAttributes1 pathAttributes1 = pathAttributes.getAugmentation(PathAttributes1.class);
54 if (pathAttributes1 == null) {
57 MpReachNlri mpReachNlri = pathAttributes1.getMpReachNlri();
58 ByteBuf reachBuffer = Unpooled.buffer();
59 this.reg.serializeMpReach(mpReachNlri, reachBuffer);
61 for (NlriSerializer nlriSerializer : this.reg.getSerializers()) {
62 nlriSerializer.serializeAttribute(attribute, reachBuffer);
65 if (reachBuffer.writerIndex() > MAX_ATTR_LENGTH_FOR_SINGLE_BYTE) {
66 byteAggregator.writeByte(AttributeFlags.OPTIONAL | AttributeFlags.EXTENDED);
67 byteAggregator.writeByte(TYPE);
68 byteAggregator.writeShort(reachBuffer.writerIndex());
70 byteAggregator.writeByte(AttributeFlags.OPTIONAL);
71 byteAggregator.writeByte(TYPE);
72 byteAggregator.writeByte(reachBuffer.writerIndex());
74 byteAggregator.writeBytes(reachBuffer);