5451b491dd39fac1bddc9cf5cc548a33d5b0c79d
[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 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 void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
94         final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
95         if (destination != null) {
96             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes
97                     = destination.getChild(NLRI_ROUTES_LIST);
98             if (maybeRoutes.isPresent()) {
99                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
100                 if (routes instanceof UnkeyedListNode) {
101                     final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
102                     for (final UnkeyedListEntryNode e : ((UnkeyedListNode) routes).getValue()) {
103                         final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
104                         function.apply(tx, base, routeKey, e, attributes);
105                     }
106                 } else {
107                     LOG.warn("Routes {} are not a map", routes);
108                 }
109             }
110         }
111     }
112
113
114     protected List<CLabeledUnicastDestination> extractRoutes(final Collection<MapEntryNode> routes) {
115         return routes.stream().map(this::extractCLabeledUnicastDestination).collect(Collectors.toList());
116     }
117
118     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode labeledUnicast) {
119         final ByteBuf buffer = Unpooled.buffer();
120
121         final CLabeledUnicastDestination dest = extractCLabeledUnicastDestination(labeledUnicast);
122         LUNlriParser.serializeNlri(Collections.singletonList(dest), false, buffer);
123         final String routeKeyValue = ByteArray.encodeBase64(buffer);
124         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf
125                 = labeledUnicast.getChild(routePathIdNid());
126         return PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), routeKeyValue, maybePathIdLeaf);
127     }
128
129     /**
130      * Conversion from DataContainer to LabeledUnicastDestination Object
131      *
132      * @param route DataContainer
133      * @return LabeledUnicastDestination Object
134      */
135     private CLabeledUnicastDestination extractCLabeledUnicastDestination(
136             final DataContainerNode<? extends PathArgument> route) {
137         final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
138         builder.setPrefix(extractPrefix(route, prefixNid()));
139         builder.setLabelStack(extractLabel(route, LABEL_STACK_NID, LV_NID));
140         builder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
141         return builder.build();
142     }
143
144     protected abstract IpPrefix extractPrefix(final DataContainerNode<? extends PathArgument> route,
145             final NodeIdentifier prefixTypeNid);
146
147     public static List<LabelStack> extractLabel(final DataContainerNode<? extends PathArgument> route,
148             final NodeIdentifier labelStackNid, final NodeIdentifier labelValueNid) {
149         final List<LabelStack> labels = new ArrayList<>();
150         final Optional<DataContainerChild<? extends PathArgument, ?>> labelStacks = route.getChild(labelStackNid);
151         if (labelStacks.isPresent()) {
152             for (final UnkeyedListEntryNode label : ((UnkeyedListNode) labelStacks.get()).getValue()) {
153                 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStack
154                         = 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());
159                 }
160             }
161         }
162         return labels;
163     }
164
165     @Override
166     public final LabeledUnicastRouteKey createRouteListKey(final long pathId, final String routeKey) {
167         return new LabeledUnicastRouteKey(new PathId(pathId), routeKey);
168     }
169
170     @Override
171     public final LabeledUnicastRoute createRoute(final LabeledUnicastRoute route, final String routeKey,
172             final long pathId, final Attributes attributes) {
173         final LabeledUnicastRouteBuilder builder;
174         if (route != null) {
175             builder = new LabeledUnicastRouteBuilder(route);
176         } else {
177             builder = new LabeledUnicastRouteBuilder();
178         }
179         return builder.withKey(new LabeledUnicastRouteKey(new PathId(pathId), routeKey))
180                 .setAttributes(attributes).build();
181     }
182 }