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 static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import java.util.Objects;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 * Identifier of a RPC context. This is an extension of the YANG RPC, which
21 * always has global context. It allows an RPC to have a instance identifier
22 * attached, so that there can be multiple implementations bound to different
23 * contexts concurrently.
25 public abstract class DOMRpcIdentifier {
27 private static final class Global extends DOMRpcIdentifier {
28 private Global(final @NonNull SchemaPath type) {
33 public YangInstanceIdentifier getContextReference() {
34 return YangInstanceIdentifier.EMPTY;
38 private static final class Local extends DOMRpcIdentifier {
39 private final YangInstanceIdentifier contextReference;
41 private Local(final @NonNull SchemaPath type, final @NonNull YangInstanceIdentifier contextReference) {
43 this.contextReference = requireNonNull(contextReference);
47 public YangInstanceIdentifier getContextReference() {
48 return contextReference;
52 private final SchemaPath type;
54 private DOMRpcIdentifier(final SchemaPath type) {
55 this.type = requireNonNull(type);
59 * Create a global RPC identifier.
61 * @param type RPC type, SchemaPath of its definition, may not be null
62 * @return A global RPC identifier, guaranteed to be non-null.
64 public static @NonNull DOMRpcIdentifier create(final @NonNull SchemaPath type) {
65 return new Global(type);
69 * Create an RPC identifier with a particular context reference.
71 * @param type RPC type, SchemaPath of its definition, may not be null
72 * @param contextReference Context reference, null means a global RPC identifier.
73 * @return A global RPC identifier, guaranteed to be non-null.
75 public static @NonNull DOMRpcIdentifier create(final @NonNull SchemaPath type,
76 final @Nullable YangInstanceIdentifier contextReference) {
77 if (contextReference == null || contextReference.isEmpty()) {
78 return new Global(type);
80 return new Local(type, contextReference);
83 public static DOMRpcIdentifier fromMdsal(final org.opendaylight.mdsal.dom.api.DOMRpcIdentifier mdsal) {
84 return create(mdsal.getType(), mdsal.getContextReference());
87 public org.opendaylight.mdsal.dom.api.DOMRpcIdentifier toMdsal() {
88 return org.opendaylight.mdsal.dom.api.DOMRpcIdentifier.create(type, getContextReference());
92 * Return the RPC type.
96 public final @NonNull SchemaPath getType() {
101 * Return the RPC context reference. Null value indicates global context.
103 * @return RPC context reference.
105 public abstract @NonNull YangInstanceIdentifier getContextReference();
108 public final int hashCode() {
109 final int prime = 31;
111 result = prime * result + type.hashCode();
112 result = prime * result + getContextReference().hashCode();
117 public final boolean equals(final Object obj) {
121 if (!(obj instanceof DOMRpcIdentifier)) {
124 DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
125 if (!type.equals(other.type)) {
128 return Objects.equals(getContextReference(), other.getContextReference());
132 public final String toString() {
133 return MoreObjects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference",
134 getContextReference()).toString();