Fix checkstyle violations in sal-dom-api
[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     /**
83      * Return the RPC type.
84      *
85      * @return RPC type.
86      */
87     public final @Nonnull SchemaPath getType() {
88         return type;
89     }
90
91     /**
92      * Return the RPC context reference. Null value indicates global context.
93      *
94      * @return RPC context reference.
95      */
96     public abstract @Nonnull YangInstanceIdentifier getContextReference();
97
98     @Override
99     public final int hashCode() {
100         final int prime = 31;
101         int result = 1;
102         result = prime * result + type.hashCode();
103         result = prime * result + (getContextReference() == null ? 0 : getContextReference().hashCode());
104         return result;
105     }
106
107     @Override
108     public final boolean equals(final Object obj) {
109         if (this == obj) {
110             return true;
111         }
112         if (!(obj instanceof DOMRpcIdentifier)) {
113             return false;
114         }
115         DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
116         if (!type.equals(other.type)) {
117             return false;
118         }
119         return Objects.equals(getContextReference(), other.getContextReference());
120     }
121
122     @Override
123     public final String toString() {
124         return MoreObjects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference",
125                 getContextReference()).toString();
126     }
127 }