Bug 2366 - Effective statments impl merge, retest & bugfix
[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 org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
11
12 import org.opendaylight.yangtools.yang.model.util.Leafref;
13 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
14 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveStatementBase;
15 import java.util.Collections;
16 import java.util.List;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.YangConstants;
19 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.Status;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
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 SchemaPath PATH = SchemaPath.create(true, QNAME);
34     private static final String DESCRIPTION = "The leafref type is used to reference a particular leaf instance in the data tree.";
35     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.9";
36     private static final String UNITS = "";
37
38     private RevisionAwareXPath xpath;
39     private final SchemaPath path;
40
41     public LeafrefSpecificationEffectiveStatementImpl(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 + ((xpath == null) ? 0 : xpath.hashCode());
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         if (xpath == null) {
124             if (other.xpath != null) {
125                 return false;
126             }
127         } else if (!xpath.equals(other.xpath)) {
128             return false;
129         }
130         return true;
131     }
132
133     @Override
134     public String toString() {
135         StringBuilder builder = new StringBuilder();
136         builder.append("type ");
137         builder.append(QNAME);
138         builder.append(" [xpath=");
139         builder.append(xpath);
140         builder.append("]");
141         return builder.toString();
142     }
143
144     private Leafref leafrefInstance = null;
145
146     @Override
147     public Leafref buildType() {
148
149         if (leafrefInstance != null) {
150             return leafrefInstance;
151         }
152         leafrefInstance = Leafref.create(path, xpath);
153
154         return leafrefInstance;
155     }
156 }