Use case-based addressing in RIBSupport
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / AbstractFlowspecRIBSupport.java
index 0cbe7c3d5679e43dd3203d7307d03ebe9e9a28cd..7be057a1debeadae4e15d019e2c7b96c22b0ffee 100644 (file)
  */
 package org.opendaylight.protocol.bgp.flowspec;
 
-import com.google.common.collect.ImmutableCollection;
-import com.google.common.collect.ImmutableSet;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.annotations.Beta;
 import com.google.common.collect.Iterables;
 import java.util.Collection;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import java.util.Optional;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
+import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.FlowspecSubsequentAddressFamily;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.flowspec.ipv6.routes.flowspec.ipv6.routes.FlowspecRoute;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
+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.multiprotocol.rev180329.destination.DestinationType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
+import org.opendaylight.yangtools.yang.binding.ChildOf;
 import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.Identifiable;
+import org.opendaylight.yangtools.yang.binding.Identifier;
 import org.opendaylight.yangtools.yang.common.QName;
 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;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
-import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
-
-public abstract class AbstractFlowspecRIBSupport extends AbstractRIBSupport {
-
-    protected abstract static class ApplyRoute {
-        abstract void apply(DOMDataWriteTransaction tx, YangInstanceIdentifier base, NodeIdentifierWithPredicates routeKey, DataContainerNode<?> route, final ContainerNode attributes);
-    }
-
-    protected static final class DeleteRoute extends ApplyRoute {
-        @Override
-        void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base, final NodeIdentifierWithPredicates routeKey, final DataContainerNode<?> route, final ContainerNode attributes) {
-            tx.delete(LogicalDatastoreType.OPERATIONAL, base.node(routeKey));
-        }
-    }
-
-    protected final class PutRoute extends ApplyRoute {
-        @Override
-        void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base, final NodeIdentifierWithPredicates routeKey,
-            final DataContainerNode<?> route, final ContainerNode attributes) {
-            final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> b = ImmutableNodes.mapEntryBuilder();
-            b.withNodeIdentifier(routeKey);
 
-            // FIXME: All route children, there should be a utility somewhere to do this
-            for (final DataContainerChild<? extends PathArgument, ?> child : route.getValue()) {
-                b.withChild(child);
-            }
-            // Add attributes
-            final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> cb = Builders.containerBuilder(attributes);
-            cb.withNodeIdentifier(routeAttributesIdentifier());
-            b.withChild(cb.build());
-            tx.put(LogicalDatastoreType.OPERATIONAL, base.node(routeKey), b.build());
-        }
-    }
-
-    private static final QName ROUTE_KEY = QName.create(FlowspecRoute.QNAME, "route-key").intern();
-    private static final ApplyRoute DELETE_ROUTE = new DeleteRoute();
-    private final ApplyRoute putRoute = new PutRoute();
-
-    protected AbstractFlowspecRIBSupport(final Class<? extends Routes> cazeClass, final Class<? extends DataObject> containerClass,
-        final Class<? extends Route> listClass) {
-        super(cazeClass, containerClass, listClass);
+@Beta
+public abstract class AbstractFlowspecRIBSupport<
+        T extends AbstractFlowspecNlriParser,
+        C extends Routes & DataObject,
+        S extends ChildOf<? super C>,
+        R extends Route & ChildOf<? super S> & Identifiable<I>,
+        I extends Identifier<R>> extends AbstractRIBSupport<C, S, R, I> {
+    protected final T nlriParser;
+
+    protected AbstractFlowspecRIBSupport(
+            final BindingNormalizedNodeSerializer mappingService,
+            final Class<C> cazeClass,
+            final Class<S> containerClass,
+            final Class<R> listClass,
+            final Class<? extends AddressFamily> afiClass,
+            final Class<? extends SubsequentAddressFamily> safiClass,
+            final QName dstContainerClassQName,
+            final T nlriParser
+    ) {
+        super(mappingService, cazeClass, containerClass, listClass, afiClass, safiClass, dstContainerClassQName);
+
+        this.nlriParser = requireNonNull(nlriParser);
     }
 
-    protected abstract NodeIdentifier routeIdentifier();
-
-    protected abstract AbstractFSNlriParser getParser();
-
-    protected abstract Class<? extends AddressFamily> getAfiClass();
-
     @Override
-    public final ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
-        return ImmutableSet.of();
+    protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
+        final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
+        final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
+        return this.nlriParser.createAdvertizedRoutesDestinationType(
+            new Object[] {this.nlriParser.extractFlowspec(routesCont)},
+            pathId
+        );
     }
 
     @Override
-    public final ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
-        return ImmutableSet.of();
+    protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
+        final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
+        final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
+        return this.nlriParser.createWithdrawnDestinationType(
+            new Object[] {this.nlriParser.extractFlowspec(Iterables.getOnlyElement(routes))},
+            pathId
+        );
     }
 
     @Override
-    public final boolean isComplexRoute() {
-        return true;
-    }
-
-    @Override
-    protected final void putDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
-        final ContainerNode destination, final ContainerNode attributes, final NodeIdentifier routesNodeId) {
-        processDestination(tx, tablePath.node(routesNodeId), destination, attributes, this.putRoute);
-    }
-
-    @Override
-    protected final void deleteDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
-        final ContainerNode destination, final NodeIdentifier routesNodeId) {
-        processDestination(tx, tablePath.node(routesNodeId), destination, null, DELETE_ROUTE);
-    }
-
-    private void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
-        final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
+    protected final void processDestination(
+        final DOMDataWriteTransaction tx,
+        final YangInstanceIdentifier routesPath,
+        final ContainerNode destination,
+        final ContainerNode attributes,
+        final ApplyRoute function
+    ) {
         if (destination != null) {
-            final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeIdentifier());
-            final NodeIdentifierWithPredicates routeKey = new NodeIdentifierWithPredicates(FlowspecRoute.QNAME, ROUTE_KEY, getParser().stringNlri(destination));
-            function.apply(tx, base, routeKey,  destination, attributes);
+            final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeQName());
+            final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf
+                    = destination.getChild(routePathIdNid());
+            final String routeKeyValue = this.nlriParser.stringNlri(destination);
+            final NodeIdentifierWithPredicates routeKey = PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
+                    pathIdQName(), routeKeyValue, maybePathIdLeaf);
+            function.apply(tx, base, routeKey, destination, attributes);
         }
     }
-
-    @Override
-    protected final MpReachNlri buildReach(final Collection<MapEntryNode> routes, final CNextHop hop) {
-        final MpReachNlriBuilder mb = new MpReachNlriBuilder();
-        mb.setAfi(getAfiClass());
-        mb.setSafi(FlowspecSubsequentAddressFamily.class);
-        mb.setCNextHop(hop);
-
-        mb.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
-            getParser().createAdvertizedRoutesDestinationType(
-                getParser().extractFlowspec(Iterables.getOnlyElement(routes)))).build());
-        return mb.build();
-    }
-
-    @Override
-    protected final MpUnreachNlri buildUnreach(final Collection<MapEntryNode> routes) {
-        final MpUnreachNlriBuilder mb = new MpUnreachNlriBuilder();
-        mb.setAfi(getAfiClass());
-        mb.setSafi(FlowspecSubsequentAddressFamily.class);
-
-        mb.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
-            getParser().createWidthdrawnDestinationType(
-                getParser().extractFlowspec(Iterables.getOnlyElement(routes)))).build());
-        return mb.build();
-    }
 }