BUG-2633 - Netconf northbound mapping.
[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.Preconditions;
11 import java.util.Objects;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16
17 /**
18  * Identifier of a RPC context. This is an extension of the YANG RPC, which
19  * always has global context. It allows an RPC to have a instance identifier
20  * attached, so that there can be multiple implementations bound to different
21  * contexts concurrently.
22  */
23 public abstract class DOMRpcIdentifier {
24     private static final class Global extends DOMRpcIdentifier {
25         private Global(final @Nonnull SchemaPath type) {
26             super(type);
27         }
28
29         @Override
30         public YangInstanceIdentifier getContextReference() {
31             return null;
32         }
33     }
34
35     private static final class Local extends DOMRpcIdentifier {
36         private final YangInstanceIdentifier contextReference;
37
38         private Local(final @Nonnull SchemaPath type, final @Nonnull YangInstanceIdentifier contextReference) {
39             super(type);
40             this.contextReference = Preconditions.checkNotNull(contextReference);
41         }
42
43         @Override
44         public YangInstanceIdentifier getContextReference() {
45             return contextReference;
46         }
47     }
48
49     private final SchemaPath type;
50
51     private DOMRpcIdentifier(final SchemaPath type) {
52         this.type = Preconditions.checkNotNull(type);
53     }
54
55     /**
56      * Create a global RPC identifier.
57      *
58      * @param type RPC type, SchemaPath of its definition, may not be null
59      * @return A global RPC identifier, guaranteed to be non-null.
60      */
61     public static @Nonnull DOMRpcIdentifier create(final @Nonnull SchemaPath type) {
62         return new Global(type);
63     }
64
65     /**
66      * Create an RPC identifier with a particular context reference.
67      *
68      * @param type RPC type, SchemaPath of its definition, may not be null
69      * @param contextReference Context reference, null means a global RPC identifier.
70      * @return A global RPC identifier, guaranteed to be non-null.
71      */
72     public static @Nonnull DOMRpcIdentifier create(final @Nonnull SchemaPath type, final @Nullable YangInstanceIdentifier contextReference) {
73         if (contextReference == null) {
74             return new Global(type);
75         } else {
76             return new Local(type, contextReference);
77         }
78     }
79
80     /**
81      * Return the RPC type.
82      *
83      * @return RPC type.
84      */
85     public final @Nonnull SchemaPath getType() {
86         return type;
87     }
88
89     /**
90      * Return the RPC context reference. Null value indicates global context.
91      *
92      * @return RPC context reference.
93      */
94     public abstract @Nullable YangInstanceIdentifier getContextReference();
95
96     @Override
97     public final int hashCode() {
98         final int prime = 31;
99         int result = 1;
100         result = prime * result + type.hashCode();
101         result = prime * result + (getContextReference() == null ? 0 : getContextReference().hashCode());
102         return result;
103     }
104
105     @Override
106     public final boolean equals(final Object obj) {
107         if (this == obj) {
108             return true;
109         }
110         if (!(obj instanceof DOMRpcIdentifier)) {
111             return false;
112         }
113         DOMRpcIdentifier other = (DOMRpcIdentifier) obj;
114         if (!type.equals(other.type)) {
115             return false;
116         }
117         return Objects.equals(getContextReference(), other.getContextReference());
118     }
119
120     @Override
121     public final String toString() {
122         return com.google.common.base.Objects.toStringHelper(this).omitNullValues().add("type", type).add("contextReference", getContextReference()).toString();
123     }
124 }