63e646debd7a884699c73ff4fb881d851ec8892e
[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
26     private static final class Global extends DOMRpcIdentifier {
27         private Global(final @Nonnull 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 @Nonnull SchemaPath type, final @Nonnull YangInstanceIdentifier contextReference) {
41             super(type);
42             this.contextReference = Preconditions.checkNotNull(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 = Preconditions.checkNotNull(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 @Nonnull DOMRpcIdentifier create(final @Nonnull 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 @Nonnull DOMRpcIdentifier create(final @Nonnull SchemaPath type,
75             final @Nullable YangInstanceIdentifier contextReference) {
76         if (contextReference == null || contextReference.isEmpty()) {
77             return new Global(type);
78         }
79         return new Local(type, contextReference);
80     }
81
82     public static DOMRpcIdentifier fromMdsal(final org.opendaylight.mdsal.dom.api.DOMRpcIdentifier mdsal) {
83         return create(mdsal.getType(), mdsal.getContextReference());
84     }
85
86     public org.opendaylight.mdsal.dom.api.DOMRpcIdentifier toMdsal() {
87         return org.opendaylight.mdsal.dom.api.DOMRpcIdentifier.create(type, getContextReference());
88     }
89
90     /**
91      * Return the RPC type.
92      *
93      * @return RPC type.
94      */
95     public final @Nonnull SchemaPath getType() {
96         return type;
97     }
98
99     /**
100      * Return the RPC context reference. Null value indicates global context.
101      *
102      * @return RPC context reference.
103      */
104     public abstract @Nonnull YangInstanceIdentifier getContextReference();
105
106     @Override
107     public final int hashCode() {
108         final int prime = 31;
109         int result = 1;
110         result = prime * result + type.hashCode();
111         result = prime * result + getContextReference().hashCode();
112         return result;
113     }
114
115     @Override
116     public final boolean equals(final Object obj) {
117         if (this == obj) {
118             return true;
119         }
120         if (!(obj instanceof DOMRpcIdentifier)) {
121             return false;
122         }
123         DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
124         if (!type.equals(other.type)) {
125             return false;
126         }
127         return Objects.equals(getContextReference(), other.getContextReference());
128     }
129
130     @Override
131     public final String toString() {
132         return MoreObjects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference",
133                 getContextReference()).toString();
134     }
135 }