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 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 always has global context. It allows an RPC
20 * to have a instance identifier attached, so that there can be multiple implementations bound to different contexts
23 public abstract class DOMRpcIdentifier {
24 private static final class Global extends DOMRpcIdentifier {
25 private Global(@Nonnull final SchemaPath type) {
30 public YangInstanceIdentifier getContextReference() {
31 return YangInstanceIdentifier.EMPTY;
35 private static final class Local extends DOMRpcIdentifier {
36 private final YangInstanceIdentifier contextReference;
38 private Local(@Nonnull final SchemaPath type, @Nonnull final 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.
62 public static DOMRpcIdentifier create(@Nonnull final 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.
74 public static DOMRpcIdentifier create(@Nonnull final SchemaPath type,
75 @Nullable final YangInstanceIdentifier contextReference) {
76 if (contextReference == null || contextReference.isEmpty()) {
77 return new Global(type);
80 return new Local(type, contextReference);
84 * Return the RPC type.
89 public final SchemaPath getType() {
94 * Return the RPC context reference. Null value indicates global context.
96 * @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 + (getContextReference() == null ? 0 : getContextReference().hashCode());
111 public final boolean equals(final Object obj) {
115 if (!(obj instanceof DOMRpcIdentifier)) {
118 DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
119 if (!type.equals(other.type)) {
122 return Objects.equals(getContextReference(), other.getContextReference());
126 public final String toString() {
127 return MoreObjects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference",
128 getContextReference()).toString();