Provide Add Path support for all AFI/SAFI
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableCollection;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Iterables;
16 import java.util.Collection;
17 import java.util.Optional;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
19 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
20 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.PathId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.destination.DestinationType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.Identifier;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
36
37 @Beta
38 public abstract class AbstractFlowspecRIBSupport<T extends AbstractFlowspecNlriParser,
39         R extends Route, S extends Identifier> extends AbstractRIBSupport<R, S> {
40     protected final T nlriParser;
41
42     protected AbstractFlowspecRIBSupport(
43         final Class<? extends Routes> cazeClass,
44         final Class<? extends DataObject> containerClass,
45         final Class<? extends Route> listClass,
46         final Class<? extends AddressFamily> afiClass,
47         final Class<? extends SubsequentAddressFamily> safiClass,
48         final QName dstContainerClassQName,
49         final T nlriParser
50     ) {
51         super(cazeClass, containerClass, listClass, afiClass, safiClass, dstContainerClassQName);
52
53         this.nlriParser = requireNonNull(nlriParser);
54     }
55
56     @Override
57     public final ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
58         return ImmutableSet.of();
59     }
60
61     @Override
62     public final ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
63         return ImmutableSet.of();
64     }
65
66     @Override
67     public final boolean isComplexRoute() {
68         return true;
69     }
70
71     @Override
72     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
73         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
74         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
75         return this.nlriParser.createAdvertizedRoutesDestinationType(
76             new Object[] {this.nlriParser.extractFlowspec(routesCont)},
77             pathId
78         );
79     }
80
81     @Override
82     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
83         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
84         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
85         return this.nlriParser.createWithdrawnDestinationType(
86             new Object[] {this.nlriParser.extractFlowspec(Iterables.getOnlyElement(routes))},
87             pathId
88         );
89     }
90
91     @Override
92     protected final void processDestination(
93         final DOMDataWriteTransaction tx,
94         final YangInstanceIdentifier routesPath,
95         final ContainerNode destination,
96         final ContainerNode attributes,
97         final ApplyRoute function
98     ) {
99         if (destination != null) {
100             final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeQName());
101             final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf
102                     = destination.getChild(routePathIdNid());
103             final String routeKeyValue = this.nlriParser.stringNlri(destination);
104             final NodeIdentifierWithPredicates routeKey = PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
105                     pathIdQName(), routeKeyValue, maybePathIdLeaf);
106             function.apply(tx, base, routeKey, destination, attributes);
107         }
108     }
109 }