BUG-2982 : moved path-attributes container to grouping
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / MPReachAttributeParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.parser.impl.message.update;
9
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.Attributes1;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27
28
29 public final class MPReachAttributeParser implements AttributeParser, AttributeSerializer {
30
31     public static final int TYPE = 14;
32
33     private final NlriRegistry reg;
34
35     public MPReachAttributeParser(final NlriRegistry reg) {
36         this.reg = Preconditions.checkNotNull(reg);
37     }
38
39     @Override
40     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException {
41         try {
42             final Attributes1 a = new Attributes1Builder().setMpReachNlri(this.reg.parseMpReach(buffer)).build();
43             builder.addAugmentation(Attributes1.class, a);
44         } catch (final BGPParsingException e) {
45             throw new BGPDocumentedException("Could not parse MP_REACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
46         }
47     }
48
49     @Override
50     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
51         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
52         final Attributes pathAttributes = (Attributes) attribute;
53         final Attributes1 pathAttributes1 = pathAttributes.getAugmentation(Attributes1.class);
54         if (pathAttributes1 == null) {
55             return;
56         }
57         final MpReachNlri mpReachNlri = pathAttributes1.getMpReachNlri();
58         final ByteBuf reachBuffer = Unpooled.buffer();
59         this.reg.serializeMpReach(mpReachNlri, reachBuffer);
60
61         for (final NlriSerializer nlriSerializer : this.reg.getSerializers()) {
62             nlriSerializer.serializeAttribute(attribute, reachBuffer);
63         }
64         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, reachBuffer, byteAggregator);
65     }
66 }