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 java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 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.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.Attributes2Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.update.attributes.MpUnreachNlri;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
31 public final class MPUnreachAttributeParser implements AttributeParser, AttributeSerializer {
32 public static final int TYPE = 15;
34 private final NlriRegistry reg;
36 public MPUnreachAttributeParser(final NlriRegistry reg) {
37 this.reg = requireNonNull(reg);
41 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException {
42 parseAttribute(buffer, builder, null);
46 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder, final PeerSpecificParserConstraint constraint)
47 throws BGPDocumentedException {
49 final MpUnreachNlri mpUnreachNlri = this.reg.parseMpUnreach(buffer, constraint);
50 final Attributes2 a = new Attributes2Builder().setMpUnreachNlri(mpUnreachNlri).build();
51 builder.addAugmentation(Attributes2.class, a);
52 } catch (final BGPParsingException e) {
53 throw new BGPDocumentedException("Could not parse MP_UNREACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
58 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
59 Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
60 final Attributes pathAttributes = (Attributes) attribute;
61 final Attributes2 pathAttributes2 = pathAttributes.getAugmentation(Attributes2.class);
62 if (pathAttributes2 == null) {
65 final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
66 final ByteBuf unreachBuffer = Unpooled.buffer();
67 this.reg.serializeMpUnReach(mpUnreachNlri, unreachBuffer);
68 for (final NlriSerializer nlriSerializer : this.reg.getSerializers()){
69 nlriSerializer.serializeAttribute(attribute,unreachBuffer);
71 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, unreachBuffer, byteAggregator);