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.base.MoreObjects;
13 import java.util.Objects;
14 import org.eclipse.jdt.annotation.NonNullByDefault;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 * Identifier of a RPC context. This is an extension of the YANG RPC, which always has global context. It allows an RPC
21 * to have a instance identifier attached, so that there can be multiple implementations bound to different contexts
25 public abstract class DOMRpcIdentifier {
26 private static final class Global extends DOMRpcIdentifier {
27 private Global(final SchemaPath type) {
32 public YangInstanceIdentifier getContextReference() {
33 return YangInstanceIdentifier.EMPTY;
37 private static final class Local extends DOMRpcIdentifier {
38 private final YangInstanceIdentifier contextReference;
40 private Local(final SchemaPath type, final YangInstanceIdentifier contextReference) {
42 this.contextReference = requireNonNull(contextReference);
46 public YangInstanceIdentifier getContextReference() {
47 return contextReference;
51 private final SchemaPath type;
53 private DOMRpcIdentifier(final SchemaPath type) {
54 this.type = requireNonNull(type);
58 * Create a global RPC identifier.
60 * @param type RPC type, SchemaPath of its definition, may not be null
61 * @return A global RPC identifier, guaranteed to be non-null.
63 public static DOMRpcIdentifier create(final SchemaPath type) {
64 return new Global(type);
68 * Create an RPC identifier with a particular context reference.
70 * @param type RPC type, SchemaPath of its definition, may not be null
71 * @param contextReference Context reference, null means a global RPC identifier.
72 * @return A global RPC identifier, guaranteed to be non-null.
74 public static DOMRpcIdentifier create(final SchemaPath type,
75 final @Nullable YangInstanceIdentifier contextReference) {
76 if (contextReference == null || contextReference.isEmpty()) {
77 return new Global(type);
80 return new Local(type, contextReference);
84 * Return the RPC type.
88 public final SchemaPath getType() {
93 * Return the RPC context reference. Null value indicates global context.
95 * @return RPC context reference.
97 public abstract YangInstanceIdentifier getContextReference();
100 public final int hashCode() {
101 final int prime = 31;
103 result = prime * result + type.hashCode();
104 result = prime * result + (getContextReference() == null ? 0 : getContextReference().hashCode());
109 public final boolean equals(final @Nullable Object obj) {
113 if (!(obj instanceof DOMRpcIdentifier)) {
116 DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
117 if (!type.equals(other.type)) {
120 return Objects.equals(getContextReference(), other.getContextReference());
124 public final String toString() {
125 return MoreObjects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference",
126 getContextReference()).toString();