2 * Copyright (c) 2015 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.labeled.unicast;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableCollection;
12 import com.google.common.collect.ImmutableSet;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.stream.Collectors;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
22 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
23 import org.opendaylight.protocol.bgp.rib.spi.MultiPathAbstractRIBSupport;
24 import org.opendaylight.protocol.util.ByteArray;
25 import org.opendaylight.protocol.util.Ipv4Util;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.LabeledUnicastSubsequentAddressFamily;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.bgp.rib.rib.loc.rib.tables.routes.LabeledUnicastRoutesCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStackBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.destination.CLabeledUnicastDestination;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.LabeledUnicastRoutes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.labeled.unicast.routes.LabeledUnicastRoute;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicast;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
43 import org.opendaylight.yangtools.yang.binding.DataObject;
44 import org.opendaylight.yangtools.yang.common.QName;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
51 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 public final class LabeledUnicastRIBSupport extends MultiPathAbstractRIBSupport {
60 private static final Logger LOG = LoggerFactory.getLogger(LabeledUnicastRIBSupport.class);
61 private static final NodeIdentifier PREFIX_TYPE_NID = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "prefix").intern());
62 private static final NodeIdentifier LABEL_STACK_NID = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "label-stack").intern());
63 private static final NodeIdentifier LV_NID = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "label-value").intern());
64 private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(CLabeledUnicastDestination.QNAME);
66 public LabeledUnicastRIBSupport(final Class<? extends AddressFamily> afiType) {
67 super(LabeledUnicastRoutesCase.class, LabeledUnicastRoutes.class, LabeledUnicastRoute.class, afiType, LabeledUnicastSubsequentAddressFamily
68 .class, "route-key", DestinationLabeledUnicast.QNAME);
72 public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
73 return ImmutableSet.of();
77 public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
78 return ImmutableSet.of();
82 public boolean isComplexRoute() {
87 protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
88 final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
89 if (destination != null) {
90 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination.getChild(NLRI_ROUTES_LIST);
91 if (maybeRoutes.isPresent()) {
92 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
93 if (routes instanceof UnkeyedListNode) {
94 final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
95 for (final UnkeyedListEntryNode e : ((UnkeyedListNode)routes).getValue()) {
96 final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
97 function.apply(tx, base, routeKey, e, attributes);
100 LOG.warn("Routes {} are not a map", routes);
106 private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode labeledUnicast) {
107 final ByteBuf buffer = Unpooled.buffer();
109 final CLabeledUnicastDestination dest = extractCLabeledUnicastDestination(labeledUnicast);
110 LUNlriParser.serializeNlri(Collections.singletonList(dest), buffer);
111 final byte[] routeKeyValue = ByteArray.readAllBytes(buffer);
112 final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf = labeledUnicast.getChild(routePathIdNid());
113 final NodeIdentifierWithPredicates routeKey = PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), routeKeyValue, maybePathIdLeaf);
119 protected DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes) {
120 return new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
121 new DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(extractRoutes(routes)).build()).build();
126 protected DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes) {
127 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
128 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(
129 extractRoutes(routes)).build()).build();
132 private List<CLabeledUnicastDestination> extractRoutes(final Collection<MapEntryNode> routes) {
133 return routes.stream().map(this::extractCLabeledUnicastDestination).collect(Collectors.toList());
137 * Conversion from DataContainer to LabeledUnicastDestination Object
139 * @param route DataContainer
140 * @return LabeledUnicastDestination Object
142 private CLabeledUnicastDestination extractCLabeledUnicastDestination(final DataContainerNode<? extends PathArgument> route) {
143 final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
144 builder.setPrefix(extractPrefix(route, PREFIX_TYPE_NID));
145 builder.setLabelStack(extractLabel(route, LABEL_STACK_NID, LV_NID));
146 builder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
147 return builder.build();
150 public static IpPrefix extractPrefix(final DataContainerNode<? extends PathArgument> route, final NodeIdentifier prefixTypeNid) {
151 if (route.getChild(prefixTypeNid).isPresent()) {
152 final String prefixType = (String) route.getChild(prefixTypeNid).get().getValue();
154 Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix(prefixType));
155 return new IpPrefix(new Ipv4Prefix(prefixType));
156 } catch (final IllegalArgumentException e) {
157 LOG.debug("Creating Ipv6 prefix because", e);
158 return new IpPrefix(new Ipv6Prefix(prefixType));
164 public static List<LabelStack> extractLabel(final DataContainerNode<? extends PathArgument> route, final NodeIdentifier labelStackNid, final NodeIdentifier labelValueNid) {
165 final List<LabelStack> labels = new ArrayList<>();
166 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStacks = route.getChild(labelStackNid);
167 if (labelStacks.isPresent()) {
168 for (final UnkeyedListEntryNode label : ((UnkeyedListNode) labelStacks.get()).getValue()) {
169 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStack = label.getChild(labelValueNid);
170 if (labelStack.isPresent()) {
171 final LabelStackBuilder labelStackbuilder = new LabelStackBuilder();
172 labelStackbuilder.setLabelValue(new MplsLabel((Long) labelStack.get().getValue()));
173 labels.add(labelStackbuilder.build());