d432f3a74d8306e94f2fde7c2b52e5fde4e503b1
[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
9 package org.opendaylight.protocol.bgp.route.targetcontrain.impl;
10
11 import com.google.common.collect.ImmutableCollection;
12 import com.google.common.collect.ImmutableSet;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Optional;
20 import java.util.stream.Collectors;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
23 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
24 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
25 import org.opendaylight.protocol.bgp.route.targetcontrain.impl.nlri.SimpleRouteTargetConstrainNlriRegistry;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.RouteTargetConstrainSubsequentAddressFamily;
32 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;
33 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;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.RouteTargetConstrainChoice;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestination;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestinationBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutesBuilder;
39 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;
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.RouteTargetConstrainRouteBuilder;
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.RouteTargetConstrainRouteKey;
42 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;
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.destination.route.target.constrain.advertized._case.DestinationRouteTargetConstrain;
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.DestinationRouteTargetConstrainBuilder;
45 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;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
47 import org.opendaylight.yangtools.yang.binding.DataObject;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
53 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
55 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
59 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 /**
65  * Route Target Constrains RIBSupport.
66  *
67  * @author Claudio D. Gasparini
68  */
69 public final class RouteTargetConstrainRIBSupport
70         extends AbstractRIBSupport<RouteTargetConstrainRoutesCase, RouteTargetConstrainRoutes,
71         RouteTargetConstrainRoute, RouteTargetConstrainRouteKey> {
72     private static final Logger LOG = LoggerFactory.getLogger(RouteTargetConstrainRIBSupport.class);
73
74     private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(RouteTargetConstrainDestination.QNAME);
75     private static final RouteTargetConstrainRoutes EMPTY_CONTAINER
76             = new RouteTargetConstrainRoutesBuilder().setRouteTargetConstrainRoute(Collections.emptyList()).build();
77     private static final RouteTargetConstrainRoutesCase EMPTY_CASE =
78             new RouteTargetConstrainRoutesCaseBuilder().setRouteTargetConstrainRoutes(EMPTY_CONTAINER).build();
79     private static final String ORIGIN_AS = "origin-as";
80     private static RouteTargetConstrainRIBSupport SINGLETON;
81     private final ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects
82             = ImmutableSet.of(RouteTargetConstrainRoutesCase.class);
83     private final NodeIdentifier originAsNid;
84
85     /**
86      * Default constructor. Requires the QName of the container augmented under the routes choice
87      * node in instantiations of the rib grouping. It is assumed that this container is defined by
88      * the same model which populates it with route grouping instantiation, and by extension with
89      * the route attributes container.
90      *
91      * @param mappingService Serialization service
92      */
93     private RouteTargetConstrainRIBSupport(final BindingNormalizedNodeSerializer mappingService) {
94         super(mappingService,
95                 RouteTargetConstrainRoutesCase.class,
96                 RouteTargetConstrainRoutes.class,
97                 RouteTargetConstrainRoute.class,
98                 Ipv4AddressFamily.class,
99                 RouteTargetConstrainSubsequentAddressFamily.class,
100                 DestinationRouteTargetConstrain.QNAME);
101         this.originAsNid = new NodeIdentifier(QName.create(routeQName(), ORIGIN_AS).intern());
102     }
103
104     public static synchronized RouteTargetConstrainRIBSupport getInstance(
105             final BindingNormalizedNodeSerializer mappingService) {
106         if (SINGLETON == null) {
107             SINGLETON = new RouteTargetConstrainRIBSupport(mappingService);
108         }
109         return SINGLETON;
110     }
111
112     @Override
113     public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
114         return this.cacheableNlriObjects;
115     }
116
117     @Override
118     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
119         return new DestinationRouteTargetConstrainAdvertizedCaseBuilder().setDestinationRouteTargetConstrain(
120                 new DestinationRouteTargetConstrainBuilder()
121                         .setRouteTargetConstrainDestination(extractRoutes(routes)).build()).build();
122     }
123
124     @Override
125     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
126         return new DestinationRouteTargetConstrainWithdrawnCaseBuilder()
127                 .setDestinationRouteTargetConstrain(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
128                         .yang.bgp.route.target.constrain.rev180618.update.attributes.mp.unreach.nlri.withdrawn.routes
129                         .destination.type.destination.route.target.constrain.withdrawn._case
130                         .DestinationRouteTargetConstrainBuilder()
131                         .setRouteTargetConstrainDestination(extractRoutes(routes)).build()).build();
132     }
133
134     private List<RouteTargetConstrainDestination> extractRoutes(final Collection<MapEntryNode> routes) {
135         return routes.stream().map(this::extractDestination).collect(Collectors.toList());
136     }
137
138     private RouteTargetConstrainDestination extractDestination(final DataContainerNode<? extends PathArgument> rtDest) {
139         final RouteTargetConstrainDestinationBuilder builder = new RouteTargetConstrainDestinationBuilder()
140                 .setPathId(PathIdUtil.buildPathId(rtDest, routePathIdNid()))
141                 .setRouteTargetConstrainChoice(extractRouteTargetChoice(rtDest));
142         final Optional<Object> originAs = NormalizedNodes
143                 .findNode(rtDest, this.originAsNid).map(NormalizedNode::getValue);
144         originAs.ifPresent(o -> builder.setOriginAs(new AsNumber((Long) o)));
145         return builder.build();
146     }
147
148     private RouteTargetConstrainChoice extractRouteTargetChoice(final DataContainerNode<? extends PathArgument> route) {
149         final DataObject nn = this.mappingService.fromNormalizedNode(this.routeDefaultYii, route).getValue();
150         return ((RouteTargetConstrainRoute) nn).getRouteTargetConstrainChoice();
151     }
152
153     @Override
154     protected Collection<NodeIdentifierWithPredicates> processDestination(
155             final DOMDataWriteTransaction tx,
156             final YangInstanceIdentifier routesPath,
157             final ContainerNode destination,
158             final ContainerNode attributes,
159             final ApplyRoute function) {
160         if (destination != null) {
161             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination
162                     .getChild(NLRI_ROUTES_LIST);
163             if (maybeRoutes.isPresent()) {
164                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
165                 if (routes instanceof UnkeyedListNode) {
166                     final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
167                     final Collection<UnkeyedListEntryNode> routesList = ((UnkeyedListNode) routes).getValue();
168                     final List<NodeIdentifierWithPredicates> keys = new ArrayList<>(routesList.size());
169                     for (final UnkeyedListEntryNode rtDest : routesList) {
170                         final NodeIdentifierWithPredicates routeKey = createRouteKey(rtDest);
171                         function.apply(tx, base, routeKey, rtDest, attributes);
172                         keys.add(routeKey);
173                     }
174                     return keys;
175                 } else {
176                     LOG.warn("Routes {} are not a map", routes);
177                 }
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(), routeKeyQName(),
191                 pathIdQName(), 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 RouteTargetConstrainRoutesCase emptyRoutesCase() {
208         return EMPTY_CASE;
209     }
210
211
212     @Override
213     public RouteTargetConstrainRoutes emptyRoutesContainer() {
214         return EMPTY_CONTAINER;
215     }
216
217     @Override
218     public RouteTargetConstrainRouteKey createRouteListKey(final PathId pathId, final String routeKey) {
219         return new RouteTargetConstrainRouteKey(pathId, routeKey);
220     }
221
222     @Override
223     public List<RouteTargetConstrainRoute> routesFromContainer(final RouteTargetConstrainRoutes container) {
224         return container.getRouteTargetConstrainRoute();
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 }