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
8 package org.opendaylight.protocol.bgp.evpn.impl.nlri;
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;
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.IpAddress;
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;
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;
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();
52 final MacAddress mac = IetfYangUtil.INSTANCE.macAddressFor(ByteArray.readBytes(buffer, MAC_ADDRESS_LENGTH));
53 final IpAddress ip = parseIp(buffer);
54 final MplsLabel label1 = mplsLabelForByteBuf(buffer);
56 if (buffer.readableBytes() > 0) {
57 label2 = mplsLabelForByteBuf(buffer);
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();
68 protected NlriType getType() {
69 return NlriType.MacIpAdv;
73 public ByteBuf serializeBody(final EvpnChoice evpnChoice) {
74 Preconditions.checkArgument(evpnChoice instanceof MacIpAdvRouteCase,
75 "Unknown evpn instance. Passed %s. Needed MacIpAdvRouteCase.", evpnChoice.getClass());
77 final ByteBuf body = Unpooled.buffer();
78 final MacIpAdvRoute evpn = ((MacIpAdvRouteCase) evpnChoice).getMacIpAdvRoute();
79 final Esi esi = evpn.getEsi();
81 SimpleEsiTypeRegistry.getInstance().serializeEsi(evpn.getEsi(), body);
83 ByteBufUtils.writeOrZero(body, evpn.getEthernetTagId().getVlanId());
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();
93 body.writeBytes(byteBufForMplsLabel(mpls1));
95 final MplsLabel mpls2 = evpn.getMplsLabel2();
97 body.writeBytes(byteBufForMplsLabel(mpls2));
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();
112 public EvpnChoice createRouteKey(final ContainerNode evpn) {
113 return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(serializeKeyModel(evpn).build()).build();
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));
124 private static ByteBuf serializeIp(final IpAddress ipAddress) {
125 final ByteBuf body = Unpooled.buffer();
126 if (ipAddress != null) {
127 if (ipAddress.getIpv4Address() != null) {
128 body.writeByte(Ipv4Util.IP4_BITS_LENGTH);
129 body.writeBytes(Ipv4Util.bytesForAddress(ipAddress.getIpv4Address()));
130 } else if (ipAddress.getIpv6Address() != null) {
131 body.writeByte(Ipv6Util.IPV6_BITS_LENGTH);
132 body.writeBytes(Ipv6Util.bytesForAddress(ipAddress.getIpv6Address()));
142 private static IpAddress parseIp(final ByteBuf buffer) {
143 final int ipLength = buffer.readUnsignedByte();
144 if (ipLength == Ipv6Util.IPV6_BITS_LENGTH) {
145 return new IpAddress(Ipv6Util.addressForByteBuf(buffer));
146 } else if (ipLength == Ipv4Util.IP4_BITS_LENGTH) {
147 return new IpAddress(Ipv4Util.addressForByteBuf(buffer));