Fixed resolving of leafref types with relative xpath.
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / 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.yangtools.yang.model.util;
9
10 import java.util.Collections;
11 import java.util.List;
12
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.Status;
17 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
19
20 /**
21  * The <code>default</code> implementation of Instance Leafref Type Definition
22  * interface.
23  *
24  * @see LeafrefTypeDefinition
25  */
26 public final class Leafref implements LeafrefTypeDefinition {
27     private static final QName NAME = BaseTypes.constructQName("leafref");
28     private static final SchemaPath PATH = BaseTypes.schemaPath(NAME);
29     private static final String DESCRIPTION = "The leafref type is used to reference a particular leaf instance in the data tree.";
30     private static final String REF = "https://tools.ietf.org/html/rfc6020#section-9.9";
31
32     private final RevisionAwareXPath xpath;
33
34     public Leafref(final RevisionAwareXPath xpath) {
35         this.xpath = xpath;
36     }
37
38     @Override
39     public LeafrefTypeDefinition getBaseType() {
40         return this;
41     }
42
43     @Override
44     public String getUnits() {
45         return "";
46     }
47
48     @Override
49     public Object getDefaultValue() {
50         return this;
51     }
52
53     @Override
54     public QName getQName() {
55         return NAME;
56     }
57
58     @Override
59     public SchemaPath getPath() {
60         return PATH;
61     }
62
63     @Override
64     public String getDescription() {
65         return DESCRIPTION;
66     }
67
68     @Override
69     public String getReference() {
70         return REF;
71     }
72
73     @Override
74     public Status getStatus() {
75         return Status.CURRENT;
76     }
77
78     @Override
79     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
80         return Collections.emptyList();
81     }
82
83     @Override
84     public RevisionAwareXPath getPathStatement() {
85         return xpath;
86     }
87
88     @Override
89     public int hashCode() {
90         final int prime = 31;
91         int result = 1;
92         result = prime * result + ((xpath == null) ? 0 : xpath.hashCode());
93         return result;
94     }
95
96     @Override
97     public boolean equals(Object obj) {
98         if (this == obj) {
99             return true;
100         }
101         if (obj == null) {
102             return false;
103         }
104         if (getClass() != obj.getClass()) {
105             return false;
106         }
107         Leafref other = (Leafref) obj;
108         if (xpath == null) {
109             if (other.xpath != null) {
110                 return false;
111             }
112         } else if (!xpath.equals(other.xpath)) {
113             return false;
114         }
115         return true;
116     }
117
118     @Override
119     public String toString() {
120         StringBuilder builder = new StringBuilder();
121         builder.append("type ");
122         builder.append(NAME);
123         builder.append(" [xpath=");
124         builder.append(xpath);
125         builder.append("]");
126         return builder.toString();
127     }
128 }