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.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import java.util.Objects;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 * Identifier of a RPC context. This is an extension of the YANG RPC, which
20 * always has global context. It allows an RPC to have a instance identifier
21 * attached, so that there can be multiple implementations bound to different
22 * contexts concurrently.
24 public abstract class DOMRpcIdentifier {
25 private static final class Global extends DOMRpcIdentifier {
26 private Global(final @Nonnull SchemaPath type) {
31 public YangInstanceIdentifier getContextReference() {
36 private static final class Local extends DOMRpcIdentifier {
37 private final YangInstanceIdentifier contextReference;
39 private Local(final @Nonnull SchemaPath type, final @Nonnull YangInstanceIdentifier contextReference) {
41 this.contextReference = Preconditions.checkNotNull(contextReference);
45 public YangInstanceIdentifier getContextReference() {
46 return contextReference;
50 private final SchemaPath type;
52 private DOMRpcIdentifier(final SchemaPath type) {
53 this.type = Preconditions.checkNotNull(type);
57 * Create a global RPC identifier.
59 * @param type RPC type, SchemaPath of its definition, may not be null
60 * @return A global RPC identifier, guaranteed to be non-null.
62 public static @Nonnull DOMRpcIdentifier create(final @Nonnull SchemaPath type) {
63 return new Global(type);
67 * Create an RPC identifier with a particular context reference.
69 * @param type RPC type, SchemaPath of its definition, may not be null
70 * @param contextReference Context reference, null means a global RPC identifier.
71 * @return A global RPC identifier, guaranteed to be non-null.
73 public static @Nonnull DOMRpcIdentifier create(final @Nonnull SchemaPath type, final @Nullable YangInstanceIdentifier contextReference) {
74 if (contextReference == null) {
75 return new Global(type);
77 return new Local(type, contextReference);
82 * Return the RPC type.
86 public final @Nonnull SchemaPath getType() {
91 * Return the RPC context reference. Null value indicates global context.
93 * @return RPC context reference.
95 public abstract @Nullable YangInstanceIdentifier getContextReference();
98 public final int hashCode() {
101 result = prime * result + type.hashCode();
102 result = prime * result + (getContextReference() == null ? 0 : getContextReference().hashCode());
107 public final boolean equals(final Object obj) {
111 if (!(obj instanceof DOMRpcIdentifier)) {
114 DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
115 if (!type.equals(other.type)) {
118 return Objects.equals(getContextReference(), other.getContextReference());
122 public final String toString() {
123 return MoreObjects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference", getContextReference()).toString();