8f52edd651d79e9ae1a12df080a478098209816a
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import java.util.Objects;
14 import org.eclipse.jdt.annotation.NonNull;
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
21  * always has global context. It allows an RPC to have a instance identifier
22  * attached, so that there can be multiple implementations bound to different
23  * contexts concurrently.
24  *
25  * @deprecated Use {@link org.opendaylight.mdsal.dom.api.DOMRpcIdentifier} instead.
26  */
27 @Deprecated
28 public abstract class DOMRpcIdentifier {
29
30     private static final class Global extends DOMRpcIdentifier {
31         private Global(final @NonNull SchemaPath type) {
32             super(type);
33         }
34
35         @Override
36         public YangInstanceIdentifier getContextReference() {
37             return YangInstanceIdentifier.empty();
38         }
39     }
40
41     private static final class Local extends DOMRpcIdentifier {
42         private final YangInstanceIdentifier contextReference;
43
44         private Local(final @NonNull SchemaPath type, final @NonNull YangInstanceIdentifier contextReference) {
45             super(type);
46             this.contextReference = requireNonNull(contextReference);
47         }
48
49         @Override
50         public YangInstanceIdentifier getContextReference() {
51             return contextReference;
52         }
53     }
54
55     private final SchemaPath type;
56
57     private DOMRpcIdentifier(final SchemaPath type) {
58         this.type = requireNonNull(type);
59     }
60
61     /**
62      * Create a global RPC identifier.
63      *
64      * @param type RPC type, SchemaPath of its definition, may not be null
65      * @return A global RPC identifier, guaranteed to be non-null.
66      */
67     public static @NonNull DOMRpcIdentifier create(final @NonNull SchemaPath type) {
68         return new Global(type);
69     }
70
71     /**
72      * Create an RPC identifier with a particular context reference.
73      *
74      * @param type RPC type, SchemaPath of its definition, may not be null
75      * @param contextReference Context reference, null means a global RPC identifier.
76      * @return A global RPC identifier, guaranteed to be non-null.
77      */
78     public static @NonNull DOMRpcIdentifier create(final @NonNull SchemaPath type,
79             final @Nullable YangInstanceIdentifier contextReference) {
80         if (contextReference == null || contextReference.isEmpty()) {
81             return new Global(type);
82         }
83         return new Local(type, contextReference);
84     }
85
86     public static DOMRpcIdentifier fromMdsal(final org.opendaylight.mdsal.dom.api.DOMRpcIdentifier mdsal) {
87         return create(mdsal.getType(), mdsal.getContextReference());
88     }
89
90     public org.opendaylight.mdsal.dom.api.DOMRpcIdentifier toMdsal() {
91         return org.opendaylight.mdsal.dom.api.DOMRpcIdentifier.create(type, getContextReference());
92     }
93
94     /**
95      * Return the RPC type.
96      *
97      * @return RPC type.
98      */
99     public final @NonNull SchemaPath getType() {
100         return type;
101     }
102
103     /**
104      * Return the RPC context reference. Null value indicates global context.
105      *
106      * @return RPC context reference.
107      */
108     public abstract @NonNull YangInstanceIdentifier getContextReference();
109
110     @Override
111     public final int hashCode() {
112         final int prime = 31;
113         int result = 1;
114         result = prime * result + type.hashCode();
115         result = prime * result + getContextReference().hashCode();
116         return result;
117     }
118
119     @Override
120     public final boolean equals(final Object obj) {
121         if (this == obj) {
122             return true;
123         }
124         if (!(obj instanceof DOMRpcIdentifier)) {
125             return false;
126         }
127         DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
128         if (!type.equals(other.type)) {
129             return false;
130         }
131         return Objects.equals(getContextReference(), other.getContextReference());
132     }
133
134     @Override
135     public final String toString() {
136         return MoreObjects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference",
137                 getContextReference()).toString();
138     }
139 }