0a39473844a03046b62ab932e939f5911a5f5a94
[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.base.Optional;
12 import com.google.common.collect.ImmutableCollection;
13 import com.google.common.collect.ImmutableSet;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
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.rib.rev171207.Route;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.tables.Routes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
41 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
43 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 abstract class AbstractLabeledUnicastRIBSupport extends MultiPathAbstractRIBSupport {
51     private static final NodeIdentifier PREFIX_TYPE_NID = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "prefix").intern());
52     private static final NodeIdentifier LABEL_STACK_NID = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "label-stack").intern());
53     private static final NodeIdentifier LV_NID = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "label-value").intern());
54     private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(CLabeledUnicastDestination.QNAME);
55     private static final Logger LOG = LoggerFactory.getLogger(AbstractLabeledUnicastRIBSupport.class);
56
57     /**
58      * Default constructor. Requires the QName of the container augmented under the routes choice
59      * node in instantiations of the rib grouping. It is assumed that this container is defined by
60      * the same model which populates it with route grouping instantiation, and by extension with
61      * the route attributes container.
62      *  @param cazeClass Binding class of the AFI/SAFI-specific case statement, must not be null
63      * @param containerClass Binding class of the container in routes choice, must not be null.
64      * @param listClass Binding class of the route list, nust not be null;
65      * @param addressFamilyClass address Family Class
66      * @param safiClass SubsequentAddressFamily
67      * @param destinationQname destination Qname
68      */
69     AbstractLabeledUnicastRIBSupport(final Class<? extends Routes> cazeClass, final Class<? extends DataObject> containerClass,
70         final Class<? extends Route> listClass, final Class<? extends AddressFamily> addressFamilyClass,
71         final Class<? extends SubsequentAddressFamily> safiClass, final QName destinationQname) {
72         super(cazeClass, containerClass, listClass, addressFamilyClass, safiClass, "route-key", destinationQname);
73     }
74
75     @Override
76     public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
77         return ImmutableSet.of();
78     }
79
80     @Override
81     public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
82         return ImmutableSet.of();
83     }
84
85     @Override
86     public boolean isComplexRoute() {
87         return true;
88     }
89
90     @Override
91     protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
92         final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
93         if (destination != null) {
94             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination.getChild(NLRI_ROUTES_LIST);
95             if (maybeRoutes.isPresent()) {
96                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
97                 if (routes instanceof UnkeyedListNode) {
98                     final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
99                     for (final UnkeyedListEntryNode e : ((UnkeyedListNode) routes).getValue()) {
100                         final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
101                         function.apply(tx, base, routeKey, e, attributes);
102                     }
103                 } else {
104                     LOG.warn("Routes {} are not a map", routes);
105                 }
106             }
107         }
108     }
109
110
111     protected List<CLabeledUnicastDestination> extractRoutes(final Collection<MapEntryNode> routes) {
112         return routes.stream().map(this::extractCLabeledUnicastDestination).collect(Collectors.toList());
113     }
114
115     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode labeledUnicast) {
116         final ByteBuf buffer = Unpooled.buffer();
117
118         final CLabeledUnicastDestination dest = extractCLabeledUnicastDestination(labeledUnicast);
119         LUNlriParser.serializeNlri(Collections.singletonList(dest), false, buffer);
120         final String routeKeyValue = ByteArray.encodeBase64(buffer);
121         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf = labeledUnicast.getChild(routePathIdNid());
122         final NodeIdentifierWithPredicates routeKey = PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), routeKeyValue, maybePathIdLeaf);
123         return routeKey;
124     }
125
126     /**
127      * Conversion from DataContainer to LabeledUnicastDestination Object
128      *
129      * @param route DataContainer
130      * @return LabeledUnicastDestination Object
131      */
132     private CLabeledUnicastDestination extractCLabeledUnicastDestination(final DataContainerNode<? extends PathArgument> route) {
133         final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
134         builder.setPrefix(extractPrefix(route, PREFIX_TYPE_NID));
135         builder.setLabelStack(extractLabel(route, LABEL_STACK_NID, LV_NID));
136         builder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
137         return builder.build();
138     }
139
140     protected abstract IpPrefix extractPrefix(final DataContainerNode<? extends PathArgument> route, final NodeIdentifier prefixTypeNid);
141
142     public static List<LabelStack> extractLabel(final DataContainerNode<? extends PathArgument> route, final NodeIdentifier labelStackNid,
143         final NodeIdentifier labelValueNid) {
144         final List<LabelStack> labels = new ArrayList<>();
145         final Optional<DataContainerChild<? extends PathArgument, ?>> labelStacks = route.getChild(labelStackNid);
146         if (labelStacks.isPresent()) {
147             for (final UnkeyedListEntryNode label : ((UnkeyedListNode) labelStacks.get()).getValue()) {
148                 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStack = label.getChild(labelValueNid);
149                 if (labelStack.isPresent()) {
150                     final LabelStackBuilder labelStackbuilder = new LabelStackBuilder();
151                     labelStackbuilder.setLabelValue(new MplsLabel((Long) labelStack.get().getValue()));
152                     labels.add(labelStackbuilder.build());
153                 }
154             }
155         }
156         return labels;
157     }
158 }