Replace Preconditions.CheckNotNull per RequireNonNull
[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.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.ImmutableCollection;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.collect.Iterables;
17 import java.util.Collection;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
21 import org.opendaylight.protocol.bgp.rib.spi.MultiPathAbstractRIBSupport;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
36
37 public abstract class AbstractFlowspecRIBSupport<T extends AbstractFlowspecNlriParser> extends MultiPathAbstractRIBSupport {
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, "route-key", dstContainerClassQName);
50
51         this.nlriParser = requireNonNull(nlriParser);
52     }
53
54     @Override
55     @Nonnull
56     public final ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
57         return ImmutableSet.of();
58     }
59
60     @Override
61     @Nonnull
62     public final ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
63         return ImmutableSet.of();
64     }
65
66     @Override
67     public final boolean isComplexRoute() {
68         return true;
69     }
70
71     @Nonnull
72     @Override
73     protected DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes) {
74         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
75         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
76         return this.nlriParser.createAdvertizedRoutesDestinationType(
77             new Object[] {this.nlriParser.extractFlowspec(routesCont)},
78             pathId
79         );
80     }
81
82     @Nonnull
83     @Override
84     protected DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes) {
85         final MapEntryNode routesCont = Iterables.getOnlyElement(routes);
86         final PathId pathId = PathIdUtil.buildPathId(routesCont, routePathIdNid());
87         return this.nlriParser.createWithdrawnDestinationType(
88             new Object[] {this.nlriParser.extractFlowspec(Iterables.getOnlyElement(routes))},
89             pathId
90         );
91     }
92
93     @Override
94     protected final void processDestination(
95         final DOMDataWriteTransaction tx,
96         final YangInstanceIdentifier routesPath,
97         final ContainerNode destination,
98         final ContainerNode attributes,
99         final ApplyRoute function
100     ) {
101         if (destination != null) {
102             final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeQName());
103             final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf = destination.getChild(routePathIdNid());
104             final String routeKeyValue = this.nlriParser.stringNlri(destination);
105             final NodeIdentifierWithPredicates routeKey = PathIdUtil.createNidKey(routeQName(), routeKeyQName(), pathIdQName(), routeKeyValue, maybePathIdLeaf);
106             function.apply(tx, base, routeKey, destination, attributes);
107         }
108     }
109 }