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