9f6e3b45da7c9b88339be54b78888043e2cdf768
[bgpcep.git] / bgp / l3vpn / src / main / java / org / opendaylight / protocol / bgp / l3vpn / unicast / 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.unicast;
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.protocol.bgp.parser.BgpTableTypeImpl;
18 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupportUtil;
19 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
20 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestination;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestinationBuilder;
26
27 final class VpnDestinationUtil {
28     private VpnDestinationUtil() {
29         throw new UnsupportedOperationException();
30     }
31
32     static List<VpnDestination> parseNlri(
33             final ByteBuf nlri,
34             final PeerSpecificParserConstraint constraints,
35             final Class<? extends AddressFamily> afi,
36             final Class<? extends SubsequentAddressFamily> safi) {
37         if (!nlri.isReadable()) {
38             return null;
39         }
40         final List<VpnDestination> dests = new ArrayList<>();
41
42         while (nlri.isReadable()) {
43             final VpnDestinationBuilder builder = new VpnDestinationBuilder();
44             if (MultiPathSupportUtil.isTableTypeSupported(constraints, new BgpTableTypeImpl(afi, safi))) {
45                 builder.setPathId(PathIdUtil.readPathId(nlri));
46             }
47             final short length = nlri.readUnsignedByte();
48             final List<LabelStack> labels = LUNlriParser.parseLabel(nlri);
49             builder.setLabelStack(labels);
50             final int labelNum = labels != null ? labels.size() : 1;
51             final int prefixLen = length - (LUNlriParser.LABEL_LENGTH * Byte.SIZE * labelNum)
52                     - (RouteDistinguisherUtil.RD_LENGTH * Byte.SIZE);
53             builder.setRouteDistinguisher(RouteDistinguisherUtil.parseRouteDistinguisher(nlri));
54             Preconditions.checkState(prefixLen > 0, "A valid VPN IP prefix is required.");
55             builder.setPrefix(LUNlriParser.parseIpPrefix(nlri, prefixLen, afi));
56             dests.add(builder.build());
57         }
58         return dests;
59     }
60 }