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