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.rev180329.path.attributes.Attributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
32 public final class MPReachAttributeParser implements AttributeParser, AttributeSerializer {
34 public static final int TYPE = 14;
36 private final NlriRegistry reg;
38 public MPReachAttributeParser(final NlriRegistry reg) {
39 this.reg = requireNonNull(reg);
43 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException {
44 parseAttribute(buffer, builder, null);
48 public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder, final PeerSpecificParserConstraint constraint)
49 throws BGPDocumentedException {
51 final MpReachNlri mpReachNlri = this.reg.parseMpReach(buffer, constraint);
52 final Attributes1 a = new Attributes1Builder().setMpReachNlri(mpReachNlri).build();
53 builder.addAugmentation(Attributes1.class, a);
54 } catch (final BGPParsingException e) {
55 throw new BGPDocumentedException("Could not parse MP_REACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
60 public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
61 Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
62 final Attributes pathAttributes = (Attributes) attribute;
63 final Attributes1 pathAttributes1 = pathAttributes.getAugmentation(Attributes1.class);
64 if (pathAttributes1 == null) {
67 final MpReachNlri mpReachNlri = pathAttributes1.getMpReachNlri();
68 final ByteBuf reachBuffer = Unpooled.buffer();
69 this.reg.serializeMpReach(mpReachNlri, reachBuffer);
71 for (final NlriSerializer nlriSerializer : this.reg.getSerializers()) {
72 nlriSerializer.serializeAttribute(attribute, reachBuffer);
74 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, reachBuffer, byteAggregator);