BGPCEP-754: Rework EffectiveRibInWriter
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / l3vpn / AbstractFlowspecL3vpnRIBSupport.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.l3vpn;
9
10 import static org.opendaylight.bgp.concepts.RouteDistinguisherUtil.extractRouteDistinguisher;
11
12 import com.google.common.collect.Iterables;
13 import java.util.Collection;
14 import javax.annotation.Nonnull;
15
16 import org.opendaylight.protocol.bgp.flowspec.AbstractFlowspecRIBSupport;
17 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev171207.FlowspecL3vpnSubsequentAddressFamily;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.PathId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.destination.DestinationType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.tables.Routes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisher;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.Identifier;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
30
31 public abstract class AbstractFlowspecL3vpnRIBSupport
32         <T extends AbstractFlowspecL3vpnNlriParser, C extends Routes, R extends Route, S extends Identifier>
33         extends AbstractFlowspecRIBSupport<T, C, R, S> {
34     private final NodeIdentifier routeDistinguisherNID;
35
36     protected AbstractFlowspecL3vpnRIBSupport(
37         final Class<? extends Routes> cazeClass,
38         final Class<? extends DataObject> containerClass,
39         final Class<? extends Route> listClass,
40         final QName dstContainerClassQName,
41         final Class<? extends AddressFamily> afiClass,
42         final T flowspecNlriParser
43     ) {
44         super(cazeClass, containerClass, listClass, afiClass, FlowspecL3vpnSubsequentAddressFamily.class,
45                 dstContainerClassQName, flowspecNlriParser);
46         this.routeDistinguisherNID
47                 = new NodeIdentifier(QName.create(routeQName(), "route-distinguisher").intern());
48     }
49
50     @Nonnull
51     @Override
52     protected DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes) {
53         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
54         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
55         final RouteDistinguisher rd = extractRouteDistinguisher(routesCont, this.routeDistinguisherNID);
56         return this.nlriParser.createAdvertizedRoutesDestinationType(
57             new Object[] {rd, this.nlriParser.extractFlowspec(routesCont)},
58             pathId
59         );
60     }
61
62     @Nonnull
63     @Override
64     protected DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes) {
65         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
66         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
67             final RouteDistinguisher rd = extractRouteDistinguisher(routesCont, this.routeDistinguisherNID);
68         return this.nlriParser.createWithdrawnDestinationType(
69             new Object[] {rd, this.nlriParser.extractFlowspec(Iterables.getOnlyElement(routes))},
70             pathId
71         );
72     }
73 }