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.spi;
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.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 public final class IdentifierUtils {
22 private static final Predicate<PathArgument> IS_PEER = input -> input
23 instanceof NodeIdentifierWithPredicates && Peer.QNAME.equals(input.getNodeType());
24 private static final Predicate<PathArgument> IS_TABLES = input -> input
25 instanceof NodeIdentifierWithPredicates && Tables.QNAME.equals(input.getNodeType());
26 private static final QName PEER_ID = QName.create(Peer.QNAME, "peer-id").intern();
28 private IdentifierUtils() {
29 throw new UnsupportedOperationException();
32 // FIXME: implement as id.firstIdentifierOf(IS_PEER), null indicating not found
33 private static NodeIdentifierWithPredicates firstKeyOf(final YangInstanceIdentifier id,
34 final Predicate<PathArgument> match) {
35 final PathArgument ret = id.getPathArguments().stream().filter(match::apply).findFirst().get();
36 Preconditions.checkArgument(ret instanceof NodeIdentifierWithPredicates,
37 "Non-key peer identifier %s", ret);
38 return (NodeIdentifierWithPredicates) ret;
41 private static YangInstanceIdentifier firstIdentifierOf(final YangInstanceIdentifier id,
42 final Predicate<PathArgument> match) {
43 final int idx = Iterables.indexOf(id.getPathArguments(), match);
44 Preconditions.checkArgument(idx != -1, "Failed to find %s in %s", match, id);
45 // we want the element at index idx to be included in the list
46 return YangInstanceIdentifier.create(Iterables.limit(id.getPathArguments(), idx + 1));
49 public static YangInstanceIdentifier peerPath(final YangInstanceIdentifier id) {
50 return firstIdentifierOf(id, IS_PEER);
53 public static NodeIdentifierWithPredicates peerKey(final YangInstanceIdentifier id) {
54 return firstKeyOf(id, IS_PEER);
57 public static PeerId peerId(final NodeIdentifierWithPredicates peerKey) {
58 // We could use a codec, but this is simple enough
59 return new PeerId((String) peerKey.getValue(PEER_ID));
62 public static PeerId peerKeyToPeerId(final YangInstanceIdentifier id) {
63 return peerId(peerKey(id));
67 static NodeIdentifierWithPredicates tableKey(final YangInstanceIdentifier id) {
68 return firstKeyOf(id, IS_TABLES);
71 public static NodeIdentifierWithPredicates domPeerId(final PeerId peer) {
72 return NodeIdentifierWithPredicates.of(Peer.QNAME, PEER_ID, peer.getValue());