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