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