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 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.Optional;
20 import java.util.stream.Collectors;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.LabelStack;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.LabelStackBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.destination.CLabeledUnicastDestination;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.routes.list.LabeledUnicastRoute;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.routes.list.LabeledUnicastRouteBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev171207.labeled.unicast.routes.list.LabeledUnicastRouteKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.PathId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.tables.Routes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
46 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
48 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 abstract class AbstractLabeledUnicastRIBSupport<C extends Routes>
56 extends MultiPathAbstractRIBSupport<C, LabeledUnicastRoute, LabeledUnicastRouteKey> {
57 private static final NodeIdentifier PREFIX_TYPE_NID = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "prefix").intern());
58 private static final NodeIdentifier LABEL_STACK_NID = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "label-stack").intern());
59 private static final NodeIdentifier LV_NID = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "label-value").intern());
60 private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(CLabeledUnicastDestination.QNAME);
61 private static final Logger LOG = LoggerFactory.getLogger(AbstractLabeledUnicastRIBSupport.class);
64 * Default constructor. Requires the QName of the container augmented under the routes choice
65 * node in instantiations of the rib grouping. It is assumed that this container is defined by
66 * the same model which populates it with route grouping instantiation, and by extension with
67 * the route attributes container.
68 * @param cazeClass Binding class of the AFI/SAFI-specific case statement, must not be null
69 * @param containerClass Binding class of the container in routes choice, must not be null.
70 * @param listClass Binding class of the route list, nust not be null;
71 * @param addressFamilyClass address Family Class
72 * @param safiClass SubsequentAddressFamily
73 * @param destinationQname destination Qname
75 AbstractLabeledUnicastRIBSupport(final Class<? extends Routes> cazeClass, final Class<? extends DataObject> containerClass,
76 final Class<? extends Route> listClass, final Class<? extends AddressFamily> addressFamilyClass,
77 final Class<? extends SubsequentAddressFamily> safiClass, final QName destinationQname) {
78 super(cazeClass, containerClass, listClass, addressFamilyClass, safiClass, ROUTE_KEY, destinationQname);
82 public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
83 return ImmutableSet.of();
87 public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
88 return ImmutableSet.of();
92 public boolean isComplexRoute() {
97 protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
98 final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
99 if (destination != null) {
100 final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination.getChild(NLRI_ROUTES_LIST);
101 if (maybeRoutes.isPresent()) {
102 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
103 if (routes instanceof UnkeyedListNode) {
104 final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
105 for (final UnkeyedListEntryNode e : ((UnkeyedListNode) routes).getValue()) {
106 final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
107 function.apply(tx, base, routeKey, e, attributes);
110 LOG.warn("Routes {} are not a map", routes);
117 protected List<CLabeledUnicastDestination> extractRoutes(final Collection<MapEntryNode> routes) {
118 return routes.stream().map(this::extractCLabeledUnicastDestination).collect(Collectors.toList());
121 private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode labeledUnicast) {
122 final ByteBuf buffer = Unpooled.buffer();
124 final CLabeledUnicastDestination dest = extractCLabeledUnicastDestination(labeledUnicast);
125 LUNlriParser.serializeNlri(Collections.singletonList(dest), false, buffer);
126 final String routeKeyValue = ByteArray.encodeBase64(buffer);
127 final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf = labeledUnicast.getChild(routePathIdNid());
128 final NodeIdentifierWithPredicates routeKey = PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), routeKeyValue, maybePathIdLeaf);
133 * Conversion from DataContainer to LabeledUnicastDestination Object
135 * @param route DataContainer
136 * @return LabeledUnicastDestination Object
138 private CLabeledUnicastDestination extractCLabeledUnicastDestination(final DataContainerNode<? extends PathArgument> route) {
139 final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
140 builder.setPrefix(extractPrefix(route, PREFIX_TYPE_NID));
141 builder.setLabelStack(extractLabel(route, LABEL_STACK_NID, LV_NID));
142 builder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
143 return builder.build();
146 protected abstract IpPrefix extractPrefix(final DataContainerNode<? extends PathArgument> route, final NodeIdentifier prefixTypeNid);
148 public static List<LabelStack> extractLabel(final DataContainerNode<? extends PathArgument> route, final NodeIdentifier labelStackNid,
149 final NodeIdentifier labelValueNid) {
150 final List<LabelStack> labels = new ArrayList<>();
151 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStacks = route.getChild(labelStackNid);
152 if (labelStacks.isPresent()) {
153 for (final UnkeyedListEntryNode label : ((UnkeyedListNode) labelStacks.get()).getValue()) {
154 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStack = label.getChild(labelValueNid);
155 if (labelStack.isPresent()) {
156 final LabelStackBuilder labelStackbuilder = new LabelStackBuilder();
157 labelStackbuilder.setLabelValue(new MplsLabel((Long) labelStack.get().getValue()));
158 labels.add(labelStackbuilder.build());
166 public final LabeledUnicastRouteKey createNewRouteKey(final PathId pathId, final LabeledUnicastRouteKey routeKey) {
167 return new LabeledUnicastRouteKey(pathId, routeKey.getRouteKey());
171 public final LabeledUnicastRouteKey extractRouteKey(final LabeledUnicastRoute route) {
172 return route.getKey();
176 public final LabeledUnicastRoute createRoute(final LabeledUnicastRoute route,
177 final LabeledUnicastRouteKey routeKey,
179 final Attributes attributes) {
180 final LabeledUnicastRouteBuilder builder;
182 builder = new LabeledUnicastRouteBuilder(route);
184 builder = new LabeledUnicastRouteBuilder();
186 return builder.setRouteKey(routeKey.getRouteKey()).setPathId(pathId).setAttributes(attributes).build();