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.Collections;
13 import java.util.Objects;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 * Identifier of a RPC context. This is an extension of the YANG RPC, which
22 * always has global context. It allows an RPC to have a instance identifier
23 * attached, so that there can be multiple implementations bound to different
24 * contexts concurrently.
26 public abstract class DOMRpcIdentifier {
28 private static final YangInstanceIdentifier GLOBAL_CONTEXT = YangInstanceIdentifier.create(Collections.<PathArgument>emptySet());
30 private static final class Global extends DOMRpcIdentifier {
31 private Global(final @Nonnull SchemaPath type) {
36 public YangInstanceIdentifier getContextReference() {
37 return GLOBAL_CONTEXT;
41 private static final class Local extends DOMRpcIdentifier {
42 private final YangInstanceIdentifier contextReference;
44 private Local(final @Nonnull SchemaPath type, final @Nonnull YangInstanceIdentifier contextReference) {
46 this.contextReference = Preconditions.checkNotNull(contextReference);
50 public YangInstanceIdentifier getContextReference() {
51 return contextReference;
55 private final SchemaPath type;
57 private DOMRpcIdentifier(final SchemaPath type) {
58 this.type = Preconditions.checkNotNull(type);
62 * Create a global RPC identifier.
64 * @param type RPC type, SchemaPath of its definition, may not be null
65 * @return A global RPC identifier, guaranteed to be non-null.
67 public static @Nonnull DOMRpcIdentifier create(final @Nonnull SchemaPath type) {
68 return new Global(type);
72 * Create an RPC identifier with a particular context reference.
74 * @param type RPC type, SchemaPath of its definition, may not be null
75 * @param contextReference Context reference, null means a global RPC identifier.
76 * @return A global RPC identifier, guaranteed to be non-null.
78 public static @Nonnull DOMRpcIdentifier create(final @Nonnull SchemaPath type, final @Nullable YangInstanceIdentifier contextReference) {
79 if (contextReference == null || GLOBAL_CONTEXT.equals(contextReference)) {
80 return new Global(type);
82 return new Local(type, contextReference);
87 * Return the RPC type.
91 public final @Nonnull SchemaPath getType() {
96 * Return the RPC context reference. Null value indicates global context.
98 * @return RPC context reference.
100 public abstract @Nonnull 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", getContextReference()).toString();