Bump versions to 0.21.8-SNAPSHOT
[bgpcep.git] / bgp / extensions / route-target / src / main / java / org / opendaylight / protocol / bgp / route / targetcontrain / impl / RouteTargetConstrainRIBSupport.java
index d2a719b3716d34027a68c7b74b57bacdbfee36ca..5eb40d82fbe1e86b6c1f5575bc2f2036ad83bf0a 100644 (file)
@@ -5,7 +5,6 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.protocol.bgp.route.targetcontrain.impl;
 
 import com.google.common.collect.ImmutableCollection;
@@ -18,38 +17,33 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
 import java.util.stream.Collectors;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
-import org.opendaylight.protocol.bgp.route.targetcontrain.impl.nlri.SimpleRouteTargetConstrainNlriRegistry;
+import org.opendaylight.protocol.bgp.route.targetcontrain.impl.nlri.ImmutableRouteTargetConstrainNlriRegistry;
 import org.opendaylight.protocol.util.ByteArray;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.RouteTargetConstrainSubsequentAddressFamily;
 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;
-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;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.RouteTargetConstrainChoice;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestination;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.destination.RouteTargetConstrainDestinationBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.route.target.constrain.rev180618.route.target.constrain.routes.RouteTargetConstrainRoutesBuilder;
 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;
-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;
-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;
 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;
 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;
 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;
 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;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
+import org.opendaylight.yangtools.yang.binding.BindingObject;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
@@ -68,17 +62,12 @@ import org.slf4j.LoggerFactory;
  */
 public final class RouteTargetConstrainRIBSupport
         extends AbstractRIBSupport<RouteTargetConstrainRoutesCase, RouteTargetConstrainRoutes,
-        RouteTargetConstrainRoute, RouteTargetConstrainRouteKey> {
+        RouteTargetConstrainRoute> {
     private static final Logger LOG = LoggerFactory.getLogger(RouteTargetConstrainRIBSupport.class);
 
     private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(RouteTargetConstrainDestination.QNAME);
-    private static final RouteTargetConstrainRoutes EMPTY_CONTAINER
-            = new RouteTargetConstrainRoutesBuilder().setRouteTargetConstrainRoute(Collections.emptyList()).build();
-    private static final RouteTargetConstrainRoutesCase EMPTY_CASE =
-            new RouteTargetConstrainRoutesCaseBuilder().setRouteTargetConstrainRoutes(EMPTY_CONTAINER).build();
     private static final String ORIGIN_AS = "origin-as";
-    private static RouteTargetConstrainRIBSupport SINGLETON;
-    private final ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects
+    private final ImmutableCollection<Class<? extends BindingObject>> cacheableNlriObjects
             = ImmutableSet.of(RouteTargetConstrainRoutesCase.class);
     private final NodeIdentifier originAsNid;
 
@@ -90,28 +79,20 @@ public final class RouteTargetConstrainRIBSupport
      *
      * @param mappingService Serialization service
      */
-    private RouteTargetConstrainRIBSupport(final BindingNormalizedNodeSerializer mappingService) {
+    public RouteTargetConstrainRIBSupport(final BindingNormalizedNodeSerializer mappingService) {
         super(mappingService,
-                RouteTargetConstrainRoutesCase.class,
-                RouteTargetConstrainRoutes.class,
-                RouteTargetConstrainRoute.class,
-                Ipv4AddressFamily.class,
-                RouteTargetConstrainSubsequentAddressFamily.class,
-                DestinationRouteTargetConstrain.QNAME);
-        this.originAsNid = new NodeIdentifier(QName.create(routeQName(), ORIGIN_AS).intern());
-    }
-
-    public static synchronized RouteTargetConstrainRIBSupport getInstance(
-            final BindingNormalizedNodeSerializer mappingService) {
-        if (SINGLETON == null) {
-            SINGLETON = new RouteTargetConstrainRIBSupport(mappingService);
-        }
-        return SINGLETON;
+            RouteTargetConstrainRoutesCase.class, RouteTargetConstrainRoutesCase.QNAME,
+            RouteTargetConstrainRoutes.class, RouteTargetConstrainRoutes.QNAME,
+            RouteTargetConstrainRoute.class, RouteTargetConstrainRoute.QNAME,
+            Ipv4AddressFamily.VALUE, Ipv4AddressFamily.QNAME,
+            RouteTargetConstrainSubsequentAddressFamily.VALUE, RouteTargetConstrainSubsequentAddressFamily.QNAME,
+            DestinationRouteTargetConstrain.QNAME);
+        originAsNid = new NodeIdentifier(QName.create(routeQName(), ORIGIN_AS).intern());
     }
 
     @Override
-    public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
-        return this.cacheableNlriObjects;
+    public ImmutableCollection<Class<? extends BindingObject>> cacheableNlriObjects() {
+        return cacheableNlriObjects;
     }
 
     @Override
@@ -135,36 +116,34 @@ public final class RouteTargetConstrainRIBSupport
         return routes.stream().map(this::extractDestination).collect(Collectors.toList());
     }
 
-    private RouteTargetConstrainDestination extractDestination(final DataContainerNode<? extends PathArgument> rtDest) {
+    private RouteTargetConstrainDestination extractDestination(final DataContainerNode rtDest) {
         final RouteTargetConstrainDestinationBuilder builder = new RouteTargetConstrainDestinationBuilder()
                 .setPathId(PathIdUtil.buildPathId(rtDest, routePathIdNid()))
                 .setRouteTargetConstrainChoice(extractRouteTargetChoice(rtDest));
         final Optional<Object> originAs = NormalizedNodes
-                .findNode(rtDest, this.originAsNid).map(NormalizedNode::getValue);
-        originAs.ifPresent(o -> builder.setOriginAs(new AsNumber((Long) o)));
+                .findNode(rtDest, originAsNid).map(NormalizedNode::body);
+        originAs.ifPresent(o -> builder.setOriginAs(new AsNumber((Uint32) o)));
         return builder.build();
     }
 
-    private RouteTargetConstrainChoice extractRouteTargetChoice(final DataContainerNode<? extends PathArgument> route) {
-        final DataObject nn = this.mappingService.fromNormalizedNode(this.routeDefaultYii, route).getValue();
+    private RouteTargetConstrainChoice extractRouteTargetChoice(final DataContainerNode route) {
+        final DataObject nn = mappingService.fromNormalizedNode(routeDefaultYii, route).getValue();
         return ((RouteTargetConstrainRoute) nn).getRouteTargetConstrainChoice();
     }
 
     @Override
     protected Collection<NodeIdentifierWithPredicates> processDestination(
-            final DOMDataWriteTransaction tx,
+            final DOMDataTreeWriteTransaction tx,
             final YangInstanceIdentifier routesPath,
             final ContainerNode destination,
             final ContainerNode attributes,
             final ApplyRoute function) {
         if (destination != null) {
-            final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination
-                    .getChild(NLRI_ROUTES_LIST);
-            if (maybeRoutes.isPresent()) {
-                final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
+            final DataContainerChild routes = destination.childByArg(NLRI_ROUTES_LIST);
+            if (routes != null) {
                 if (routes instanceof UnkeyedListNode) {
                     final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
-                    final Collection<UnkeyedListEntryNode> routesList = ((UnkeyedListNode) routes).getValue();
+                    final Collection<UnkeyedListEntryNode> routesList = ((UnkeyedListNode) routes).body();
                     final List<NodeIdentifierWithPredicates> keys = new ArrayList<>(routesList.size());
                     for (final UnkeyedListEntryNode rtDest : routesList) {
                         final NodeIdentifierWithPredicates routeKey = createRouteKey(rtDest);
@@ -172,9 +151,8 @@ public final class RouteTargetConstrainRIBSupport
                         keys.add(routeKey);
                     }
                     return keys;
-                } else {
-                    LOG.warn("Routes {} are not a map", routes);
                 }
+                LOG.warn("Routes {} are not a map", routes);
             }
         }
         return Collections.emptyList();
@@ -183,44 +161,9 @@ public final class RouteTargetConstrainRIBSupport
     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode routeTarget) {
         final ByteBuf buffer = Unpooled.buffer();
         final RouteTargetConstrainDestination dest = extractDestination(routeTarget);
-        buffer.writeBytes(SimpleRouteTargetConstrainNlriRegistry.getInstance()
+        buffer.writeBytes(ImmutableRouteTargetConstrainNlriRegistry.getInstance()
                 .serializeRouteTargetConstrain(dest.getRouteTargetConstrainChoice()));
-        final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
-                routeTarget.getChild(routePathIdNid());
-        return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
-                pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
-    }
-
-    @Override
-    public RouteTargetConstrainRoute createRoute(
-            final RouteTargetConstrainRoute route,
-            final String routeKey,
-            final long pathId,
-            final Attributes attributes) {
-        final RouteTargetConstrainRouteBuilder builder;
-        if (route != null) {
-            builder = new RouteTargetConstrainRouteBuilder(route);
-        } else {
-            builder = new RouteTargetConstrainRouteBuilder();
-        }
-        return builder.withKey(createRouteListKey(pathId, routeKey)).setAttributes(attributes).build();
-    }
-
-
-    @Override
-    public RouteTargetConstrainRoutesCase emptyRoutesCase() {
-        return EMPTY_CASE;
-    }
-
-
-    @Override
-    public RouteTargetConstrainRoutes emptyRoutesContainer() {
-        return EMPTY_CONTAINER;
-    }
-
-
-    @Override
-    public RouteTargetConstrainRouteKey createRouteListKey(final long pathId, final String routeKey) {
-        return new RouteTargetConstrainRouteKey(new PathId(pathId), routeKey);
+        return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
+                ByteArray.encodeBase64(buffer), routeTarget.findChildByArg(routePathIdNid()));
     }
 }