Update As4 Extended communities
[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.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28
29
30 public final class MPReachAttributeParser implements AttributeParser, AttributeSerializer {
31
32     public static final int TYPE = 14;
33
34     private final NlriRegistry reg;
35
36     public MPReachAttributeParser(final NlriRegistry reg) {
37         this.reg = Preconditions.checkNotNull(reg);
38     }
39
40     @Override
41     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException {
42         parseAttribute(buffer, builder, null);
43     }
44
45     @Override
46     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder, final PeerSpecificParserConstraint constraint)
47             throws BGPDocumentedException {
48         try {
49             final MpReachNlri mpReachNlri = this.reg.parseMpReach(buffer, constraint);
50             final Attributes1 a = new Attributes1Builder().setMpReachNlri(mpReachNlri).build();
51             builder.addAugmentation(Attributes1.class, a);
52         } catch (final BGPParsingException e) {
53             throw new BGPDocumentedException("Could not parse MP_REACH_NLRI", BGPError.OPT_ATTR_ERROR, e);
54         }
55     }
56
57     @Override
58     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
59         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
60         final Attributes pathAttributes = (Attributes) attribute;
61         final Attributes1 pathAttributes1 = pathAttributes.getAugmentation(Attributes1.class);
62         if (pathAttributes1 == null) {
63             return;
64         }
65         final MpReachNlri mpReachNlri = pathAttributes1.getMpReachNlri();
66         final ByteBuf reachBuffer = Unpooled.buffer();
67         this.reg.serializeMpReach(mpReachNlri, reachBuffer);
68
69         for (final NlriSerializer nlriSerializer : this.reg.getSerializers()) {
70             nlriSerializer.serializeAttribute(attribute, reachBuffer);
71         }
72         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, reachBuffer, byteAggregator);
73     }
74 }