Merge "Fixed testool performance bottleneck due to testtool using NetconfMonitoring...
[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.Collections;
13 import java.util.Objects;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19
20 /**
21  * Identifier of a RPC context. This is an extension of the YANG RPC, which
22  * always has global context. It allows an RPC to have a instance identifier
23  * attached, so that there can be multiple implementations bound to different
24  * contexts concurrently.
25  */
26 public abstract class DOMRpcIdentifier {
27
28     private static final YangInstanceIdentifier GLOBAL_CONTEXT = YangInstanceIdentifier.create(Collections.<PathArgument>emptySet());
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 GLOBAL_CONTEXT;
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 = Preconditions.checkNotNull(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 = Preconditions.checkNotNull(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, final @Nullable YangInstanceIdentifier contextReference) {
79         if (contextReference == null || GLOBAL_CONTEXT.equals(contextReference)) {
80             return new Global(type);
81         } else {
82             return new Local(type, contextReference);
83         }
84     }
85
86     /**
87      * Return the RPC type.
88      *
89      * @return RPC type.
90      */
91     public final @Nonnull SchemaPath getType() {
92         return type;
93     }
94
95     /**
96      * Return the RPC context reference. Null value indicates global context.
97      *
98      * @return RPC context reference.
99      */
100     public abstract @Nonnull YangInstanceIdentifier getContextReference();
101
102     @Override
103     public final int hashCode() {
104         final int prime = 31;
105         int result = 1;
106         result = prime * result + type.hashCode();
107         result = prime * result + (getContextReference() == null ? 0 : getContextReference().hashCode());
108         return result;
109     }
110
111     @Override
112     public final boolean equals(final Object obj) {
113         if (this == obj) {
114             return true;
115         }
116         if (!(obj instanceof DOMRpcIdentifier)) {
117             return false;
118         }
119         DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
120         if (!type.equals(other.type)) {
121             return false;
122         }
123         return Objects.equals(getContextReference(), other.getContextReference());
124     }
125
126     @Override
127     public final String toString() {
128         return MoreObjects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference", getContextReference()).toString();
129     }
130 }