f0254e3c0bcab1fec2c3eccef7ddccde50a7177d
[bgpcep.git] / bgp / extensions / route-target / src / main / java / org / opendaylight / protocol / bgp / route / targetcontrain / impl / RouteTargetConstrainRIBSupport.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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 package org.opendaylight.protocol.bgp.route.targetcontrain.impl;
9
10 import static com.google.common.base.Verify.verify;
11
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.Optional;
21 import java.util.stream.Collectors;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
24 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
25 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
26 import org.opendaylight.protocol.bgp.route.targetcontrain.impl.nlri.SimpleRouteTargetConstrainNlriRegistry;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.RouteTargetConstrainSubsequentAddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.bgp.rib.rib.loc.rib.tables.routes.RouteTargetConstrainRoutesCase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.RouteTargetConstrainChoice;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestination;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestinationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.route.target.constrain.routes.RouteTargetConstrainRoute;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.route.target.constrain.routes.RouteTargetConstrainRouteBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.route.target.constrain.routes.RouteTargetConstrainRouteKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationRouteTargetConstrainAdvertizedCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.route.target.constrain.advertized._case.DestinationRouteTargetConstrain;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.route.target.constrain.advertized._case.DestinationRouteTargetConstrainBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationRouteTargetConstrainWithdrawnCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
48 import org.opendaylight.yangtools.yang.binding.BindingObject;
49 import org.opendaylight.yangtools.yang.binding.DataObject;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.common.Uint32;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
56 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
58 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
62 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67 /**
68  * Route Target Constrains RIBSupport.
69  *
70  * @author Claudio D. Gasparini
71  */
72 public final class RouteTargetConstrainRIBSupport
73         extends AbstractRIBSupport<RouteTargetConstrainRoutesCase, RouteTargetConstrainRoutes,
74         RouteTargetConstrainRoute, RouteTargetConstrainRouteKey> {
75     private static final Logger LOG = LoggerFactory.getLogger(RouteTargetConstrainRIBSupport.class);
76
77     private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(RouteTargetConstrainDestination.QNAME);
78     private static final RouteTargetConstrainRoutes EMPTY_CONTAINER
79             = new RouteTargetConstrainRoutesBuilder().setRouteTargetConstrainRoute(Collections.emptyList()).build();
80     private static final String ORIGIN_AS = "origin-as";
81     private static RouteTargetConstrainRIBSupport SINGLETON;
82     private final ImmutableCollection<Class<? extends BindingObject>> cacheableNlriObjects
83             = ImmutableSet.of(RouteTargetConstrainRoutesCase.class);
84     private final NodeIdentifier originAsNid;
85
86     /**
87      * Default constructor. Requires the QName of the container augmented under the routes choice
88      * node in instantiations of the rib grouping. It is assumed that this container is defined by
89      * the same model which populates it with route grouping instantiation, and by extension with
90      * the route attributes container.
91      *
92      * @param mappingService Serialization service
93      */
94     private RouteTargetConstrainRIBSupport(final BindingNormalizedNodeSerializer mappingService) {
95         super(mappingService,
96                 RouteTargetConstrainRoutesCase.class,
97                 RouteTargetConstrainRoutes.class,
98                 RouteTargetConstrainRoute.class,
99                 Ipv4AddressFamily.class,
100                 RouteTargetConstrainSubsequentAddressFamily.class,
101                 DestinationRouteTargetConstrain.QNAME);
102         this.originAsNid = new NodeIdentifier(QName.create(routeQName(), ORIGIN_AS).intern());
103     }
104
105     public static synchronized RouteTargetConstrainRIBSupport getInstance(
106             final BindingNormalizedNodeSerializer mappingService) {
107         if (SINGLETON == null) {
108             SINGLETON = new RouteTargetConstrainRIBSupport(mappingService);
109         }
110         return SINGLETON;
111     }
112
113     @Override
114     public ImmutableCollection<Class<? extends BindingObject>> cacheableNlriObjects() {
115         return this.cacheableNlriObjects;
116     }
117
118     @Override
119     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
120         return new DestinationRouteTargetConstrainAdvertizedCaseBuilder().setDestinationRouteTargetConstrain(
121                 new DestinationRouteTargetConstrainBuilder()
122                         .setRouteTargetConstrainDestination(extractRoutes(routes)).build()).build();
123     }
124
125     @Override
126     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
127         return new DestinationRouteTargetConstrainWithdrawnCaseBuilder()
128                 .setDestinationRouteTargetConstrain(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
129                         .yang.bgp.route.target.constrain.rev180618.update.attributes.mp.unreach.nlri.withdrawn.routes
130                         .destination.type.destination.route.target.constrain.withdrawn._case
131                         .DestinationRouteTargetConstrainBuilder()
132                         .setRouteTargetConstrainDestination(extractRoutes(routes)).build()).build();
133     }
134
135     private List<RouteTargetConstrainDestination> extractRoutes(final Collection<MapEntryNode> routes) {
136         return routes.stream().map(this::extractDestination).collect(Collectors.toList());
137     }
138
139     private RouteTargetConstrainDestination extractDestination(final DataContainerNode<? extends PathArgument> rtDest) {
140         final RouteTargetConstrainDestinationBuilder builder = new RouteTargetConstrainDestinationBuilder()
141                 .setPathId(PathIdUtil.buildPathId(rtDest, routePathIdNid()))
142                 .setRouteTargetConstrainChoice(extractRouteTargetChoice(rtDest));
143         final Optional<Object> originAs = NormalizedNodes
144                 .findNode(rtDest, this.originAsNid).map(NormalizedNode::getValue);
145         originAs.ifPresent(o -> builder.setOriginAs(new AsNumber((Uint32) o)));
146         return builder.build();
147     }
148
149     private RouteTargetConstrainChoice extractRouteTargetChoice(final DataContainerNode<? extends PathArgument> route) {
150         final DataObject nn = this.mappingService.fromNormalizedNode(this.routeDefaultYii, route).getValue();
151         return ((RouteTargetConstrainRoute) nn).getRouteTargetConstrainChoice();
152     }
153
154     @Override
155     protected Collection<NodeIdentifierWithPredicates> processDestination(
156             final DOMDataTreeWriteTransaction tx,
157             final YangInstanceIdentifier routesPath,
158             final ContainerNode destination,
159             final ContainerNode attributes,
160             final ApplyRoute function) {
161         if (destination != null) {
162             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination
163                     .getChild(NLRI_ROUTES_LIST);
164             if (maybeRoutes.isPresent()) {
165                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
166                 if (routes instanceof UnkeyedListNode) {
167                     final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
168                     final Collection<UnkeyedListEntryNode> routesList = ((UnkeyedListNode) routes).getValue();
169                     final List<NodeIdentifierWithPredicates> keys = new ArrayList<>(routesList.size());
170                     for (final UnkeyedListEntryNode rtDest : routesList) {
171                         final NodeIdentifierWithPredicates routeKey = createRouteKey(rtDest);
172                         function.apply(tx, base, routeKey, rtDest, attributes);
173                         keys.add(routeKey);
174                     }
175                     return keys;
176                 }
177                 LOG.warn("Routes {} are not a map", routes);
178             }
179         }
180         return Collections.emptyList();
181     }
182
183     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode routeTarget) {
184         final ByteBuf buffer = Unpooled.buffer();
185         final RouteTargetConstrainDestination dest = extractDestination(routeTarget);
186         buffer.writeBytes(SimpleRouteTargetConstrainNlriRegistry.getInstance()
187                 .serializeRouteTargetConstrain(dest.getRouteTargetConstrainChoice()));
188         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
189                 routeTarget.getChild(routePathIdNid());
190         return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
191                 ByteArray.encodeBase64(buffer), maybePathIdLeaf);
192     }
193
194     @Override
195     public RouteTargetConstrainRoute createRoute(final RouteTargetConstrainRoute route,
196             final RouteTargetConstrainRouteKey key, final Attributes attributes) {
197         final RouteTargetConstrainRouteBuilder builder;
198         if (route != null) {
199             builder = new RouteTargetConstrainRouteBuilder(route);
200         } else {
201             builder = new RouteTargetConstrainRouteBuilder();
202         }
203         return builder.withKey(key).setAttributes(attributes).build();
204     }
205
206     @Override
207     public RouteTargetConstrainRoutes emptyRoutesContainer() {
208         return EMPTY_CONTAINER;
209     }
210
211     @Override
212     public RouteTargetConstrainRouteKey createRouteListKey(final PathId pathId, final String routeKey) {
213         return new RouteTargetConstrainRouteKey(pathId, routeKey);
214     }
215
216     @Override
217     public PathId extractPathId(final RouteTargetConstrainRouteKey routeListKey) {
218         return routeListKey.getPathId();
219     }
220
221     @Override
222     public String extractRouteKey(final RouteTargetConstrainRouteKey routeListKey) {
223         return routeListKey.getRouteKey();
224     }
225
226     @Override
227     public List<RouteTargetConstrainRoute> extractAdjRibInRoutes(final Routes routes) {
228         verify(routes instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target
229             .constrain.rev180618.bgp.rib.rib.peer.adj.rib.in.tables.routes.RouteTargetConstrainRoutesCase,
230             "Unrecognized routes %s", routes);
231         return ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618
232                 .bgp.rib.rib.peer.adj.rib.in.tables.routes.RouteTargetConstrainRoutesCase) routes)
233                 .getRouteTargetConstrainRoutes().nonnullRouteTargetConstrainRoute();
234     }
235 }