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.protocol.bgp.parser.spi.PathIdUtil;
21 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStackBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.destination.CLabeledUnicastDestination;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRoute;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRouteBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRouteKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
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 extends AbstractRIBSupport<LabeledUnicastRoute, LabeledUnicastRouteKey> {
55 private static final NodeIdentifier PREFIX_TYPE_NID
56 = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "prefix").intern());
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 cazeClass Binding class of the AFI/SAFI-specific case statement, must not be null
70 * @param containerClass Binding class of the container in routes choice, must not be null.
71 * @param listClass Binding class of the route list, nust not be null;
72 * @param addressFamilyClass address Family Class
73 * @param safiClass SubsequentAddressFamily
74 * @param destinationQname destination Qname
76 AbstractLabeledUnicastRIBSupport(final Class<? extends Routes> cazeClass,
77 final Class<? extends DataObject> containerClass,
78 final Class<? extends Route> listClass,
79 final Class<? extends AddressFamily> addressFamilyClass,
80 final Class<? extends SubsequentAddressFamily> safiClass,
81 final QName destinationQname) {
82 super(cazeClass, containerClass, listClass, addressFamilyClass, safiClass, destinationQname);
86 protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
87 final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
88 if (destination != null) {
89 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination.getChild(NLRI_ROUTES_LIST);
90 if (maybeRoutes.isPresent()) {
91 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
92 if (routes instanceof UnkeyedListNode) {
93 final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
94 for (final UnkeyedListEntryNode e : ((UnkeyedListNode) routes).getValue()) {
95 final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
96 function.apply(tx, base, routeKey, e, attributes);
99 LOG.warn("Routes {} are not a map", routes);
106 protected List<CLabeledUnicastDestination> extractRoutes(final Collection<MapEntryNode> routes) {
107 return routes.stream().map(this::extractCLabeledUnicastDestination).collect(Collectors.toList());
110 private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode labeledUnicast) {
111 final ByteBuf buffer = Unpooled.buffer();
113 final CLabeledUnicastDestination dest = extractCLabeledUnicastDestination(labeledUnicast);
114 LUNlriParser.serializeNlri(Collections.singletonList(dest), false, buffer);
115 final String routeKeyValue = ByteArray.encodeBase64(buffer);
116 final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf
117 = labeledUnicast.getChild(routePathIdNid());
118 return PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), routeKeyValue, maybePathIdLeaf);
122 * Conversion from DataContainer to LabeledUnicastDestination Object
124 * @param route DataContainer
125 * @return LabeledUnicastDestination Object
127 private CLabeledUnicastDestination extractCLabeledUnicastDestination(
128 final DataContainerNode<? extends PathArgument> route) {
129 final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
130 builder.setPrefix(extractPrefix(route, PREFIX_TYPE_NID));
131 builder.setLabelStack(extractLabel(route, LABEL_STACK_NID, LV_NID));
132 builder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
133 return builder.build();
136 protected abstract IpPrefix extractPrefix(final DataContainerNode<? extends PathArgument> route,
137 final NodeIdentifier prefixTypeNid);
139 public static List<LabelStack> extractLabel(final DataContainerNode<? extends PathArgument> route,
140 final NodeIdentifier labelStackNid, final NodeIdentifier labelValueNid) {
141 final List<LabelStack> labels = new ArrayList<>();
142 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStacks = route.getChild(labelStackNid);
143 if (labelStacks.isPresent()) {
144 for (final UnkeyedListEntryNode label : ((UnkeyedListNode) labelStacks.get()).getValue()) {
145 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStack
146 = label.getChild(labelValueNid);
147 if (labelStack.isPresent()) {
148 final LabelStackBuilder labelStackbuilder = new LabelStackBuilder();
149 labelStackbuilder.setLabelValue(new MplsLabel((Long) labelStack.get().getValue()));
150 labels.add(labelStackbuilder.build());
158 public final LabeledUnicastRouteKey createRouteListKey(final long pathId, final String routeKey) {
159 return new LabeledUnicastRouteKey(new PathId(pathId), routeKey);
163 public final LabeledUnicastRoute createRoute(final LabeledUnicastRoute route, final String routeKey,
164 final long pathId, final Attributes attributes) {
165 final LabeledUnicastRouteBuilder builder;
167 builder = new LabeledUnicastRouteBuilder(route);
169 builder = new LabeledUnicastRouteBuilder();
171 return builder.setKey(new LabeledUnicastRouteKey(new PathId(pathId), routeKey))
172 .setAttributes(attributes).build();