BUG-6647 Increase code coverage and clean up III
[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.rev130925.Route;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.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, 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, final QName destinationQname) {
50         super(cazeClass, containerClass, listClass, addressFamilyClass, safiClass, destinationQname);
51         this.routeKeyQname = QName.create(routeQName(), routeKeyNaming).intern();
52         this.pathIdQname = QName.create(routeQName(), "path-id").intern();
53         this.pathIdNid = new NodeIdentifier(this.pathIdQname);
54     }
55
56     protected final NodeIdentifier routePathIdNid() {
57         return this.pathIdNid;
58     }
59
60     protected final QName pathIdQName() {
61         return this.pathIdQname;
62     }
63
64     public final QName routeKeyQName() {
65         return this.routeKeyQname;
66     }
67
68     @Override
69     public final Long extractPathId(final NormalizedNode<?, ?> data) {
70         final Long pathId = PathIdUtil.extractPathId(data, this.routePathIdNid());
71         if(pathId == null) {
72             return PathIdUtil.NON_PATH_ID;
73         }
74         return pathId;
75     }
76
77     @Nonnull
78     @Override
79     public final PathArgument getRouteIdAddPath(final long pathId, final PathArgument routeId) {
80         return PathIdUtil.createNidKey(pathId, routeId, routeQName(), pathIdQName(), routeKeyQName());
81     }
82
83     @Override
84     public final PathArgument createRouteKeyPathArgument(final PathArgument routeKey) {
85         final ImmutableMap<QName, Object> keyValues = ImmutableMap.of(routeKeyQName(), PathIdUtil.getObjectKey(routeKey, routeKeyQName()));
86         return new NodeIdentifierWithPredicates(routeQName(), keyValues);
87     }
88
89 }