Refactor DOM{Action,Rpc}Implementation
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.MoreObjects;
14 import java.util.Objects;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19
20 /**
21  * Identifier of a RPC context. This is an extension of the YANG RPC, which always has global context. It allows an RPC
22  * to have a instance identifier attached, so that there can be multiple implementations bound to different contexts
23  * concurrently.
24  */
25 @NonNullByDefault
26 public abstract class DOMRpcIdentifier {
27     @VisibleForTesting
28     static final class Global extends DOMRpcIdentifier {
29         private Global(final QName type) {
30             super(type);
31         }
32
33         @Override
34         public YangInstanceIdentifier getContextReference() {
35             return YangInstanceIdentifier.of();
36         }
37     }
38
39     @VisibleForTesting
40     static final class Local extends DOMRpcIdentifier {
41         private final YangInstanceIdentifier contextReference;
42
43         private Local(final QName type, final YangInstanceIdentifier contextReference) {
44             super(type);
45             this.contextReference = requireNonNull(contextReference);
46         }
47
48         @Override
49         public YangInstanceIdentifier getContextReference() {
50             return contextReference;
51         }
52     }
53
54     private final QName type;
55
56     private DOMRpcIdentifier(final QName type) {
57         this.type = requireNonNull(type);
58     }
59
60     /**
61      * Create a global RPC identifier.
62      *
63      * @param type RPC type, schema node identifier of its definition, may not be null
64      * @return A global RPC identifier, guaranteed to be non-null.
65      */
66     public static DOMRpcIdentifier create(final QName type) {
67         return new Global(type);
68     }
69
70     /**
71      * Create an RPC identifier with a particular context reference.
72      *
73      * @param type RPC type, schema node identifier of its definition, may not be null
74      * @param contextReference Context reference, null means a global RPC identifier.
75      * @return A global RPC identifier, guaranteed to be non-null.
76      */
77     public static DOMRpcIdentifier create(final QName type, final @Nullable YangInstanceIdentifier contextReference) {
78         if (contextReference == null || contextReference.isEmpty()) {
79             return new Global(type);
80         }
81
82         return new Local(type, contextReference);
83     }
84
85     /**
86      * Return the RPC type.
87      *
88      * @return RPC type.
89      */
90     public final QName 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     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 + Objects.hashCode(getContextReference());
107         return result;
108     }
109
110     @Override
111     public final boolean equals(final @Nullable Object obj) {
112         return this == obj || obj instanceof DOMRpcIdentifier other && type.equals(other.type)
113             && Objects.equals(getContextReference(), other.getContextReference());
114     }
115
116     @Override
117     public final String toString() {
118         return MoreObjects.toStringHelper(this).omitNullValues()
119             .add("type", type)
120             .add("contextReference", getContextReference())
121             .toString();
122     }
123 }