2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
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;
21 final class IdentifierUtils {
22 private static final Predicate<PathArgument> IS_PEER = new Predicate<PathArgument>() {
24 public boolean apply(final PathArgument input) {
25 return input instanceof NodeIdentifierWithPredicates && Peer.QNAME.equals(input.getNodeType());
28 private static final Predicate<PathArgument> IS_TABLES = new Predicate<PathArgument>() {
30 public boolean apply(final PathArgument input) {
31 return Tables.QNAME.equals(input.getNodeType());
34 private static final QName PEER_ID = QName.cachedReference(QName.create(Peer.QNAME, "peer-id"));
36 private IdentifierUtils() {
37 throw new UnsupportedOperationException();
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;
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));
54 static YangInstanceIdentifier peerPath(final YangInstanceIdentifier id) {
55 return firstIdentifierOf(id, IS_PEER);
58 static NodeIdentifierWithPredicates peerKey(final YangInstanceIdentifier id) {
59 return firstKeyOf(id, IS_PEER);
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));
67 static NodeIdentifierWithPredicates tableKey(final YangInstanceIdentifier id) {
68 return firstKeyOf(id, IS_TABLES);
71 static NodeIdentifierWithPredicates domPeerId(final PeerId peer) {
72 return new NodeIdentifierWithPredicates(Peer.QNAME, PEER_ID, peer.getValue());