Remove deprecated methods from DOMDataTreeCommitCohort
[mdsal.git] / dom / mdsal-dom-api / src / main / java / org / opendaylight / mdsal / dom / api / DOMRpcIdentifier.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.api;
9
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;
17
18 /**
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
21  * concurrently.
22  */
23 public abstract class DOMRpcIdentifier {
24     private static final class Global extends DOMRpcIdentifier {
25         private Global(@Nonnull final SchemaPath type) {
26             super(type);
27         }
28
29         @Override
30         public YangInstanceIdentifier getContextReference() {
31             return YangInstanceIdentifier.EMPTY;
32         }
33     }
34
35     private static final class Local extends DOMRpcIdentifier {
36         private final YangInstanceIdentifier contextReference;
37
38         private Local(@Nonnull final SchemaPath type, @Nonnull final YangInstanceIdentifier contextReference) {
39             super(type);
40             this.contextReference = Preconditions.checkNotNull(contextReference);
41         }
42
43         @Override
44         public YangInstanceIdentifier getContextReference() {
45             return contextReference;
46         }
47     }
48
49     private final SchemaPath type;
50
51     private DOMRpcIdentifier(final SchemaPath type) {
52         this.type = Preconditions.checkNotNull(type);
53     }
54
55     /**
56      * Create a global RPC identifier.
57      *
58      * @param type RPC type, SchemaPath of its definition, may not be null
59      * @return A global RPC identifier, guaranteed to be non-null.
60      */
61     @Nonnull
62     public static DOMRpcIdentifier create(@Nonnull final SchemaPath type) {
63         return new Global(type);
64     }
65
66     /**
67      * Create an RPC identifier with a particular context reference.
68      *
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.
72      */
73     @Nonnull
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);
78         }
79
80         return new Local(type, contextReference);
81     }
82
83     /**
84      * Return the RPC type.
85      *
86      * @return RPC type.
87      */
88     @Nonnull
89     public final SchemaPath getType() {
90         return type;
91     }
92
93     /**
94      * Return the RPC context reference. Null value indicates global context.
95      *
96      * @return RPC context reference.
97      */
98     @Nonnull
99     public abstract YangInstanceIdentifier getContextReference();
100
101     @Override
102     public final int hashCode() {
103         final int prime = 31;
104         int result = 1;
105         result = prime * result + type.hashCode();
106         result = prime * result + (getContextReference() == null ? 0 : getContextReference().hashCode());
107         return result;
108     }
109
110     @Override
111     public final boolean equals(final Object obj) {
112         if (this == obj) {
113             return true;
114         }
115         if (!(obj instanceof DOMRpcIdentifier)) {
116             return false;
117         }
118         DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
119         if (!type.equals(other.type)) {
120             return false;
121         }
122         return Objects.equals(getContextReference(), other.getContextReference());
123     }
124
125     @Override
126     public final String toString() {
127         return MoreObjects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference",
128                 getContextReference()).toString();
129     }
130 }