Merge "Bug 2538: Remove redundant Augmentation checks and tests"
[controller.git] / opendaylight / md-sal / sal-dom-api / src / main / java / org / opendaylight / controller / md / sal / 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.controller.md.sal.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(final @Nonnull SchemaPath type) {
27             super(type);
28         }
29
30         @Override
31         public YangInstanceIdentifier getContextReference() {
32             return null;
33         }
34     }
35
36     private static final class Local extends DOMRpcIdentifier {
37         private final YangInstanceIdentifier contextReference;
38
39         private Local(final @Nonnull SchemaPath type, final @Nonnull 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     public static @Nonnull DOMRpcIdentifier create(final @Nonnull 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     public static @Nonnull DOMRpcIdentifier create(final @Nonnull SchemaPath type, final @Nullable YangInstanceIdentifier contextReference) {
74         if (contextReference == null) {
75             return new Global(type);
76         } else {
77             return new Local(type, contextReference);
78         }
79     }
80
81     /**
82      * Return the RPC type.
83      *
84      * @return RPC type.
85      */
86     public final @Nonnull SchemaPath getType() {
87         return type;
88     }
89
90     /**
91      * Return the RPC context reference. Null value indicates global context.
92      *
93      * @return RPC context reference.
94      */
95     public abstract @Nullable YangInstanceIdentifier getContextReference();
96
97     @Override
98     public final int hashCode() {
99         final int prime = 31;
100         int result = 1;
101         result = prime * result + type.hashCode();
102         result = prime * result + (getContextReference() == null ? 0 : getContextReference().hashCode());
103         return result;
104     }
105
106     @Override
107     public final boolean equals(final Object obj) {
108         if (this == obj) {
109             return true;
110         }
111         if (!(obj instanceof DOMRpcIdentifier)) {
112             return false;
113         }
114         DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
115         if (!type.equals(other.type)) {
116             return false;
117         }
118         return Objects.equals(getContextReference(), other.getContextReference());
119     }
120
121     @Override
122     public final String toString() {
123         return MoreObjects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference", getContextReference()).toString();
124     }
125 }