02f891c3cd26963734552b819425242996d1c144
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / LeafrefSpecificationEffectiveStatementImpl.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.yangtools.yang.parser.stmt.rfc6020.effective.type;
9
10 import java.util.Collections;
11 import java.util.List;
12 import java.util.Objects;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.common.YangConstants;
15 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.Status;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.LeafrefSpecification;
23 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.util.Leafref;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.PathEffectiveStatementImpl;
29
30 public class LeafrefSpecificationEffectiveStatementImpl extends EffectiveStatementBase<String, LeafrefSpecification>
31         implements LeafrefTypeDefinition, TypeEffectiveStatement<LeafrefSpecification> {
32
33     public static final String LOCAL_NAME = "leafref";
34     private static final QName QNAME = QName.create(YangConstants.RFC6020_YANG_MODULE, LOCAL_NAME);
35     private static final String DESCRIPTION = "The leafref type is used to reference a particular leaf instance in the data tree.";
36     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.9";
37     private static final String UNITS = "";
38
39     private final SchemaPath path;
40     private RevisionAwareXPath xpath;
41     private Leafref leafrefInstance = null;
42
43     public LeafrefSpecificationEffectiveStatementImpl(final StmtContext<String, LeafrefSpecification, EffectiveStatement<String, LeafrefSpecification>> ctx) {
44         super(ctx);
45
46         path = Utils.getSchemaPath(ctx.getParentContext()).createChild(QNAME);
47
48         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
49             if (effectiveStatement instanceof PathEffectiveStatementImpl) {
50                 xpath = ((PathEffectiveStatementImpl) effectiveStatement).argument();
51             }
52         }
53     }
54
55     @Override
56     public RevisionAwareXPath getPathStatement() {
57         return xpath;
58     }
59
60     @Override
61     public LeafrefTypeDefinition getBaseType() {
62         return null;
63     }
64
65     @Override
66     public String getUnits() {
67         return UNITS;
68     }
69
70     @Override
71     public Object getDefaultValue() {
72         return this;
73     }
74
75     @Override
76     public QName getQName() {
77         return QNAME;
78     }
79
80     @Override
81     public SchemaPath getPath() {
82         return path;
83     }
84
85     @Override
86     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
87         return Collections.emptyList();
88     }
89
90     @Override
91     public String getDescription() {
92         return DESCRIPTION;
93     }
94
95     @Override
96     public String getReference() {
97         return REFERENCE;
98     }
99
100     @Override
101     public Status getStatus() {
102         return Status.CURRENT;
103     }
104
105     @Override
106     public int hashCode() {
107         final int prime = 31;
108         int result = 1;
109         result = prime * result + Objects.hashCode(xpath);
110         return result;
111     }
112
113     @Override
114     public boolean equals(final Object obj) {
115         if (this == obj) {
116             return true;
117         }
118         if (obj == null) {
119             return false;
120         }
121         if (getClass() != obj.getClass()) {
122             return false;
123         }
124         LeafrefSpecificationEffectiveStatementImpl other = (LeafrefSpecificationEffectiveStatementImpl) obj;
125         return Objects.equals(xpath, other.xpath);
126     }
127
128     @Override
129     public String toString() {
130         StringBuilder builder = new StringBuilder();
131         builder.append("type ");
132         builder.append(QNAME);
133         builder.append(" [xpath=");
134         builder.append(xpath);
135         builder.append("]");
136         return builder.toString();
137     }
138
139     @Override
140     public TypeDefinition<?> getTypeDefinition() {
141         if (leafrefInstance != null) {
142             return leafrefInstance;
143         }
144         leafrefInstance = Leafref.create(path, xpath);
145
146         return leafrefInstance;
147     }
148 }