Fix sonar complains
[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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
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.BgpPrefixSidTlvRegistry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.BgpPrefixSid;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.BgpPrefixSidBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.bgp.prefix.sid.BgpPrefixSidTlvs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.bgp.prefix.sid.BgpPrefixSidTlvsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.BgpPrefixSidTlv;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
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 = Preconditions.checkNotNull(registry);
36     }
37
38     @Override
39     public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
40         Preconditions.checkArgument(attribute instanceof Attributes, "Attribute parameter is not a PathAttribute object.");
41         final Attributes pathAttributes = (Attributes) attribute;
42         final BgpPrefixSid prefixSid = pathAttributes.getBgpPrefixSid();
43         if (prefixSid == null) {
44             return;
45         }
46         for (final BgpPrefixSidTlvs tlv : prefixSid.getBgpPrefixSidTlvs()) {
47             this.reg.serializeBgpPrefixSidTlv(tlv.getBgpPrefixSidTlv(), byteAggregator);
48         }
49     }
50
51     @Override
52     public void parseAttribute(final ByteBuf buffer, final AttributesBuilder builder) throws BGPDocumentedException, BGPParsingException {
53         final BgpPrefixSidBuilder sid = new BgpPrefixSidBuilder();
54         final List<BgpPrefixSidTlvs> tlvList = new ArrayList<>();
55         while (buffer.isReadable()) {
56             final BgpPrefixSidTlv tlv = this.reg.parseBgpPrefixSidTlv(buffer.readUnsignedByte(), buffer);
57             tlvList.add(new BgpPrefixSidTlvsBuilder().setBgpPrefixSidTlv(tlv).build());
58         }
59         builder.setBgpPrefixSid(sid.setBgpPrefixSidTlvs(tlvList).build());
60     }
61
62 }