Migrate to MD-SAL APIs
[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.rev180329.PathId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.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.bgp.rib.rib.loc.rib.tables.routes.RouteTargetConstrainRoutesCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.RouteTargetConstrainChoice;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestination;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestinationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutesBuilder;
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.RouteTargetConstrainRoute;
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.RouteTargetConstrainRouteBuilder;
43 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;
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.DestinationRouteTargetConstrainAdvertizedCaseBuilder;
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.DestinationRouteTargetConstrain;
46 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;
47 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;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
49 import org.opendaylight.yangtools.yang.binding.BindingObject;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.common.QName;
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     @Deprecated
81     private static final RouteTargetConstrainRoutesCase EMPTY_CASE =
82             new RouteTargetConstrainRoutesCaseBuilder().setRouteTargetConstrainRoutes(EMPTY_CONTAINER).build();
83     private static final String ORIGIN_AS = "origin-as";
84     private static RouteTargetConstrainRIBSupport SINGLETON;
85     private final ImmutableCollection<Class<? extends BindingObject>> cacheableNlriObjects
86             = ImmutableSet.of(RouteTargetConstrainRoutesCase.class);
87     private final NodeIdentifier originAsNid;
88
89     /**
90      * Default constructor. Requires the QName of the container augmented under the routes choice
91      * node in instantiations of the rib grouping. It is assumed that this container is defined by
92      * the same model which populates it with route grouping instantiation, and by extension with
93      * the route attributes container.
94      *
95      * @param mappingService Serialization service
96      */
97     private RouteTargetConstrainRIBSupport(final BindingNormalizedNodeSerializer mappingService) {
98         super(mappingService,
99                 RouteTargetConstrainRoutesCase.class,
100                 RouteTargetConstrainRoutes.class,
101                 RouteTargetConstrainRoute.class,
102                 Ipv4AddressFamily.class,
103                 RouteTargetConstrainSubsequentAddressFamily.class,
104                 DestinationRouteTargetConstrain.QNAME);
105         this.originAsNid = new NodeIdentifier(QName.create(routeQName(), ORIGIN_AS).intern());
106     }
107
108     public static synchronized RouteTargetConstrainRIBSupport getInstance(
109             final BindingNormalizedNodeSerializer mappingService) {
110         if (SINGLETON == null) {
111             SINGLETON = new RouteTargetConstrainRIBSupport(mappingService);
112         }
113         return SINGLETON;
114     }
115
116     @Override
117     public ImmutableCollection<Class<? extends BindingObject>> cacheableNlriObjects() {
118         return this.cacheableNlriObjects;
119     }
120
121     @Override
122     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
123         return new DestinationRouteTargetConstrainAdvertizedCaseBuilder().setDestinationRouteTargetConstrain(
124                 new DestinationRouteTargetConstrainBuilder()
125                         .setRouteTargetConstrainDestination(extractRoutes(routes)).build()).build();
126     }
127
128     @Override
129     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
130         return new DestinationRouteTargetConstrainWithdrawnCaseBuilder()
131                 .setDestinationRouteTargetConstrain(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
132                         .yang.bgp.route.target.constrain.rev180618.update.attributes.mp.unreach.nlri.withdrawn.routes
133                         .destination.type.destination.route.target.constrain.withdrawn._case
134                         .DestinationRouteTargetConstrainBuilder()
135                         .setRouteTargetConstrainDestination(extractRoutes(routes)).build()).build();
136     }
137
138     private List<RouteTargetConstrainDestination> extractRoutes(final Collection<MapEntryNode> routes) {
139         return routes.stream().map(this::extractDestination).collect(Collectors.toList());
140     }
141
142     private RouteTargetConstrainDestination extractDestination(final DataContainerNode<? extends PathArgument> rtDest) {
143         final RouteTargetConstrainDestinationBuilder builder = new RouteTargetConstrainDestinationBuilder()
144                 .setPathId(PathIdUtil.buildPathId(rtDest, routePathIdNid()))
145                 .setRouteTargetConstrainChoice(extractRouteTargetChoice(rtDest));
146         final Optional<Object> originAs = NormalizedNodes
147                 .findNode(rtDest, this.originAsNid).map(NormalizedNode::getValue);
148         originAs.ifPresent(o -> builder.setOriginAs(new AsNumber((Long) o)));
149         return builder.build();
150     }
151
152     private RouteTargetConstrainChoice extractRouteTargetChoice(final DataContainerNode<? extends PathArgument> route) {
153         final DataObject nn = this.mappingService.fromNormalizedNode(this.routeDefaultYii, route).getValue();
154         return ((RouteTargetConstrainRoute) nn).getRouteTargetConstrainChoice();
155     }
156
157     @Override
158     protected Collection<NodeIdentifierWithPredicates> processDestination(
159             final DOMDataTreeWriteTransaction tx,
160             final YangInstanceIdentifier routesPath,
161             final ContainerNode destination,
162             final ContainerNode attributes,
163             final ApplyRoute function) {
164         if (destination != null) {
165             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination
166                     .getChild(NLRI_ROUTES_LIST);
167             if (maybeRoutes.isPresent()) {
168                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
169                 if (routes instanceof UnkeyedListNode) {
170                     final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
171                     final Collection<UnkeyedListEntryNode> routesList = ((UnkeyedListNode) routes).getValue();
172                     final List<NodeIdentifierWithPredicates> keys = new ArrayList<>(routesList.size());
173                     for (final UnkeyedListEntryNode rtDest : routesList) {
174                         final NodeIdentifierWithPredicates routeKey = createRouteKey(rtDest);
175                         function.apply(tx, base, routeKey, rtDest, attributes);
176                         keys.add(routeKey);
177                     }
178                     return keys;
179                 } else {
180                     LOG.warn("Routes {} are not a map", routes);
181                 }
182             }
183         }
184         return Collections.emptyList();
185     }
186
187     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode routeTarget) {
188         final ByteBuf buffer = Unpooled.buffer();
189         final RouteTargetConstrainDestination dest = extractDestination(routeTarget);
190         buffer.writeBytes(SimpleRouteTargetConstrainNlriRegistry.getInstance()
191                 .serializeRouteTargetConstrain(dest.getRouteTargetConstrainChoice()));
192         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
193                 routeTarget.getChild(routePathIdNid());
194         return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
195                 pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
196     }
197
198     @Override
199     public RouteTargetConstrainRoute createRoute(final RouteTargetConstrainRoute route,
200             final RouteTargetConstrainRouteKey key, final Attributes attributes) {
201         final RouteTargetConstrainRouteBuilder builder;
202         if (route != null) {
203             builder = new RouteTargetConstrainRouteBuilder(route);
204         } else {
205             builder = new RouteTargetConstrainRouteBuilder();
206         }
207         return builder.withKey(key).setAttributes(attributes).build();
208     }
209
210     @Override
211     @Deprecated
212     public RouteTargetConstrainRoutesCase emptyRoutesCase() {
213         return EMPTY_CASE;
214     }
215
216
217     @Override
218     public RouteTargetConstrainRoutes emptyRoutesContainer() {
219         return EMPTY_CONTAINER;
220     }
221
222     @Override
223     public RouteTargetConstrainRouteKey createRouteListKey(final PathId pathId, final String routeKey) {
224         return new RouteTargetConstrainRouteKey(pathId, routeKey);
225     }
226
227     @Override
228     public PathId extractPathId(final RouteTargetConstrainRouteKey routeListKey) {
229         return routeListKey.getPathId();
230     }
231
232     @Override
233     public String extractRouteKey(final RouteTargetConstrainRouteKey routeListKey) {
234         return routeListKey.getRouteKey();
235     }
236
237     @Override
238     public List<RouteTargetConstrainRoute> extractAdjRibInRoutes(final Routes routes) {
239         verify(routes instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target
240             .constrain.rev180618.bgp.rib.rib.peer.adj.rib.in.tables.routes.RouteTargetConstrainRoutesCase,
241             "Unrecognized routes %s", routes);
242         return ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618
243                 .bgp.rib.rib.peer.adj.rib.in.tables.routes.RouteTargetConstrainRoutesCase) routes)
244                 .getRouteTargetConstrainRoutes().nonnullRouteTargetConstrainRoute();
245     }
246 }