Create common parent for extensions families
[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.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.rev130715.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.NlriType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.esi.Esi;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.ethernet.tag.id.EthernetTagId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.ethernet.tag.id.EthernetTagIdBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.EvpnChoice;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.MacIpAdvRouteCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.MacIpAdvRouteCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.mac.ip.adv.route.MacIpAdvRoute;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.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)
63                 .setMacAddress(mac).setIpAddress(ip)
64             .setMplsLabel1(label1).setMplsLabel2(label2);
65         return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(builder.build()).build();
66     }
67
68     @Override
69     protected NlriType getType() {
70         return NlriType.MacIpAdv;
71     }
72
73     @Override
74     public ByteBuf serializeBody(final EvpnChoice evpnChoice) {
75         Preconditions.checkArgument(evpnChoice instanceof MacIpAdvRouteCase,
76                 "Unknown evpn instance. Passed %s. Needed MacIpAdvRouteCase.", evpnChoice.getClass());
77
78         final ByteBuf body = Unpooled.buffer();
79         final MacIpAdvRoute evpn = ((MacIpAdvRouteCase) evpnChoice).getMacIpAdvRoute();
80         final Esi esi = evpn.getEsi();
81         if (esi != null) {
82             SimpleEsiTypeRegistry.getInstance().serializeEsi(evpn.getEsi(), body);
83         }
84         ByteBufWriteUtil.writeUnsignedInt(evpn.getEthernetTagId().getVlanId(), body);
85
86         final MacAddress mac = evpn.getMacAddress();
87         body.writeByte(MAC_ADDRESS_LENGTH * BITS_SIZE);
88         body.writeBytes(IetfYangUtil.INSTANCE.bytesFor(mac));
89         final ByteBuf ipAddress = serializeIp(evpn.getIpAddress());
90         Preconditions.checkArgument(ipAddress.readableBytes() > 0);
91         body.writeBytes(ipAddress);
92         final MplsLabel mpls1 = evpn.getMplsLabel1();
93         if (mpls1 != null) {
94             body.writeBytes(byteBufForMplsLabel(mpls1));
95         }
96         final MplsLabel mpls2 = evpn.getMplsLabel2();
97         if (mpls2 != null) {
98             body.writeBytes(byteBufForMplsLabel(mpls2));
99         }
100         return body;
101     }
102
103     @Override
104     public EvpnChoice serializeEvpnModel(final ContainerNode evpn) {
105         final MacIpAdvRouteBuilder builder = serializeKeyModel(evpn);
106         builder.setEsi(serializeEsi(evpn));
107         builder.setMplsLabel1(extractMplsLabel(evpn, MPLS1_NID));
108         builder.setMplsLabel2(extractMplsLabel(evpn, MPLS2_NID));
109         return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(builder.build()).build();
110     }
111
112     @Override
113     public EvpnChoice createRouteKey(final ContainerNode evpn) {
114         return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(serializeKeyModel(evpn).build()).build();
115     }
116
117     private static MacIpAdvRouteBuilder serializeKeyModel(final ContainerNode evpn) {
118         final MacIpAdvRouteBuilder builder = new MacIpAdvRouteBuilder();
119         builder.setEthernetTagId(extractETI(evpn));
120         builder.setMacAddress(extractMAC(evpn));
121         builder.setIpAddress(extractIp(evpn));
122         return builder;
123     }
124
125     private static ByteBuf serializeIp(final IpAddress ipAddress) {
126         final ByteBuf body = Unpooled.buffer();
127         if (ipAddress != null) {
128             if (ipAddress.getIpv4Address() != null) {
129                 body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
130                 body.writeBytes(Ipv4Util.bytesForAddress(ipAddress.getIpv4Address()));
131             } else if (ipAddress.getIpv6Address() != null) {
132                 body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
133                 body.writeBytes(Ipv6Util.bytesForAddress(ipAddress.getIpv6Address()));
134             } else {
135                 body.writeZero(ZERO_BYTE);
136             }
137         } else {
138             body.writeZero(ZERO_BYTE);
139         }
140         return body;
141     }
142
143     private static IpAddress parseIp(final ByteBuf buffer) {
144         final int ipLength = buffer.readUnsignedByte();
145         if (ipLength == Ipv6Util.IPV6_BITS_LENGTH) {
146             return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
147         } else if (ipLength == Ipv4Util.IP4_BITS_LENGTH) {
148             return new IpAddress(Ipv4Util.addressForByteBuf(buffer));
149         }
150         return null;
151     }
152 }