BUG-113: Create NlriRegistry and use it
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / MPUnreachAttributeParser.java
1 package org.opendaylight.protocol.bgp.parser.impl.message.update;
2
3 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
4 import org.opendaylight.protocol.bgp.parser.BGPError;
5 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
6 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
7 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributesBuilder;
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes2;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes2Builder;
11
12 import com.google.common.base.Preconditions;
13
14 final class MPUnreachAttributeParser implements AttributeParser {
15         static final int TYPE = 15;
16
17         private final NlriRegistry reg;
18
19         MPUnreachAttributeParser(final NlriRegistry reg) {
20                 this.reg = Preconditions.checkNotNull(reg);
21         }
22
23         @Override
24         public void parseAttribute(final byte[] bytes, final PathAttributesBuilder builder) throws BGPDocumentedException {
25                 try {
26                         final PathAttributes2 a = new PathAttributes2Builder().setMpUnreachNlri(reg.parseMpUnreach(bytes)).build();
27                         builder.addAugmentation(PathAttributes2.class, a);
28                 } catch (final BGPParsingException e) {
29                         throw new BGPDocumentedException("Could not parse MP_UNREACH_NLRI: " + e.getMessage(), BGPError.OPT_ATTR_ERROR);
30                 }
31         }
32 }