24d8eecdd409230140265ca6fcef9582b9a661d6
[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.collect.ImmutableCollection;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.Iterables;
15 import java.util.Collection;
16 import java.util.Optional;
17 import javax.annotation.Nonnull;
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.MultiPathAbstractRIBSupport;
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.rev171207.Route;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.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.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
34 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
35
36 public abstract class AbstractFlowspecRIBSupport<T extends AbstractFlowspecNlriParser> extends MultiPathAbstractRIBSupport {
37     protected final T nlriParser;
38
39     protected AbstractFlowspecRIBSupport(
40         final Class<? extends Routes> cazeClass,
41         final Class<? extends DataObject> containerClass,
42         final Class<? extends Route> listClass,
43         final Class<? extends AddressFamily> afiClass,
44         final Class<? extends SubsequentAddressFamily> safiClass,
45         final QName dstContainerClassQName,
46         final T nlriParser
47     ) {
48         super(cazeClass, containerClass, listClass, afiClass, safiClass, ROUTE_KEY, dstContainerClassQName);
49
50         this.nlriParser = requireNonNull(nlriParser);
51     }
52
53     @Override
54     @Nonnull
55     public final ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
56         return ImmutableSet.of();
57     }
58
59     @Override
60     @Nonnull
61     public final ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
62         return ImmutableSet.of();
63     }
64
65     @Override
66     public final boolean isComplexRoute() {
67         return true;
68     }
69
70     @Nonnull
71     @Override
72     protected DestinationType buildDestination(@Nonnull 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     @Nonnull
82     @Override
83     protected DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes) {
84         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
85         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
86         return this.nlriParser.createWithdrawnDestinationType(
87             new Object[] {this.nlriParser.extractFlowspec(Iterables.getOnlyElement(routes))},
88             pathId
89         );
90     }
91
92     @Override
93     protected final void processDestination(
94         final DOMDataWriteTransaction tx,
95         final YangInstanceIdentifier routesPath,
96         final ContainerNode destination,
97         final ContainerNode attributes,
98         final ApplyRoute function
99     ) {
100         if (destination != null) {
101             final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeQName());
102             final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf = destination.getChild(routePathIdNid());
103             final String routeKeyValue = this.nlriParser.stringNlri(destination);
104             final NodeIdentifierWithPredicates routeKey = PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), routeKeyValue, maybePathIdLeaf);
105             function.apply(tx, base, routeKey, destination, attributes);
106         }
107     }
108 }