2264e3873b6c0d9a45fd7a5a9892cd183d3be2a9
[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 javax.annotation.Nonnull;
13 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.Route;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.tables.Routes;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
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 extends AbstractRIBSupport {
29     private final QName routeKeyQname;
30     private final QName pathIdQname;
31     private final NodeIdentifier pathIdNid;
32
33     /**
34      * Default constructor. Requires the QName of the container augmented under the routes choice
35      * node in instantiations of the rib grouping. It is assumed that this container is defined by
36      * the same model which populates it with route grouping instantiation, and by extension with
37      * the route attributes container.
38      *
39      * @param cazeClass          Binding class of the AFI/SAFI-specific case statement, must not be null
40      * @param containerClass     Binding class of the container in routes choice, must not be null.
41      * @param listClass          Binding class of the route list, nust not be null;
42      * @param addressFamilyClass address Family Class
43      * @param safiClass          SubsequentAddressFamily
44      * @param routeKeyNaming     Route Key name (prefix/ route-key / etc..)
45      * @param destinationQname   destination Qname
46      */
47     protected MultiPathAbstractRIBSupport(final Class<? extends Routes> cazeClass,
48             final Class<? extends DataObject> containerClass,
49             final Class<? extends Route> listClass, final Class<? extends AddressFamily> addressFamilyClass,
50             final Class<? extends SubsequentAddressFamily> safiClass, final String routeKeyNaming,
51             final QName destinationQname) {
52         super(cazeClass, containerClass, listClass, addressFamilyClass, safiClass, destinationQname);
53         this.routeKeyQname = QName.create(routeQName(), routeKeyNaming).intern();
54         this.pathIdQname = QName.create(routeQName(), "path-id").intern();
55         this.pathIdNid = new NodeIdentifier(this.pathIdQname);
56     }
57
58     protected final NodeIdentifier routePathIdNid() {
59         return this.pathIdNid;
60     }
61
62     protected final QName pathIdQName() {
63         return this.pathIdQname;
64     }
65
66     public final QName routeKeyQName() {
67         return this.routeKeyQname;
68     }
69
70     @Override
71     public final Long extractPathId(final NormalizedNode<?, ?> data) {
72         final Long pathId = PathIdUtil.extractPathId(data, this.routePathIdNid());
73         if (pathId == null) {
74             return PathIdUtil.NON_PATH_ID;
75         }
76         return pathId;
77     }
78
79     @Nonnull
80     @Override
81     public final PathArgument getRouteIdAddPath(final long pathId, final PathArgument routeId) {
82         return PathIdUtil.createNidKey(pathId, routeId, routeQName(), pathIdQName(), routeKeyQName());
83     }
84
85     @Override
86     public final PathArgument createRouteKeyPathArgument(final PathArgument routeKey) {
87         final ImmutableMap<QName, Object> keyValues = ImmutableMap.of(routeKeyQName(),
88                 PathIdUtil.getObjectKey(routeKey, routeKeyQName()));
89         return new NodeIdentifierWithPredicates(routeQName(), keyValues);
90     }
91
92 }