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