Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bgp / 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 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.AbstractRIBSupport;
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.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.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.rev180329.Route;
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.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;
54
55 abstract class AbstractLabeledUnicastRIBSupport
56         extends AbstractRIBSupport<LabeledUnicastRoute, LabeledUnicastRouteKey> {
57     private static final NodeIdentifier PREFIX_TYPE_NID
58             = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "prefix").intern());
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 cazeClass Binding class of the AFI/SAFI-specific case statement, must not be null
72      * @param containerClass Binding class of the container in routes choice, must not be null.
73      * @param listClass Binding class of the route list, nust not be null;
74      * @param addressFamilyClass address Family Class
75      * @param safiClass SubsequentAddressFamily
76      * @param destinationQname destination Qname
77      */
78     AbstractLabeledUnicastRIBSupport(final Class<? extends Routes> cazeClass,
79             final Class<? extends DataObject> containerClass,
80             final Class<? extends Route> listClass,
81             final Class<? extends AddressFamily> addressFamilyClass,
82             final Class<? extends SubsequentAddressFamily> safiClass,
83             final QName destinationQname) {
84         super(cazeClass, containerClass, listClass, addressFamilyClass, safiClass, destinationQname);
85     }
86
87     @Override
88     public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
89         return ImmutableSet.of();
90     }
91
92     @Override
93     public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
94         return ImmutableSet.of();
95     }
96
97     @Override
98     public boolean isComplexRoute() {
99         return true;
100     }
101
102     @Override
103     protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
104         final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
105         if (destination != null) {
106             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination.getChild(NLRI_ROUTES_LIST);
107             if (maybeRoutes.isPresent()) {
108                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
109                 if (routes instanceof UnkeyedListNode) {
110                     final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
111                     for (final UnkeyedListEntryNode e : ((UnkeyedListNode) routes).getValue()) {
112                         final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
113                         function.apply(tx, base, routeKey, e, attributes);
114                     }
115                 } else {
116                     LOG.warn("Routes {} are not a map", routes);
117                 }
118             }
119         }
120     }
121
122
123     protected List<CLabeledUnicastDestination> extractRoutes(final Collection<MapEntryNode> routes) {
124         return routes.stream().map(this::extractCLabeledUnicastDestination).collect(Collectors.toList());
125     }
126
127     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode labeledUnicast) {
128         final ByteBuf buffer = Unpooled.buffer();
129
130         final CLabeledUnicastDestination dest = extractCLabeledUnicastDestination(labeledUnicast);
131         LUNlriParser.serializeNlri(Collections.singletonList(dest), false, buffer);
132         final String routeKeyValue = ByteArray.encodeBase64(buffer);
133         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf
134                 = labeledUnicast.getChild(routePathIdNid());
135         return PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), routeKeyValue, maybePathIdLeaf);
136     }
137
138     /**
139      * Conversion from DataContainer to LabeledUnicastDestination Object
140      *
141      * @param route DataContainer
142      * @return LabeledUnicastDestination Object
143      */
144     private CLabeledUnicastDestination extractCLabeledUnicastDestination(
145             final DataContainerNode<? extends PathArgument> route) {
146         final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
147         builder.setPrefix(extractPrefix(route, PREFIX_TYPE_NID));
148         builder.setLabelStack(extractLabel(route, LABEL_STACK_NID, LV_NID));
149         builder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
150         return builder.build();
151     }
152
153     protected abstract IpPrefix extractPrefix(final DataContainerNode<? extends PathArgument> route,
154             final NodeIdentifier prefixTypeNid);
155
156     public static List<LabelStack> extractLabel(final DataContainerNode<? extends PathArgument> route,
157             final NodeIdentifier labelStackNid, final NodeIdentifier labelValueNid) {
158         final List<LabelStack> labels = new ArrayList<>();
159         final Optional<DataContainerChild<? extends PathArgument, ?>> labelStacks = route.getChild(labelStackNid);
160         if (labelStacks.isPresent()) {
161             for (final UnkeyedListEntryNode label : ((UnkeyedListNode) labelStacks.get()).getValue()) {
162                 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStack
163                         = label.getChild(labelValueNid);
164                 if (labelStack.isPresent()) {
165                     final LabelStackBuilder labelStackbuilder = new LabelStackBuilder();
166                     labelStackbuilder.setLabelValue(new MplsLabel((Long) labelStack.get().getValue()));
167                     labels.add(labelStackbuilder.build());
168                 }
169             }
170         }
171         return labels;
172     }
173
174     @Override
175     public final LabeledUnicastRouteKey createRouteListKey(final long pathId, final String routeKey) {
176         return new LabeledUnicastRouteKey(new PathId(pathId), routeKey);
177     }
178
179     @Override
180     public final LabeledUnicastRoute createRoute(final LabeledUnicastRoute route, final String routeKey,
181             final long pathId, final Attributes attributes) {
182         final LabeledUnicastRouteBuilder builder;
183         if (route != null) {
184             builder = new LabeledUnicastRouteBuilder(route);
185         } else {
186             builder = new LabeledUnicastRouteBuilder();
187         }
188         return builder.setKey(new LabeledUnicastRouteKey(new PathId(pathId), routeKey))
189                 .setAttributes(attributes).build();
190     }
191 }