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