Merge "PCEPReplyMessageParser fix - separate reply's serialization"
[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.AttributeFlags;
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPError;
16 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
17 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
18 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
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.update.PathAttributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.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     public static final int MAX_ATTR_LENGTH_FOR_SINGLE_BYTE = 127;
31
32     private final NlriRegistry reg;
33
34     public MPUnreachAttributeParser(final NlriRegistry reg) {
35         this.reg = Preconditions.checkNotNull(reg);
36     }
37
38     @Override
39     public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
40         try {
41             final PathAttributes2 a = new PathAttributes2Builder().setMpUnreachNlri(this.reg.parseMpUnreach(buffer)).build();
42             builder.addAugmentation(PathAttributes2.class, a);
43         } catch (final BGPParsingException e) {
44             throw new BGPDocumentedException("Could not parse MP_UNREACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
45         }
46     }
47
48     @Override
49     public void serializeAttribute(DataObject attribute, ByteBuf byteAggregator) {
50         PathAttributes pathAttributes = (PathAttributes) attribute;
51         PathAttributes2 pathAttributes2 = pathAttributes.getAugmentation(PathAttributes2.class);
52         if (pathAttributes2 == null) {
53             return;
54         }
55         MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
56         ByteBuf unreachBuffer = Unpooled.buffer();
57         this.reg.serializeMpUnReach(mpUnreachNlri, unreachBuffer);
58         for (NlriSerializer nlriSerializer:this.reg.getSerializers()){
59             nlriSerializer.serializeAttribute(attribute,unreachBuffer );
60         }
61
62         if (unreachBuffer.writerIndex() > MAX_ATTR_LENGTH_FOR_SINGLE_BYTE) {
63             byteAggregator.writeByte(AttributeFlags.OPTIONAL | AttributeFlags.EXTENDED);
64             byteAggregator.writeByte(TYPE);
65             byteAggregator.writeShort(unreachBuffer.writerIndex());
66         } else {
67             byteAggregator.writeByte(AttributeFlags.OPTIONAL);
68             byteAggregator.writeByte(TYPE);
69             byteAggregator.writeByte(unreachBuffer.writerIndex());
70         }
71         byteAggregator.writeBytes(unreachBuffer);
72     }
73 }