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