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.BGPDocumentedException;
14 import org.opendaylight.protocol.bgp.parser.BGPError;
15 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
16 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
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.path.attributes.Attributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
28 public final class MPUnreachAttributeParser implements AttributeParser, AttributeSerializer {
29 public static final int TYPE = 15;
31 private final NlriRegistry reg;
33 public MPUnreachAttributeParser(final NlriRegistry reg) {
34 this.reg = Preconditions.checkNotNull(reg);
38 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException {
40 final Attributes2 a = new Attributes2Builder().setMpUnreachNlri(this.reg.parseMpUnreach(buffer)).build();
41 builder.addAugmentation(Attributes2.class, a);
42 } catch (final BGPParsingException e) {
43 throw new BGPDocumentedException("Could not parse MP_UNREACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
48 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
49 Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
50 final Attributes pathAttributes = (Attributes) attribute;
51 final Attributes2 pathAttributes2 = pathAttributes.getAugmentation(Attributes2.class);
52 if (pathAttributes2 == null) {
55 final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
56 final ByteBuf unreachBuffer = Unpooled.buffer();
57 this.reg.serializeMpUnReach(mpUnreachNlri, unreachBuffer);
58 for (final NlriSerializer nlriSerializer : this.reg.getSerializers()){
59 nlriSerializer.serializeAttribute(attribute,unreachBuffer);
61 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, unreachBuffer, byteAggregator);