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