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