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