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