BUG-2982 : moved path-attributes container to grouping
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / MPUnreachAttributeParser.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.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;
27
28 public final class MPUnreachAttributeParser implements AttributeParser, AttributeSerializer {
29     public static final int TYPE = 15;
30
31     private final NlriRegistry reg;
32
33     public MPUnreachAttributeParser(final NlriRegistry reg) {
34         this.reg = Preconditions.checkNotNull(reg);
35     }
36
37     @Override
38     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException {
39         try {
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);
44         }
45     }
46
47     @Override
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) {
53             return;
54         }
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);
60         }
61         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, unreachBuffer, byteAggregator);
62     }
63 }