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