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.controller.md.sal.dom.api;
10 import com.google.common.base.Preconditions;
11 import java.util.Objects;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 * Identifier of a RPC context. This is an extension of the YANG RPC, which
19 * always has global context. It allows an RPC to have a instance identifier
20 * attached, so that there can be multiple implementations bound to different
21 * contexts concurrently.
23 public abstract class DOMRpcIdentifier {
24 private static final class Global extends DOMRpcIdentifier {
25 private Global(final @Nonnull SchemaPath type) {
30 public YangInstanceIdentifier getContextReference() {
35 private static final class Local extends DOMRpcIdentifier {
36 private final YangInstanceIdentifier contextReference;
38 private Local(final @Nonnull SchemaPath type, final @Nonnull YangInstanceIdentifier contextReference) {
40 this.contextReference = Preconditions.checkNotNull(contextReference);
44 public YangInstanceIdentifier getContextReference() {
45 return contextReference;
49 private final SchemaPath type;
51 private DOMRpcIdentifier(final SchemaPath type) {
52 this.type = Preconditions.checkNotNull(type);
56 * Create a global RPC identifier.
58 * @param type RPC type, SchemaPath of its definition, may not be null
59 * @return A global RPC identifier, guaranteed to be non-null.
61 public static @Nonnull DOMRpcIdentifier create(final @Nonnull SchemaPath type) {
62 return new Global(type);
66 * Create an RPC identifier with a particular context reference.
68 * @param type RPC type, SchemaPath of its definition, may not be null
69 * @param contextReference Context reference, null means a global RPC identifier.
70 * @return A global RPC identifier, guaranteed to be non-null.
72 public static @Nonnull DOMRpcIdentifier create(final @Nonnull SchemaPath type, final @Nullable YangInstanceIdentifier contextReference) {
73 if (contextReference == null) {
74 return new Global(type);
76 return new Local(type, contextReference);
81 * Return the RPC type.
85 public final @Nonnull SchemaPath getType() {
90 * Return the RPC context reference. Null value indicates global context.
92 * @return RPC context reference.
94 public abstract @Nullable YangInstanceIdentifier getContextReference();
97 public final int hashCode() {
100 result = prime * result + type.hashCode();
101 result = prime * result + (getContextReference() == null ? 0 : getContextReference().hashCode());
106 public final boolean equals(final Object obj) {
110 if (!(obj instanceof DOMRpcIdentifier)) {
113 DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
114 if (!type.equals(other.type)) {
117 return Objects.equals(getContextReference(), other.getContextReference());
121 public final String toString() {
122 return com.google.common.base.Objects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference", getContextReference()).toString();