Reduce ByteBuf.writeZero() usage
[bgpcep.git] / bgp / extensions / evpn / src / main / java / org / opendaylight / protocol / bgp / evpn / impl / nlri / MACIpAdvRParser.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
9 package org.opendaylight.protocol.bgp.evpn.impl.nlri;
10
11 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.MPLS1_NID;
12 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.MPLS2_NID;
13 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.extractETI;
14 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.extractIp;
15 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.extractMAC;
16 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.extractMplsLabel;
17 import static org.opendaylight.protocol.util.MplsLabelUtil.byteBufForMplsLabel;
18 import static org.opendaylight.protocol.util.MplsLabelUtil.mplsLabelForByteBuf;
19
20 import com.google.common.base.Preconditions;
21 import io.netty.buffer.ByteBuf;
22 import io.netty.buffer.Unpooled;
23 import org.opendaylight.protocol.bgp.evpn.spi.pojo.SimpleEsiTypeRegistry;
24 import org.opendaylight.protocol.util.ByteArray;
25 import org.opendaylight.protocol.util.ByteBufUtils;
26 import org.opendaylight.protocol.util.ByteBufWriteUtil;
27 import org.opendaylight.protocol.util.Ipv4Util;
28 import org.opendaylight.protocol.util.Ipv6Util;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.NlriType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.esi.Esi;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.ethernet.tag.id.EthernetTagId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.ethernet.tag.id.EthernetTagIdBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.EvpnChoice;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.MacIpAdvRouteCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.MacIpAdvRouteCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.mac.ip.adv.route.MacIpAdvRoute;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.mac.ip.adv.route.MacIpAdvRouteBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
44
45 final class MACIpAdvRParser extends AbstractEvpnNlri {
46     static final NodeIdentifier MAC_IP_ADV_ROUTE_NID = new NodeIdentifier(MacIpAdvRoute.QNAME);
47     private static final int BITS_SIZE = 8;
48
49     @Override
50     public EvpnChoice parseEvpn(final ByteBuf buffer) {
51         final Esi esi = SimpleEsiTypeRegistry.getInstance().parseEsi(buffer.readSlice(ESI_SIZE));
52         final EthernetTagId eti = new EthernetTagIdBuilder().setVlanId(ByteBufUtils.readUint32(buffer)).build();
53         buffer.skipBytes(1);
54         final MacAddress mac = IetfYangUtil.INSTANCE.macAddressFor(ByteArray.readBytes(buffer, MAC_ADDRESS_LENGTH));
55         final IpAddress ip = parseIp(buffer);
56         final MplsLabel label1 = mplsLabelForByteBuf(buffer);
57         MplsLabel label2;
58         if (buffer.readableBytes() > 0) {
59             label2 = mplsLabelForByteBuf(buffer);
60         } else {
61             label2 = null;
62         }
63         final MacIpAdvRouteBuilder builder = new MacIpAdvRouteBuilder().setEsi(esi).setEthernetTagId(eti)
64                 .setMacAddress(mac).setIpAddress(ip)
65             .setMplsLabel1(label1).setMplsLabel2(label2);
66         return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(builder.build()).build();
67     }
68
69     @Override
70     protected NlriType getType() {
71         return NlriType.MacIpAdv;
72     }
73
74     @Override
75     public ByteBuf serializeBody(final EvpnChoice evpnChoice) {
76         Preconditions.checkArgument(evpnChoice instanceof MacIpAdvRouteCase,
77                 "Unknown evpn instance. Passed %s. Needed MacIpAdvRouteCase.", evpnChoice.getClass());
78
79         final ByteBuf body = Unpooled.buffer();
80         final MacIpAdvRoute evpn = ((MacIpAdvRouteCase) evpnChoice).getMacIpAdvRoute();
81         final Esi esi = evpn.getEsi();
82         if (esi != null) {
83             SimpleEsiTypeRegistry.getInstance().serializeEsi(evpn.getEsi(), body);
84         }
85         ByteBufWriteUtil.writeUnsignedInt(evpn.getEthernetTagId().getVlanId(), body);
86
87         final MacAddress mac = evpn.getMacAddress();
88         body.writeByte(MAC_ADDRESS_LENGTH * BITS_SIZE);
89         body.writeBytes(IetfYangUtil.INSTANCE.bytesFor(mac));
90         final ByteBuf ipAddress = serializeIp(evpn.getIpAddress());
91         Preconditions.checkArgument(ipAddress.readableBytes() > 0);
92         body.writeBytes(ipAddress);
93         final MplsLabel mpls1 = evpn.getMplsLabel1();
94         if (mpls1 != null) {
95             body.writeBytes(byteBufForMplsLabel(mpls1));
96         }
97         final MplsLabel mpls2 = evpn.getMplsLabel2();
98         if (mpls2 != null) {
99             body.writeBytes(byteBufForMplsLabel(mpls2));
100         }
101         return body;
102     }
103
104     @Override
105     public EvpnChoice serializeEvpnModel(final ContainerNode evpn) {
106         final MacIpAdvRouteBuilder builder = serializeKeyModel(evpn);
107         builder.setEsi(serializeEsi(evpn));
108         builder.setMplsLabel1(extractMplsLabel(evpn, MPLS1_NID));
109         builder.setMplsLabel2(extractMplsLabel(evpn, MPLS2_NID));
110         return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(builder.build()).build();
111     }
112
113     @Override
114     public EvpnChoice createRouteKey(final ContainerNode evpn) {
115         return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(serializeKeyModel(evpn).build()).build();
116     }
117
118     private static MacIpAdvRouteBuilder serializeKeyModel(final ContainerNode evpn) {
119         final MacIpAdvRouteBuilder builder = new MacIpAdvRouteBuilder();
120         builder.setEthernetTagId(extractETI(evpn));
121         builder.setMacAddress(extractMAC(evpn));
122         builder.setIpAddress(extractIp(evpn));
123         return builder;
124     }
125
126     private static ByteBuf serializeIp(final IpAddress ipAddress) {
127         final ByteBuf body = Unpooled.buffer();
128         if (ipAddress != null) {
129             if (ipAddress.getIpv4Address() != null) {
130                 body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
131                 body.writeBytes(Ipv4Util.bytesForAddress(ipAddress.getIpv4Address()));
132             } else if (ipAddress.getIpv6Address() != null) {
133                 body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
134                 body.writeBytes(Ipv6Util.bytesForAddress(ipAddress.getIpv6Address()));
135             } else {
136                 body.writeByte(0);
137             }
138         } else {
139             body.writeByte(0);
140         }
141         return body;
142     }
143
144     private static IpAddress parseIp(final ByteBuf buffer) {
145         final int ipLength = buffer.readUnsignedByte();
146         if (ipLength == Ipv6Util.IPV6_BITS_LENGTH) {
147             return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
148         } else if (ipLength == Ipv4Util.IP4_BITS_LENGTH) {
149             return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
150         }
151         return null;
152     }
153 }