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.mdsal.dom.api;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.MoreObjects;
14 import java.util.Objects;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 * Identifier of a RPC context. This is an extension of the YANG RPC, which always has global context. It allows an RPC
22 * to have a instance identifier attached, so that there can be multiple implementations bound to different contexts
26 public abstract class DOMRpcIdentifier {
28 static final class Global extends DOMRpcIdentifier {
29 private Global(final QName type) {
34 public YangInstanceIdentifier getContextReference() {
35 return YangInstanceIdentifier.of();
40 static final class Local extends DOMRpcIdentifier {
41 private final YangInstanceIdentifier contextReference;
43 private Local(final QName type, final YangInstanceIdentifier contextReference) {
45 this.contextReference = requireNonNull(contextReference);
49 public YangInstanceIdentifier getContextReference() {
50 return contextReference;
54 private final QName type;
56 private DOMRpcIdentifier(final QName type) {
57 this.type = requireNonNull(type);
61 * Create a global RPC identifier.
63 * @param type RPC type, schema node identifier of its definition, may not be null
64 * @return A global RPC identifier, guaranteed to be non-null.
66 public static DOMRpcIdentifier create(final QName type) {
67 return new Global(type);
71 * Create an RPC identifier with a particular context reference.
73 * @param type RPC type, schema node identifier of its definition, may not be null
74 * @param contextReference Context reference, null means a global RPC identifier.
75 * @return A global RPC identifier, guaranteed to be non-null.
77 public static DOMRpcIdentifier create(final QName type, final @Nullable YangInstanceIdentifier contextReference) {
78 if (contextReference == null || contextReference.isEmpty()) {
79 return new Global(type);
82 return new Local(type, contextReference);
86 * Return the RPC type.
90 public final QName getType() {
95 * Return the RPC context reference. Null value indicates global context.
97 * @return RPC context reference.
99 public abstract YangInstanceIdentifier getContextReference();
102 public final int hashCode() {
103 final int prime = 31;
105 result = prime * result + type.hashCode();
106 result = prime * result + Objects.hashCode(getContextReference());
111 public final boolean equals(final @Nullable Object obj) {
112 return this == obj || obj instanceof DOMRpcIdentifier other && type.equals(other.type)
113 && Objects.equals(getContextReference(), other.getContextReference());
117 public final String toString() {
118 return MoreObjects.toStringHelper(this).omitNullValues()
120 .add("contextReference", getContextReference())