b2a5f37f96d81ad4c9be2af40622a97e301d812e
[bgpcep.git] / bgp / extensions / labeled-unicast / src / main / java / org / opendaylight / protocol / bgp / labeled / unicast / AbstractLabeledUnicastRIBSupport.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  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.labeled.unicast;
10
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.LabeledUnicastRoutesList;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.LabeledUnicastSubsequentAddressFamily;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStack;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.LabelStackBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.destination.CLabeledUnicastDestination;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRoute;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRouteBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev180329.labeled.unicast.routes.list.LabeledUnicastRouteKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
39 import org.opendaylight.yangtools.yang.binding.ChildOf;
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;
54
55 abstract class AbstractLabeledUnicastRIBSupport<
56         C extends Routes & DataObject,
57         S extends ChildOf<? super C> & LabeledUnicastRoutesList>
58         extends AbstractRIBSupport<C, S, LabeledUnicastRoute, LabeledUnicastRouteKey> {
59     private static final NodeIdentifier LABEL_STACK_NID
60             = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "label-stack").intern());
61     private static final NodeIdentifier LV_NID
62             = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "label-value").intern());
63     private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(CLabeledUnicastDestination.QNAME);
64     private static final Logger LOG = LoggerFactory.getLogger(AbstractLabeledUnicastRIBSupport.class);
65
66     /**
67      * Default constructor. Requires the QName of the container augmented under the routes choice
68      * node in instantiations of the rib grouping. It is assumed that this container is defined by
69      * the same model which populates it with route grouping instantiation, and by extension with
70      * the route attributes container.
71      * @param mappingService  Binding Normalized Node Serializer
72      * @param cazeClass Binding class of the AFI/SAFI-specific case statement, must not be null
73      * @param containerClass Binding class of the container in routes choice, must not be null.
74      * @param addressFamilyClass address Family Class
75      * @param destinationQname destination Qname
76      */
77     AbstractLabeledUnicastRIBSupport(
78             final BindingNormalizedNodeSerializer mappingService,
79             final Class<C> cazeClass,
80             final Class<S> containerClass,
81             final Class<? extends AddressFamily> addressFamilyClass,
82             final QName destinationQname) {
83         super(mappingService,
84                 cazeClass,
85                 containerClass,
86                 LabeledUnicastRoute.class,
87                 addressFamilyClass,
88                 LabeledUnicastSubsequentAddressFamily.class,
89                 destinationQname);
90     }
91
92     @Override
93     protected Collection<NodeIdentifierWithPredicates> processDestination(final DOMDataWriteTransaction tx,
94                                                                           final YangInstanceIdentifier routesPath,
95                                                                           final ContainerNode destination,
96                                                                           final ContainerNode attributes,
97                                                                           final ApplyRoute function) {
98         if (destination != null) {
99             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes
100                     = 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 = routesYangInstanceIdentifier(routesPath);
105                     final Collection<UnkeyedListEntryNode> routesList = ((UnkeyedListNode) routes).getValue();
106                     final List<NodeIdentifierWithPredicates> keys = new ArrayList<>(routesList.size());
107                     for (final UnkeyedListEntryNode labeledUcastDest : routesList) {
108                         final NodeIdentifierWithPredicates routeKey = createRouteKey(labeledUcastDest);
109                         function.apply(tx, base, routeKey, labeledUcastDest, attributes);
110                         keys.add(routeKey);
111                     }
112                     return keys;
113                 } else {
114                     LOG.warn("Routes {} are not a map", routes);
115                 }
116             }
117         }
118         return Collections.emptyList();
119     }
120
121
122     protected List<CLabeledUnicastDestination> extractRoutes(final Collection<MapEntryNode> routes) {
123         return routes.stream().map(this::extractCLabeledUnicastDestination).collect(Collectors.toList());
124     }
125
126     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode labeledUnicast) {
127         final ByteBuf buffer = Unpooled.buffer();
128
129         final CLabeledUnicastDestination dest = extractCLabeledUnicastDestination(labeledUnicast);
130         LUNlriParser.serializeNlri(Collections.singletonList(dest), false, buffer);
131         final String routeKeyValue = ByteArray.encodeBase64(buffer);
132         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf
133                 = labeledUnicast.getChild(routePathIdNid());
134         return PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), routeKeyValue, maybePathIdLeaf);
135     }
136
137     /**
138      * Conversion from DataContainer to LabeledUnicastDestination Object
139      *
140      * @param route DataContainer
141      * @return LabeledUnicastDestination Object
142      */
143     private CLabeledUnicastDestination extractCLabeledUnicastDestination(
144             final DataContainerNode<? extends PathArgument> route) {
145         final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
146         builder.setPrefix(extractPrefix(route, prefixNid()));
147         builder.setLabelStack(extractLabel(route, LABEL_STACK_NID, LV_NID));
148         builder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
149         return builder.build();
150     }
151
152     protected abstract IpPrefix extractPrefix(final DataContainerNode<? extends PathArgument> route,
153             final NodeIdentifier prefixTypeNid);
154
155     public static List<LabelStack> extractLabel(final DataContainerNode<? extends PathArgument> route,
156             final NodeIdentifier labelStackNid, final NodeIdentifier labelValueNid) {
157         final List<LabelStack> labels = new ArrayList<>();
158         final Optional<DataContainerChild<? extends PathArgument, ?>> labelStacks = route.getChild(labelStackNid);
159         if (labelStacks.isPresent()) {
160             for (final UnkeyedListEntryNode label : ((UnkeyedListNode) labelStacks.get()).getValue()) {
161                 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStack
162                         = label.getChild(labelValueNid);
163                 if (labelStack.isPresent()) {
164                     final LabelStackBuilder labelStackbuilder = new LabelStackBuilder();
165                     labelStackbuilder.setLabelValue(new MplsLabel((Long) labelStack.get().getValue()));
166                     labels.add(labelStackbuilder.build());
167                 }
168             }
169         }
170         return labels;
171     }
172
173     @Override
174     public final LabeledUnicastRouteKey createRouteListKey(final long pathId, final String routeKey) {
175         return new LabeledUnicastRouteKey(new PathId(pathId), routeKey);
176     }
177
178     @Override
179     public final LabeledUnicastRoute createRoute(final LabeledUnicastRoute route, final String routeKey,
180             final long pathId, final Attributes attributes) {
181         final LabeledUnicastRouteBuilder builder;
182         if (route != null) {
183             builder = new LabeledUnicastRouteBuilder(route);
184         } else {
185             builder = new LabeledUnicastRouteBuilder();
186         }
187         return builder.withKey(new LabeledUnicastRouteKey(new PathId(pathId), routeKey))
188                 .setAttributes(attributes).build();
189     }
190 }