YANG revision dates mass-update
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / BgpPrefixSidAttributeParser.java
1 /*
2  * Copyright (c) 2016 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 static java.util.Objects.requireNonNull;
11
12 import io.netty.buffer.ByteBuf;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
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.BgpPrefixSidTlvRegistry;
20 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.BgpPrefixSid;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.BgpPrefixSidBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.bgp.prefix.sid.BgpPrefixSidTlvs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.bgp.prefix.sid.BgpPrefixSidTlvsBuilder;
27
28 public final class BgpPrefixSidAttributeParser implements AttributeParser, AttributeSerializer {
29
30     public static final int TYPE = 40;
31
32     private final BgpPrefixSidTlvRegistry reg;
33
34     public BgpPrefixSidAttributeParser(final BgpPrefixSidTlvRegistry registry) {
35         this.reg = requireNonNull(registry);
36     }
37
38     @Override
39     public void serializeAttribute(final Attributes pathAttributes, final ByteBuf byteAggregator) {
40         final BgpPrefixSid prefixSid = pathAttributes.getBgpPrefixSid();
41         if (prefixSid == null) {
42             return;
43         }
44         for (final BgpPrefixSidTlvs tlv : prefixSid.getBgpPrefixSidTlvs()) {
45             this.reg.serializeBgpPrefixSidTlv(tlv.getBgpPrefixSidTlv(), byteAggregator);
46         }
47     }
48
49     @Override
50     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder,
51             final PeerSpecificParserConstraint constraint) throws BGPDocumentedException, BGPParsingException {
52         final BgpPrefixSidBuilder sid = new BgpPrefixSidBuilder();
53         final List<BgpPrefixSidTlvs> tlvList = new ArrayList<>();
54         while (buffer.isReadable()) {
55             tlvList.add(new BgpPrefixSidTlvsBuilder()
56                 .setBgpPrefixSidTlv(this.reg.parseBgpPrefixSidTlv(buffer.readUnsignedByte(), buffer))
57                 .build());
58         }
59         builder.setBgpPrefixSid(sid.setBgpPrefixSidTlvs(tlvList).build());
60     }
61
62 }