Reduce ByteBuf.writeZero() usage
[bgpcep.git] / bgp / extensions / evpn / src / main / java / org / opendaylight / protocol / bgp / evpn / impl / nlri / AbstractEvpnNlri.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.evpn.impl.nlri;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import org.opendaylight.protocol.bgp.evpn.spi.EvpnParser;
13 import org.opendaylight.protocol.bgp.evpn.spi.EvpnSerializer;
14 import org.opendaylight.protocol.bgp.evpn.spi.pojo.SimpleEsiTypeRegistry;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.NlriType;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.esi.Esi;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.EvpnChoice;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
21
22 abstract class AbstractEvpnNlri implements EvpnParser, EvpnSerializer {
23
24     static final int MAC_ADDRESS_LENGTH = 6;
25     static final int ESI_SIZE = 10;
26     private static final NodeIdentifier ESI_NID = NodeIdentifier.create(Esi.QNAME);
27
28     @Override
29     public final ByteBuf serializeEvpn(final EvpnChoice evpn, final ByteBuf common) {
30         final ByteBuf output = Unpooled.buffer();
31         final ByteBuf body = serializeBody(evpn);
32         output.writeByte(getType().getIntValue());
33         output.writeByte(body.readableBytes() + common.readableBytes());
34         output.writeBytes(common);
35         output.writeBytes(body);
36         return output;
37     }
38
39     protected abstract NlriType getType();
40
41     protected abstract ByteBuf serializeBody(EvpnChoice evpn);
42
43     protected static Esi serializeEsi(final ContainerNode evpn) {
44         return SimpleEsiTypeRegistry.getInstance().parseEsiModel((ChoiceNode) evpn.getChild(ESI_NID).get());
45     }
46 }