Migrate to MD-SAL APIs
[bgpcep.git] / bgp / extensions / 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.Iterables;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.Optional;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
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.rev180329.PathId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.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.rev180329.AddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
27 import org.opendaylight.yangtools.yang.binding.ChildOf;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.Identifiable;
30 import org.opendaylight.yangtools.yang.binding.Identifier;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
38
39 @Beta
40 public abstract class AbstractFlowspecRIBSupport<
41         T extends AbstractFlowspecNlriParser,
42         C extends Routes & DataObject,
43         S extends ChildOf<? super C>,
44         R extends Route & ChildOf<? super S> & Identifiable<I>,
45         I extends Identifier<R>> extends AbstractRIBSupport<C, S, R, I> {
46     protected final T nlriParser;
47
48     protected AbstractFlowspecRIBSupport(
49             final BindingNormalizedNodeSerializer mappingService,
50             final Class<C> cazeClass,
51             final Class<S> containerClass,
52             final Class<R> listClass,
53             final Class<? extends AddressFamily> afiClass,
54             final Class<? extends SubsequentAddressFamily> safiClass,
55             final QName dstContainerClassQName,
56             final T nlriParser
57     ) {
58         super(mappingService, cazeClass, containerClass, listClass, afiClass, safiClass, dstContainerClassQName);
59
60         this.nlriParser = requireNonNull(nlriParser);
61     }
62
63     @Override
64     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
65         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
66         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
67         return this.nlriParser.createAdvertizedRoutesDestinationType(
68             new Object[] {this.nlriParser.extractFlowspec(routesCont)},
69             pathId
70         );
71     }
72
73     @Override
74     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
75         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
76         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
77         return this.nlriParser.createWithdrawnDestinationType(
78             new Object[] {this.nlriParser.extractFlowspec(Iterables.getOnlyElement(routes))},
79             pathId
80         );
81     }
82
83     @Override
84     protected final Collection<NodeIdentifierWithPredicates> processDestination(
85         final DOMDataTreeWriteTransaction tx,
86         final YangInstanceIdentifier routesPath,
87         final ContainerNode destination,
88         final ContainerNode attributes,
89         final ApplyRoute function
90     ) {
91         if (destination == null) {
92             return Collections.emptyList();
93         }
94         final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
95
96         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf
97                 = destination.getChild(routePathIdNid());
98         final String routeKeyValue = this.nlriParser.stringNlri(destination);
99         final NodeIdentifierWithPredicates routeKey = PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
100                 pathIdQName(), routeKeyValue, maybePathIdLeaf);
101         function.apply(tx, base, routeKey, destination, attributes);
102
103         return Collections.singletonList(routeKey);
104     }
105 }