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.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;
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;
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();
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);
58 if (buffer.readableBytes() > 0) {
59 label2 = mplsLabelForByteBuf(buffer);
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();
70 protected NlriType getType() {
71 return NlriType.MacIpAdv;
75 public ByteBuf serializeBody(final EvpnChoice evpnChoice) {
76 Preconditions.checkArgument(evpnChoice instanceof MacIpAdvRouteCase,
77 "Unknown evpn instance. Passed %s. Needed MacIpAdvRouteCase.", evpnChoice.getClass());
79 final ByteBuf body = Unpooled.buffer();
80 final MacIpAdvRoute evpn = ((MacIpAdvRouteCase) evpnChoice).getMacIpAdvRoute();
81 final Esi esi = evpn.getEsi();
83 SimpleEsiTypeRegistry.getInstance().serializeEsi(evpn.getEsi(), body);
85 ByteBufWriteUtil.writeUnsignedInt(evpn.getEthernetTagId().getVlanId(), body);
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();
95 body.writeBytes(byteBufForMplsLabel(mpls1));
97 final MplsLabel mpls2 = evpn.getMplsLabel2();
99 body.writeBytes(byteBufForMplsLabel(mpls2));
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();
114 public EvpnChoice createRouteKey(final ContainerNode evpn) {
115 return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(serializeKeyModel(evpn).build()).build();
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));
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()));
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));