Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / MPUnreachAttributeParser.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
16 import org.opendaylight.protocol.bgp.parser.BGPError;
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.AttributeUtil;
21 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
22 import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
23 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30
31 public final class MPUnreachAttributeParser implements AttributeParser, AttributeSerializer {
32     public static final int TYPE = 15;
33
34     private final NlriRegistry reg;
35
36     public MPUnreachAttributeParser(final NlriRegistry reg) {
37         this.reg = requireNonNull(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 MpUnreachNlri mpUnreachNlri = this.reg.parseMpUnreach(buffer, constraint);
50             final Attributes2 a = new Attributes2Builder().setMpUnreachNlri(mpUnreachNlri).build();
51             builder.addAugmentation(Attributes2.class, a);
52         } catch (final BGPParsingException e) {
53             throw new BGPDocumentedException("Could not parse MP_UNREACH_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 Attributes2 pathAttributes2 = pathAttributes.getAugmentation(Attributes2.class);
62         if (pathAttributes2 == null) {
63             return;
64         }
65         final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
66         final ByteBuf unreachBuffer = Unpooled.buffer();
67         this.reg.serializeMpUnReach(mpUnreachNlri, unreachBuffer);
68         for (final NlriSerializer nlriSerializer : this.reg.getSerializers()){
69             nlriSerializer.serializeAttribute(attribute,unreachBuffer);
70         }
71         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, TYPE, unreachBuffer, byteAggregator);
72     }
73 }