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