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
9 package org.opendaylight.protocol.bgp.labeled.unicast;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.stream.Collectors;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
21 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
22 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.LabeledUnicastSubsequentAddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStackBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.destination.CLabeledUnicastDestination;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRoute;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRouteBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRouteKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
46 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 abstract class AbstractLabeledUnicastRIBSupport<
54 C extends Routes & DataObject,
56 extends AbstractRIBSupport<C, S, LabeledUnicastRoute, LabeledUnicastRouteKey> {
57 private static final NodeIdentifier LABEL_STACK_NID
58 = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "label-stack").intern());
59 private static final NodeIdentifier LV_NID
60 = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "label-value").intern());
61 private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(CLabeledUnicastDestination.QNAME);
62 private static final Logger LOG = LoggerFactory.getLogger(AbstractLabeledUnicastRIBSupport.class);
65 * Default constructor. Requires the QName of the container augmented under the routes choice
66 * node in instantiations of the rib grouping. It is assumed that this container is defined by
67 * the same model which populates it with route grouping instantiation, and by extension with
68 * the route attributes container.
69 * @param mappingService Binding Normalized Node Serializer
70 * @param cazeClass Binding class of the AFI/SAFI-specific case statement, must not be null
71 * @param containerClass Binding class of the container in routes choice, must not be null.
72 * @param addressFamilyClass address Family Class
73 * @param destinationQname destination Qname
75 AbstractLabeledUnicastRIBSupport(
76 final BindingNormalizedNodeSerializer mappingService,
77 final Class<C> cazeClass,
78 final Class<S> containerClass,
79 final Class<? extends AddressFamily> addressFamilyClass,
80 final QName destinationQname) {
84 LabeledUnicastRoute.class,
86 LabeledUnicastSubsequentAddressFamily.class,
91 protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
92 final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
93 if (destination != null) {
94 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes
95 = destination.getChild(NLRI_ROUTES_LIST);
96 if (maybeRoutes.isPresent()) {
97 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
98 if (routes instanceof UnkeyedListNode) {
99 final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
100 for (final UnkeyedListEntryNode e : ((UnkeyedListNode) routes).getValue()) {
101 final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
102 function.apply(tx, base, routeKey, e, attributes);
105 LOG.warn("Routes {} are not a map", routes);
112 protected List<CLabeledUnicastDestination> extractRoutes(final Collection<MapEntryNode> routes) {
113 return routes.stream().map(this::extractCLabeledUnicastDestination).collect(Collectors.toList());
116 private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode labeledUnicast) {
117 final ByteBuf buffer = Unpooled.buffer();
119 final CLabeledUnicastDestination dest = extractCLabeledUnicastDestination(labeledUnicast);
120 LUNlriParser.serializeNlri(Collections.singletonList(dest), false, buffer);
121 final String routeKeyValue = ByteArray.encodeBase64(buffer);
122 final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf
123 = labeledUnicast.getChild(routePathIdNid());
124 return PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), routeKeyValue, maybePathIdLeaf);
128 * Conversion from DataContainer to LabeledUnicastDestination Object
130 * @param route DataContainer
131 * @return LabeledUnicastDestination Object
133 private CLabeledUnicastDestination extractCLabeledUnicastDestination(
134 final DataContainerNode<? extends PathArgument> route) {
135 final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
136 builder.setPrefix(extractPrefix(route, prefixNid()));
137 builder.setLabelStack(extractLabel(route, LABEL_STACK_NID, LV_NID));
138 builder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
139 return builder.build();
142 protected abstract IpPrefix extractPrefix(final DataContainerNode<? extends PathArgument> route,
143 final NodeIdentifier prefixTypeNid);
145 public static List<LabelStack> extractLabel(final DataContainerNode<? extends PathArgument> route,
146 final NodeIdentifier labelStackNid, final NodeIdentifier labelValueNid) {
147 final List<LabelStack> labels = new ArrayList<>();
148 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStacks = route.getChild(labelStackNid);
149 if (labelStacks.isPresent()) {
150 for (final UnkeyedListEntryNode label : ((UnkeyedListNode) labelStacks.get()).getValue()) {
151 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStack
152 = label.getChild(labelValueNid);
153 if (labelStack.isPresent()) {
154 final LabelStackBuilder labelStackbuilder = new LabelStackBuilder();
155 labelStackbuilder.setLabelValue(new MplsLabel((Long) labelStack.get().getValue()));
156 labels.add(labelStackbuilder.build());
164 public final LabeledUnicastRouteKey createRouteListKey(final long pathId, final String routeKey) {
165 return new LabeledUnicastRouteKey(new PathId(pathId), routeKey);
169 public final LabeledUnicastRoute createRoute(final LabeledUnicastRoute route, final String routeKey,
170 final long pathId, final Attributes attributes) {
171 final LabeledUnicastRouteBuilder builder;
173 builder = new LabeledUnicastRouteBuilder(route);
175 builder = new LabeledUnicastRouteBuilder();
177 return builder.setKey(new LabeledUnicastRouteKey(new PathId(pathId), routeKey))
178 .setAttributes(attributes).build();