Remove duplicated code
[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.Iterables;
14 import java.util.Collection;
15 import java.util.Optional;
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.AbstractRIBSupport;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
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;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
34
35 @Beta
36 public abstract class AbstractFlowspecRIBSupport<T extends AbstractFlowspecNlriParser,
37         R extends Route, S extends Identifier> extends AbstractRIBSupport<R, S> {
38     protected final T nlriParser;
39
40     protected AbstractFlowspecRIBSupport(
41         final Class<? extends Routes> cazeClass,
42         final Class<? extends DataObject> containerClass,
43         final Class<? extends Route> listClass,
44         final Class<? extends AddressFamily> afiClass,
45         final Class<? extends SubsequentAddressFamily> safiClass,
46         final QName dstContainerClassQName,
47         final T nlriParser
48     ) {
49         super(cazeClass, containerClass, listClass, afiClass, safiClass, dstContainerClassQName);
50
51         this.nlriParser = requireNonNull(nlriParser);
52     }
53
54     @Override
55     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
56         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
57         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
58         return this.nlriParser.createAdvertizedRoutesDestinationType(
59             new Object[] {this.nlriParser.extractFlowspec(routesCont)},
60             pathId
61         );
62     }
63
64     @Override
65     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
66         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
67         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
68         return this.nlriParser.createWithdrawnDestinationType(
69             new Object[] {this.nlriParser.extractFlowspec(Iterables.getOnlyElement(routes))},
70             pathId
71         );
72     }
73
74     @Override
75     protected final void processDestination(
76         final DOMDataWriteTransaction tx,
77         final YangInstanceIdentifier routesPath,
78         final ContainerNode destination,
79         final ContainerNode attributes,
80         final ApplyRoute function
81     ) {
82         if (destination != null) {
83             final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeQName());
84             final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf
85                     = destination.getChild(routePathIdNid());
86             final String routeKeyValue = this.nlriParser.stringNlri(destination);
87             final NodeIdentifierWithPredicates routeKey = PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
88                     pathIdQName(), routeKeyValue, maybePathIdLeaf);
89             function.apply(tx, base, routeKey, destination, attributes);
90         }
91     }
92 }