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