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