Simplify DOMNotificationService contract
[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.base.MoreObjects;
13 import java.util.Objects;
14 import org.eclipse.jdt.annotation.NonNullByDefault;
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;
18
19 /**
20  * Identifier of a RPC context. This is an extension of the YANG RPC, which always has global context. It allows an RPC
21  * to have a instance identifier attached, so that there can be multiple implementations bound to different contexts
22  * concurrently.
23  */
24 @NonNullByDefault
25 public abstract class DOMRpcIdentifier {
26     private static final class Global extends DOMRpcIdentifier {
27         private Global(final SchemaPath type) {
28             super(type);
29         }
30
31         @Override
32         public YangInstanceIdentifier getContextReference() {
33             return YangInstanceIdentifier.empty();
34         }
35     }
36
37     private static final class Local extends DOMRpcIdentifier {
38         private final YangInstanceIdentifier contextReference;
39
40         private Local(final SchemaPath type, final YangInstanceIdentifier contextReference) {
41             super(type);
42             this.contextReference = requireNonNull(contextReference);
43         }
44
45         @Override
46         public YangInstanceIdentifier getContextReference() {
47             return contextReference;
48         }
49     }
50
51     private final SchemaPath type;
52
53     private DOMRpcIdentifier(final SchemaPath type) {
54         this.type = requireNonNull(type);
55     }
56
57     /**
58      * Create a global RPC identifier.
59      *
60      * @param type RPC type, SchemaPath of its definition, may not be null
61      * @return A global RPC identifier, guaranteed to be non-null.
62      */
63     public static DOMRpcIdentifier create(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     public static DOMRpcIdentifier create(final SchemaPath type,
75             final @Nullable 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     public final SchemaPath getType() {
89         return type;
90     }
91
92     /**
93      * Return the RPC context reference. Null value indicates global context.
94      *
95      * @return RPC context reference.
96      */
97     public abstract YangInstanceIdentifier getContextReference();
98
99     @Override
100     public final int hashCode() {
101         final int prime = 31;
102         int result = 1;
103         result = prime * result + type.hashCode();
104         result = prime * result + (getContextReference() == null ? 0 : getContextReference().hashCode());
105         return result;
106     }
107
108     @Override
109     public final boolean equals(final @Nullable Object obj) {
110         if (this == obj) {
111             return true;
112         }
113         if (!(obj instanceof DOMRpcIdentifier)) {
114             return false;
115         }
116         DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
117         if (!type.equals(other.type)) {
118             return false;
119         }
120         return Objects.equals(getContextReference(), other.getContextReference());
121     }
122
123     @Override
124     public final String toString() {
125         return MoreObjects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference",
126                 getContextReference()).toString();
127     }
128 }