BUG-5785 Support for dissemination of L3VPN flow spec II
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / AbstractFlowspecRIBSupport.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  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.flowspec;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableCollection;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.Iterables;
14 import java.util.Collection;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
17 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
18 import org.opendaylight.protocol.bgp.rib.spi.MultiPathAbstractRIBSupport;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.FlowspecSubsequentAddressFamily;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
33
34 public abstract class AbstractFlowspecRIBSupport extends MultiPathAbstractRIBSupport {
35     protected AbstractFlowspecRIBSupport(final Class<? extends Routes> cazeClass, final Class<? extends DataObject> containerClass,
36         final Class<? extends Route> listClass, final Class<? extends AddressFamily> afiClass, final QName destinationQname) {
37         super(cazeClass, containerClass, listClass, afiClass, FlowspecSubsequentAddressFamily.class, "route-key", destinationQname);
38     }
39
40     protected abstract AbstractFlowspecNlriParser getParser();
41
42     @Override
43     public final ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
44         return ImmutableSet.of();
45     }
46
47     @Override
48     public final ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
49         return ImmutableSet.of();
50     }
51
52     @Override
53     public final boolean isComplexRoute() {
54         return true;
55     }
56
57     @Nonnull
58     @Override
59     protected DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes) {
60         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
61         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
62         return getParser().createAdvertizedRoutesDestinationType(new Object[] {getParser().extractFlowspec(routesCont)}, pathId);
63     }
64
65     @Nonnull
66     @Override
67     protected DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes) {
68         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
69         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
70         return getParser().createWithdrawnDestinationType(new Object[] {getParser().extractFlowspec(Iterables.getOnlyElement(routes))}, pathId);
71     }
72
73     @Override
74     protected final void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
75         final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
76         if (destination != null) {
77             final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeQName());
78             final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf = destination.getChild(routePathIdNid());
79             final String routeKeyValue = getParser().stringNlri(destination);
80             final NodeIdentifierWithPredicates routeKey = PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), routeKeyValue, maybePathIdLeaf);
81             function.apply(tx, base, routeKey, destination, attributes);
82         }
83     }
84 }