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