Merge "BUG-611 : cleaned up advertized/withdrawn routes."
[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
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29
30 public final class MPUnreachAttributeParser implements AttributeParser, AttributeSerializer {
31     public static final int TYPE = 15;
32     public static final int MAX_ATTR_LENGTH_FOR_SINGLE_BYTE = 127;
33
34     private final NlriRegistry reg;
35
36     public MPUnreachAttributeParser(final NlriRegistry reg) {
37         this.reg = Preconditions.checkNotNull(reg);
38     }
39
40     @Override
41     public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPDocumentedException {
42         try {
43             final PathAttributes2 a = new PathAttributes2Builder().setMpUnreachNlri(this.reg.parseMpUnreach(buffer)).build();
44             builder.addAugmentation(PathAttributes2.class, a);
45         } catch (final BGPParsingException e) {
46             throw new BGPDocumentedException("Could not parse MP_UNREACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
47         }
48     }
49
50     @Override
51     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
52         final PathAttributes pathAttributes = (PathAttributes) attribute;
53         final PathAttributes2 pathAttributes2 = pathAttributes.getAugmentation(PathAttributes2.class);
54         if (pathAttributes2 == null) {
55             return;
56         }
57         final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
58         final ByteBuf unreachBuffer = Unpooled.buffer();
59         this.reg.serializeMpUnReach(mpUnreachNlri, unreachBuffer);
60         for (final NlriSerializer nlriSerializer : this.reg.getSerializers()){
61             nlriSerializer.serializeAttribute(attribute,unreachBuffer);
62         }
63         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, unreachBuffer, byteAggregator);
64     }
65 }