BGPCEP-754: Rework EffectiveRibInWriter
[bgpcep.git] / bgp / rib-spi / src / main / java / org / opendaylight / protocol / bgp / rib / spi / MultiPathAbstractRIBSupport.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.protocol.bgp.rib.spi;
10
11 import com.google.common.collect.ImmutableMap;
12 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.tables.Routes;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.Identifier;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24
25 /**
26  * Implements common methods for Advertisement of Multiple Paths on ribSupport.
27  */
28 public abstract class MultiPathAbstractRIBSupport<C extends Routes, R extends Route, N extends Identifier>
29         extends AbstractRIBSupport<C, R, N> {
30     private final QName routeKeyQname;
31     private final QName pathIdQname;
32     private final NodeIdentifier pathIdNid;
33
34     /**
35      * Default constructor. Requires the QName of the container augmented under the routes choice
36      * node in instantiations of the rib grouping. It is assumed that this container is defined by
37      * the same model which populates it with route grouping instantiation, and by extension with
38      * the route attributes container.
39      *
40      * @param cazeClass          Binding class of the AFI/SAFI-specific case statement, must not be null
41      * @param containerClass     Binding class of the container in routes choice, must not be null.
42      * @param listClass          Binding class of the route list, nust not be null;
43      * @param addressFamilyClass address Family Class
44      * @param safiClass          SubsequentAddressFamily
45      * @param routeKeyNaming     Route Key name (prefix/ route-key / etc..)
46      * @param destinationQname   destination Qname
47      */
48     protected MultiPathAbstractRIBSupport(final Class<? extends Routes> cazeClass,
49             final Class<? extends DataObject> containerClass,
50             final Class<? extends Route> listClass, final Class<? extends AddressFamily> addressFamilyClass,
51             final Class<? extends SubsequentAddressFamily> safiClass, final String routeKeyNaming,
52             final QName destinationQname) {
53         super(cazeClass, containerClass, listClass, addressFamilyClass, safiClass, destinationQname);
54         this.routeKeyQname = QName.create(routeQName(), routeKeyNaming).intern();
55         this.pathIdQname = QName.create(routeQName(), "path-id").intern();
56         this.pathIdNid = new NodeIdentifier(this.pathIdQname);
57     }
58
59     protected final NodeIdentifier routePathIdNid() {
60         return this.pathIdNid;
61     }
62
63     protected final QName pathIdQName() {
64         return this.pathIdQname;
65     }
66
67     public final QName routeKeyQName() {
68         return this.routeKeyQname;
69     }
70
71     @Override
72     public final long extractPathId(final NormalizedNode<?, ?> data) {
73         final Long pathId = PathIdUtil.extractPathId(data, this.routePathIdNid());
74         if (pathId == null) {
75             return PathIdUtil.NON_PATH_ID;
76         }
77         return pathId;
78     }
79
80     public final NodeIdentifierWithPredicates getRouteIdAddPath(final long pathId, final PathArgument routeId) {
81         return PathIdUtil.createNidKey(pathId, routeId, routeQName(), pathIdQName(), routeKeyQName());
82     }
83
84     @Override
85     public final NodeIdentifierWithPredicates createRouteKeyPathArgument(final NodeIdentifierWithPredicates routeKey) {
86         final ImmutableMap<QName, Object> keyValues = ImmutableMap.of(routeKeyQName(),
87                 PathIdUtil.getObjectKey(routeKey, routeKeyQName()));
88         return new NodeIdentifierWithPredicates(routeQName(), keyValues);
89     }
90
91 }