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
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(@Nonnull final SchemaPath type) {
31 public YangInstanceIdentifier getContextReference() {
32 return YangInstanceIdentifier.EMPTY;
36 private static final class Local extends DOMRpcIdentifier {
37 private final YangInstanceIdentifier contextReference;
39 private Local(@Nonnull final SchemaPath type, @Nonnull final 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.
63 public static DOMRpcIdentifier create(@Nonnull 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.
75 public static DOMRpcIdentifier create(@Nonnull final SchemaPath type,
76 @Nullable final YangInstanceIdentifier contextReference) {
77 if (contextReference == null || contextReference.isEmpty()) {
78 return new Global(type);
80 return new Local(type, contextReference);
85 * Return the RPC type.
90 public final SchemaPath getType() {
95 * Return the RPC context reference. Null value indicates global context.
97 * @return RPC context reference.
100 public abstract YangInstanceIdentifier getContextReference();
103 public final int hashCode() {
104 final int prime = 31;
106 result = prime * result + type.hashCode();
107 result = prime * result + (getContextReference() == null ? 0 : getContextReference().hashCode());
112 public final boolean equals(final Object obj) {
116 if (!(obj instanceof DOMRpcIdentifier)) {
119 DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
120 if (!type.equals(other.type)) {
123 return Objects.equals(getContextReference(), other.getContextReference());
127 public final String toString() {
128 return MoreObjects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference",
129 getContextReference()).toString();