Migrate NodeIdentifierWithPredicates users
[bgpcep.git] / bgp / rib-spi / src / main / java / org / opendaylight / protocol / bgp / rib / spi / IdentifierUtils.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.rib.spi;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Predicate;
12 import com.google.common.collect.Iterables;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.bgp.rib.rib.Peer;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
16 import org.opendaylight.yangtools.util.SharedSingletonMapTemplate;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21
22 public final class IdentifierUtils {
23     private static final Predicate<PathArgument> IS_PEER = input -> input
24             instanceof NodeIdentifierWithPredicates && Peer.QNAME.equals(input.getNodeType());
25     private static final Predicate<PathArgument> IS_TABLES = input -> input
26             instanceof NodeIdentifierWithPredicates && Tables.QNAME.equals(input.getNodeType());
27     private static final QName PEER_ID = QName.create(Peer.QNAME, "peer-id").intern();
28     private static final SharedSingletonMapTemplate<QName> PEER_ID_TEMPLATE =
29             SharedSingletonMapTemplate.ordered(PEER_ID);
30
31     private IdentifierUtils() {
32         throw new UnsupportedOperationException();
33     }
34
35     // FIXME: implement as id.firstIdentifierOf(IS_PEER), null indicating not found
36     private static NodeIdentifierWithPredicates firstKeyOf(final YangInstanceIdentifier id,
37             final Predicate<PathArgument> match) {
38         final PathArgument ret = id.getPathArguments().stream().filter(match::apply).findFirst().get();
39         Preconditions.checkArgument(ret instanceof NodeIdentifierWithPredicates,
40                 "Non-key peer identifier %s", ret);
41         return (NodeIdentifierWithPredicates) ret;
42     }
43
44     private static YangInstanceIdentifier firstIdentifierOf(final YangInstanceIdentifier id,
45             final Predicate<PathArgument> match) {
46         final int idx = Iterables.indexOf(id.getPathArguments(), match);
47         Preconditions.checkArgument(idx != -1, "Failed to find %s in %s", match, id);
48         // we want the element at index idx to be included in the list
49         return YangInstanceIdentifier.create(Iterables.limit(id.getPathArguments(), idx + 1));
50     }
51
52     public static YangInstanceIdentifier peerPath(final YangInstanceIdentifier id) {
53         return firstIdentifierOf(id, IS_PEER);
54     }
55
56     public static NodeIdentifierWithPredicates peerKey(final YangInstanceIdentifier id) {
57         return firstKeyOf(id, IS_PEER);
58     }
59
60     public static PeerId peerId(final NodeIdentifierWithPredicates peerKey) {
61         // We could use a codec, but this is simple enough
62         return new PeerId((String) peerKey.getValue(PEER_ID));
63     }
64
65     public static PeerId peerKeyToPeerId(final YangInstanceIdentifier id) {
66         return peerId(peerKey(id));
67     }
68
69
70     static NodeIdentifierWithPredicates tableKey(final YangInstanceIdentifier id) {
71         return firstKeyOf(id, IS_TABLES);
72     }
73
74     public static NodeIdentifierWithPredicates domPeerId(final PeerId peer) {
75         return NodeIdentifierWithPredicates.of(Peer.QNAME, PEER_ID_TEMPLATE.instantiateWithValue(peer.getValue()));
76     }
77 }