MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / l3vpn / src / main / java / org / opendaylight / protocol / bgp / l3vpn / VpnDestinationUtil.java
1 /*
2  * Copyright (c) 2017 AT&T Intellectual Property. 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
9 package org.opendaylight.protocol.bgp.l3vpn;
10
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.rev180329.labeled.unicast.LabelStack;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestination;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestinationBuilder;
21
22 final class VpnDestinationUtil {
23     private VpnDestinationUtil() {
24         throw new UnsupportedOperationException();
25     }
26
27     static List<VpnDestination> parseNlri(final ByteBuf nlri, final Class<? extends AddressFamily> afi) {
28         if (!nlri.isReadable()) {
29             return null;
30         }
31         final List<VpnDestination> dests = new ArrayList<>();
32
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());
45         }
46         return dests;
47     }
48 }