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