Refactored YANG types resolving.
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-util / src / main / java / org / opendaylight / controller / yang / model / util / Leafref.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.yang.model.util;\r
9 \r
10 import java.net.URI;\r
11 import java.util.Collections;\r
12 import java.util.Date;\r
13 import java.util.List;\r
14 \r
15 import org.opendaylight.controller.yang.common.QName;\r
16 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;\r
17 import org.opendaylight.controller.yang.model.api.SchemaPath;\r
18 import org.opendaylight.controller.yang.model.api.Status;\r
19 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;\r
20 import org.opendaylight.controller.yang.model.api.type.LeafrefTypeDefinition;\r
21 \r
22 /**\r
23  * The <code>default</code> implementation of Instance Leafref Type Definition\r
24  * interface.\r
25  *\r
26  * @see LeafrefTypeDefinition\r
27  */\r
28 public class Leafref implements LeafrefTypeDefinition {\r
29     private static final QName name = BaseTypes.constructQName("leafref");\r
30     private static final String description = "The leafref type is used to reference a "\r
31             + "particular leaf instance in the data tree.";\r
32     private static final String reference = "https://tools.ietf.org/html/rfc6020#section-9.9";\r
33     private final SchemaPath path;\r
34     private final RevisionAwareXPath xpath;\r
35     private final String units = "";\r
36     private final LeafrefTypeDefinition baseType;\r
37 \r
38     public Leafref(final RevisionAwareXPath xpath) {\r
39         this.xpath = xpath;\r
40         this.path = BaseTypes.schemaPath(name);\r
41         this.baseType = this;\r
42     }\r
43 \r
44     public Leafref(final List<String> actualPath, final URI namespace,\r
45             final Date revision, final RevisionAwareXPath xpath) {\r
46         super();\r
47         this.path = BaseTypes.schemaPath(actualPath, namespace, revision);\r
48         this.xpath = xpath;\r
49         baseType = new Leafref(xpath);\r
50     }\r
51 \r
52     public Leafref(final List<String> actualPath, final URI namespace,\r
53             final Date revision, final LeafrefTypeDefinition baseType,\r
54             final RevisionAwareXPath xpath) {\r
55         super();\r
56         this.path = BaseTypes.schemaPath(actualPath, namespace, revision);\r
57         this.xpath = xpath;\r
58         this.baseType = baseType;\r
59     }\r
60 \r
61     /*\r
62      * (non-Javadoc)\r
63      *\r
64      * @see\r
65      * org.opendaylight.controller.yang.model.api.TypeDefinition#getBaseType()\r
66      */\r
67     @Override\r
68     public LeafrefTypeDefinition getBaseType() {\r
69         return baseType;\r
70     }\r
71 \r
72     /*\r
73      * (non-Javadoc)\r
74      *\r
75      * @see org.opendaylight.controller.yang.model.api.TypeDefinition#getUnits()\r
76      */\r
77     @Override\r
78     public String getUnits() {\r
79         return units;\r
80     }\r
81 \r
82     /*\r
83      * (non-Javadoc)\r
84      *\r
85      * @see\r
86      * org.opendaylight.controller.yang.model.api.TypeDefinition#getDefaultValue\r
87      * ()\r
88      */\r
89     @Override\r
90     public Object getDefaultValue() {\r
91         return this;\r
92     }\r
93 \r
94     /*\r
95      * (non-Javadoc)\r
96      *\r
97      * @see org.opendaylight.controller.yang.model.api.SchemaNode#getQName()\r
98      */\r
99     @Override\r
100     public QName getQName() {\r
101         return name;\r
102     }\r
103 \r
104     /*\r
105      * (non-Javadoc)\r
106      *\r
107      * @see org.opendaylight.controller.yang.model.api.SchemaNode#getPath()\r
108      */\r
109     @Override\r
110     public SchemaPath getPath() {\r
111         return path;\r
112     }\r
113 \r
114     /*\r
115      * (non-Javadoc)\r
116      *\r
117      * @see\r
118      * org.opendaylight.controller.yang.model.api.SchemaNode#getDescription()\r
119      */\r
120     @Override\r
121     public String getDescription() {\r
122         return description;\r
123     }\r
124 \r
125     /*\r
126      * (non-Javadoc)\r
127      *\r
128      * @see org.opendaylight.controller.yang.model.api.SchemaNode#getReference()\r
129      */\r
130     @Override\r
131     public String getReference() {\r
132         return reference;\r
133     }\r
134 \r
135     /*\r
136      * (non-Javadoc)\r
137      *\r
138      * @see org.opendaylight.controller.yang.model.api.SchemaNode#getStatus()\r
139      */\r
140     @Override\r
141     public Status getStatus() {\r
142         return Status.CURRENT;\r
143     }\r
144 \r
145     /*\r
146      * (non-Javadoc)\r
147      *\r
148      * @see\r
149      * org.opendaylight.controller.yang.model.api.SchemaNode#getExtensionSchemaNodes\r
150      * ()\r
151      */\r
152     @Override\r
153     public List<UnknownSchemaNode> getUnknownSchemaNodes() {\r
154         return Collections.emptyList();\r
155     }\r
156 \r
157     /*\r
158      * (non-Javadoc)\r
159      *\r
160      * @see\r
161      * org.opendaylight.controller.yang.model.api.type.LeafrefTypeDefinition\r
162      * #getPathStatement()\r
163      */\r
164     @Override\r
165     public RevisionAwareXPath getPathStatement() {\r
166         return xpath;\r
167     }\r
168 \r
169     @Override\r
170     public int hashCode() {\r
171         final int prime = 31;\r
172         int result = 1;\r
173         result = prime * result + ((path == null) ? 0 : path.hashCode());\r
174         result = prime * result + ((units == null) ? 0 : units.hashCode());\r
175         result = prime * result + ((xpath == null) ? 0 : xpath.hashCode());\r
176         return result;\r
177     }\r
178 \r
179     @Override\r
180     public boolean equals(Object obj) {\r
181         if (this == obj) {\r
182             return true;\r
183         }\r
184         if (obj == null) {\r
185             return false;\r
186         }\r
187         if (getClass() != obj.getClass()) {\r
188             return false;\r
189         }\r
190         Leafref other = (Leafref) obj;\r
191         if (path == null) {\r
192             if (other.path != null) {\r
193                 return false;\r
194             }\r
195         } else if (!path.equals(other.path)) {\r
196             return false;\r
197         }\r
198         if (units == null) {\r
199             if (other.units != null) {\r
200                 return false;\r
201             }\r
202         } else if (!units.equals(other.units)) {\r
203             return false;\r
204         }\r
205         if (xpath == null) {\r
206             if (other.xpath != null) {\r
207                 return false;\r
208             }\r
209         } else if (!xpath.equals(other.xpath)) {\r
210             return false;\r
211         }\r
212         return true;\r
213     }\r
214 \r
215     @Override\r
216     public String toString() {\r
217         StringBuilder builder = new StringBuilder();\r
218         builder.append("Leafref [path=");\r
219         builder.append(path);\r
220         builder.append(", xpath=");\r
221         builder.append(xpath);\r
222         builder.append(", units=");\r
223         builder.append(units);\r
224         builder.append("]");\r
225         return builder.toString();\r
226     }\r
227 }\r