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;
13 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
16 import org.opendaylight.protocol.bgp.parser.BGPError;
17 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
19 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
20 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
21 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
22 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
30 public final class MPUnreachAttributeParser implements AttributeParser, AttributeSerializer {
31 public static final int TYPE = 15;
32 public static final int MAX_ATTR_LENGTH_FOR_SINGLE_BYTE = 127;
34 private final NlriRegistry reg;
36 public MPUnreachAttributeParser(final NlriRegistry reg) {
37 this.reg = Preconditions.checkNotNull(reg);
41 public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
43 final PathAttributes2 a = new PathAttributes2Builder().setMpUnreachNlri(this.reg.parseMpUnreach(buffer)).build();
44 builder.addAugmentation(PathAttributes2.class, a);
45 } catch (final BGPParsingException e) {
46 throw new BGPDocumentedException("Could not parse MP_UNREACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
51 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
52 final PathAttributes pathAttributes = (PathAttributes) attribute;
53 final PathAttributes2 pathAttributes2 = pathAttributes.getAugmentation(PathAttributes2.class);
54 if (pathAttributes2 == null) {
57 final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
58 final ByteBuf unreachBuffer = Unpooled.buffer();
59 this.reg.serializeMpUnReach(mpUnreachNlri, unreachBuffer);
60 for (final NlriSerializer nlriSerializer : this.reg.getSerializers()){
61 nlriSerializer.serializeAttribute(attribute,unreachBuffer);
63 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, unreachBuffer, byteAggregator);