2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.evpn.impl.nlri;
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;
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.rev171213.NlriType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.esi.Esi;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.ethernet.tag.id.EthernetTagId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.ethernet.tag.id.EthernetTagIdBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.EvpnChoice;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.evpn.choice.MacIpAdvRouteCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.evpn.choice.MacIpAdvRouteCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.mac.ip.adv.route.MacIpAdvRoute;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.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;
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;
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();
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);
57 if (buffer.readableBytes() > 0) {
58 label2 = mplsLabelForByteBuf(buffer);
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();
69 protected NlriType getType() {
70 return NlriType.MacIpAdv;
74 public ByteBuf serializeBody(final EvpnChoice evpnChoice) {
75 Preconditions.checkArgument(evpnChoice instanceof MacIpAdvRouteCase,
76 "Unknown evpn instance. Passed %s. Needed MacIpAdvRouteCase.", evpnChoice.getClass());
78 final ByteBuf body = Unpooled.buffer();
79 final MacIpAdvRoute evpn = ((MacIpAdvRouteCase) evpnChoice).getMacIpAdvRoute();
80 final Esi esi = evpn.getEsi();
82 SimpleEsiTypeRegistry.getInstance().serializeEsi(evpn.getEsi(), body);
84 ByteBufWriteUtil.writeUnsignedInt(evpn.getEthernetTagId().getVlanId(), body);
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();
94 body.writeBytes(byteBufForMplsLabel(mpls1));
96 final MplsLabel mpls2 = evpn.getMplsLabel2();
98 body.writeBytes(byteBufForMplsLabel(mpls2));
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();
113 public EvpnChoice createRouteKey(final ContainerNode evpn) {
114 return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(serializeKeyModel(evpn).build()).build();
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));
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()));
135 body.writeZero(ZERO_BYTE);
138 body.writeZero(ZERO_BYTE);
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));