2 * Copyright (c) 2017 AT&T Intellectual Property. 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.l3vpn;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
16 import org.opendaylight.protocol.bgp.labeled.unicast.LUNlriParser;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.LabelStack;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.destination.type.VpnDestination;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev171207.l3vpn.ip.destination.type.VpnDestinationBuilder;
22 final class VpnDestinationUtil {
23 private VpnDestinationUtil() {
24 throw new UnsupportedOperationException();
27 static List<VpnDestination> parseNlri(final ByteBuf nlri, final Class<? extends AddressFamily> afi) {
28 if (!nlri.isReadable()) {
31 final List<VpnDestination> dests = new ArrayList<>();
33 while (nlri.isReadable()) {
34 final VpnDestinationBuilder builder = new VpnDestinationBuilder();
35 final short length = nlri.readUnsignedByte();
36 final List<LabelStack> labels = LUNlriParser.parseLabel(nlri);
37 builder.setLabelStack(labels);
38 final int labelNum = labels != null ? labels.size() : 1;
39 final int prefixLen = length - (LUNlriParser.LABEL_LENGTH * Byte.SIZE * labelNum)
40 - (RouteDistinguisherUtil.RD_LENGTH * Byte.SIZE);
41 builder.setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(nlri));
42 Preconditions.checkState(prefixLen > 0, "A valid VPN IP prefix is required.");
43 builder.setPrefix(LUNlriParser.parseIpPrefix(nlri, prefixLen, afi));
44 dests.add(builder.build());